2022-04-02 11:04:11 -04:00

82 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Entity : MonoBehaviour
{
[field: SerializeField]public float Health { get; private set; }
[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;
Rigidbody2D rb;
void Awake() => rb = GetComponent<Rigidbody2D>();
protected virtual void Start() {
direction = new Vector3(1,0,0);
attackTimer = attackCooldown;
}
protected virtual void Attack(){
// jason: TODO Either have target be Entity instead of transform, or skip Attack when GetComponent<Entity>() is null
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()){
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){
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;
}
}