Fix health bar flip

This commit is contained in:
Yann Dupont 01 2022-04-03 00:58:11 -04:00
parent 48041ea26e
commit 3700cf800a
3 changed files with 148 additions and 120 deletions

View File

@ -48,6 +48,7 @@ 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:
arena: {fileID: 0}
gameFlowManager: {fileID: 0} gameFlowManager: {fileID: 0}
<Health>k__BackingField: 40 <Health>k__BackingField: 40
healthBar: {fileID: 1378753993005748510} healthBar: {fileID: 1378753993005748510}
@ -343,11 +344,23 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 0.4 value: 1.2
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}
propertyPath: m_SizeDelta.y 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} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}

View File

@ -130,6 +130,7 @@ 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:
arena: {fileID: 0}
gameFlowManager: {fileID: 0} gameFlowManager: {fileID: 0}
<Health>k__BackingField: 40 <Health>k__BackingField: 40
healthBar: {fileID: 7668921808924868904} healthBar: {fileID: 7668921808924868904}
@ -353,11 +354,23 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 0.4 value: 1.2
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} - target: {fileID: 2962150095602046910, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}
propertyPath: m_SizeDelta.y 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} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3} m_SourcePrefab: {fileID: 100100000, guid: d1d8f8326b5490848a9400aa9bd6b2f4, type: 3}

View File

