Fix health bar flip
This commit is contained in:
parent
48041ea26e
commit
3700cf800a
@ -48,6 +48,7 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: c537e1bd61b8d5c42b1ec03f90e14855, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
arena: {fileID: 0}
|
||||
gameFlowManager: {fileID: 0}
|
||||
<Health>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}
|
||||
|
||||
@ -130,6 +130,7 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 7e480b0ef5998894283e8091830941cb, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
arena: {fileID: 0}
|
||||
gameFlowManager: {fileID: 0}
|
||||
<Health>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}
|
||||
|
||||
@ -2,15 +2,15 @@
|
||||
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();
|
||||
@ -18,13 +18,13 @@ public class AIEntity : Entity
|
||||
|
||||
override protected void Update() {
|
||||
base.Update();
|
||||
if (currentState.UpdateState() is {} newState)
|
||||
if (currentState.UpdateState() is { } newState)
|
||||
SwitchState(newState);
|
||||
}
|
||||
|
||||
override protected void FixedUpdate() {
|
||||
base.FixedUpdate();
|
||||
if (currentState.FixedUpdateState() is {} newState)
|
||||
if (currentState.FixedUpdateState() is { } newState)
|
||||
SwitchState(newState);
|
||||
FlipAccordingToInput();
|
||||
}
|
||||
@ -49,9 +49,9 @@ public class AIEntity : Entity
|
||||
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?
|
||||
if (target != null && target.GetComponent<VampireEntity>() is {})
|
||||
if (target != null && target.GetComponent<VampireEntity>() 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,103 +81,103 @@ public class AIEntity : Entity
|
||||
#endregion
|
||||
|
||||
|
||||
protected abstract class BaseStateAI : BaseState{
|
||||
protected abstract class BaseStateAI : BaseState {
|
||||
protected AIEntity entity;
|
||||
public BaseStateAI(AIEntity entity){
|
||||
public BaseStateAI(AIEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
}
|
||||
|
||||
protected class SeekState : BaseStateAI{
|
||||
public SeekState(AIEntity entity) : base(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{
|
||||
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()){
|
||||
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 {
|
||||
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,41 +186,41 @@ 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){
|
||||
if (entity.IsInAttackRange()) {
|
||||
if (entity.attackTimer >= entity.attackCooldown) {
|
||||
entity.attackTimer = 0;
|
||||
return Attack();
|
||||
}else{
|
||||
} else {
|
||||
entity.attackTimer += Time.deltaTime;
|
||||
}
|
||||
|
||||
}else
|
||||
} else
|
||||
return new SeekState(entity);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private BaseState? Attack(){
|
||||
private BaseState? Attack() {
|
||||
Entity targetEntity = entity.target.GetComponent<Entity>();
|
||||
if(targetEntity != null){
|
||||
if (targetEntity != null) {
|
||||
targetEntity.TakeDamage(entity.attackDmg, entity);
|
||||
bool isTargetAlive = targetEntity.IsAlive();
|
||||
if(!isTargetAlive){
|
||||
if (!isTargetAlive) {
|
||||
return new FindTargetState(entity);
|
||||
}
|
||||
}
|
||||
@ -226,8 +228,8 @@ public class AIEntity : Entity
|
||||
}
|
||||
}
|
||||
|
||||
protected class DeadState : BaseStateAI{
|
||||
public DeadState(AIEntity entity) : base(entity){
|
||||
protected class DeadState : BaseStateAI {
|
||||
public DeadState(AIEntity entity) : base(entity) {
|
||||
|
||||
}
|
||||
|
||||
@ -235,7 +237,7 @@ public class AIEntity : Entity
|
||||
entity.animator.Play("Death");
|
||||
}
|
||||
|
||||
public override BaseState? UpdateState(){
|
||||
public override BaseState? UpdateState() {
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user