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.

74 lines
2.0 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. private GameObject healthDisp;
  12. // Use this for initialization
  13. protected virtual void Start () {
  14. lastShotTime += initShotWait;
  15. healthDisp = GetComponentInChildren<ParticleSystem>().gameObject;
  16. player = GameObject.FindGameObjectWithTag("Player");
  17. playerRigid = player.GetComponent<Rigidbody2D>();
  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. Destroy(gameObject);
  46. }
  47. Vector3 newScale = new Vector3(health / 10f, health / 10f, health / 10f);
  48. Debug.Log("new scale = " + newScale);
  49. if (healthDisp != null) {
  50. healthDisp.transform.localScale = newScale;
  51. }
  52. transform.localScale = newScale;
  53. }
  54. }