77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Entity : MonoBehaviour
|
|
{
|
|
[SerializeField]private float health;
|
|
[SerializeField]private float movementSpeed;
|
|
[SerializeField]private float rotSpeed;
|
|
[SerializeField]private float fov;
|
|
[SerializeField]private float attackRange;
|
|
[SerializeField]private float attackDmg;
|
|
[SerializeField]protected float attackCooldown;
|
|
protected float attackTimer;
|
|
[SerializeField]private Transform target;
|
|
private new string name;
|
|
private Collider atkCollider;
|
|
private Vector3 direction;
|
|
|
|
protected virtual void Start() {
|
|
direction = new Vector3(1,0,0);
|
|
attackTimer = attackCooldown;
|
|
}
|
|
|
|
protected virtual void Attack(){
|
|
Entity targetEntity = target.GetComponent<Entity>();
|
|
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
|
}
|
|
|
|
protected virtual void SpecialAttack(){
|
|
|
|
}
|
|
|
|
protected virtual void MoveToTarget(float deltaTime){
|
|
|
|
direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*deltaTime, 0.0f);
|
|
if(!IsInAttackRange()){
|
|
transform.Translate(direction * movementSpeed* deltaTime);
|
|
}
|
|
|
|
}
|
|
|
|
public void SetTarget(Transform newTarget){
|
|
target = newTarget;
|
|
}
|
|
|
|
public Transform GetTarget(){
|
|
return target;
|
|
}
|
|
|
|
//Apply damage to the entity, returns true if it is still alive
|
|
public bool TakeDamage(float amount){
|
|
health -= amount;
|
|
if(health <= 0){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void SetName(string name){
|
|
this.name = name;
|
|
}
|
|
|
|
protected string GetName(){
|
|
return this.name;
|
|
}
|
|
|
|
protected bool IsInAttackRange(){
|
|
return Vector2.Distance(transform.position, target.position) <= attackRange;
|
|
}
|
|
|
|
protected bool IsLookingAtTarget(){
|
|
float angle = Vector2.SignedAngle(direction, (target.position - transform.position));
|
|
return angle >= -fov && angle <= fov;
|
|
}
|
|
}
|