/Assets/Scripts/Characters/Enemy/Imp/ImpAI.cs

https://bitbucket.org/BitDemon/groupproject · C# · 129 lines · 109 code · 17 blank · 3 comment · 12 complexity · 39b096f97a2679db3bde3ebba375fc30 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ImpAI : Enemy{
  4. ImpLeaderAI impLeaderAi;
  5. public Transform impLeader = null;
  6. public bool attack, died, dissolve;
  7. public int jumpHeight = 3;
  8. public int sideJumpDist = 3;
  9. public enum AttackState{
  10. Idle,
  11. Walk,
  12. Run,
  13. Attack,
  14. Spell,
  15. Death
  16. }
  17. public AttackState attackState = AttackState.Walk;
  18. void Start(){
  19. InitEntity("Imp", player, 100, 100, 8, 0, 3, 10, 9f, 30, 1);
  20. }
  21. void FixedUpdate(){
  22. rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
  23. AnimaionManager();
  24. if(!isDead){
  25. Attack();
  26. if(isInDistance || isAggro){
  27. if(isAttacking){
  28. if(attackTime < coolDownTime){
  29. attackTime += Time.deltaTime;
  30. }else if(attackTime > coolDownTime){
  31. attackTime = 0.0f;
  32. if(!attack)StartCoroutine(AttackPlayer());
  33. }
  34. }else{
  35. myTransform.LookAt(target);
  36. myTransform.position += myTransform.forward * 20 * Time.deltaTime;
  37. }
  38. }else{
  39. GameObject[] imps = GameObject.FindGameObjectsWithTag("Enemy");
  40. foreach(GameObject go in imps){
  41. if(go.name == "Imp"){
  42. if(go.GetComponent<ImpAI>().isAggro){
  43. isAggro = true;
  44. attackState = AttackState.Run;
  45. }
  46. }
  47. }
  48. }
  49. }else{
  50. if(!died)StartCoroutine(Death());
  51. if(dissolve){
  52. animation.Stop();
  53. float shininess = Mathf.PingPong(Time.time, 1.0F);
  54. //GameObject.Find("ChargerModel").renderer.material.SetFloat("_SliceAmount", Mathf.Sin(Time.time) * 1.0f);
  55. }
  56. }
  57. }
  58. void AnimaionManager(){
  59. switch(attackState){
  60. case AttackState.Idle:
  61. animation.clip = animation.GetClip("idle");
  62. animation.CrossFade(animation.clip.name, 0.2f);
  63. break;
  64. case AttackState.Walk:
  65. animation.clip = animation.GetClip("walk");
  66. animation[animation.clip.name].speed = 1.5f;
  67. animation.CrossFade(animation.clip.name, 0.2f);
  68. break;
  69. case AttackState.Run:
  70. animation.clip = animation.GetClip("run");
  71. animation[animation.clip.name].speed = 2f;
  72. animation.CrossFade(animation.clip.name, 0.2f);
  73. break;
  74. case AttackState.Attack:
  75. animation.clip = animation.GetClip("attack");
  76. animation.CrossFade(animation.clip.name, 0.2f);
  77. break;
  78. case AttackState.Spell:
  79. animation.clip = animation.GetClip("spell");
  80. animation.CrossFade(animation.clip.name, 0.2f);
  81. break;
  82. case AttackState.Death:
  83. animation.clip = animation.GetClip("death");
  84. animation.Play();
  85. //animation[animation.clip.name].speed = 2.0f;
  86. //animation.CrossFade(animation.clip.name, 0.2f);
  87. break;
  88. }
  89. }
  90. IEnumerator AttackPlayer(){
  91. attack = true;
  92. attackState = AttackState.Attack;
  93. Entity entity = (Entity)target.GetComponent(typeof(Entity));
  94. entity.AdjustHealth(strength);
  95. yield return new WaitForSeconds(animation.clip.length);
  96. attackState = AttackState.Run;
  97. attack = false;
  98. }
  99. IEnumerator Death(){
  100. died = true;
  101. attackState = AttackState.Death;
  102. print(animation.clip.length);
  103. yield return new WaitForSeconds(animation.clip.length);
  104. DropLoot();
  105. dissolve = true;
  106. yield return new WaitForSeconds(0.7f);
  107. Destroy(gameObject);
  108. }
  109. float CalculateJumpVerticalSpeed(){
  110. return Mathf.Sqrt(2 * jumpHeight * gravity);
  111. }
  112. }