You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class EnemyController : InteractableObject {
  4. public int health;
  5. private GameObject player;
  6. private Rigidbody2D playerRigid;
  7. public GameObject bullet;
  8. public float shotCoolDown = 0.5f;
  9. public float initShotWait = 0.0f;
  10. protected float lastShotTime = 0.0f;
  11. public GameObject healthDisp;
  12. // Use this for initialization
  13. protected virtual void Start () {
  14. lastShotTime = Time.time + initShotWait;
  15. player = GameObject.FindGameObjectWithTag("Player");
  16. playerRigid = player.GetComponent<Rigidbody2D>();
  17. healthDisp.SetActive(true);
  18. }
  19. // Update is called once per frame
  20. void FixedUpdate () {
  21. if (Time.time - lastShotTime >= shotCoolDown) {
  22. lastShotTime = Time.time;
  23. shoot(Vector2.up);
  24. shoot(Vector2.down);
  25. shoot(Vector2.left);
  26. shoot(Vector2.right);
  27. }
  28. }
  29. protected void shoot(Vector2 startVelocity) {
  30. GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
  31. MissileController bulletScript = bulletClone.GetComponent<MissileController>();
  32. bulletScript.startVelocity = startVelocity;
  33. if (player != null)
  34. bulletScript.targetPos = player.transform.position;
  35. bulletScript.target = player;
  36. bulletScript.targetRigid = playerRigid;
  37. bulletScript.ignoreList = GetComponentsInChildren<Collider2D>();
  38. bulletScript.spawningObject = this;
  39. bulletScript.enabled = true;
  40. }
  41. public override void shot() {
  42. health--;
  43. //Debug.Log("I got hit, health: " + health);
  44. if (health <= 0) {
  45. GameMemory.control.enemyDeath();
  46. Destroy(gameObject);
  47. }
  48. Vector3 newScale = new Vector3(health / 10f, health / 10f, health / 10f);
  49. //Debug.Log("new scale = " + newScale);
  50. if (healthDisp != null) {
  51. healthDisp.transform.localScale = newScale;
  52. }
  53. transform.localScale = newScale;
  54. }
  55. }