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

using UnityEngine;
using System.Collections;
public class EnemyController : InteractableObject {
public int health;
private GameObject player;
private Rigidbody2D playerRigid;
public GameObject bullet;
public float shotCoolDown = 0.5f;
public float initShotWait = 0.0f;
protected float lastShotTime = 0.0f;
private GameObject healthDisp;
// Use this for initialization
protected virtual void Start () {
lastShotTime += initShotWait;
healthDisp = GetComponentInChildren<ParticleSystem>().gameObject;
player = GameObject.FindGameObjectWithTag("Player");
playerRigid = player.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
if (Time.time - lastShotTime >= shotCoolDown) {
lastShotTime = Time.time;
shoot(Vector2.up);
shoot(Vector2.down);
shoot(Vector2.left);
shoot(Vector2.right);
}
}
protected void shoot(Vector2 startVelocity) {
GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
MissileController bulletScript = bulletClone.GetComponent<MissileController>();
bulletScript.startVelocity = startVelocity;
if (player != null)
bulletScript.targetPos = player.transform.position;
bulletScript.target = player;
bulletScript.targetRigid = playerRigid;
bulletScript.ignoreList = GetComponentsInChildren<Collider2D>();
bulletScript.spawningObject = this;
bulletScript.enabled = true;
}
public override void shot() {
health--;
//Debug.Log("I got hit, health: " + health);
if (health <= 0) {
Destroy(gameObject);
}
Vector3 newScale = new Vector3(health / 10f, health / 10f, health / 10f);
Debug.Log("new scale = " + newScale);
if (healthDisp != null) {
healthDisp.transform.localScale = newScale;
}
transform.localScale = newScale;
}
}