Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6.  
  7.  
  8. public class EnemyAIRanged : Bolt.EntityEventListener<IIonCanonSpider>
  9. {
  10.  
  11.  
  12. [SerializeField]
  13. GameObject ionBulletSpawnPoint;
  14.  
  15. [SerializeField]
  16. GameObject ionBulletPrefab;
  17.  
  18. [SerializeField]
  19. float ionBulletSpeed = 200f;
  20.  
  21. [SerializeField]
  22. GameObject sheelIjectionPrefab;
  23.  
  24. float muzzleFlashTimer;
  25. float muzzleFlashTimerDuration = 1f;
  26.  
  27. Transform _target;
  28. bool _setNewTarget = true;
  29. float _timeOutTimer;
  30. float _timeOutTimervalue = 3;
  31. NavMeshAgent _agent;
  32. bool _canTrackPlayer = false;
  33.  
  34. [SerializeField]
  35. List<GameObject> _wayPoints;
  36. bool _canTravelToWayPoints = true;
  37. private float _wayPointWaitTimer;
  38. private float _wayPointWaitTimerValue;
  39. int _nextWayPoint = 0;
  40.  
  41. private bool _canSetTracking = true;
  42.  
  43. // Shoot references
  44. public float _shootRateTimerDuration = 1f;
  45. private float _shootTimer;
  46.  
  47. [SerializeField]
  48. Animator _anim;
  49.  
  50. [SerializeField]
  51. private float _viewAngle = 45;
  52.  
  53. [SerializeField]
  54. private float _attackDistance = 2;
  55.  
  56. [SerializeField]
  57. private float _rangedAttackDistance = 10;
  58.  
  59. bool _canAttack;
  60. float distance;
  61. public float wayPointStoppingDistance = 2;
  62.  
  63. Vector3 direction;
  64.  
  65. [SerializeField]
  66. GameObject muzzleFlash;
  67.  
  68. private bool hasStopped;
  69.  
  70. public GameObject _shellIjectionPosition;
  71.  
  72.  
  73. IBMPlayerState playerState;
  74.  
  75. public override void Attached()
  76. {
  77.  
  78.  
  79. if (!entity.hasControl && !entity.isOwner)
  80. {
  81. state.SetTransforms(state.transform, transform);
  82. state.SetAnimator(GetComponent<Animator>());
  83. state.Animator.SetLayerWeight(0, 1);
  84. state.Animator.SetLayerWeight(1, 1);
  85. }
  86.  
  87.  
  88.  
  89. //_target.position = Vector3.zero;
  90. base.Attached();
  91.  
  92. if (BoltNetwork.IsServer)
  93. {
  94. state.SetTransforms(state.transform, transform);
  95. state.SetAnimator(GetComponent<Animator>());
  96. state.Animator.SetLayerWeight(0, 1);
  97. state.Animator.SetLayerWeight(1, 1);
  98.  
  99. //state.Target = _target.transform.position;
  100.  
  101.  
  102. _anim = GetComponent<Animator>();
  103. state.SetAnimator(GetComponent<Animator>());
  104. _agent = GetComponent<NavMeshAgent>();
  105. _agent.stoppingDistance = wayPointStoppingDistance;
  106. _canTravelToWayPoints = true;
  107.  
  108. _wayPoints.AddRange(GameObject.FindGameObjectsWithTag("EnemyWayPoints"));
  109. var rand = Random.Range(0, _wayPoints.Count);
  110. _nextWayPoint = rand;
  111. _agent.SetDestination(_wayPoints[rand].transform.position);
  112. _agent.speed = 4;
  113. _agent.isStopped = false;
  114. if (_anim != null)
  115. {
  116. _anim.speed = 1;
  117. _anim.SetBool("Run", false);
  118. _anim.SetBool("Idle", false);
  119. _anim.SetBool("Walk", true);
  120. state.Idle = _anim.GetBool("Idle");
  121. state.Walk = _anim.GetBool("Walk");
  122. state.Run = _anim.GetBool("Run");
  123. }
  124. }
  125. }
  126.  
  127. // Update is called once per frame
  128.  
  129. void FixedUpdate()
  130. {
  131.  
  132. if (BoltNetwork.IsServer)
  133. {
  134. RangedAttack();
  135. }
  136.  
  137. }
  138.  
  139. private void RangedAttack()
  140. {
  141.  
  142.  
  143. if (_target != null)
  144. {
  145. //state.Target = _target.position;
  146. DirectionIs();
  147. float angle = Vector3.Angle(direction, transform.forward);
  148.  
  149. distance = Vector3.Distance(_target.position, this.transform.position);
  150.  
  151. direction.y = 0;
  152. if (_target != null)
  153. {
  154. BoltEntity pEntity = _target.GetComponent<BoltEntity>();
  155. playerState = pEntity.GetState<IBMPlayerState>();
  156. }
  157. if (distance < 20 && angle < _viewAngle && !playerState.Dead)
  158. {
  159. _canTrackPlayer = true;
  160. _canTravelToWayPoints = false;
  161. _agent.stoppingDistance = _attackDistance;
  162. if (_anim != null)
  163. {
  164. _anim.SetBool("Run", false);
  165. _anim.SetBool("Idle", true);
  166. _anim.SetBool("Walk", false);
  167. state.Idle = _anim.GetBool("Idle");
  168. state.Walk = _anim.GetBool("Walk");
  169. state.Run = _anim.GetBool("Run");
  170. }
  171. }
  172.  
  173. if (_canTrackPlayer)
  174. {
  175. if (playerState.Dead)
  176. {
  177. _agent.isStopped = false;
  178. _target = null;
  179. _timeOutTimer = 0;
  180. _setNewTarget = true;
  181. _canTrackPlayer = false;
  182. _agent.stoppingDistance = .5f;
  183. _agent.speed = 2f;
  184. _canTravelToWayPoints = true;
  185. _anim.speed = 1;
  186. state.AnimSpeed = _anim.speed;
  187. _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
  188.  
  189. if (_anim != null)
  190. {
  191. _anim.SetBool("Run", false);
  192. _anim.SetBool("Idle", false);
  193. _anim.SetBool("Walk", true);
  194. state.Idle = _anim.GetBool("Idle");
  195. state.Walk = _anim.GetBool("Walk");
  196. state.Run = _anim.GetBool("Run");
  197. }
  198. return;
  199. }
  200.  
  201.  
  202. transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.3f);
  203.  
  204. if (distance <= _attackDistance && !playerState.Dead)
  205. {
  206. _shootTimer += Time.deltaTime;
  207. if (_shootTimer > _shootRateTimerDuration)
  208. {
  209.  
  210. if (BoltNetwork.IsServer)
  211. {
  212. //Instantiate Bullet
  213. var entity = GetComponentInParent<BoltEntity>();
  214. var rotation = entity.GetComponent<Transform>().rotation;
  215. var newpos = new Vector3(muzzleFlash.transform.position.x, muzzleFlash.transform.position.y, muzzleFlash.transform.position.z + 1);
  216. // create shell
  217. var _ionBullet = BoltNetwork.Instantiate(ionBulletPrefab, ionBulletSpawnPoint.transform.position, rotation);
  218.  
  219. _ionBullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, ionBulletSpeed), ForceMode.VelocityChange);
  220.  
  221. GameObject go = BoltNetwork.Instantiate(sheelIjectionPrefab, _shellIjectionPosition.transform.position, rotation);
  222. go.GetComponent<Rigidbody>().AddRelativeForce(2f, 3f, -2f, ForceMode.Impulse);
  223. go.GetComponent<Rigidbody>().AddTorque(0, 120, 400, ForceMode.VelocityChange);
  224.  
  225. CmdAttack();
  226. }
  227. _shootTimer = 0;
  228. }
  229.  
  230.  
  231. _agent.isStopped = true;
  232. _canAttack = true;
  233. _timeOutTimer = 0;
  234. _anim.speed = 1;
  235. state.AnimSpeed = _anim.speed;
  236. _agent.stoppingDistance = _attackDistance;
  237.  
  238. if (_anim != null)
  239. {
  240. _anim.SetBool("Run", false);
  241. _anim.SetBool("Idle", true);
  242. _anim.SetBool("Walk", false);
  243. state.Idle = _anim.GetBool("Idle");
  244. state.Walk = _anim.GetBool("Walk");
  245. state.Run = _anim.GetBool("Run");
  246. }
  247. }
  248.  
  249. if (distance < 15 && distance > _attackDistance)
  250. {
  251.  
  252. _agent.isStopped = false;
  253. _canAttack = false;
  254. _agent.SetDestination(_target.position);
  255. _agent.speed = 8;
  256. _timeOutTimer = 0;
  257. _anim.speed = 2;
  258. state.AnimSpeed = _anim.speed;
  259. _agent.stoppingDistance = _attackDistance;
  260. if (_anim != null)
  261. {
  262. _anim.SetBool("Run", true);
  263. _anim.SetBool("Idle", false);
  264. _anim.SetBool("Walk", false);
  265. state.Idle = _anim.GetBool("Idle");
  266. state.Walk = _anim.GetBool("Walk");
  267. state.Run = _anim.GetBool("Run");
  268. }
  269. }
  270. else if (distance >= 15)// just changed
  271. {
  272.  
  273.  
  274. _timeOutTimer += Time.deltaTime;
  275. if (_timeOutTimer >= _timeOutTimervalue)
  276. {
  277. //_target.position = Vector3.zero;
  278. _agent.isStopped = false;
  279. _target = null;
  280. _timeOutTimer = 0;
  281. _setNewTarget = true;
  282. _canTrackPlayer = false;
  283. _agent.stoppingDistance = wayPointStoppingDistance;
  284. _canTravelToWayPoints = true;
  285. _anim.speed = 1;
  286. state.AnimSpeed = _anim.speed;
  287. _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
  288. if (_anim != null)
  289. {
  290. _anim.SetBool("Run", false);
  291. _anim.SetBool("Idle", false);
  292. _anim.SetBool("Walk", true);
  293. state.Idle = _anim.GetBool("Idle");
  294. state.Walk = _anim.GetBool("Walk");
  295. state.Run = _anim.GetBool("Run");
  296. }
  297.  
  298. }
  299. }
  300.  
  301. }
  302.  
  303. NavigateEnemy();
  304. }
  305. }
  306.  
  307.  
  308.  
  309. private void MeleeWander()
  310. {
  311.  
  312. }
  313.  
  314. private void MuzzleFlash()
  315. {
  316.  
  317. }
  318.  
  319.  
  320. private void NavigateEnemy()
  321. {
  322.  
  323. if (_canTravelToWayPoints)
  324. {
  325. RotationDirection();
  326.  
  327. var waypointNextDistance = Vector3.Distance(_target.position, _wayPoints[_nextWayPoint].transform.position);
  328.  
  329. if (waypointNextDistance < wayPointStoppingDistance && !hasStopped)
  330. {
  331. Debug.Log("Starting navigation sequence!");
  332. hasStopped = true;
  333. StartCoroutine(NavigateLogic());
  334.  
  335. }
  336. else if (hasStopped && waypointNextDistance > 2f)
  337. {
  338. hasStopped = false;
  339. }
  340. }
  341.  
  342. }
  343.  
  344. IEnumerator NavigateLogic()
  345. {
  346. hasStopped = true;
  347. _agent.isStopped = true;
  348. _agent.speed = 0;
  349. if (_anim != null)
  350. {
  351. _anim.SetBool("Run", false);
  352. _anim.SetBool("Idle", true);
  353. _anim.SetBool("Walk", false);
  354. state.Idle = _anim.GetBool("Idle");
  355. state.Walk = _anim.GetBool("Walk");
  356. state.Run = _anim.GetBool("Run");
  357. }
  358. Debug.Log("Navigating in Idle");
  359. yield return new WaitForSeconds(Random.Range(.5f, 2f));
  360.  
  361. _nextWayPoint = Random.Range(0, _wayPoints.Count);
  362. _wayPointWaitTimerValue = Random.Range(.5f, 2f);
  363. //RandomNavmeshLocation(4f);
  364. _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
  365. _wayPointWaitTimer = 0;
  366. _agent.isStopped = false;
  367. _agent.speed = 4;
  368.  
  369. if (_anim != null)
  370. {
  371. _anim.SetBool("Run", false);
  372. _anim.SetBool("Idle", false);
  373. _anim.SetBool("Walk", true);
  374. state.Idle = _anim.GetBool("Idle");
  375. state.Walk = _anim.GetBool("Walk");
  376. state.Run = _anim.GetBool("Run");
  377. }
  378. Debug.Log("Going to Way Point");
  379. }
  380.  
  381. private void DirectionIs()
  382. {
  383.  
  384. direction = _target.position - this.transform.position;
  385.  
  386. }
  387. private void RotateToAttacker()
  388. {
  389.  
  390. Vector3 newDirection = _target.position - transform.position;
  391. newDirection.y = 0;
  392.  
  393. if (newDirection != Vector3.zero)
  394. {
  395. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newDirection), 0.3f);
  396. }
  397.  
  398. }
  399.  
  400. private void RotationDirection()
  401. {
  402.  
  403. Vector3 newDirection = _wayPoints[_nextWayPoint].transform.position - transform.position;
  404. newDirection.y = 0;
  405.  
  406. if (newDirection != Vector3.zero)
  407. {
  408. if (transform.forward != Vector3.zero)
  409. {
  410. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(transform.forward), Time.deltaTime * 0.3f
  411. );
  412. }
  413. }
  414.  
  415. }
  416.  
  417. //[Command] ------------------Update
  418. private void CmdAttack()
  419. {
  420. if (BoltNetwork.IsServer)
  421. {
  422. muzzleFlash.SetActive(true);
  423. Invoke("ResetFlash", .1f);
  424. Debug.Log("Fired and should of flashed");
  425. }
  426. }
  427.  
  428. private void ResetFlash()
  429. {
  430. if (BoltNetwork.IsServer)
  431. {
  432. muzzleFlash.SetActive(false);
  433. }
  434. }
  435.  
  436. public bool CanAttack()
  437. {
  438. return _canAttack;
  439. }
  440.  
  441. private void OnTriggerEnter(Collider other)
  442. {
  443.  
  444.  
  445. if (other.gameObject.tag == "Player")
  446. {
  447. if (_setNewTarget)
  448. {
  449. _target = other.gameObject.transform;
  450. //state.Target = _target.transform.position;
  451. playerState = _target.GetComponent<IBMPlayerState>();
  452. _setNewTarget = false;
  453. }
  454. }
  455.  
  456. }
  457. private void OnTriggerStay(Collider other)
  458. {
  459.  
  460. // Debug.Log("ENTERED ONTRIGGER: " + other.gameObject.name.ToString());
  461.  
  462. if (_target == null && other.gameObject.tag == "Player")
  463. {
  464.  
  465. _target = other.gameObject.transform;
  466. //state.Target = _target.transform.position;
  467. playerState = _target.GetComponent<IBMPlayerState>();
  468. _setNewTarget = false;
  469.  
  470. }
  471.  
  472. }
  473.  
  474. public void DestroyMe()
  475. {
  476. if (BoltNetwork.IsServer)
  477. {
  478. BoltNetwork.Destroy(entity.gameObject);
  479. }
  480. }
  481.  
  482. //Wandom random point
  483. public Vector3 RandomNavmeshLocation(float radius)
  484. {
  485. Vector3 randomDirection = Random.insideUnitSphere * radius;
  486. randomDirection += transform.position;
  487. NavMeshHit hit;
  488. Vector3 finalPosition = Vector3.zero;
  489. if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1))
  490. {
  491. finalPosition = hit.position;
  492. }
  493. return finalPosition;
  494. }
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement