using UnityEngine;
using Status;
using ObjectScripts;
using System;
///
/// Details sets the model for the corresponding details level, handles animation and sets the color of the object
///
public class Details : MonoBehaviour
{
///
/// Child component for the one model no color
///
private GameObject LessDetails { get; set; }
///
/// Child component for the box model for all
///
private GameObject Block { get; set; }
///
/// Child component for the one model with color
///
private GameObject FullModel { get; set; }
///
/// Current Object
///
private GameObject Parent { get; set; }
///
/// DataObject Skript of the Object containing information about the Object (Position, Rotation, Color,..)
///
private DataObject DataObject { get; set; }
///
/// Animator Component of the Object
///
private Animator Animator { get; set; }
///
/// LODGroup Component of the Object
///
private LODGroup LODGroup { get; set; }
///
/// Top Renderer only renderer for single color entities and first color of models with 2 colors
///
private Renderer TopRenderer { get; set; }
///
/// Bottom Renderer only used for models with 2 colors
///
private Renderer BottomRenderer { get; set; }
///
/// Currently assigned Top color (human top color, entities with only a single color complete color)
///
private uint OldColorTop { get; set; }
///
/// Currently assigned Bottom color (human only)
///
private uint OldColorBot { get; set; }
///
/// Current State
///
private int OldState { get; set; }
///
/// Current Velocity
///
private double OldVelocity { get; set; }
///
/// Current Game Velocity
///
private float OldGameVelocity { get; set; }
///
/// Hash for finding animator moving bool
///
private int MoveHash { get; set; }
// called once at the start
private void Start()
{
DataObject = gameObject.GetComponent();
Parent = transform.gameObject;
LODGroup = Parent.GetComponent();
// assign models, animator, hash for finding animator bool and renderer depending on Entity type
switch (DataObject.Type)
{
case CSVReader.EntityType.PERSON:
FullModel = transform.GetChild(0).transform.GetChild(1).gameObject;
Block = transform.GetChild(1).gameObject;
LessDetails = transform.GetChild(0).transform.GetChild(2).gameObject;
TopRenderer = FullModel.transform.GetChild(0).gameObject.GetComponent();
BottomRenderer = FullModel.transform.GetChild(2).gameObject.GetComponent();
Animator = transform.GetChild(0).GetComponent();
MoveHash = Animator.StringToHash("moving");
break;
case CSVReader.EntityType.CAR:
FullModel = transform.GetChild(0).gameObject;
Block = transform.GetChild(1).gameObject;
LessDetails = transform.GetChild(2).gameObject;
TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent();
Animator = transform.GetChild(0).GetComponent();
MoveHash = Animator.StringToHash("moving");
break;
case CSVReader.EntityType.TRUCK:
FullModel = transform.GetChild(0).gameObject;
Block = transform.GetChild(1).gameObject;
LessDetails = transform.GetChild(2).gameObject;
TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent();
Animator = transform.GetChild(0).GetComponent();
MoveHash = Animator.StringToHash("moving");
break;
case CSVReader.EntityType.BIKE:
FullModel = transform.GetChild(0).gameObject;
Block = transform.GetChild(1).gameObject;
LessDetails = transform.GetChild(2).gameObject;
TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent();
Animator = transform.GetChild(0).GetComponent();
MoveHash = Animator.StringToHash("moving");
break;
default:
break;
}
SetColor(DataObject.TopColor, DataObject.BotColor);
SetDetails();
HandleAnimator();
}
// called every frame
void Update()
{
bool valid = false;
// check for color changes and set new color if changed
if (OldColorTop != DataObject.TopColor || OldColorBot != DataObject.BotColor)
{
SetColor(DataObject.TopColor, DataObject.BotColor);
}
// check for state changes and set models depending on state
if (OldState != DetailsLevel.State)
{
SetDetails();
valid = true;
}
// check for velocity changes
if ((Math.Abs(OldVelocity - DataObject.Velocity) > 0.1) || (DataObject.Handler.GameVelocity != OldGameVelocity))
{
valid = true;
}
// set animation speed depending on veleocity
if (valid)
HandleAnimator();
}
// Set colors of the object
private void SetColor(uint TopColor, uint BotColor)
{
// transform uint color to byte argb color
byte ATop = (byte)(TopColor >> 24 & 255);
byte RTop = (byte)(TopColor >> 16 & 255);
byte GTop = (byte)(TopColor >> 8 & 255);
byte BTop = (byte)(TopColor >> 0 & 255);
byte ABot = (byte)(BotColor >> 24 & 255);
byte RBot = (byte)(BotColor >> 16 & 255);
byte GBot = (byte)(BotColor >> 8 & 255);
byte BBot = (byte)(BotColor >> 0 & 255);
// set colors for the two body regions (BottomColor32 only used for human model)
Color32 TopColor32 = new Color32(RTop, GTop, BTop, ATop);
Color32 BottomColor32 = new Color32(RBot, GBot, BBot, ABot);
if (DataObject.Type == CSVReader.EntityType.PERSON)
{
// set material colors for top and bottom
TopRenderer.material.SetColor("_Color", TopColor32);
BottomRenderer.material.SetColor("_Color", BottomColor32);
}
if (DataObject.Type == CSVReader.EntityType.CAR || DataObject.Type == CSVReader.EntityType.TRUCK || DataObject.Type == CSVReader.EntityType.BIKE)
{
// set material color
TopRenderer.material.SetColor("_Color", TopColor32);
}
OldColorTop = DataObject.TopColor;
OldColorBot = DataObject.BotColor;
}
private void SetDetails()
{
// set models depending on state
switch (DetailsLevel.State)
{
// LOD groupe active
case 0:
LessDetails.SetActive(true);
Block.SetActive(true);
FullModel.SetActive(true);
LODGroup.enabled = true;
ToggleAnimators(true);
break;
// Block mode active
case 1:
LessDetails.SetActive(false);
Block.SetActive(true);
FullModel.SetActive(false);
LODGroup.enabled = false;
ToggleAnimators(false);
break;
// One model no color
case 2:
Block.SetActive(false);
FullModel.SetActive(false);
LessDetails.SetActive(true);
LODGroup.enabled = false;
ToggleAnimators(true);
break;
// One model different colors active
case 3:
LessDetails.SetActive(false);
Block.SetActive(false);
FullModel.SetActive(true);
LODGroup.enabled = false;
ToggleAnimators(true);
break;
default:
break;
}
OldState = DetailsLevel.State;
}
// Set animation speed and animation depending on the game and object velocity
private void HandleAnimator()
{
Animator.SetBool(MoveHash, DataObject.Velocity > 0);
Animator.speed = (float)(DataObject.Handler.GameVelocity * DataObject.Velocity * 0.62);
OldVelocity = DataObject.Velocity;
OldGameVelocity = DataObject.Handler.GameVelocity;
}
// toggle animator on/off
private void ToggleAnimators(bool b)
{
Animator.enabled = b;
}
}