Details.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using UnityEngine;
  2. using Status;
  3. using ObjectScripts;
  4. using System;
  5. /// <summary>
  6. /// Details sets the model for the corresponding details level, handles animation and sets the color of the object
  7. /// </summary>
  8. public class Details : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Child component for the one model no color
  12. /// </summary>
  13. private GameObject LessDetails { get; set; }
  14. /// <summary>
  15. /// Child component for the box model for all
  16. /// </summary>
  17. private GameObject Block { get; set; }
  18. /// <summary>
  19. /// Child component for the one model with color
  20. /// </summary>
  21. private GameObject FullModel { get; set; }
  22. /// <summary>
  23. /// Current Object
  24. /// </summary>
  25. private GameObject Parent { get; set; }
  26. /// <summary>
  27. /// DataObject Skript of the Object containing information about the Object (Position, Rotation, Color,..)
  28. /// </summary>
  29. private DataObject DataObject { get; set; }
  30. /// <summary>
  31. /// Animator Component of the Object
  32. /// </summary>
  33. private Animator Animator { get; set; }
  34. /// <summary>
  35. /// LODGroup Component of the Object
  36. /// </summary>
  37. private LODGroup LODGroup { get; set; }
  38. /// <summary>
  39. /// Top Renderer only renderer for single color entities and first color of models with 2 colors
  40. /// </summary>
  41. private Renderer TopRenderer { get; set; }
  42. /// <summary>
  43. /// Bottom Renderer only used for models with 2 colors
  44. /// </summary>
  45. private Renderer BottomRenderer { get; set; }
  46. /// <summary>
  47. /// Currently assigned Top color (human top color, entities with only a single color complete color)
  48. /// </summary>
  49. private uint OldColorTop { get; set; }
  50. /// <summary>
  51. /// Currently assigned Bottom color (human only)
  52. /// </summary>
  53. private uint OldColorBot { get; set; }
  54. /// <summary>
  55. /// Current State
  56. /// </summary>
  57. private int OldState { get; set; }
  58. /// <summary>
  59. /// Current Velocity
  60. /// </summary>
  61. private double OldVelocity { get; set; }
  62. /// <summary>
  63. /// Current Game Velocity
  64. /// </summary>
  65. private float OldGameVelocity { get; set; }
  66. /// <summary>
  67. /// Hash for finding animator moving bool
  68. /// </summary>
  69. private int MoveHash { get; set; }
  70. // called once at the start
  71. private void Start()
  72. {
  73. DataObject = gameObject.GetComponent<DataObject>();
  74. Parent = transform.gameObject;
  75. LODGroup = Parent.GetComponent<LODGroup>();
  76. // assign models, animator, hash for finding animator bool and renderer depending on Entity type
  77. switch (DataObject.Type)
  78. {
  79. case CSVReader.EntityType.PERSON:
  80. FullModel = transform.GetChild(0).transform.GetChild(1).gameObject;
  81. Block = transform.GetChild(1).gameObject;
  82. LessDetails = transform.GetChild(0).transform.GetChild(2).gameObject;
  83. TopRenderer = FullModel.transform.GetChild(0).gameObject.GetComponent<Renderer>();
  84. BottomRenderer = FullModel.transform.GetChild(2).gameObject.GetComponent<Renderer>();
  85. Animator = transform.GetChild(0).GetComponent<Animator>();
  86. MoveHash = Animator.StringToHash("moving");
  87. break;
  88. case CSVReader.EntityType.CAR:
  89. FullModel = transform.GetChild(0).gameObject;
  90. Block = transform.GetChild(1).gameObject;
  91. LessDetails = transform.GetChild(2).gameObject;
  92. TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent<Renderer>();
  93. Animator = transform.GetChild(0).GetComponent<Animator>();
  94. MoveHash = Animator.StringToHash("moving");
  95. break;
  96. case CSVReader.EntityType.TRUCK:
  97. FullModel = transform.GetChild(0).gameObject;
  98. Block = transform.GetChild(1).gameObject;
  99. LessDetails = transform.GetChild(2).gameObject;
  100. TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent<Renderer>();
  101. Animator = transform.GetChild(0).GetComponent<Animator>();
  102. MoveHash = Animator.StringToHash("moving");
  103. break;
  104. case CSVReader.EntityType.BIKE:
  105. FullModel = transform.GetChild(0).gameObject;
  106. Block = transform.GetChild(1).gameObject;
  107. LessDetails = transform.GetChild(2).gameObject;
  108. TopRenderer = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent<Renderer>();
  109. Animator = transform.GetChild(0).GetComponent<Animator>();
  110. MoveHash = Animator.StringToHash("moving");
  111. break;
  112. default:
  113. break;
  114. }
  115. SetColor(DataObject.TopColor, DataObject.BotColor);
  116. SetDetails();
  117. HandleAnimator();
  118. }
  119. // called every frame
  120. void Update()
  121. {
  122. bool valid = false;
  123. // check for color changes and set new color if changed
  124. if (OldColorTop != DataObject.TopColor || OldColorBot != DataObject.BotColor)
  125. {
  126. SetColor(DataObject.TopColor, DataObject.BotColor);
  127. }
  128. // check for state changes and set models depending on state
  129. if (OldState != DetailsLevel.State)
  130. {
  131. SetDetails();
  132. valid = true;
  133. }
  134. // check for velocity changes
  135. if ((Math.Abs(OldVelocity - DataObject.Velocity) > 0.1) || (DataObject.Handler.GameVelocity != OldGameVelocity))
  136. {
  137. valid = true;
  138. }
  139. // set animation speed depending on veleocity
  140. if (valid)
  141. HandleAnimator();
  142. }
  143. // Set colors of the object
  144. private void SetColor(uint TopColor, uint BotColor)
  145. {
  146. // transform uint color to byte argb color
  147. byte ATop = (byte)(TopColor >> 24 & 255);
  148. byte RTop = (byte)(TopColor >> 16 & 255);
  149. byte GTop = (byte)(TopColor >> 8 & 255);
  150. byte BTop = (byte)(TopColor >> 0 & 255);
  151. byte ABot = (byte)(BotColor >> 24 & 255);
  152. byte RBot = (byte)(BotColor >> 16 & 255);
  153. byte GBot = (byte)(BotColor >> 8 & 255);
  154. byte BBot = (byte)(BotColor >> 0 & 255);
  155. // set colors for the two body regions (BottomColor32 only used for human model)
  156. Color32 TopColor32 = new Color32(RTop, GTop, BTop, ATop);
  157. Color32 BottomColor32 = new Color32(RBot, GBot, BBot, ABot);
  158. if (DataObject.Type == CSVReader.EntityType.PERSON)
  159. {
  160. // set material colors for top and bottom
  161. TopRenderer.material.SetColor("_Color", TopColor32);
  162. BottomRenderer.material.SetColor("_Color", BottomColor32);
  163. }
  164. if (DataObject.Type == CSVReader.EntityType.CAR || DataObject.Type == CSVReader.EntityType.TRUCK || DataObject.Type == CSVReader.EntityType.BIKE)
  165. {
  166. // set material color
  167. TopRenderer.material.SetColor("_Color", TopColor32);
  168. }
  169. OldColorTop = DataObject.TopColor;
  170. OldColorBot = DataObject.BotColor;
  171. }
  172. private void SetDetails()
  173. {
  174. // set models depending on state
  175. switch (DetailsLevel.State)
  176. {
  177. // LOD groupe active
  178. case 0:
  179. LessDetails.SetActive(true);
  180. Block.SetActive(true);
  181. FullModel.SetActive(true);
  182. LODGroup.enabled = true;
  183. ToggleAnimators(true);
  184. break;
  185. // Block mode active
  186. case 1:
  187. LessDetails.SetActive(false);
  188. Block.SetActive(true);
  189. FullModel.SetActive(false);
  190. LODGroup.enabled = false;
  191. ToggleAnimators(false);
  192. break;
  193. // One model no color
  194. case 2:
  195. Block.SetActive(false);
  196. FullModel.SetActive(false);
  197. LessDetails.SetActive(true);
  198. LODGroup.enabled = false;
  199. ToggleAnimators(true);
  200. break;
  201. // One model different colors active
  202. case 3:
  203. LessDetails.SetActive(false);
  204. Block.SetActive(false);
  205. FullModel.SetActive(true);
  206. LODGroup.enabled = false;
  207. ToggleAnimators(true);
  208. break;
  209. default:
  210. break;
  211. }
  212. OldState = DetailsLevel.State;
  213. }
  214. // Set animation speed and animation depending on the game and object velocity
  215. private void HandleAnimator()
  216. {
  217. Animator.SetBool(MoveHash, DataObject.Velocity > 0);
  218. Animator.speed = (float)(DataObject.Handler.GameVelocity * DataObject.Velocity * 0.62);
  219. OldVelocity = DataObject.Velocity;
  220. OldGameVelocity = DataObject.Handler.GameVelocity;
  221. }
  222. // toggle animator on/off
  223. private void ToggleAnimators(bool b)
  224. {
  225. Animator.enabled = b;
  226. }
  227. }