Last active
June 29, 2023 15:00
-
-
Save nilpunch/4ebd7d596f3a0639b794e4975225bee3 to your computer and use it in GitHub Desktop.
Simplest possible object-oriented FSM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AttackState : IBehaviorState | |
{ | |
private readonly ICharacter _character; | |
public AttackState(ICharacter character) | |
{ | |
_character = character; | |
} | |
public IBehaviorState Execute(long time) | |
{ | |
if (_character.Dead) | |
return new DeadState().Execute(time); | |
if (_character.HasTarget() == false) | |
return new FindTargetState(_character).Execute(time); | |
_character.AttackTarget(); | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeadState : IBehaviorState | |
{ | |
public DeadState() | |
{ | |
} | |
public IBehaviorState Execute(long time) | |
{ | |
// Playing death animation or smth | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FindTargetState : IBehaviorState | |
{ | |
private readonly ICharacter _character; | |
public FindTargetState(ICharacter character) | |
{ | |
_character = character; | |
} | |
public IBehaviorState Execute(long time) | |
{ | |
if (_character.Dead) | |
return new DeadState().Execute(time); | |
if (_character.HasTarget()) | |
return new AttackState(_character).Execute(time); | |
_character.FindTarget(); | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IBehaviorState | |
{ | |
/// <summary> | |
/// Updates state and checks for transitions. | |
/// </summary> | |
/// <returns> | |
/// Next state or self. | |
/// </returns> | |
IBehaviorState Execute(long time); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <remark> | |
/// Sample bot interface. | |
/// </remark> | |
public interface ICharacter | |
{ | |
int Health { get; } | |
bool Dead { get; } | |
bool HasTarget(); | |
void FindTarget(); | |
void AttackTarget(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment