Created
January 29, 2023 00:22
-
-
Save sanbox-irl/f81cd48b57a56b9cd783150241338471 to your computer and use it in GitHub Desktop.
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
pub struct PlayerFsm { | |
default: Default, | |
special: Special, | |
current_state: PlayerStateName, | |
next_state: Option<PlayerStateName>, | |
pub blackboard: PlayerStateBlackboard, | |
} | |
pub struct Default { | |
speed: f32, | |
} | |
pub struct Special { | |
happiness: i32, | |
} | |
pub struct PlayerStateBlackboard { | |
pub going_to_happiness: bool, | |
} | |
pub enum PlayerStateName { | |
Default, | |
Special, | |
} | |
impl PlayerFsm { | |
pub fn change_state(&mut self, player_state_name: PlayerStateName) { | |
self.next_state = Some(player_state_name); | |
} | |
pub fn tick(&mut self) -> Option<&mut dyn PlayerState> { | |
if let Some(next_state) = self.next_state.take() { | |
self.current_state = next_state; | |
Some(self.state_mut()) | |
} else { | |
None | |
} | |
} | |
pub fn state_mut(&mut self) -> &mut dyn PlayerState { | |
match self.current_state { | |
PlayerStateName::Default => &mut self.default as &mut dyn PlayerState, | |
PlayerStateName::Special => &mut self.special as &mut dyn PlayerState, | |
} | |
} | |
} | |
pub trait PlayerState {} | |
impl PlayerState for Default {} | |
impl PlayerState for Special {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment