Created
January 22, 2019 17:08
-
-
Save ItsMeThom-zz/f63f517d53e21b465a4686e103f8ab86 to your computer and use it in GitHub Desktop.
rough state machine with actions and states
This file contains hidden or 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
| from abc import abstractmethod, ABC | |
| class State(ABC): | |
| actions = [] # action(s) to take in this state | |
| next_states = [] # all states reachable from this state | |
| def test(self, entity): | |
| """ | |
| Perform some test using the entity data | |
| to see if this state is valid | |
| """ | |
| return True | |
| def next(self, entity): | |
| """ | |
| Perform some test of entity data to determine which state to | |
| return from the transitions. The returned state is the new state for | |
| this entity. | |
| """ | |
| return self.next_states[0] | |
| class Action(ABC): | |
| @abstractmethod | |
| def do(self, entity): | |
| # perform some transformation on the | |
| # entity or world | |
| pass | |
| class StateMachine: | |
| current_state = None | |
| def think(self, entity): | |
| # test all states in current_state and select. | |
| pass | |
| class Entity: | |
| x_pos = 1 | |
| y_pos = 1 | |
| hunger = 0.5 | |
| tiredness = 0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment