Entity can damage another with periodic attacks
This commit is contained in:
parent
10e9fc074b
commit
fd74f48350
@ -218,10 +218,10 @@ GameObject:
|
|||||||
- component: {fileID: 1736226634}
|
- component: {fileID: 1736226634}
|
||||||
- component: {fileID: 1736226633}
|
- component: {fileID: 1736226633}
|
||||||
- component: {fileID: 1736226632}
|
- component: {fileID: 1736226632}
|
||||||
- component: {fileID: 1736226636}
|
- component: {fileID: 1736226637}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Cube
|
m_Name: Monster
|
||||||
m_TagString: Untagged
|
m_TagString: Monster
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
m_NavMeshLayer: 0
|
m_NavMeshLayer: 0
|
||||||
m_StaticEditorFlags: 0
|
m_StaticEditorFlags: 0
|
||||||
@ -302,7 +302,7 @@ Transform:
|
|||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_RootOrder: 1
|
m_RootOrder: 1
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
--- !u!114 &1736226636
|
--- !u!114 &1736226637
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
m_CorrespondingSourceObject: {fileID: 0}
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
@ -311,15 +311,16 @@ MonoBehaviour:
|
|||||||
m_GameObject: {fileID: 1736226631}
|
m_GameObject: {fileID: 1736226631}
|
||||||
m_Enabled: 1
|
m_Enabled: 1
|
||||||
m_EditorHideFlags: 0
|
m_EditorHideFlags: 0
|
||||||
m_Script: {fileID: 11500000, guid: ea4f715fcb3a0324a9e5d7c1ac896b9d, type: 3}
|
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
health: 0
|
health: 100
|
||||||
movementSpeed: 3
|
movementSpeed: 5
|
||||||
rotSpeed: 10
|
rotSpeed: 3
|
||||||
fov: 30
|
fov: 0
|
||||||
attackRange: 1.5
|
attackRange: 1.5
|
||||||
attackDmg: 0
|
attackDmg: 10
|
||||||
|
attackCooldown: 1
|
||||||
target: {fileID: 1996499837}
|
target: {fileID: 1996499837}
|
||||||
--- !u!1 &1996499833
|
--- !u!1 &1996499833
|
||||||
GameObject:
|
GameObject:
|
||||||
@ -333,9 +334,10 @@ GameObject:
|
|||||||
- component: {fileID: 1996499836}
|
- component: {fileID: 1996499836}
|
||||||
- component: {fileID: 1996499835}
|
- component: {fileID: 1996499835}
|
||||||
- component: {fileID: 1996499834}
|
- component: {fileID: 1996499834}
|
||||||
|
- component: {fileID: 1996499838}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Target
|
m_Name: Gladiator
|
||||||
m_TagString: Untagged
|
m_TagString: Gladiator
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
m_NavMeshLayer: 0
|
m_NavMeshLayer: 0
|
||||||
m_StaticEditorFlags: 0
|
m_StaticEditorFlags: 0
|
||||||
@ -416,3 +418,23 @@ Transform:
|
|||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_RootOrder: 2
|
m_RootOrder: 2
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
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}
|
||||||
|
|||||||
@ -10,44 +10,67 @@ public class Entity : MonoBehaviour
|
|||||||
[SerializeField]private float fov;
|
[SerializeField]private float fov;
|
||||||
[SerializeField]private float attackRange;
|
[SerializeField]private float attackRange;
|
||||||
[SerializeField]private float attackDmg;
|
[SerializeField]private float attackDmg;
|
||||||
|
[SerializeField]protected float attackCooldown;
|
||||||
|
protected float attackTimer;
|
||||||
[SerializeField]private Transform target;
|
[SerializeField]private Transform target;
|
||||||
|
private new string name;
|
||||||
|
private Collider atkCollider;
|
||||||
private Vector3 direction;
|
private Vector3 direction;
|
||||||
|
|
||||||
public Entity(float health, float movementSpeed, float rotSpeed, float attackRange, float attackDmg, Transform target){
|
protected virtual void Start() {
|
||||||
this.health = health;
|
|
||||||
this.movementSpeed = movementSpeed;
|
|
||||||
this.rotSpeed = rotSpeed;
|
|
||||||
this.attackRange = attackRange;
|
|
||||||
this.attackDmg = attackDmg;
|
|
||||||
this.target = target;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Start() {
|
|
||||||
direction = new Vector3(1,0,0);
|
direction = new Vector3(1,0,0);
|
||||||
}
|
attackTimer = attackCooldown;
|
||||||
private void Update() {
|
|
||||||
MoveToTarget(transform, Time.deltaTime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Attack(){
|
protected virtual void Attack(){
|
||||||
|
Entity targetEntity = target.GetComponent<Entity>();
|
||||||
|
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void SpecialAttack(){
|
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);
|
direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*deltaTime, 0.0f);
|
||||||
if( !IsInAttackRange())
|
if(!IsInAttackRange()){
|
||||||
transform.Translate(direction * movementSpeed* deltaTime);
|
transform.Translate(direction * movementSpeed* deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void SetTarget(Transform newTarget){
|
public void SetTarget(Transform newTarget){
|
||||||
target = 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(){
|
protected bool IsInAttackRange(){
|
||||||
return Vector2.Distance(transform.position, target.position) <= attackRange;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
Assets/Scripts/Gladiator.cs
Normal file
19
Assets/Scripts/Gladiator.cs
Normal 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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/Gladiator.cs.meta
Normal file
11
Assets/Scripts/Gladiator.cs.meta
Normal 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
27
Assets/Scripts/Monster.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/Monster.cs.meta
Normal file
11
Assets/Scripts/Monster.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7e480b0ef5998894283e8091830941cb
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -3,7 +3,9 @@
|
|||||||
--- !u!78 &1
|
--- !u!78 &1
|
||||||
TagManager:
|
TagManager:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
tags: []
|
tags:
|
||||||
|
- Monster
|
||||||
|
- Gladiator
|
||||||
layers:
|
layers:
|
||||||
- Default
|
- Default
|
||||||
- TransparentFX
|
- TransparentFX
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user