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.

69 lines
1.9 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. private float lastShotTime = 0.0f;
  10. private GameObject healthDisp;
  11. // Use this for initialization
  12. void Start () {
  13. healthDisp = GetComponentInChildren<ParticleSystem>().gameObject;
  14. player = GameObject.FindGameObjectWithTag("Player");
  15. playerRigid = player.GetComponent<Rigidbody2D>();
  16. }
  17. // Update is called once per frame
  18. void FixedUpdate () {
  19. if (Time.time - lastShotTime >= shotCoolDown) {
  20. lastShotTime = Time.time;
  21. shoot(Vector2.up);
  22. shoot(Vector2.down);
  23. shoot(Vector2.left);
  24. shoot(Vector2.right);
  25. }
  26. }
  27. private void shoot(Vector2 startVelocity) {
  28. GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
  29. MissileController bulletScript = bulletClone.GetComponent<MissileController>();
  30. bulletScript.startVelocity = startVelocity;
  31. bulletScript.targetPos = player.transform.position;
  32. bulletScript.target = player;
  33. bulletScript.targetRigid = playerRigid;
  34. bulletScript.ignoreList = GetComponentsInChildren<Collider2D>();
  35. bulletScript.spawningObject = this;
  36. bulletScript.enabled = true;
  37. }
  38. public override void shot() {
  39. health--;
  40. //Debug.Log("I got hit, health: " + health);
  41. if (health <= 0) {
  42. Destroy(gameObject);
  43. }
  44. Vector3 newScale = new Vector3(health / 10f, health / 10f, health / 10f);
  45. Debug.Log("new scale = " + newScale);
  46. healthDisp.transform.localScale = newScale;
  47. transform.localScale = newScale;
  48. }
  49. }