@ -2,41 +2,41 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
public class AIEntity : Entity public class AIEntity : Entity {
{ [FormerlySerializedAs("stats")]
[FormerlySerializedAs("stats")] [SerializeField] [SerializeField]
public AIStats AIStats = null!; public AIStats AIStats = null!;
BaseState currentState = null!; BaseState currentState = null!;
public EntityFlag enemies { get; protected set; } public EntityFlag enemies { get; protected set; }
public bool facingRight { get; private set; } = true; public bool facingRight { get; private set; } = true;
override protected void Start(){ override protected void Start() {
base.Start(); base.Start();
currentState = CreateInitialState(); currentState = CreateInitialState();
currentState.EnterState(); currentState.EnterState();
} }
override protected void Update() { override protected void Update() {
base.Update(); base.Update();
if (currentState.UpdateState() is {} newState) if (currentState.UpdateState() is { } newState)
SwitchState(newState); SwitchState(newState);
} }
override protected void FixedUpdate() { override protected void FixedUpdate() {
base.FixedUpdate(); base.FixedUpdate();
if (currentState.FixedUpdateState() is {} newState) if (currentState.FixedUpdateState() is { } newState)
SwitchState(newState); SwitchState(newState);
FlipAccordingToInput(); FlipAccordingToInput();
} }
void OnDrawGizmos() => currentState?.OnDrawGizmos(); void OnDrawGizmos() => currentState?.OnDrawGizmos();
protected override void OnDied() { protected override void OnDied() {
base.OnDied(); base.OnDied();
transform.SetParent(arena.graveyard); transform.SetParent(arena.graveyard);
} }
void SwitchState(BaseState newState) { void SwitchState(BaseState newState) {
currentState.LeaveState(); currentState.LeaveState();
currentState = newState; currentState = newState;
newState.EnterState(); newState.EnterState();
@ -46,14 +46,14 @@ public class AIEntity : Entity
//Looks into enemy name list to see if the other is targetable //Looks into enemy name list to see if the other is targetable
virtual protected bool IsTargetable(Entity other) { 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){ override public bool TakeDamage(float amount, Entity other) {
//TODO Should we warn if target is null here? //TODO Should we warn if target is null here?
if (target != null && target.GetComponent<VampireEntity>() is {}) if (target != null && target.GetComponent<VampireEntity>() is { })
target = other.transform; target = other.transform;
return base.TakeDamage(amount, other); return base.TakeDamage(amount, other);
} }
@ -64,11 +64,13 @@ public class AIEntity : Entity
Vector3 scaler = transform.localScale; Vector3 scaler = transform.localScale;
scaler.x *= -1; scaler.x *= -1;
transform.localScale = scaler; transform.localScale = scaler;
healthBar.gameObject.transform.localScale = scaler;
} }
public void FlipAccordingToInput() { public void FlipAccordingToInput() {
Vector3 direction = rb.velocity; Vector3 direction = rb.velocity;
if(target != null) { if (target != null) {
direction = target.position - transform.position; direction = target.position - transform.position;
} }
if ((!facingRight && direction.x > 0) || (facingRight && direction.x < 0)) { if ((!facingRight && direction.x > 0) || (facingRight && direction.x < 0)) {
@ -79,60 +81,16 @@ public class AIEntity : Entity
#endregion #endregion
protected abstract class BaseStateAI : BaseState{ protected abstract class BaseStateAI : BaseState {
protected AIEntity entity; protected AIEntity entity;
public BaseStateAI(AIEntity entity){ public BaseStateAI(AIEntity entity) {
this.entity = 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<Entity>();
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<Entity>())){
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{ protected class SeekState : BaseStateAI {
float closeEnough; public SeekState(AIEntity entity) : base(entity) {
Vector3 roamPosition;
public FindTargetState(AIEntity entity) : base(entity){
} }
public override void EnterState() { public override void EnterState() {
@ -141,41 +99,85 @@ public class AIEntity : Entity
} }
} }
public override BaseState? UpdateState(){ public override BaseState? UpdateState() {
if(!entity.IsAlive()){ if (!entity.IsAlive()) {
return new DeadState(entity);
}
Entity targetEntity = entity.target.GetComponent<Entity>();
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<Entity>())) {
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); return new DeadState(entity);
} }
Transform entityParent = entity.entityType == EntityFlag.Gladiator ? entity.arena.minionParent : entity.arena.gladiatorParent; Transform entityParent = entity.entityType == EntityFlag.Gladiator ? entity.arena.minionParent : entity.arena.gladiatorParent;
float lastDist = float.MaxValue; float lastDist = float.MaxValue;
Transform chosenEntity = null!; 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); float distance = Vector2.Distance(other.position, entity.transform.position);
if(distance < lastDist){ if (distance < lastDist) {
lastDist = distance; lastDist = distance;
chosenEntity = other; chosenEntity = other;
if(lastDist <= entity.AIStats.closeEnough)break; if (lastDist <= entity.AIStats.closeEnough) break;
} }
} }
if(chosenEntity != null){ if (chosenEntity != null) {
entity.target = chosenEntity; entity.target = chosenEntity;
return new SeekState(entity); return new SeekState(entity);
}else{ } else {
if(roamPosition == new Vector3()) roamPosition = entity.AIStats.getRandomRoamPositon(); if (roamPosition == new Vector3()) roamPosition = entity.AIStats.getRandomRoamPositon();
return null; return null;
} }
} }
public override BaseState? FixedUpdateState(){ public override BaseState? FixedUpdateState() {
if(roamPosition == new Vector3()){ if (roamPosition == new Vector3()) {
// entity.animator.Play("Idle"); // entity.animator.Play("Idle");
return null; return null;
} }
entity.direction = Vector3.RotateTowards(entity.direction, (roamPosition - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f); 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){ if (Vector2.Distance(entity.transform.position, roamPosition) >= entity.attackRange) {
entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime); entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime);
}else{ } else {
roamPosition = entity.AIStats.getRandomRoamPositon(); roamPosition = entity.AIStats.getRandomRoamPositon();
} }
// entity.animator.Play("Running"); // entity.animator.Play("Running");
@ -184,50 +186,50 @@ public class AIEntity : Entity
} }
protected class AttackState : BaseStateAI{ protected class AttackState : BaseStateAI {
public AttackState(AIEntity entity) : base(entity){ public AttackState(AIEntity entity) : base(entity) {
} }
public override void EnterState() { public override void EnterState() {
entity.animator.Play("Attack"); entity.animator.Play("Attack");
} }
public override BaseState? UpdateState(){ public override BaseState? UpdateState() {
if(!entity.IsAlive()){ if (!entity.IsAlive()) {
return new DeadState(entity); return new DeadState(entity);
} }
if (entity.gameFlowManager.CanDoAction) { if (entity.gameFlowManager.CanDoAction) {
if(entity.IsInAttackRange()){ if (entity.IsInAttackRange()) {
if(entity.attackTimer >= entity.attackCooldown){ if (entity.attackTimer >= entity.attackCooldown) {
entity.attackTimer = 0; entity.attackTimer = 0;
return Attack(); return Attack();
}else{ } else {
entity.attackTimer += Time.deltaTime; entity.attackTimer += Time.deltaTime;
} }
}else } else
return new SeekState(entity); return new SeekState(entity);
} }
return null; return null;
} }
private BaseState? Attack(){ private BaseState? Attack() {
Entity targetEntity = entity.target.GetComponent<Entity>(); Entity targetEntity = entity.target.GetComponent<Entity>();
if(targetEntity != null){ if (targetEntity != null) {
targetEntity.TakeDamage(entity.attackDmg, entity); targetEntity.TakeDamage(entity.attackDmg, entity);
bool isTargetAlive = targetEntity.IsAlive(); bool isTargetAlive = targetEntity.IsAlive();
if(!isTargetAlive){ if (!isTargetAlive) {
return new FindTargetState(entity); return new FindTargetState(entity);
}
} }
}
return null; return null;
} }
} }
protected class DeadState : BaseStateAI{ protected class DeadState : BaseStateAI {
public DeadState(AIEntity entity) : base(entity){ public DeadState(AIEntity entity) : base(entity) {
} }
@ -235,8 +237,8 @@ public class AIEntity : Entity
entity.animator.Play("Death"); entity.animator.Play("Death");
} }
public override BaseState? UpdateState(){ public override BaseState? UpdateState() {
return null; return null;
} }