78 lines
2.2 KiB
C#
78 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
public class Entity : MonoBehaviour
|
|
{
|
|
[field: SerializeField]protected float Health {get; private set; }
|
|
bool isAlive = true;
|
|
[SerializeField]public float movementSpeed{get; private set; }
|
|
[SerializeField]public float rotSpeed {get; private set; }
|
|
[SerializeField]private float fov;
|
|
[SerializeField]private float attackRange;
|
|
[SerializeField]public float attackDmg {get; private set; }
|
|
[SerializeField]protected float attackCooldown;
|
|
protected float attackTimer;
|
|
[SerializeField]private Transform target;
|
|
public string entityName {get; protected set; }
|
|
private Collider atkCollider;
|
|
public Vector3 direction {get; set; }
|
|
public Rigidbody2D rb {get; private set;}
|
|
|
|
void Awake() => rb = GetComponent<Rigidbody2D>();
|
|
|
|
protected virtual void Start() {
|
|
direction = new Vector3(1,0,0);
|
|
attackTimer = attackCooldown;
|
|
}
|
|
|
|
protected virtual void Attack(){
|
|
|
|
}
|
|
|
|
protected virtual void SpecialAttack(){
|
|
|
|
}
|
|
|
|
protected virtual void MoveToTarget(float deltaTime){
|
|
|
|
direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*deltaTime, 0.0f);
|
|
if(!IsInAttackRange()){
|
|
rb.MovePosition(transform.position + 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 virtual bool TakeDamage(float amount){
|
|
Health -= amount;
|
|
if(Health <= 0){
|
|
isAlive = false;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public bool IsAlive(){
|
|
return isAlive;
|
|
}
|
|
}
|