Entity when target is dead, start roaming
This commit is contained in:
parent
0fa714cda9
commit
bdd20faaa8
@ -96,14 +96,15 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3}
|
m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
<Health>k__BackingField: 0
|
<Health>k__BackingField: 100
|
||||||
fov: 0
|
<movementSpeed>k__BackingField: 5
|
||||||
attackRange: 1.5
|
<rotSpeed>k__BackingField: 3
|
||||||
attackDmg: 10
|
fov: 30
|
||||||
|
<attackRange>k__BackingField: 1.5
|
||||||
|
<attackDmg>k__BackingField: 10
|
||||||
attackCooldown: 1
|
attackCooldown: 1
|
||||||
target: {fileID: 0}
|
target: {fileID: 0}
|
||||||
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}
|
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}
|
||||||
ennemyName: Monster
|
|
||||||
--- !u!58 &3988163462708087662
|
--- !u!58 &3988163462708087662
|
||||||
CircleCollider2D:
|
CircleCollider2D:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@ -96,12 +96,12 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
|
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
<Health>k__BackingField: 0
|
<Health>k__BackingField: 100
|
||||||
movementSpeed: 2
|
<movementSpeed>k__BackingField: 2
|
||||||
rotSpeed: 3
|
<rotSpeed>k__BackingField: 3
|
||||||
fov: 0
|
fov: 30
|
||||||
attackRange: 1.5
|
<attackRange>k__BackingField: 1.5
|
||||||
attackDmg: 10
|
<attackDmg>k__BackingField: 5
|
||||||
attackCooldown: 1
|
attackCooldown: 1
|
||||||
target: {fileID: 0}
|
target: {fileID: 0}
|
||||||
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}
|
stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2}
|
||||||
|
|||||||
@ -248,26 +248,6 @@ PrefabInstance:
|
|||||||
propertyPath: m_LocalEulerAnglesHint.z
|
propertyPath: m_LocalEulerAnglesHint.z
|
||||||
value: 0
|
value: 0
|
||||||
objectReference: {fileID: 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_RemovedComponents: []
|
||||||
m_SourcePrefab: {fileID: 100100000, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
|
m_SourcePrefab: {fileID: 100100000, guid: b25a487d193f24049b6a791adf592b2e, type: 3}
|
||||||
--- !u!1001 &1359990804
|
--- !u!1001 &1359990804
|
||||||
@ -513,32 +493,42 @@ PrefabInstance:
|
|||||||
propertyPath: m_LocalEulerAnglesHint.z
|
propertyPath: m_LocalEulerAnglesHint.z
|
||||||
value: 0
|
value: 0
|
||||||
objectReference: {fileID: 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}
|
- target: {fileID: 2399377275812995980, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
|
||||||
propertyPath: m_Name
|
propertyPath: m_Name
|
||||||
value: Monster
|
value: Monster
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
m_RemovedComponents: []
|
m_RemovedComponents: []
|
||||||
m_SourcePrefab: {fileID: 100100000, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3}
|
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
|
--- !u!1001 &1942557562
|
||||||
PrefabInstance:
|
PrefabInstance:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@ -13,3 +13,16 @@ MonoBehaviour:
|
|||||||
m_Name: AI Stats
|
m_Name: AI Stats
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
closeEnough: 2
|
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}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ public class AIEntity : Entity
|
|||||||
{
|
{
|
||||||
[SerializeField] AIStats stats = null!;
|
[SerializeField] AIStats stats = null!;
|
||||||
BaseState currentState = null!;
|
BaseState currentState = null!;
|
||||||
public string ennemyName {get; protected set; }
|
public string enemyName {get; protected set; }
|
||||||
override protected void Awake(){
|
override protected void Awake(){
|
||||||
base.Awake();
|
base.Awake();
|
||||||
currentState = new FindTargetState(this);
|
currentState = new FindTargetState(this);
|
||||||
@ -49,6 +49,9 @@ public class AIEntity : Entity
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override BaseState? UpdateState(){
|
public override BaseState? UpdateState(){
|
||||||
|
if(!entity.IsAlive()){
|
||||||
|
return new DeadState(entity);
|
||||||
|
}
|
||||||
Entity targetEntity = entity.GetTarget().GetComponent<Entity>();
|
Entity targetEntity = entity.GetTarget().GetComponent<Entity>();
|
||||||
if(targetEntity != null){
|
if(targetEntity != null){
|
||||||
if(targetEntity.IsAlive()){//target is alive, keep chasing it
|
if(targetEntity.IsAlive()){//target is alive, keep chasing it
|
||||||
@ -74,15 +77,19 @@ public class AIEntity : Entity
|
|||||||
|
|
||||||
class FindTargetState : BaseStateAI{
|
class FindTargetState : BaseStateAI{
|
||||||
float closeEnough;
|
float closeEnough;
|
||||||
|
Vector3 roamPosition;
|
||||||
public FindTargetState(AIEntity entity) : base(entity){
|
public FindTargetState(AIEntity entity) : base(entity){
|
||||||
}
|
}
|
||||||
|
|
||||||
public override BaseState? UpdateState(){
|
public override BaseState? UpdateState(){
|
||||||
|
if(!entity.IsAlive()){
|
||||||
|
return new DeadState(entity);
|
||||||
|
}
|
||||||
Entity[] entities = FindObjectsOfType<Entity>();
|
Entity[] entities = FindObjectsOfType<Entity>();
|
||||||
float lastDist = float.MaxValue;
|
float lastDist = float.MaxValue;
|
||||||
Entity chosenEntity = null!;
|
Entity chosenEntity = null!;
|
||||||
foreach (Entity other in entities){// Find the closest entity
|
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);
|
float distance = Vector2.Distance(other.transform.position, entity.transform.position);
|
||||||
if(distance < lastDist){
|
if(distance < lastDist){
|
||||||
lastDist = distance;
|
lastDist = distance;
|
||||||
@ -93,10 +100,24 @@ public class AIEntity : Entity
|
|||||||
}
|
}
|
||||||
if(chosenEntity != null){
|
if(chosenEntity != null){
|
||||||
entity.SetTarget(chosenEntity.transform);
|
entity.SetTarget(chosenEntity.transform);
|
||||||
}else{
|
|
||||||
return new RoamState(entity);
|
|
||||||
}
|
|
||||||
return new SeekState(entity);
|
return new SeekState(entity);
|
||||||
|
}else{
|
||||||
|
if(roamPosition == new Vector3()) roamPosition = entity.stats.getRandomRoamPositon();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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(){
|
public override BaseState? UpdateState(){
|
||||||
|
if(!entity.IsAlive()){
|
||||||
|
return new DeadState(entity);
|
||||||
|
}
|
||||||
if(entity.IsInAttackRange()){
|
if(entity.IsInAttackRange()){
|
||||||
if(entity.attackTimer >= entity.attackCooldown){
|
if(entity.attackTimer >= entity.attackCooldown){
|
||||||
entity.attackTimer = 0;
|
entity.attackTimer = 0;
|
||||||
@ -133,9 +157,15 @@ public class AIEntity : Entity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RoamState : BaseStateAI{
|
class DeadState : BaseStateAI{
|
||||||
public RoamState(AIEntity entity) : base(entity){
|
public DeadState(AIEntity entity) : base(entity){
|
||||||
Debug.Log("Roaming!");
|
Debug.Log("Dead!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override BaseState? UpdateState(){
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,4 +3,10 @@ using UnityEngine;
|
|||||||
[CreateAssetMenu]
|
[CreateAssetMenu]
|
||||||
public class AIStats : ScriptableObject {
|
public class AIStats : ScriptableObject {
|
||||||
public float closeEnough = 1f;
|
public float closeEnough = 1f;
|
||||||
|
public Vector3[] roamPositions = null!;
|
||||||
|
|
||||||
|
public Vector3 getRandomRoamPositon(){
|
||||||
|
int randInd = Random.Range(0,roamPositions.Length-1);
|
||||||
|
return roamPositions[randInd];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -10,7 +10,7 @@ public class Entity : MonoBehaviour
|
|||||||
[field: SerializeField]public float movementSpeed{get; private set; }
|
[field: SerializeField]public float movementSpeed{get; private set; }
|
||||||
[field: SerializeField]public float rotSpeed {get; private set; }
|
[field: SerializeField]public float rotSpeed {get; private set; }
|
||||||
[field: SerializeField]private float fov;
|
[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 attackDmg {get; private set; }
|
||||||
[field: SerializeField]protected float attackCooldown;
|
[field: SerializeField]protected float attackCooldown;
|
||||||
protected float attackTimer;
|
protected float attackTimer;
|
||||||
@ -61,7 +61,8 @@ public class Entity : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected bool IsInAttackRange(){
|
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(){
|
protected bool IsLookingAtTarget(){
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class Gladiator : AIEntity
|
|||||||
{
|
{
|
||||||
base.Start();
|
base.Start();
|
||||||
base.entityName = "Gladiator";
|
base.entityName = "Gladiator";
|
||||||
base.ennemyName = "Monster";
|
base.enemyName = "Monster";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class Monster : AIEntity
|
|||||||
{
|
{
|
||||||
base.Start();
|
base.Start();
|
||||||
base.entityName = "Monster";
|
base.entityName = "Monster";
|
||||||
base.ennemyName = "Gladiator";
|
base.enemyName = "Gladiator";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user