using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { [Header("Set in Inspector: Enemy")] public float speed = 10f; // The speed in m/s public float fireRate = 0.3f; // Seconds/shot (Unused) public float health = 10; public int score = 100; // Points earned for destroying this public float showDamageDuration = 0.1f; [Header("Set Dynamically: Enemy")] public Color[] originalColors; public Material[] materials; public bool showingDamage = false; public float damageDoneTime; public bool notifiedOfDestruction = false; protected BoundsCheck bndCheck; void Awake() { bndCheck = GetComponent(); // Get materials and colors for this GameObject and its children materials = Utils.GetAllMaterials(gameObject); originalColors = new Color[materials.Length]; for (int i = 0; i < materials.Length; ++i) { originalColors[i] = materials[i].color; } } // This is a Property: A method that acts like a field public Vector3 pos { get { return( this.transform.position ); } set { this.transform.position = value; } } public virtual void Move() { Vector3 tempPos = pos; tempPos.y -= speed * Time.deltaTime; pos = tempPos; } void OnCollisionEnter(Collision coll) { GameObject otherGO = coll.gameObject; switch (otherGO.tag) { case "ProjectileHero": Projectile p = otherGO.GetComponent(); // If this Enemy is off screen, don't damage it. if (!bndCheck.isOnScreen) { Destroy(otherGO); break; } // Hurt this Enemy ShowDamage(); health -= 1; if (health <= 0) { if (!notifiedOfDestruction) { Main.S.ShipDestroyed(this); } notifiedOfDestruction = true; // Destroy this Enemy Destroy(this.gameObject); } Destroy(otherGO); break; default: print("Enemy hit by non-ProjectileHero: " + otherGO.name ); break; } } void ShowDamage() { foreach(Material m in materials) { m.color = Color.red; } showingDamage = true; damageDoneTime = Time.time + showDamageDuration; } void UnShowDamage() { for (int i = 0; i < materials.Length; ++i) { materials[i].color = originalColors[i]; } showingDamage = false; } void Update() { Move(); if (showingDamage && Time.time > damageDoneTime) { UnShowDamage(); } if (bndCheck != null && bndCheck.offDown) { // We're off the bottom, so destroy this GameObject Destroy(gameObject); } } }