From fd74f4835087411b611cb9f827a5802c5e6fd89d Mon Sep 17 00:00:00 2001 From: Soulaha Balde Date: Sat, 2 Apr 2022 01:29:49 -0400 Subject: [PATCH] Entity can damage another with periodic attacks --- Assets/Scenes/Soulaha.unity | 46 +++++++++++++++++++------- Assets/Scripts/Entity.cs | 55 ++++++++++++++++++++++---------- Assets/Scripts/Gladiator.cs | 19 +++++++++++ Assets/Scripts/Gladiator.cs.meta | 11 +++++++ Assets/Scripts/Monster.cs | 27 ++++++++++++++++ Assets/Scripts/Monster.cs.meta | 11 +++++++ ProjectSettings/TagManager.asset | 4 ++- 7 files changed, 144 insertions(+), 29 deletions(-) create mode 100644 Assets/Scripts/Gladiator.cs create mode 100644 Assets/Scripts/Gladiator.cs.meta create mode 100644 Assets/Scripts/Monster.cs create mode 100644 Assets/Scripts/Monster.cs.meta diff --git a/Assets/Scenes/Soulaha.unity b/Assets/Scenes/Soulaha.unity index 1e7ffed..95d70a0 100644 --- a/Assets/Scenes/Soulaha.unity +++ b/Assets/Scenes/Soulaha.unity @@ -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} diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 08d8a65..4358336 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -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(); + 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; + } } diff --git a/Assets/Scripts/Gladiator.cs b/Assets/Scripts/Gladiator.cs new file mode 100644 index 0000000..81da14e --- /dev/null +++ b/Assets/Scripts/Gladiator.cs @@ -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() + { + + } +} diff --git a/Assets/Scripts/Gladiator.cs.meta b/Assets/Scripts/Gladiator.cs.meta new file mode 100644 index 0000000..1fa9cea --- /dev/null +++ b/Assets/Scripts/Gladiator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c537e1bd61b8d5c42b1ec03f90e14855 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Monster.cs b/Assets/Scripts/Monster.cs new file mode 100644 index 0000000..bd45cf7 --- /dev/null +++ b/Assets/Scripts/Monster.cs @@ -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; + } + } + } +} diff --git a/Assets/Scripts/Monster.cs.meta b/Assets/Scripts/Monster.cs.meta new file mode 100644 index 0000000..0a4acd6 --- /dev/null +++ b/Assets/Scripts/Monster.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7e480b0ef5998894283e8091830941cb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 1c92a78..4710463 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -3,7 +3,9 @@ --- !u!78 &1 TagManager: serializedVersion: 2 - tags: [] + tags: + - Monster + - Gladiator layers: - Default - TransparentFX