Files
TowerDefence/My project/Assets/Scripts/Enemy.cs
Caleb Sandford deQuincey 334021d04e Started project
2025-11-14 17:30:41 +00:00

84 lines
2.4 KiB
C#

using UnityEngine;
public class Enemy : MonoBehaviour
{
public int health;
public int damageToPlayer;
public int moneyOnDeath;
public float moveSpeed;
//Path
private Transform[] path;
private int curPathWaypoint;
private Animator anim;
void Start()
{
path = GameManager.instance.enemyPath.waypoints;
// Get the Animator component
anim = GetComponentInChildren<Animator>();
anim.SetInteger("WeaponType_int", 0); // Default as no weapon for movement, to change when attacking
anim.SetBool("Static_b", false);
}
void Update()
{
MoveAlongPath();
}
void MoveAlongPath()
{
if(curPathWaypoint < path.Length)
{
transform.position = Vector3.MoveTowards(transform.position, path[curPathWaypoint].position, moveSpeed * Time.deltaTime);
transform.LookAt(path[curPathWaypoint].position);
anim.SetFloat("Speed_f", moveSpeed);
anim.SetFloat("Body_Horizontal_f", 0.3f);
anim.SetFloat("Body_Vertical_f", 0.6f);
if(transform.position == path[curPathWaypoint].position)
{
curPathWaypoint++;
}
}
else
{
SetIdleState();
GameManager.instance.TakeDamage(damageToPlayer);
GameManager.instance.onEnemyDestroyed.Invoke();
Destroy(gameObject);
}
}
void SetIdleState()
{
anim.SetFloat("Speed_f", 0.0f);
// Idle body offsets for pistol (no attack)
anim.SetFloat("Body_Horizontal_f", 0f);
anim.SetFloat("Body_Vertical_f", 0.6f); // Idle = 0, 0 for Pistol
// Optional: set a default idle pose if you want a specific idle animation
anim.SetInteger("Animation_int", 2); // 0 = normal idle
}
public void TakeDamage(int damage)
{
health -= damage;
if(health <= 0)
{
GameManager.instance.AddMoney(moneyOnDeath);
GameManager.instance.onEnemyDestroyed.Invoke();
StopMovement();
anim.SetInteger("DeathType_int", 1);
anim.SetTrigger("Death_b");
Destroy(gameObject, 2f); // Delay to allow death animation to play
}
}
void StopMovement()
{
moveSpeed = 0;
anim.SetFloat("Speed_f", 0f);
curPathWaypoint = path.Length; // Prevent further movement
}
}