EnemyControllerCS.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * FUNCTION:
  3. * - This script controls the Enemy (Police Car) based on the player’s movement.
  4. * - It controls the enemy's animatations and it's behavior if the player stumbles.
  5. *
  6. * USED BY:
  7. * This script is a part of the “Enemy” prefab.
  8. *
  9. */
  10. using UnityEngine;
  11. using System.Collections;
  12. public class EnemyControllerCS : MonoBehaviour {
  13. private Transform tEnemy; //enemy transform
  14. private Transform tPlayer;//player transform
  15. private int iEnemyState = 0;
  16. private float fDeathRotation = 0.0f;
  17. private float fCosLerp = 0.0f; //used for Lerp
  18. //script references
  19. private InGameScriptCS hInGameScriptCS;
  20. private ControllerScriptCS hControllerScriptCS;
  21. private SoundManagerCS hSoundManagerCS;
  22. //enemy logic
  23. private float fEnemyPosition = 0.0f;
  24. private float fEnemyPositionX = -5;
  25. private float fEnemyPositionY = 0;
  26. private float fStumbleStartTime;
  27. private float fChaseTime = 5;
  28. public Animator Animator;
  29. void Start()
  30. {
  31. tPlayer = GameObject.Find("Player").transform;
  32. tEnemy = this.transform;
  33. hInGameScriptCS = (InGameScriptCS)GameObject.Find("Player").GetComponent(typeof(InGameScriptCS));
  34. hControllerScriptCS = (ControllerScriptCS)GameObject.Find("Player").GetComponent(typeof(ControllerScriptCS));
  35. hSoundManagerCS = (SoundManagerCS)GameObject.Find("SoundManager").GetComponent(typeof(SoundManagerCS));
  36. }
  37. /*
  38. * FUNCTION: Starting the chasing sequence
  39. * CALLED BY: ControllerScript.launchGame()
  40. */
  41. public void launchEnemy()
  42. {
  43. iEnemyState = 2;
  44. }
  45. void FixedUpdateX ()
  46. {
  47. if(hInGameScriptCS.isGamePaused()==true)
  48. return;
  49. //set the position of guard in current frame
  50. tEnemy.position = new Vector3(Mathf.Lerp(tEnemy.position.x, (tPlayer.position.x - fEnemyPosition), Time.deltaTime*10),
  51. tEnemy.position.y, tEnemy.position.z);
  52. if (!hControllerScriptCS.isInAir())//follow the player in y-axis if he's not jumping (cars cant jump)
  53. tEnemy.position = new Vector3(tEnemy.position.x, Mathf.Lerp(tEnemy.position.y, tPlayer.position.y + fEnemyPositionY, Time.deltaTime*8),
  54. tEnemy.position.z);
  55. //ignore y-axis rotation and horizontal movement in idle and death state
  56. if (iEnemyState < 4)
  57. {
  58. tEnemy.position = new Vector3(tEnemy.position.x, tEnemy.position.y, Mathf.Lerp(tEnemy.position.z, tPlayer.position.z, Time.deltaTime*10));
  59. tEnemy.localEulerAngles = new Vector3(tEnemy.localEulerAngles.x,-hControllerScriptCS.getCurrentPlayerRotation(), tEnemy.localEulerAngles.z);
  60. }
  61. if (iEnemyState == 1)//hide the chasing character
  62. {
  63. fCosLerp += (Time.deltaTime/10);
  64. fEnemyPosition = Mathf.Lerp(fEnemyPosition, fEnemyPositionX + 45, Mathf.Cos(fCosLerp)/1000);
  65. if (fCosLerp >= 0.7f)
  66. {
  67. fCosLerp = 0.0f;
  68. iEnemyState = 0;
  69. hSoundManagerCS.stopSound(SoundManagerCS.EnemySounds.Siren);
  70. }
  71. }
  72. else if (iEnemyState == 2)//show the chasing character
  73. {
  74. hSoundManagerCS.playSound(SoundManagerCS.EnemySounds.Siren);
  75. fCosLerp += (Time.deltaTime/4);
  76. fEnemyPosition = Mathf.Lerp(fEnemyPosition, fEnemyPositionX, Mathf.Cos(fCosLerp));
  77. if (fCosLerp >= 1.5f)
  78. {
  79. fCosLerp = 0.0f;
  80. iEnemyState = 3;
  81. }
  82. }
  83. else if (iEnemyState == 3)//wait for 'fChaseTime' after showing character
  84. {
  85. if ( (Time.time - fStumbleStartTime)%60 >= fChaseTime)
  86. iEnemyState = 1;
  87. }
  88. //DEATH SEQUENCE
  89. else if (iEnemyState == 4)//on death
  90. {
  91. Animator.SetBool("PlayDeath", true);
  92. //to ensure correct rotation animation
  93. tEnemy.localEulerAngles = new Vector3(tEnemy.localEulerAngles.x, 350, tEnemy.localEulerAngles.z);
  94. hSoundManagerCS.playSound(SoundManagerCS.EnemySounds.TiresSqueal);
  95. iEnemyState = 5;
  96. }
  97. else if (iEnemyState == 5)//pin behind the player
  98. {
  99. fEnemyPosition = Mathf.Lerp(fEnemyPosition, fEnemyPositionX+20, Time.fixedDeltaTime*50);//vertical position after skid
  100. tEnemy.position = new Vector3(tEnemy.position.x, tEnemy.position.y,
  101. Mathf.Lerp(tEnemy.position.z, tPlayer.position.z - 1, Time.deltaTime*10));//horizontal position after skid
  102. tEnemy.localEulerAngles = Vector3.Lerp(tEnemy.localEulerAngles, new Vector3(0,260,0), Time.deltaTime*10);//90 degree rotation
  103. if (tEnemy.localEulerAngles.y <= 180)
  104. iEnemyState = 6;
  105. }
  106. else if (iEnemyState == 6)
  107. {
  108. hSoundManagerCS.stopSound(SoundManagerCS.EnemySounds.Siren);
  109. }
  110. }//end of Update
  111. /*
  112. * FUNCTION: Animate enemy
  113. * RETURNS: 'true' if the enemy was already chasing player
  114. * 'false' if the enemy was not chasing the player
  115. * CALLED BY: ControllerScript.processStumble()
  116. */
  117. public bool processStumble()
  118. {
  119. if (isEnemyActive())//if enemy is already chasing player
  120. {
  121. iEnemyState = 0;
  122. return true;
  123. }
  124. else
  125. {
  126. fStumbleStartTime = Time.time;
  127. iEnemyState = 2;
  128. return false;
  129. }
  130. }
  131. public void playDeathAnimation() { iEnemyState = 4; }
  132. public void hideEnemy() { iEnemyState = 1; }
  133. /*
  134. * FUNCTION: Check if the enemy is chasing the player
  135. */
  136. public bool isEnemyActive()
  137. {
  138. if (iEnemyState == 2 || iEnemyState == 3)
  139. return true;
  140. else
  141. return false;
  142. }
  143. }