Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5. [RequireComponent(typeof(Character))]
  6.  
  7. public class Enemy : MonoBehaviour {
  8.  
  9. public enum EnemyState
  10.     {
  11.     searching,
  12.     following
  13.     }
  14.  
  15. //time in seconds till the character determines a new heading.
  16.  
  17. public float timeToChangeDirection=5.0f;
  18.  
  19. //amount of damage to do to the player
  20.  
  21. public int damage=5;
  22.  
  23. //how fast this can deal damage
  24.  
  25. public float damageFrequency=1.0f;
  26.      
  27. //internal timer to track last time dealt damage.
  28.  
  29. private float damageTimer;
  30.  
  31. //reference to the character script.
  32.  
  33. private Character character;
  34.  
  35. //internal timer to keep track of elapsed time.
  36.  
  37. private float internalTimer=0.0f;
  38.  
  39. //direction relative to the character to head.
  40.  
  41. private Vector2 heading;
  42.  
  43. private EnemyState currentState;
  44.  
  45. private GameObject target;
  46.  
  47. private GameObject spawnedBy;
  48.  
  49. /// <summary>
  50.  
  51. /// Used for initializing your script component
  52.  
  53. /// </summary>
  54.  
  55. private void Awake () {
  56.  
  57. //cache the character script
  58.  
  59. this.character = this.GetComponent<Character>();
  60.  
  61. //set an initial heading.
  62.  
  63. this.createNewHeading();
  64.  
  65. //set internal timer for damagefrequency
  66.  
  67. this.damageTimer=this.damageFrequency;
  68.  
  69. this.currentState=EnemyState.searching;
  70.  
  71. }
  72.  
  73. /// <summary>
  74.  
  75. /// called after all awakes are called.
  76.  
  77. /// </summary>
  78.  
  79. private void Start()
  80.  
  81. {
  82.  
  83. this.character.SetDeathFunction(() => {
  84.  
  85. //destroy the game object.
  86.  
  87. Destroy(this.gameObject);
  88.  
  89. });
  90.  
  91. }
  92.  
  93.  
  94.  
  95. /// <summary>
  96.  
  97. /// Update that is called once per frame.
  98.  
  99. /// </summary>
  100.  
  101. private void Update () {
  102.  
  103. //increment our damage timer by the time it took to complete the last frame.
  104.  
  105. this.damageTimer+=Time.deltaTime;
  106.  
  107. }
  108.  
  109. /// <summary>
  110.  
  111. /// Update that is kept in sync with physics.
  112.  
  113. /// </summary>
  114.  
  115. private void FixedUpdate()
  116.  
  117. {
  118.  
  119. //if we are searching, call the search function
  120.  
  121. if(this.currentState==EnemyState.searching)
  122.  
  123. {
  124.  
  125. this.Search();
  126.  
  127. }else if(this.currentState==EnemyState.following)
  128.  
  129. {
  130.  
  131. //if we are following, call the follow function
  132.  
  133. this.Follow();
  134.  
  135. }
  136.  
  137. }
  138.  
  139. /// <summary>
  140.  
  141. /// The enemy follows the player.
  142.  
  143. /// </summary>
  144.  
  145. private void Follow()
  146.  
  147. {
  148.  
  149. //vector math says position to go minus current position = vector we should head in.
  150.  
  151. Vector3 nTransform=this.target.transform.position-this.transform.position;
  152.  
  153. //make sure its of magnitude 1.
  154.  
  155. nTransform.Normalize();
  156.  
  157. //move that direction
  158.  
  159. character.Move(nTransform);
  160.  
  161. }
  162.  
  163. /// <summary>
  164.  
  165. /// The enemy searches for the player.
  166.  
  167. /// </summary>
  168.  
  169. private void Search()
  170.  
  171. {
  172.  
  173. //increase the internal timer by
  174.  
  175. //the amount of time that has passed.
  176.  
  177. this.internalTimer+=Time.deltaTime;
  178.  
  179. //determine if we need to create a new heading.
  180.  
  181. if (this.internalTimer>this.timeToChangeDirection)
  182.  
  183. {
  184.  
  185. //create a new heading
  186.  
  187. this.createNewHeading();
  188.  
  189. //reset the timer.
  190.  
  191. this.internalTimer=0.0f;
  192.  
  193. }
  194.  
  195. //move the character in the direction of its heading
  196.  
  197. character.Move(heading);
  198.  
  199. }
  200.  
  201. /// <summary>
  202.  
  203. /// Generates a new random heading for the character
  204.  
  205. /// and applies that heading immediately.
  206.  
  207. /// </summary>
  208.  
  209. private void createNewHeading()
  210.  
  211. {
  212.  
  213. //I used -100 to 100 to give an equal chance for any direction
  214.  
  215. float xCoord=Random.Range(-100.0f, 100.0f);
  216.  
  217. float yCoord=Random.Range(-100.0f, 100.0f);
  218.  
  219. //set our heading to the random headings.
  220.  
  221. heading=new Vector2(xCoord, yCoord);
  222.  
  223. //make sure the magnitude of our heading is 1
  224.  
  225. heading.Normalize();
  226.  
  227. }
  228.  
  229. /// <summary>
  230.  
  231. /// Called when a collision happens between the character
  232.  
  233. /// and something else.
  234.  
  235. /// </summary>
  236.  
  237. /// <param name=“other”>What you collided with.</param>
  238.  
  239. private void OnCollisionEnter2D(Collision2D other)
  240.  
  241. {
  242.  
  243. //make sure we don’t do too much damage to the player
  244.  
  245. if (other.gameObject.tag=="Player" && this.damageTimer>this.damageFrequency)
  246.  
  247. {
  248.  
  249. //deal some damage!
  250.  
  251. other.gameObject.GetComponent<Character>().AdjustHealth(-1*this.damage);
  252.  
  253. //reset the timer so we don’t damage too much again.
  254.  
  255. this.damageTimer=0.0f;
  256.  
  257. }
  258.  
  259. //create a new heading, you ran into something.
  260.  
  261. this.createNewHeading();
  262.  
  263. }
  264.  
  265. public void SetSpawnedBy(GameObject o)
  266.  
  267. {
  268.  
  269. this.spawnedBy=o;
  270.  
  271. }
  272.  
  273. public GameObject GetSpawnedBy()
  274.  
  275. {
  276.  
  277. return this.spawnedBy;
  278.  
  279. }
  280.  
  281. /// <summary>
  282.  
  283. /// Occurs when something enters its trigger zone.
  284.  
  285. /// </summary>
  286.  
  287. /// <param name=“other”></param>
  288.  
  289. private void OnTriggerEnter2D(Collider2D other)
  290.  
  291. {
  292.  
  293. //check to see what this thing is
  294.  
  295. if (other.gameObject.tag=="Player")
  296.  
  297. {
  298.  
  299. //Its the player! FOLLOW IT!
  300.  
  301. this.currentState=EnemyState.following;
  302.  
  303. //set the target.
  304.  
  305. this.target=other.gameObject;
  306.  
  307. }
  308.  
  309. }
  310.  
  311. /// <summary>
  312.  
  313. /// Occurs when something enters its trigger zone.
  314.  
  315. /// </summary>
  316.  
  317. /// <param name=“other”></param>
  318.  
  319. private void OnTriggerExit2D(Collider2D other)
  320.  
  321. {
  322.  
  323. //Check to see what it is
  324.  
  325. if (other.gameObject.tag=="Player")
  326.  
  327. {
  328.  
  329. //Its the player, I can’t see it anymore, stop following.
  330.  
  331. this.currentState=EnemyState.searching;
  332.  
  333. }
  334.  
  335. }
  336.  
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement