diff --git a/Assets/Prefabs/Gladiator.prefab b/Assets/Prefabs/Gladiator.prefab index 6c83e05..02961d2 100644 --- a/Assets/Prefabs/Gladiator.prefab +++ b/Assets/Prefabs/Gladiator.prefab @@ -96,14 +96,15 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3} m_Name: m_EditorClassIdentifier: - k__BackingField: 0 - fov: 0 - attackRange: 1.5 - attackDmg: 10 + k__BackingField: 100 + k__BackingField: 5 + k__BackingField: 3 + fov: 30 + k__BackingField: 1.5 + k__BackingField: 10 attackCooldown: 1 target: {fileID: 0} stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2} - ennemyName: Monster --- !u!58 &3988163462708087662 CircleCollider2D: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/Monster.prefab b/Assets/Prefabs/Monster.prefab index c04df97..65e35e8 100644 --- a/Assets/Prefabs/Monster.prefab +++ b/Assets/Prefabs/Monster.prefab @@ -96,12 +96,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3} m_Name: m_EditorClassIdentifier: - k__BackingField: 0 - movementSpeed: 2 - rotSpeed: 3 - fov: 0 - attackRange: 1.5 - attackDmg: 10 + k__BackingField: 100 + k__BackingField: 2 + k__BackingField: 3 + fov: 30 + k__BackingField: 1.5 + k__BackingField: 5 attackCooldown: 1 target: {fileID: 0} stats: {fileID: 11400000, guid: 9d8a9a664d932d0498d5eca7607eeb53, type: 2} diff --git a/Assets/Scenes/SoulahaScene.unity b/Assets/Scenes/SoulahaScene.unity index 5ae07de..dd3d6ae 100644 --- a/Assets/Scenes/SoulahaScene.unity +++ b/Assets/Scenes/SoulahaScene.unity @@ -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: k__BackingField - value: 100 - objectReference: {fileID: 0} - - target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3} - propertyPath: k__BackingField - value: 3 - objectReference: {fileID: 0} - - target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3} - propertyPath: k__BackingField - value: 10 - objectReference: {fileID: 0} - - target: {fileID: 197677485360569566, guid: b25a487d193f24049b6a791adf592b2e, type: 3} - propertyPath: 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: k__BackingField - value: 100 - objectReference: {fileID: 0} - - target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3} - propertyPath: k__BackingField - value: 3 - objectReference: {fileID: 0} - - target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3} - propertyPath: k__BackingField - value: 10 - objectReference: {fileID: 0} - - target: {fileID: 2399377275812995974, guid: 5f633c05dee3f4b4784f5702b2365f02, type: 3} - propertyPath: 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 diff --git a/Assets/Scripts/AI Stats.asset b/Assets/Scripts/AI Stats.asset index fc57117..33007e2 100644 --- a/Assets/Scripts/AI Stats.asset +++ b/Assets/Scripts/AI Stats.asset @@ -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} diff --git a/Assets/Scripts/AIEntity.cs b/Assets/Scripts/AIEntity.cs index 80aea9d..aa69dd0 100644 --- a/Assets/Scripts/AIEntity.cs +++ b/Assets/Scripts/AIEntity.cs @@ -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(); 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(); 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; } } } diff --git a/Assets/Scripts/AIStats.cs b/Assets/Scripts/AIStats.cs index e1d503a..3dd2ea6 100644 --- a/Assets/Scripts/AIStats.cs +++ b/Assets/Scripts/AIStats.cs @@ -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]; + } } \ No newline at end of file diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 2ef502a..705d10b 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -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(){ diff --git a/Assets/Scripts/Gladiator.cs b/Assets/Scripts/Gladiator.cs index d4ea8ad..076e7b2 100644 --- a/Assets/Scripts/Gladiator.cs +++ b/Assets/Scripts/Gladiator.cs @@ -9,7 +9,7 @@ public class Gladiator : AIEntity { base.Start(); base.entityName = "Gladiator"; - base.ennemyName = "Monster"; + base.enemyName = "Monster"; } } diff --git a/Assets/Scripts/Monster.cs b/Assets/Scripts/Monster.cs index 6b74525..b874f16 100644 --- a/Assets/Scripts/Monster.cs +++ b/Assets/Scripts/Monster.cs @@ -9,7 +9,7 @@ public class Monster : AIEntity { base.Start(); base.entityName = "Monster"; - base.ennemyName = "Gladiator"; + base.enemyName = "Gladiator"; } }