Entity when target is dead, start roaming

This commit is contained in:
Soulaha Balde 2022-04-02 14:02:27 -04:00
parent 0fa714cda9
commit bdd20faaa8
9 changed files with 103 additions and 62 deletions

View File

@ -96,14 +96,15 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3}
m_Name:
m_EditorClassIdentifier:
<Health>k__BackingField: 0
fov: 0
attackRange: 1.5
attackDmg: 10
<Health>k__BackingField: 100
<movementSpeed>k__BackingField: 5
<rotSpeed>k__BackingField: 3
fov: 30
<attackRange>k__BackingField: 1.5
<attackDmg>k__BackingField: 10
attackCooldown: 1
target: {fileID: 0}
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}
ennemyName: Monster
--- !u!58 &3988163462708087662
CircleCollider2D:
m_ObjectHideFlags: 0

View File

@ -96,12 +96,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
m_Name:
m_EditorClassIdentifier:
<Health>k__BackingField: 0
movementSpeed: 2
rotSpeed: 3
fov: 0
attackRange: 1.5
attackDmg: 10
<Health>k__BackingField: 100
<movementSpeed>k__BackingField: 2
<rotSpeed>k__BackingField: 3
fov: 30
<attackRange>k__BackingField: 1.5
<attackDmg>k__BackingField: 5
attackCooldown: 1
target: {fileID: 0}
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}

View File

@ -248,26 +248,6 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
propertyPath: fov
value: 30
objectReference: {fileID: 0}
- target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
propertyPath: <Health>k__BackingField
value: 100
objectReference: {fileID: 0}
- target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
propertyPath: <rotSpeed>k__BackingField
value: 3
objectReference: {fileID: 0}
- target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
propertyPath: <attackDmg>k__BackingField
value: 10
objectReference: {fileID: 0}
- target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
propertyPath: <movementSpeed>k__BackingField
value: 5
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
--- !u!1001 &1359990804
@ -513,32 +493,42 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: fov
value: 30
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: <Health>k__BackingField
value: 100
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: <rotSpeed>k__BackingField
value: 3
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: <attackDmg>k__BackingField
value: 10
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: <movementSpeed>k__BackingField
value: 2
objectReference: {fileID: 0}
- target: {fileID: 2399377275812995980, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
propertyPath: m_Name
value: Monster
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
--- !u!1 &1836818727
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1836818728}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1836818728
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1836818727}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 14.96, y: -6.01, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1942557562
PrefabInstance:
m_ObjectHideFlags: 0

View File

@ -13,3 +13,16 @@ MonoBehaviour:
m_Name: AI Stats
m_EditorClassIdentifier:
closeEnough: 2
roamPositions:
- {x: -14, y: 6, z: 0}
- {x: -14, y: 0, z: 0}
- {x: -14, y: -6, z: 0}
- {x: -5, y: 6, z: 0}
- {x: -5, y: 0, z: 0}
- {x: -5, y: -6, z: 0}
- {x: 5, y: 6, z: 0}
- {x: 5, y: 0, z: 0}
- {x: 5, y: -6, z: 0}
- {x: 14, y: 0, z: 0}
- {x: 14, y: 0, z: 0}
- {x: 14, y: 0, z: 0}

View File

@ -7,7 +7,7 @@ public class AIEntity : Entity
{
[SerializeField] AIStats stats = null!;
BaseState currentState = null!;
public string ennemyName {get; protected set; }
public string enemyName {get; protected set; }
override protected void Awake(){
base.Awake();
currentState = new FindTargetState(this);
@ -49,6 +49,9 @@ public class AIEntity : Entity
}
public override BaseState? UpdateState(){
if(!entity.IsAlive()){
return new DeadState(entity);
}
Entity targetEntity = entity.GetTarget().GetComponent<Entity>();
if(targetEntity != null){
if(targetEntity.IsAlive()){//target is alive, keep chasing it
@ -74,15 +77,19 @@ public class AIEntity : Entity
class FindTargetState : BaseStateAI{
float closeEnough;
Vector3 roamPosition;
public FindTargetState(AIEntity entity) : base(entity){
}
public override BaseState? UpdateState(){
if(!entity.IsAlive()){
return new DeadState(entity);
}
Entity[] entities = FindObjectsOfType<Entity>();
float lastDist = float.MaxValue;
Entity chosenEntity = null!;
foreach (Entity other in entities){// Find the closest entity
if(other.name == entity.ennemyName && other.IsAlive()){
if(other.name == entity.enemyName && other.IsAlive()){
float distance = Vector2.Distance(other.transform.position, entity.transform.position);
if(distance < lastDist){
lastDist = distance;
@ -93,10 +100,24 @@ public class AIEntity : Entity
}
if(chosenEntity != null){
entity.SetTarget(chosenEntity.transform);
return new SeekState(entity);
}else{
return new RoamState(entity);
if(roamPosition == new Vector3()) roamPosition = entity.stats.getRandomRoamPositon();
return null;
}
return new SeekState(entity);
}
public override BaseState? FixedUpdateState(){
if(roamPosition == new Vector3()){
return null;
}
entity.direction = Vector3.RotateTowards(entity.direction, (roamPosition - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f);
if(Vector2.Distance(entity.transform.position, roamPosition) >= entity.attackRange){
entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime);
}else{
roamPosition = entity.stats.getRandomRoamPositon();
}
return null;
}
}
@ -106,6 +127,9 @@ public class AIEntity : Entity
}
public override BaseState? UpdateState(){
if(!entity.IsAlive()){
return new DeadState(entity);
}
if(entity.IsInAttackRange()){
if(entity.attackTimer >= entity.attackCooldown){
entity.attackTimer = 0;
@ -133,9 +157,15 @@ public class AIEntity : Entity
}
}
class RoamState : BaseStateAI{
public RoamState(AIEntity entity) : base(entity){
Debug.Log("Roaming!");
class DeadState : BaseStateAI{
public DeadState(AIEntity entity) : base(entity){
Debug.Log("Dead!");
}
public override BaseState? UpdateState(){
return null;
}
}
}

View File

@ -3,4 +3,10 @@ using UnityEngine;
[CreateAssetMenu]
public class AIStats : ScriptableObject {
public float closeEnough = 1f;
public Vector3[] roamPositions = null!;
public Vector3 getRandomRoamPositon(){
int randInd = Random.Range(0,roamPositions.Length-1);
return roamPositions[randInd];
}
}

View File

@ -10,7 +10,7 @@ public class Entity : MonoBehaviour
[field: SerializeField]public float movementSpeed{get; private set; }
[field: SerializeField]public float rotSpeed {get; private set; }
[field: SerializeField]private float fov;
[field: SerializeField]private float attackRange;
[field: SerializeField]protected float attackRange {get; private set; }
[field: SerializeField]protected float attackDmg {get; private set; }
[field: SerializeField]protected float attackCooldown;
protected float attackTimer;
@ -61,7 +61,8 @@ public class Entity : MonoBehaviour
}
protected bool IsInAttackRange(){
return Vector2.Distance(transform.position, target.position) <= attackRange;
float distance = Vector2.Distance(transform.position, target.position);
return distance <= attackRange;
}
protected bool IsLookingAtTarget(){

View File

@ -9,7 +9,7 @@ public class Gladiator : AIEntity
{
base.Start();
base.entityName = "Gladiator";
base.ennemyName = "Monster";
base.enemyName = "Monster";
}
}

View File

@ -9,7 +9,7 @@ public class Monster : AIEntity
{
base.Start();
base.entityName = "Monster";
base.ennemyName = "Gladiator";
base.enemyName = "Gladiator";
}
}