2024-03-13 20:01:39 -05:00
|
|
|
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
|
2024-03-14 18:40:19 -05:00
|
|
|
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;
|
2024-03-13 20:01:39 -05:00
|
|
|
protected BoundsCheck bndCheck;
|
|
|
|
|
|
|
|
void Awake() {
|
|
|
|
bndCheck = GetComponent<BoundsCheck>();
|
2024-03-14 18:40:19 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2024-03-13 20:01:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2024-03-14 18:40:19 -05:00
|
|
|
switch (otherGO.tag) {
|
|
|
|
case "ProjectileHero":
|
|
|
|
Projectile p = otherGO.GetComponent<Projectile>();
|
|
|
|
// 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;
|
2024-03-13 20:01:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:40:19 -05:00
|
|
|
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;
|
2024-03-13 20:01:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
Move();
|
2024-03-14 18:40:19 -05:00
|
|
|
if (showingDamage && Time.time > damageDoneTime) {
|
|
|
|
UnShowDamage();
|
|
|
|
}
|
2024-03-13 20:01:39 -05:00
|
|
|
if (bndCheck != null && bndCheck.offDown) {
|
|
|
|
// We're off the bottom, so destroy this GameObject
|
|
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|