Entity can damage another with periodic attacks

This commit is contained in:
Soulaha Balde 2022-04-02 01:29:49 -04:00
parent 10e9fc074b
commit fd74f48350
7 changed files with 144 additions and 29 deletions

View File

@ -218,10 +218,10 @@ GameObject:
- component: {fileID: 1736226634}
- component: {fileID: 1736226633}
- component: {fileID: 1736226632}
- component: {fileID: 1736226636}
- component: {fileID: 1736226637}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Name: Monster
m_TagString: Monster
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -302,7 +302,7 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1736226636
--- !u!114 &1736226637
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@ -311,15 +311,16 @@ MonoBehaviour:
m_GameObject: {fileID: 1736226631}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ea4f715fcb3a0324a9e5d7c1ac896b9d, type: 3}
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
m_Name:
m_EditorClassIdentifier:
health: 0
movementSpeed: 3
rotSpeed: 10
fov: 30
health: 100
movementSpeed: 5
rotSpeed: 3
fov: 0
attackRange: 1.5
attackDmg: 0
attackDmg: 10
attackCooldown: 1
target: {fileID: 1996499837}
--- !u!1 &1996499833
GameObject:
@ -333,9 +334,10 @@ GameObject:
- component: {fileID: 1996499836}
- component: {fileID: 1996499835}
- component: {fileID: 1996499834}
- component: {fileID: 1996499838}
m_Layer: 0
m_Name: Target
m_TagString: Untagged
m_Name: Gladiator
m_TagString: Gladiator
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -416,3 +418,23 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1996499838
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1996499833}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3}
m_Name:
m_EditorClassIdentifier:
health: 100
movementSpeed: 5
rotSpeed: 3
fov: 0
attackRange: 1.5
attackDmg: 10
attackCooldown: 1
target: {fileID: 1736226635}

View File

@ -10,44 +10,67 @@ public class Entity : MonoBehaviour
[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;
public Entity(float health, float movementSpeed, float rotSpeed, float attackRange, float attackDmg, Transform target){
this.health = health;
this.movementSpeed = movementSpeed;
this.rotSpeed = rotSpeed;
this.attackRange = attackRange;
this.attackDmg = attackDmg;
this.target = target;
}
private void Start() {
protected virtual void Start() {
direction = new Vector3(1,0,0);
}
private void Update() {
MoveToTarget(transform, Time.deltaTime);
attackTimer = attackCooldown;
}
protected virtual void Attack(){
Entity targetEntity = target.GetComponent<Entity>();
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
}
protected virtual void SpecialAttack(){
}
protected virtual void MoveToTarget(Transform transform, float deltaTime){
protected virtual void MoveToTarget(float deltaTime){
direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*deltaTime, 0.0f);
if( !IsInAttackRange())
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;
}
}

View File

@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gladiator : Entity
{
// Start is called before the first frame update
override protected void Start()
{
base.Start();
base.SetName("Gladiator");
}
// Update is called once per frame
void Update()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c537e1bd61b8d5c42b1ec03f90e14855
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

27
Assets/Scripts/Monster.cs Normal file
View File

@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Entity
{
// Start is called before the first frame update
override protected void Start()
{
base.Start();
base.SetName("Monster");
}
// Update is called once per frame
void Update()
{
MoveToTarget(Time.deltaTime);
if(IsInAttackRange()){
if(attackTimer >= attackCooldown){
Attack();
attackTimer = 0;
}else{
attackTimer += Time.deltaTime;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7e480b0ef5998894283e8091830941cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,7 +3,9 @@
--- !u!78 &1
TagManager:
serializedVersion: 2
tags: []
tags:
- Monster
- Gladiator
layers:
- Default
- TransparentFX