123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using UnityEngine;
- using Status;
- using ObjectScripts;
- using System;
- /// <summary>
- /// Details sets the model for the corresponding details level, handles animation and sets the color of the object
- /// </summary>
- public class Details : MonoBehaviour
- {
- /// <summary>
- /// Child component for the one model no color
- /// </summary>
- private GameObject LessDetails { get; set; }
- /// <summary>
- /// Child component for the box model for all
- /// </summary>
- private GameObject Block { get; set; }
- /// <summary>
- /// Child component for the one model with color
- /// </summary>
- private GameObject FullModel { get; set; }
- /// <summary>
- /// Current Object
- /// </summary>
- private GameObject Parent { get; set; }
- /// <summary>
- /// DataObject Skript of the Object containing information about the Object (Position, Rotation, Color,..)
- /// </summary>
- private DataObject DataObject { get; set; }
- /// <summary>
- /// Animator Component of the Object
- /// </summary>
- private Animator Animator { get; set; }
- /// <summary>
- /// LODGroup Component of the Object
- /// </summary>
- private LODGroup LODGroup { get; set; }
- /// <summary>
- /// Top Renderer only renderer for single color entities and first color of models with 2 colors
- /// </summary>
- private Renderer TopRenderer { get; set; }
- /// <summary>
- /// Bottom Renderer only used for models with 2 colors
- /// </summary>
- private Renderer BottomRenderer { get; set; }
- /// <summary>
- /// Currently assigned Top color (human top color, entities with only a single color complete color)
- /// </summary>
- private uint OldColorTop { get; set; }
- /// <summary>
- /// Currently assigned Bottom color (human only)
- /// </summary>
- private uint OldColorBot { get; set; }
- /// <summary>
- /// Current State
- /// </summary>
- private int OldState { get; set; }
- /// <summary>
- /// Current Velocity
- /// </summary>
- private double OldVelocity { get; set; }
- /// <summary>
- /// Current Game Velocity
- /// </summary>
- private float OldGameVelocity { get; set; }
- /// <summary>
- /// Hash for finding animator moving bool
- /// </summary>
- private int MoveHash { get; set; }
- // called once at the start
- private void Start()
- {
- DataObject = gameObject.GetComponent<DataObject>();
- Parent = transform.gameObject;
- LODGroup = Parent.GetComponent<LODGroup>();
- // 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<Renderer>();
- BottomRenderer = FullModel.transform.GetChild(2).gameObject.GetComponent<Renderer>();
- Animator = transform.GetChild(0).GetComponent<Animator>();
- 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<Renderer>();
- Animator = transform.GetChild(0).GetComponent<Animator>();
- 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<Renderer>();
- Animator = transform.GetChild(0).GetComponent<Animator>();
- 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<Renderer>();
- Animator = transform.GetChild(0).GetComponent<Animator>();
- 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;
- }
- }
|