diff --git a/Assets/Prefabs/Gladiator.prefab b/Assets/Prefabs/Gladiator.prefab index fb311a9..8412129 100644 --- a/Assets/Prefabs/Gladiator.prefab +++ b/Assets/Prefabs/Gladiator.prefab @@ -48,6 +48,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3} m_Name: m_EditorClassIdentifier: + arena: {fileID: 0} gameFlowManager: {fileID: 0} k__BackingField: 40 healthBar: {fileID: 1378753993005748510} @@ -343,11 +344,23 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} propertyPath: m_SizeDelta.x - value: 0.4 + value: 1.2 objectReference: {fileID: 0} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} propertyPath: m_SizeDelta.y - value: 0.05 + value: 0.15 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.x + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.y + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.z + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} diff --git a/Assets/Prefabs/Monster.prefab b/Assets/Prefabs/Monster.prefab index f5db3a5..ba89dd5 100644 --- a/Assets/Prefabs/Monster.prefab +++ b/Assets/Prefabs/Monster.prefab @@ -130,6 +130,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3} m_Name: m_EditorClassIdentifier: + arena: {fileID: 0} gameFlowManager: {fileID: 0} k__BackingField: 40 healthBar: {fileID: 7668921808924868904} @@ -353,11 +354,23 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} propertyPath: m_SizeDelta.x - value: 0.4 + value: 1.2 objectReference: {fileID: 0} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} propertyPath: m_SizeDelta.y - value: 0.05 + value: 0.15 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.x + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.y + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} + propertyPath: m_LocalScale.z + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} diff --git a/Assets/Scripts/AIEntity.cs b/Assets/Scripts/AIEntity.cs index dbe585d..6f34c98 100644 --- a/Assets/Scripts/AIEntity.cs +++ b/Assets/Scripts/AIEntity.cs @@ -2,41 +2,41 @@ using UnityEngine; using UnityEngine.Serialization; -public class AIEntity : Entity -{ - [FormerlySerializedAs("stats")] [SerializeField] +public class AIEntity : Entity { + [FormerlySerializedAs("stats")] + [SerializeField] public AIStats AIStats = null!; BaseState currentState = null!; public EntityFlag enemies { get; protected set; } public bool facingRight { get; private set; } = true; - override protected void Start(){ + override protected void Start() { base.Start(); currentState = CreateInitialState(); currentState.EnterState(); } - override protected void Update() { + override protected void Update() { base.Update(); - if (currentState.UpdateState() is {} newState) - SwitchState(newState); - } + if (currentState.UpdateState() is { } newState) + SwitchState(newState); + } - override protected void FixedUpdate() { + override protected void FixedUpdate() { base.FixedUpdate(); - if (currentState.FixedUpdateState() is {} newState) - SwitchState(newState); + if (currentState.FixedUpdateState() is { } newState) + SwitchState(newState); FlipAccordingToInput(); - } + } - void OnDrawGizmos() => currentState?.OnDrawGizmos(); + void OnDrawGizmos() => currentState?.OnDrawGizmos(); - protected override void OnDied() { - base.OnDied(); - transform.SetParent(arena.graveyard); - } + protected override void OnDied() { + base.OnDied(); + transform.SetParent(arena.graveyard); + } - void SwitchState(BaseState newState) { + void SwitchState(BaseState newState) { currentState.LeaveState(); currentState = newState; newState.EnterState(); @@ -46,14 +46,14 @@ public class AIEntity : Entity //Looks into enemy name list to see if the other is targetable virtual protected bool IsTargetable(Entity other) { - return enemies.HasFlag(other.entityType) && other.IsAlive(); + return enemies.HasFlag(other.entityType) && other.IsAlive(); } - override public bool TakeDamage(float amount, Entity other){ - //TODO Should we warn if target is null here? - if (target != null && target.GetComponent() is {}) - target = other.transform; - + override public bool TakeDamage(float amount, Entity other) { + //TODO Should we warn if target is null here? + if (target != null && target.GetComponent() is { }) + target = other.transform; + return base.TakeDamage(amount, other); } @@ -64,11 +64,13 @@ public class AIEntity : Entity Vector3 scaler = transform.localScale; scaler.x *= -1; transform.localScale = scaler; + + healthBar.gameObject.transform.localScale = scaler; } public void FlipAccordingToInput() { Vector3 direction = rb.velocity; - if(target != null) { + if (target != null) { direction = target.position - transform.position; } if ((!facingRight && direction.x > 0) || (facingRight && direction.x < 0)) { @@ -79,60 +81,16 @@ public class AIEntity : Entity #endregion - protected abstract class BaseStateAI : BaseState{ - protected AIEntity entity; - public BaseStateAI(AIEntity entity){ - this.entity = entity; - } - } - - protected class SeekState : BaseStateAI{ - public SeekState(AIEntity entity) : base(entity){ - - } - - public override void EnterState() { - if(! entity.animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) { - entity.animator.Play("Running"); - } - } - - public override BaseState? UpdateState(){ - if(!entity.IsAlive()){ - return new DeadState(entity); - } - Entity targetEntity = entity.target.GetComponent(); - if(targetEntity != null){ - if(targetEntity.IsAlive()){//target is alive, keep chasing it - return null; - }else{//target is dead, go to findTargetState - return new FindTargetState(entity);; - } - } - return null; - } - - public override BaseState? FixedUpdateState(){ - entity.direction = Vector3.RotateTowards(entity.direction, (entity.target.position - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f); - if(entity.IsTargetable(entity.target.GetComponent())){ - if(!entity.IsInAttackRange()){ - entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime); - // entity.animator.Play("Running"); - }else{ - return new AttackState(entity); - } - }else{ - return new FindTargetState(entity); - } - // entity.animator.Play("Idle"); - return null; + protected abstract class BaseStateAI : BaseState { + protected AIEntity entity; + public BaseStateAI(AIEntity entity) { + this.entity = entity; } } - protected class FindTargetState : BaseStateAI{ - float closeEnough; - Vector3 roamPosition; - public FindTargetState(AIEntity entity) : base(entity){ + protected class SeekState : BaseStateAI { + public SeekState(AIEntity entity) : base(entity) { + } public override void EnterState() { @@ -141,41 +99,85 @@ public class AIEntity : Entity } } - public override BaseState? UpdateState(){ - if(!entity.IsAlive()){ + public override BaseState? UpdateState() { + if (!entity.IsAlive()) { + return new DeadState(entity); + } + Entity targetEntity = entity.target.GetComponent(); + if (targetEntity != null) { + if (targetEntity.IsAlive()) {//target is alive, keep chasing it + return null; + } else {//target is dead, go to findTargetState + return new FindTargetState(entity); ; + } + } + return null; + } + + public override BaseState? FixedUpdateState() { + entity.direction = Vector3.RotateTowards(entity.direction, (entity.target.position - entity.transform.position), entity.rotSpeed * Time.fixedDeltaTime, 0.0f); + if (entity.IsTargetable(entity.target.GetComponent())) { + if (!entity.IsInAttackRange()) { + entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime); + // entity.animator.Play("Running"); + } else { + return new AttackState(entity); + } + } else { + return new FindTargetState(entity); + } + // entity.animator.Play("Idle"); + return null; + } + } + + protected class FindTargetState : BaseStateAI { + float closeEnough; + Vector3 roamPosition; + public FindTargetState(AIEntity entity) : base(entity) { + } + + public override void EnterState() { + if (!entity.animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) { + entity.animator.Play("Running"); + } + } + + public override BaseState? UpdateState() { + if (!entity.IsAlive()) { return new DeadState(entity); } Transform entityParent = entity.entityType == EntityFlag.Gladiator ? entity.arena.minionParent : entity.arena.gladiatorParent; float lastDist = float.MaxValue; Transform chosenEntity = null!; - foreach (Transform other in entityParent){// Find the closest entity + foreach (Transform other in entityParent) {// Find the closest entity float distance = Vector2.Distance(other.position, entity.transform.position); - if(distance < lastDist){ + if (distance < lastDist) { lastDist = distance; chosenEntity = other; - if(lastDist <= entity.AIStats.closeEnough)break; + if (lastDist <= entity.AIStats.closeEnough) break; } } - - if(chosenEntity != null){ + + if (chosenEntity != null) { entity.target = chosenEntity; return new SeekState(entity); - }else{ - if(roamPosition == new Vector3()) roamPosition = entity.AIStats.getRandomRoamPositon(); + } else { + if (roamPosition == new Vector3()) roamPosition = entity.AIStats.getRandomRoamPositon(); return null; } } - public override BaseState? FixedUpdateState(){ - if(roamPosition == new Vector3()){ + public override BaseState? FixedUpdateState() { + if (roamPosition == new Vector3()) { // entity.animator.Play("Idle"); 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.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{ + } else { roamPosition = entity.AIStats.getRandomRoamPositon(); } // entity.animator.Play("Running"); @@ -184,50 +186,50 @@ public class AIEntity : Entity } - protected class AttackState : BaseStateAI{ - public AttackState(AIEntity entity) : base(entity){ + protected class AttackState : BaseStateAI { + public AttackState(AIEntity entity) : base(entity) { } public override void EnterState() { entity.animator.Play("Attack"); } - public override BaseState? UpdateState(){ - if(!entity.IsAlive()){ + public override BaseState? UpdateState() { + if (!entity.IsAlive()) { return new DeadState(entity); } if (entity.gameFlowManager.CanDoAction) { - if(entity.IsInAttackRange()){ - if(entity.attackTimer >= entity.attackCooldown){ - entity.attackTimer = 0; - return Attack(); - }else{ - entity.attackTimer += Time.deltaTime; - } - - }else - return new SeekState(entity); + if (entity.IsInAttackRange()) { + if (entity.attackTimer >= entity.attackCooldown) { + entity.attackTimer = 0; + return Attack(); + } else { + entity.attackTimer += Time.deltaTime; + } + + } else + return new SeekState(entity); } - + return null; } - private BaseState? Attack(){ + private BaseState? Attack() { Entity targetEntity = entity.target.GetComponent(); - if(targetEntity != null){ - targetEntity.TakeDamage(entity.attackDmg, entity); - bool isTargetAlive = targetEntity.IsAlive(); - if(!isTargetAlive){ - return new FindTargetState(entity); - } + if (targetEntity != null) { + targetEntity.TakeDamage(entity.attackDmg, entity); + bool isTargetAlive = targetEntity.IsAlive(); + if (!isTargetAlive) { + return new FindTargetState(entity); } + } return null; } } - protected class DeadState : BaseStateAI{ - public DeadState(AIEntity entity) : base(entity){ + protected class DeadState : BaseStateAI { + public DeadState(AIEntity entity) : base(entity) { } @@ -235,8 +237,8 @@ public class AIEntity : Entity entity.animator.Play("Death"); } - public override BaseState? UpdateState(){ - + public override BaseState? UpdateState() { + return null; }