Skip to content

Commit

Permalink
Merge pull request chrysly#21 from alexjusung1/Win-Lose
Browse files Browse the repository at this point in the history
Win & Lose State Implementation
  • Loading branch information
chrysly authored Sep 7, 2023
2 parents 780c1f0 + 6489057 commit 2f16e35
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 16 deletions.
13 changes: 12 additions & 1 deletion Assets/_Scripts/Turn Based Mechanics/Actor Scripts/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Actor : MonoBehaviour, IComparable<Actor>

#region Variable Attributes
[SerializeField] private float _hitpoints;
private bool _defeated;
private int _stamina;
#endregion Variable Attributes

Expand All @@ -29,31 +30,41 @@ protected virtual void Start()
protected virtual void InitializeAttributes() {
_hitpoints = data.MaxHitpoints();
_stamina = data.MaxStamina();
_defeated = false;
}

//Returns true if Actor has no remaining health.
public bool DepleteHitpoints(float damage) {
if (_hitpoints - damage <= 0) {
_hitpoints = 0;
_defeated = true;
Debug.Log($"{this.data.DisplayName()} has fallen!");
return true;
}
_hitpoints -= damage;
return false;
}

//Returns true if over maximum hitpoints.
//Does not heal if Actor is defeated.
public bool RestoreHitpoints(float heal) {
if (_hitpoints + heal > data.MaxHitpoints()) {
_hitpoints = data.MaxHitpoints();
return true;
}
_hitpoints += heal;
if (!_defeated) {
_hitpoints += heal;
}
return false;
}

public float Hitpoints() {
return _hitpoints;
}

public bool Defeated() {
return _defeated;
}

public bool HasRemainingStamina() {
return _stamina > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public class AnimateState : BattleState {
public override void Enter(BattleStateInput i) {
base.Enter(i);
Debug.Log("Entering animate state");
if (Input.ActiveActor() is CharacterActor) {
Input.ActiveSkill().ActivateSkill();
} else if (Input.ActiveActor() is EnemyActor) { //get rid of this aaaaa
int target = Input.CurrTurn() % 2 == 0 ? 0 : MySM.actorList.Count - 1;
Input.SetActiveSkill(new SkillAction(Input.ActiveActor().data.SkillList()[0], MySM.actorList[target]));
Input.ActiveSkill().ActivateSkill();
}
Input.ActiveSkill().ActivateSkill();

// Old Enemy Code, should implement Enemy AI in TargetSelectState instead
//
//if (Input.ActiveActor() is CharacterActor) {
// Input.ActiveSkill().ActivateSkill();
//} else if (Input.ActiveActor() is EnemyActor) { //get rid of this aaaaa
// int target = Input.CurrTurn() % 2 == 0 ? 0 : MySM.actorList.Count - 1;
// Input.SetActiveSkill(new SkillAction(Input.ActiveActor().data.SkillList()[0], MySM.actorList[target]));
// Input.ActiveSkill().ActivateSkill();
//}

_movement.Bump(Input.ActiveActor().transform, Input.ActiveSkill().Target().transform);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ public abstract class BattleState : State<BattleStateMachine, BattleState, Battl
public virtual void EnterBattle() { MySM.Transition<TurnState>(); }

public virtual void AnimateTurn() { MySM.Transition<AnimateState>(); }

public virtual void TriggerBattleWin() { MySM.Transition<WinState>(); }

public virtual void TriggerBattleLose() { MySM.Transition<LoseState>(); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public void SetActiveSkill(SkillAction skill) {
activeSkill = skill;
}

//Advances until the next undefeated Actor. Returns to initial Actor if not available.
public void AdvanceTurn() {
if (currActorIndex < turnQueue.Count - 1) {
currActorIndex++;
} else {
currActorIndex = 0;
}
Actor initialActor = ActiveActor();
do {
currActorIndex = (currActorIndex + 1) % turnQueue.Count;
} while (ActiveActor().Defeated() && !initialActor.Equals(ActiveActor()));
currentTurn++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using UnityEngine;


public partial class
BattleStateMachine : StateMachine<BattleStateMachine, BattleStateMachine.BattleState, BattleStateInput> {

Expand Down Expand Up @@ -43,7 +42,27 @@ protected override void FixedUpdate() {

#region State Handlers
public void StartBattle() {
CurrState.EnterBattle();
// Checks whether to progress to Win/Lose state
bool allEnemiesDead = true;
bool allCharactersDead = true;
foreach (Actor actor in actorList) {
if (actor.Defeated()) {
continue;
}
if (actor is EnemyActor) {
allEnemiesDead = false;
} else if (actor is CharacterActor) {
allCharactersDead = false;
}
}

if (allEnemiesDead) {
CurrState.TriggerBattleWin();
} else if (allCharactersDead) {
CurrState.TriggerBattleLose();
} else {
CurrState.EnterBattle();
}
}

public void StartBattle(float delay) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public partial class BattleStateMachine {
public class LoseState : BattleState {
public override void Enter(BattleStateInput i) {
base.Enter(i);
Debug.Log($"You lost the battle...");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public partial class BattleStateMachine {
public class TargetSelectState : BattleState {
private SelectorManager _selectManager = new SelectorManager();

private int _nextSelectedActor = 0; // Enemy Actor Selection

public override void Enter(BattleStateInput i) {
base.Enter(i);
Debug.Log("Entering target select state");
if (Input.ActiveActor() is EnemyActor) {
// Enemy Actor Selection; pls fix thank you
Input.SetActiveSkill(new SkillAction(Input.ActiveActor().data.SkillList()[0], MySM.actorList[_nextSelectedActor]));
_nextSelectedActor = (_nextSelectedActor == 0) ? MySM.actorList.Count - 1 : 0;
MySM.Transition<AnimateState>();
}
MySM.OnStateTransition.Invoke(this, Input);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public partial class BattleStateMachine {
public class WinState : BattleState {
public override void Enter(BattleStateInput i) {
base.Enter(i);
Debug.Log($"You won the battle!");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f16e35

Please sign in to comment.