Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using Worq.AEAI.HealthAndDamage;
  6. using UnityStandardAssets.Characters.FirstPerson;
  7.  
  8.  
  9. namespace Worq.AEAI.Enemy
  10. {
  11. [DisallowMultipleComponent]
  12. [RequireComponent(typeof(Animation))]
  13. [RequireComponent(typeof(Animator))]
  14. [RequireComponent(typeof(NavMeshAgent))]
  15. [RequireComponent(typeof(AudioSource))]
  16. public class EnemyAI : MonoBehaviour
  17. {
  18. public Light lightDectector;
  19. private Transform[] patrolPoints;
  20. private GameObject player;
  21. private string enemyCharacter;
  22. private AudioClip gunfireSound;
  23. public AudioClip enemyAlertSound;
  24. private AudioClip enemyDeathSound;
  25. private AudioClip ChaseSoundTrack;
  26. private float walkSpeed;
  27. private float runSpeed;
  28. private float maxPatrolWaitTime;
  29. private float maxAttackDistance;
  30. private float minAttackDistance;
  31. private int enemyType;
  32. private NavMeshAgent agent;
  33. private Vector3 defaultPos;
  34. private Vector3 enemySize;
  35. private Vector3 playerSize;
  36. private Vector3 positionToInvestigate;
  37. private Animation anim;
  38. private Animator animator;
  39. private AudioSource mSource;
  40. private GameObject playerLookAt;
  41. private GameObject sightObject;
  42. private GameObject[] deathSpawnItems;
  43. private int destPoint = 0;
  44. private float distanceToAttackFrom;
  45. private float ammountOfPlayerDamage;
  46. private float ammountOfEnemyDamage;
  47. private float switchOffRadarDistance;
  48. private float retreatDistance;
  49. private float delayBeforeDestroy;
  50. private int difficulty;
  51. private bool hasFiredOne;
  52. private bool isWaiting;
  53. private bool isAttacking;
  54. private bool hasDeathAnim;
  55.  
  56. [SerializeField] private float retretTime;
  57.  
  58.  
  59. private int waypointCount;
  60.  
  61. //animations
  62. private AnimationType animationType;
  63. private MechanimTriggerType triggerType;
  64. private bool enableCrossFade;
  65.  
  66. private string walkAnimTrigger;
  67. private string runAnimTrigger;
  68. private string aimAnimTrigger;
  69. private string shootAnimTrigger;
  70. private string idleAnimTrigger;
  71. private string deathAnimTrigger;
  72.  
  73. private string walkBoolParam;
  74. private string runBoolParam;
  75. private string aimBoolParam;
  76. private string shootBoolParam;
  77. private string idleBoolParam;
  78. private string deathBoolParam;
  79.  
  80. private IntTrigger walkAnimInt;
  81. private IntTrigger runAnimInt;
  82. private IntTrigger aimAnimInt;
  83. private IntTrigger shootAnimInt;
  84. private IntTrigger idleAnimInt;
  85. private IntTrigger deathAnimInt;
  86.  
  87. private AnimationClip[] walkAnimations;
  88. private AnimationClip[] runAnimations;
  89. private AnimationClip[] aimAnimations;
  90. private AnimationClip[] shootAnimations;
  91. private AnimationClip[] idleAnimations;
  92.  
  93. private AnimationClip[] deathAnimations;
  94.  
  95. //Debugging
  96. public bool playerIsDead;
  97. public bool playerDetected;
  98. public bool patrolReset;
  99. public bool isSeeking;
  100. private bool enemyRetreating;
  101. private bool hasKilledEnemy;
  102. private float currentPlayerHealth;
  103. public float currentEnemyHealth = 100f;
  104. private bool randomPatroler;
  105. private bool hasPlayedDetectSound;
  106. private bool hasPlayedShootingSound;
  107. private float outRangeTimer = 0;
  108. private bool chase;
  109.  
  110. private float roarTimer = 0;
  111.  
  112. //Send Alert Phase
  113.  
  114. AIData info;
  115.  
  116. void Awake()
  117. {
  118. info = transform.parent.GetComponent<AIData>();
  119.  
  120. player = info.player;
  121.  
  122. player = info.player;
  123. if (player == null)
  124. player = GameObject.Find("Player");
  125. if (player == null)
  126. player = GameObject.FindWithTag("Player");
  127.  
  128. waypointCount = 0;
  129. Transform par = transform.parent;
  130. int childrenCount = par.childCount;
  131.  
  132. for (int i = 0; i < childrenCount; i++)
  133. {
  134. if (par.GetChild(i).GetComponent<WaypointIdentifier>() != null)
  135. {
  136. waypointCount += 1;
  137. }
  138. }
  139.  
  140. patrolPoints = new Transform[waypointCount];
  141. int curIndex = 0;
  142. for (int i = 0; i < childrenCount; i++)
  143. {
  144. if (par.GetChild(i).GetComponent<WaypointIdentifier>() != null)
  145. {
  146. patrolPoints[curIndex] = transform.parent.GetChild(i);
  147. if (patrolPoints[curIndex].gameObject.GetComponent<MeshRenderer>() != null)
  148. patrolPoints[curIndex].gameObject.GetComponent<MeshRenderer>().enabled = false;
  149. if (patrolPoints[curIndex].gameObject.GetComponent<Collider>() != null)
  150. patrolPoints[curIndex].gameObject.GetComponent<Collider>().enabled = false;
  151. curIndex++;
  152. }
  153. }
  154.  
  155. enemySize = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
  156. playerSize = new Vector3(player.transform.localScale.x, player.transform.localScale.y,
  157. player.transform.localScale.z);
  158.  
  159. sightObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  160. sightObject.transform.SetParent(transform);
  161. sightObject.transform.localPosition = new Vector3(0, (enemySize.y / 2), 0);
  162. sightObject.transform.localScale = new Vector3(enemySize.x / 5, enemySize.y / 5, enemySize.z / 5);
  163. sightObject.name = "EnemyRadar";
  164. sightObject.AddComponent<EnemyRadar>();
  165. sightObject.GetComponent<Collider>().isTrigger = true;
  166. sightObject.GetComponent<MeshRenderer>().enabled = false;
  167.  
  168. //setup player lookAt
  169. if (player.transform.Find("playerLookAt") == null)
  170. {
  171. playerLookAt = GameObject.CreatePrimitive(PrimitiveType.Cube);
  172. playerLookAt.transform.SetParent(player.transform);
  173. // playerLookAt.transform.localPosition = new Vector3(0, 0, 0);
  174. playerLookAt.transform.localScale = new Vector3(playerSize.x / 5, playerSize.y / 5, playerSize.z / 5);
  175. playerLookAt.name = "playerLookAt";
  176. playerLookAt.tag = "Player";
  177. MeshRenderer rend = playerLookAt.GetComponent<MeshRenderer>();
  178. DestroyImmediate(rend);
  179. MeshFilter mf = playerLookAt.GetComponent<MeshFilter>();
  180. DestroyImmediate(mf);
  181. playerLookAt.GetComponent<Collider>().isTrigger = true;
  182. }
  183. }
  184.  
  185. void Start()
  186. {
  187. if (player == null)
  188. Debug.Log("No player found. Please attach aplayer ot target");
  189.  
  190. if(enemyCharacter == "lightKiller"){
  191.  
  192. }
  193.  
  194. ammountOfPlayerDamage = info.playerTakeDamageValue;
  195. ammountOfEnemyDamage = info.enemyTakeDamageValue;
  196. delayBeforeDestroy = info.delayBeforeDestroy;
  197.  
  198. //get animations
  199. //animType
  200. //animationType = info.animationType;
  201. animationType = AnimationType.Mechanim;
  202. enableCrossFade = info.enableCrossFade;
  203.  
  204. //enemyCharacter
  205. enemyCharacter = info.enemyCharacter;
  206.  
  207. //mechanim
  208. //triggers
  209. walkAnimTrigger = info.walkAnimTrigger ?? "";
  210. runAnimTrigger = info.runAnimTrigger ?? "";
  211. aimAnimTrigger = info.aimAnimTrigger ?? "";
  212. shootAnimTrigger = info.shootAnimTrigger ?? "";
  213. idleAnimTrigger = info.idleAnimTrigger ?? "";
  214. deathAnimTrigger = info.deathAnimTrigger ?? "";
  215.  
  216. //bools
  217. walkBoolParam = info.walkBoolParam;
  218. runBoolParam = info.runBoolParam;
  219. aimBoolParam = info.aimBoolParam;
  220. shootBoolParam = info.shootBoolParam;
  221. idleBoolParam = info.idleBoolParam;
  222. deathBoolParam = info.deathBoolParam;
  223.  
  224. //ints
  225. walkAnimInt = info.aimAnimInt;
  226. runAnimInt = info.runAnimInt;
  227. aimAnimInt = info.aimAnimInt;
  228. shootAnimInt = info.shootAnimInt;
  229. idleAnimInt = info.idleAnimInt;
  230. deathAnimInt = info.deathAnimInt;
  231.  
  232. //legacy
  233. walkAnimations = info.walkAnimations;
  234. runAnimations = info.runAnimations;
  235. aimAnimations = info.aimAnimations;
  236. shootAnimations = info.shootAndAttackAnimations;
  237. idleAnimations = info.idleAnimations;
  238. deathAnimations = info.deathAnimations;
  239.  
  240. deathSpawnItems = info.deathSpawnItems;
  241.  
  242. gunfireSound = info.gunfireSound;
  243. enemyAlertSound = info.enemyAlertSound;
  244. enemyDeathSound = info.enemyDeathSound;
  245.  
  246. //Add a navmesh agent to the enemy
  247. if (GetComponent<NavMeshAgent>() == null)
  248. gameObject.AddComponent<NavMeshAgent>();
  249.  
  250. agent = GetComponent<NavMeshAgent>();
  251. agent.angularSpeed = 360f;
  252. agent.speed = info.walkSpeed;
  253. agent.autoBraking = false;
  254. agent.stoppingDistance = 1f;
  255. minAttackDistance = info.minAttackDistance;
  256. maxAttackDistance = info.maxAttackDistance;
  257.  
  258. if (GetComponent<AudioSource>() == null)
  259. gameObject.AddComponent<AudioSource>();
  260. mSource = GetComponent<AudioSource>();
  261. mSource.volume = info.aiVolume;
  262.  
  263. if (info.enemyBehavior == AIData.EnemyType.Patroling)
  264. enemyType = 1;
  265. else
  266. {
  267. enemyType = 2;
  268. patrolPoints = new Transform[0];
  269. }
  270.  
  271. defaultPos = gameObject.transform.position;
  272. positionToInvestigate = defaultPos;
  273. isWaiting = false;
  274.  
  275. distanceToAttackFrom = Random.Range(minAttackDistance, maxAttackDistance);
  276.  
  277. anim = GetComponent<Animation>();
  278. if (anim == null)
  279. anim = gameObject.AddComponent<Animation>();
  280.  
  281. // animationType = AnimationType.Mechanim;
  282.  
  283. if (animationType.Equals(AnimationType.Legacy))
  284. {
  285. string newName;
  286. if (idleAnimations != null && idleAnimations.Length > 0)
  287. {
  288. for (var i = 0; i < idleAnimations.Length; i += 1)
  289. {
  290. newName = idleAnimations[i].name;
  291. idleAnimations[i].legacy = true;
  292. idleAnimations[i].wrapMode = WrapMode.Loop;
  293. anim.AddClip(idleAnimations[i], newName);
  294. }
  295. }
  296.  
  297. if (walkAnimations != null && walkAnimations.Length > 0)
  298. {
  299. for (int i = 0; i < walkAnimations.Length; i += 1)
  300. {
  301. newName = walkAnimations[i].name;
  302. walkAnimations[i].legacy = true;
  303. walkAnimations[i].wrapMode = WrapMode.Loop;
  304. anim.AddClip(walkAnimations[i], newName);
  305. }
  306. }
  307.  
  308. if (runAnimations != null && runAnimations.Length > 0)
  309. {
  310. for (int i = 0; i < runAnimations.Length; i += 1)
  311. {
  312. newName = runAnimations[i].name;
  313. runAnimations[i].legacy = true;
  314. runAnimations[i].wrapMode = WrapMode.Loop;
  315. anim.AddClip(runAnimations[i], newName);
  316. }
  317. }
  318.  
  319. if (aimAnimations != null && aimAnimations.Length > 0)
  320. {
  321. for (int i = 0; i < aimAnimations.Length; i += 1)
  322. {
  323. newName = aimAnimations[i].name;
  324. aimAnimations[i].legacy = true;
  325. aimAnimations[i].wrapMode = WrapMode.Loop;
  326. anim.AddClip(aimAnimations[i], newName);
  327. }
  328. }
  329.  
  330. if (shootAnimations != null && shootAnimations.Length > 0)
  331. {
  332. for (int i = 0; i < shootAnimations.Length; i += 1)
  333. {
  334. newName = shootAnimations[i].name;
  335. shootAnimations[i].legacy = true;
  336. shootAnimations[i].wrapMode = WrapMode.Loop;
  337. anim.AddClip(shootAnimations[i], newName);
  338. }
  339. }
  340.  
  341. if (deathAnimations != null && deathAnimations.Length > 0)
  342. {
  343. for (int i = 0; i < deathAnimations.Length; i += 1)
  344. {
  345. deathAnimations[i].wrapMode = WrapMode.Once;
  346. newName = deathAnimations[i].name;
  347. deathAnimations[i].legacy = true;
  348. deathAnimations[i].wrapMode = WrapMode.Once;
  349. anim.AddClip(deathAnimations[i], newName);
  350. }
  351. }
  352. }
  353.  
  354. animator = GetComponent<Animator>();
  355.  
  356. triggerType = info.triggerType;
  357.  
  358. //play idle anim
  359. PlayIdleAnim();
  360. }
  361.  
  362. public void GotoNextPoint()
  363. {
  364. if (patrolPoints.Length == 0)
  365. return;
  366. // Debug.Log ("Going to next point...");
  367. StartCoroutine(pauseAndContinuePatrol());
  368. }
  369.  
  370. void Update()
  371. {
  372. sightObject.transform.localRotation = transform.parent.rotation;
  373. gunfireSound = info.gunfireSound;
  374. walkSpeed = info.walkSpeed;
  375. runSpeed = info.runSpeed;
  376. maxPatrolWaitTime = info.maxPatrolWaitTime;
  377. maxAttackDistance = info.maxAttackDistance;
  378. minAttackDistance = info.minAttackDistance;
  379. randomPatroler = info.randomPatroler;
  380. switchOffRadarDistance = info.switchOffRadarDistance;
  381.  
  382. if (switchOffRadarDistance < info.sightDistance && switchOffRadarDistance < info.hearingRadius)
  383. {
  384. if (info.sightDistance > info.hearingRadius)
  385. switchOffRadarDistance = info.sightDistance;
  386. else
  387. switchOffRadarDistance = info.hearingRadius;
  388. }
  389.  
  390. retreatDistance = info.retreatDistance;
  391. hasDeathAnim = info.hasDeathAnim;
  392. currentPlayerHealth = PlayerHealthManager.currentPlayerHealth;
  393.  
  394. if (PlayerHealthManager.currentPlayerHealth <= 0)
  395. {
  396. playerIsDead = true;
  397. PlayerHealthManager.ResetHealth();
  398. }
  399.  
  400. if (currentEnemyHealth <= 0 && !hasKilledEnemy)
  401. {
  402. if (enemyDeathSound != null)
  403. mSource.PlayOneShot(enemyDeathSound);
  404. KillEnemy();
  405. }
  406.  
  407. if (!hasKilledEnemy)
  408. {
  409. if (!playerDetected && !isWaiting && agent.remainingDistance <= 1f)
  410. {
  411. GotoNextPoint();
  412. }
  413. }
  414.  
  415. if (playerIsDead || patrolReset)
  416. {
  417. playerDetected = false;
  418. resetPatrol();
  419. }
  420.  
  421. // if player has gone too far
  422. if (playerDetected && agent.remainingDistance > retreatDistance)
  423. {
  424. outRangeTimer += Time.deltaTime;
  425. if(outRangeTimer >= retretTime){
  426. patrolReset = true;
  427. outRangeTimer = 0;
  428. chase = false;
  429. }
  430. }
  431.  
  432. if (getDistanceToPlayer() >= switchOffRadarDistance)
  433. {
  434. sightObject.SetActive(false);
  435. }
  436. else if (getDistanceToPlayer() < switchOffRadarDistance && !enemyRetreating)
  437. {
  438. sightObject.SetActive(true);
  439. }
  440.  
  441. if (isSeeking)
  442. {
  443. positionToInvestigate = player.transform.position;
  444. this.agent.destination = positionToInvestigate;
  445. if (this.agent.transform.position == positionToInvestigate)
  446. {
  447. pauseAndContinuePatrol();
  448. isSeeking = false;
  449. }
  450. }
  451.  
  452. // Hunger Type
  453. if (playerDetected && enemyCharacter == "hunger")
  454. {
  455. if (enemyAlertSound != null && !hasPlayedDetectSound)
  456. mSource.PlayOneShot(enemyAlertSound);
  457. mSource.PlayOneShot(ChaseSoundTrack);
  458. hasPlayedDetectSound = true;
  459.  
  460. isSeeking = false;
  461. isAttacking = true;
  462. this.agent.destination = player.transform.position;
  463.  
  464. agent.stoppingDistance = distanceToAttackFrom;
  465.  
  466. if (Vector3.Distance(transform.position, player.transform.position) > distanceToAttackFrom)
  467. {
  468. PlayRunAnim();
  469.  
  470. agent.speed = runSpeed;
  471. }
  472.  
  473. if (agent.remainingDistance <= distanceToAttackFrom)
  474. {
  475. PlayShootAnim();
  476.  
  477. Vector3 pointToLook = new Vector3(player.transform.position.x, transform.position.y,
  478. player.transform.position.z);
  479. transform.LookAt(pointToLook);
  480. if (!playerIsDead && !hasFiredOne)
  481. {
  482. StartCoroutine(addShootingRandomness());
  483. }
  484. }
  485. }
  486.  
  487. if (playerDetected && enemyCharacter == "lightKiller")
  488. {
  489.  
  490.  
  491. //Stat Enemy
  492. ammountOfPlayerDamage = 50;
  493. retretTime = Random.Range(3f,8f);
  494.  
  495. if(agent.remainingDistance <= 30f)
  496. {
  497. // if(player.GetComponent<PlayerController>().run == true)
  498. // {
  499. // this.agent.destination = player.transform.position;
  500. // }
  501. if(lightDectector.enabled == true)
  502. {
  503. chase = true;
  504. } else chase = false;
  505. }
  506. else if(agent.remainingDistance <= 2){
  507. this.agent.destination = player.transform.position;
  508. }
  509.  
  510. if (playerDetected)
  511. {
  512. if (enemyAlertSound != null && !hasPlayedDetectSound)
  513. mSource.PlayOneShot(enemyAlertSound);
  514. mSource.PlayOneShot(ChaseSoundTrack);
  515. hasPlayedDetectSound = true;
  516.  
  517. isSeeking = false;
  518. isAttacking = true;
  519. this.agent.destination = player.transform.position;
  520.  
  521. agent.stoppingDistance = distanceToAttackFrom;
  522.  
  523. if (Vector3.Distance(transform.position, player.transform.position) > distanceToAttackFrom)
  524. {
  525. PlayRunAnim();
  526.  
  527. agent.speed = runSpeed;
  528. }
  529.  
  530. if (agent.remainingDistance <= distanceToAttackFrom)
  531. {
  532. PlayShootAnim();
  533.  
  534.  
  535.  
  536. Vector3 pointToLook = new Vector3(player.transform.position.x, transform.position.y,
  537. player.transform.position.z);
  538. transform.LookAt(pointToLook);
  539. if (!playerIsDead && !hasFiredOne)
  540. {
  541. StartCoroutine(addShootingRandomness());
  542. }
  543. }
  544. }
  545. }
  546.  
  547.  
  548. if(roarTimer <= 10){
  549. roarTimer += Time.deltaTime;
  550. }
  551. //Blind Type
  552. if(enemyCharacter == "blind")
  553. {
  554. //Stat Enemy
  555. ammountOfPlayerDamage = 50;
  556. retretTime = Random.Range(3f,8f);
  557.  
  558. if(agent.remainingDistance <= 15f)
  559. {
  560. if(player.GetComponent<PlayerController>().run == true)
  561. {
  562. if(roarTimer >= 10f){
  563. animator.SetTrigger("roar");
  564. }
  565. this.agent.destination = player.transform.position;
  566. }
  567. }
  568. else if(agent.remainingDistance <= 5){
  569.  
  570. this.agent.destination = player.transform.position;
  571. }
  572.  
  573. if (playerDetected)
  574. {
  575. if (enemyAlertSound != null && !hasPlayedDetectSound)
  576. mSource.PlayOneShot(enemyAlertSound);
  577. mSource.PlayOneShot(ChaseSoundTrack);
  578. hasPlayedDetectSound = true;
  579.  
  580. isSeeking = false;
  581. isAttacking = true;
  582. this.agent.destination = player.transform.position;
  583.  
  584. agent.stoppingDistance = distanceToAttackFrom;
  585.  
  586. if (Vector3.Distance(transform.position, player.transform.position) > distanceToAttackFrom)
  587. {
  588. PlayRunAnim();
  589.  
  590. agent.speed = runSpeed;
  591. }
  592.  
  593. if (agent.remainingDistance <= distanceToAttackFrom)
  594. {
  595. PlayShootAnim();
  596.  
  597.  
  598.  
  599. Vector3 pointToLook = new Vector3(player.transform.position.x, transform.position.y,
  600. player.transform.position.z);
  601. transform.LookAt(pointToLook);
  602. if (!playerIsDead && !hasFiredOne)
  603. {
  604. StartCoroutine(addShootingRandomness());
  605. }
  606. }
  607. }
  608. }
  609. }
  610.  
  611. // Texas Smash !!
  612. void shoot()
  613. {
  614. if (gunfireSound != null && !hasPlayedShootingSound)
  615. mSource.PlayOneShot(gunfireSound);
  616. hasPlayedShootingSound = true;
  617. player.SendMessage("PlayerTakeDamage", ammountOfPlayerDamage, SendMessageOptions.DontRequireReceiver);
  618. }
  619.  
  620.  
  621. IEnumerator pauseAndContinuePatrol()
  622. {
  623. agent.stoppingDistance = 1f;
  624. isWaiting = true;
  625.  
  626. PlayIdleAnim();
  627.  
  628. float waitTime = Random.Range(maxPatrolWaitTime - 3f, maxPatrolWaitTime);
  629. if (waitTime <= 0f)
  630. waitTime = 1f;
  631.  
  632. yield return new WaitForSeconds(waitTime);
  633.  
  634. if (randomPatroler)
  635. {
  636. agent.destination = patrolPoints[destPoint].position;
  637. int nextPos;
  638. do
  639. {
  640. nextPos = Random.Range(0, patrolPoints.Length);
  641. } while (nextPos == destPoint);
  642.  
  643. destPoint = nextPos;
  644. }
  645. else
  646. {
  647. agent.destination = patrolPoints[destPoint].position;
  648. destPoint = (destPoint + 1) % patrolPoints.Length;
  649. }
  650.  
  651. PlayWalkAnim();
  652.  
  653. isWaiting = false;
  654. }
  655.  
  656. void goToNextPointDirect()
  657. {
  658. if (randomPatroler)
  659. {
  660. agent.destination = patrolPoints[destPoint].position;
  661. int nextPos;
  662. do
  663. {
  664. nextPos = Random.Range(0, patrolPoints.Length);
  665. } while (nextPos == destPoint);
  666.  
  667. destPoint = nextPos;
  668. }
  669. else
  670. {
  671. if (enemyType == 1)
  672. {
  673. agent.destination = patrolPoints[destPoint].position;
  674. destPoint = (destPoint + 1) % patrolPoints.Length;
  675. }
  676. else
  677. {
  678. agent.destination = defaultPos;
  679. }
  680. }
  681.  
  682. PlayWalkAnim();
  683. }
  684.  
  685. public void attackFromElsewhere()
  686. {
  687. StartCoroutine(addShootingRandomness());
  688. }
  689.  
  690. IEnumerator reEnableSight()
  691. {
  692. enemyRetreating = true;
  693. yield return new WaitForSeconds(10f);
  694. sightObject.SetActive(true);
  695. enemyRetreating = false;
  696. }
  697.  
  698. void resetPatrol()
  699. {
  700. hasPlayedDetectSound = false;
  701. mSource.Stop();
  702. // Debug.Log ("Patrol reset...");
  703. if (playerIsDead)
  704. {
  705. sightObject.SetActive(false);
  706. StartCoroutine(reEnableSight());
  707. }
  708.  
  709. patrolReset = false;
  710. playerIsDead = false;
  711. isAttacking = false;
  712. playerDetected = false;
  713. isSeeking = false;
  714. agent.speed = walkSpeed;
  715.  
  716. agent.stoppingDistance = 1f;
  717.  
  718. PlayWalkAnim();
  719.  
  720. if (enemyType == 1 && patrolPoints.Length > 0)
  721. goToNextPointDirect();
  722. else
  723. agent.destination = defaultPos;
  724. }
  725.  
  726. IEnumerator addShootingRandomness()
  727. {
  728. hasFiredOne = true;
  729. float n = Random.Range(1f, 4f);
  730. yield return new WaitForSeconds(n);
  731. shoot();
  732. hasFiredOne = false;
  733. hasPlayedShootingSound = false;
  734. }
  735.  
  736. void playAnimation(Object clip)
  737. {
  738. if (enableCrossFade)
  739. anim.CrossFade(clip.name);
  740. else
  741. anim.Play(clip.name);
  742. }
  743.  
  744. void playAnimation(IList<AnimationClip> clips)
  745. {
  746. if (clips.Count > 0)
  747. {
  748. int rand = Random.Range(0, clips.Count);
  749. if (clips[rand] != null)
  750. {
  751. if (enableCrossFade)
  752. anim.CrossFade(clips[rand].name);
  753. }
  754. }
  755. }
  756.  
  757. float playDeathAnimationReturnLenth(IList<AnimationClip> clips)
  758. {
  759. float duration = 0;
  760. if (clips.Count <= 0) return duration;
  761. var rand = Random.Range(0, clips.Count);
  762. if (clips[rand] == null) return duration;
  763. duration = clips[rand].length;
  764. anim.CrossFade(clips[rand].name);
  765.  
  766. return duration;
  767. }
  768.  
  769. public float getDistanceToPlayer()
  770. {
  771. return Vector3.Distance(transform.position, player.transform.position);
  772. }
  773.  
  774. public void setPlayerDetected()
  775. {
  776. playerDetected = true;
  777. }
  778.  
  779. public void setPlayerNotDetected()
  780. {
  781. playerDetected = false;
  782. }
  783.  
  784. public void setResetPatrolBool()
  785. {
  786. patrolReset = true;
  787. }
  788.  
  789. public void enemyTakeDamage()
  790. {
  791. currentEnemyHealth -= ammountOfEnemyDamage;
  792. }
  793.  
  794. void KillEnemy()
  795. {
  796. //embed enemy kill code
  797. hasKilledEnemy = true;
  798. mSource.Stop();
  799. if (hasDeathAnim && deathAnimations != null && deathAnimations.Length > 0)
  800. {
  801. if (animationType.Equals(AnimationType.Mechanim))
  802. {
  803. PlayDeathAnim();
  804. }
  805. else
  806. {
  807. var deathAnimLength = playDeathAnimationReturnLenth(deathAnimations);
  808.  
  809. if (!(delayBeforeDestroy > 0f)) return;
  810. playerDetected = false;
  811. sightObject.SetActive(false);
  812. StartCoroutine(killEnemyAfterDeathAnim(deathAnimLength));
  813. }
  814. }
  815. else
  816. {
  817. if (deathSpawnItems != null && deathSpawnItems.Length > 0)
  818. {
  819. var location = transform.position;
  820. Destroy(transform.parent.gameObject);
  821. for (int i = 0; i < deathSpawnItems.Length; i += 1)
  822. {
  823. if (deathSpawnItems[i] != null)
  824. {
  825. GameObject go = Instantiate(deathSpawnItems[i], location, Quaternion.identity);
  826. go.AddComponent<TimedDestroy>();
  827. }
  828. }
  829. }
  830. else
  831. {
  832. Destroy(transform.parent.gameObject);
  833. // Debug.Log ("Enemy killed");
  834. }
  835. }
  836. }
  837.  
  838. IEnumerator killEnemyAfterDeathAnim(float lnth)
  839. {
  840. yield return new WaitForSeconds(lnth);
  841. if (deathSpawnItems != null && deathSpawnItems.Length > 0)
  842. {
  843. Vector3 location = transform.position;
  844. Destroy(transform.parent.gameObject);
  845. for (int i = 0; i < deathSpawnItems.Length; i += 1)
  846. {
  847. if (deathSpawnItems[i] != null)
  848. {
  849. GameObject go = Instantiate(deathSpawnItems[i], location, Quaternion.identity);
  850. go.AddComponent<TimedDestroy>();
  851. }
  852. }
  853. }
  854. else
  855. {
  856. Destroy(transform.parent.gameObject, delayBeforeDestroy);
  857. }
  858. }
  859.  
  860. void ResetAllTriggersExcept(string trigger)
  861. {
  862. var allTriggers = new string[]
  863. {idleAnimTrigger, walkAnimTrigger, runAnimTrigger, aimAnimTrigger, shootAnimTrigger, deathAnimTrigger};
  864.  
  865. for (var i = 0; i < allTriggers.Length; i++)
  866. {
  867. if (!allTriggers[i].Equals(trigger))
  868. {
  869. animator.ResetTrigger(allTriggers[i]);
  870. }
  871. }
  872. }
  873.  
  874. void ResetAllBoolsExcept(string trigger)
  875. {
  876. var allTriggers = new string[]
  877. {idleBoolParam, walkBoolParam, runBoolParam, aimBoolParam, shootBoolParam, deathBoolParam};
  878.  
  879. foreach (var t in allTriggers)
  880. {
  881. if (!t.Equals(trigger))
  882. {
  883. animator.SetBool(t, false);
  884. }
  885. }
  886. }
  887.  
  888. void PlayIdleAnim()
  889. {
  890. if (animationType.Equals(AnimationType.Mechanim))
  891. {
  892. switch (triggerType)
  893. {
  894. case MechanimTriggerType.Trigger:
  895. animator.SetTrigger(idleAnimTrigger);
  896. ResetAllTriggersExcept(idleAnimTrigger);
  897. break;
  898. case MechanimTriggerType.Bool:
  899. animator.SetBool(idleBoolParam, true);
  900. ResetAllBoolsExcept(idleBoolParam);
  901. break;
  902. case MechanimTriggerType.Int:
  903. animator.SetInteger(idleAnimInt.triggerName, idleAnimInt.intValue);
  904. break;
  905. }
  906. }
  907. else if (animationType.Equals(AnimationType.Legacy) && idleAnimations != null)
  908. playAnimation(idleAnimations);
  909. }
  910.  
  911. void PlayWalkAnim()
  912. {
  913. if (animationType.Equals(AnimationType.Mechanim))
  914. {
  915. switch (triggerType)
  916. {
  917. case MechanimTriggerType.Trigger:
  918. animator.SetTrigger(walkAnimTrigger);
  919. ResetAllTriggersExcept(walkAnimTrigger);
  920. break;
  921. case MechanimTriggerType.Bool:
  922. animator.SetBool(walkBoolParam, true);
  923. ResetAllBoolsExcept(walkBoolParam);
  924. break;
  925. case MechanimTriggerType.Int:
  926. animator.SetInteger(walkAnimInt.triggerName, walkAnimInt.intValue);
  927. break;
  928. }
  929. }
  930. else if (animationType.Equals(AnimationType.Legacy) && walkAnimations != null)
  931. playAnimation(walkAnimations);
  932. }
  933.  
  934. void PlayRunAnim()
  935. {
  936. if (animationType.Equals(AnimationType.Mechanim))
  937. {
  938. switch (triggerType)
  939. {
  940. case MechanimTriggerType.Trigger:
  941. animator.SetTrigger(runAnimTrigger);
  942. ResetAllTriggersExcept(runAnimTrigger);
  943. break;
  944. case MechanimTriggerType.Bool:
  945. animator.SetBool(runBoolParam, true);
  946. ResetAllBoolsExcept(runBoolParam);
  947. break;
  948. case MechanimTriggerType.Int:
  949. animator.SetInteger(runAnimInt.triggerName, runAnimInt.intValue);
  950. break;
  951. }
  952. }
  953. else if (animationType.Equals(AnimationType.Legacy) && runAnimations != null)
  954. playAnimation(runAnimations);
  955. }
  956.  
  957. void PlayAimAnim()
  958. {
  959. if (animationType.Equals(AnimationType.Mechanim))
  960. {
  961. switch (triggerType)
  962. {
  963. case MechanimTriggerType.Trigger:
  964. animator.SetTrigger(aimAnimTrigger);
  965. ResetAllTriggersExcept(aimAnimTrigger);
  966. break;
  967. case MechanimTriggerType.Bool:
  968. animator.SetBool(aimBoolParam, true);
  969. ResetAllBoolsExcept(aimBoolParam);
  970. break;
  971. case MechanimTriggerType.Int:
  972. animator.SetInteger(aimAnimInt.triggerName, aimAnimInt.intValue);
  973. break;
  974. }
  975. }
  976. else if (animationType.Equals(AnimationType.Legacy) && aimAnimations != null)
  977. playAnimation(aimAnimations);
  978. }
  979.  
  980. void PlayShootAnim()
  981. {
  982. if (animationType.Equals(AnimationType.Mechanim))
  983. {
  984. switch (triggerType)
  985. {
  986. case MechanimTriggerType.Trigger:
  987. animator.SetTrigger(shootAnimTrigger);
  988. ResetAllTriggersExcept(shootAnimTrigger);
  989. break;
  990. case MechanimTriggerType.Bool:
  991. animator.SetBool(shootBoolParam, true);
  992. ResetAllBoolsExcept(shootBoolParam);
  993. break;
  994. case MechanimTriggerType.Int:
  995. animator.SetInteger(shootAnimInt.triggerName, shootAnimInt.intValue);
  996. break;
  997. }
  998. }
  999. else if (animationType.Equals(AnimationType.Legacy) && shootAnimations != null)
  1000. playAnimation(shootAnimations);
  1001. }
  1002.  
  1003. void PlayDeathAnim()
  1004. {
  1005. switch (triggerType)
  1006. {
  1007. case MechanimTriggerType.Trigger:
  1008. animator.SetTrigger(deathAnimTrigger);
  1009. ResetAllTriggersExcept(deathAnimTrigger);
  1010. break;
  1011. case MechanimTriggerType.Bool:
  1012. animator.SetBool(deathBoolParam, true);
  1013. ResetAllBoolsExcept(deathBoolParam);
  1014. break;
  1015. case MechanimTriggerType.Int:
  1016. animator.SetInteger(deathAnimInt.triggerName, deathAnimInt.intValue);
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement