This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
LNX-69 Add bear #14
Open
alpinus4
wants to merge
10
commits into
master
Choose a base branch
from
LNX-69-add-bear
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
LNX-69 Add bear #14
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f0c5439
[WiP] Add bear, start working on a state machine
alpinus4 626bf29
Implement new state machine, add bear state machine
alpinus4 fc82bf2
I removed error catching from runtime. Revert it.
alpinus4 53fb25a
Add add_transition() method to state machine
alpinus4 d65fb67
Add builder pattern to StateMachine state and transitions construction.
alpinus4 38ee080
Change states from strings to enums
alpinus4 7311e8b
Improve multiple-fields objects
blazej-smorawski 2a1348f
Refactor occupying multiple fields logic
alpinus4 293c3dd
Move BearStateMachine into Bear and add type hinting to StateMachine
alpinus4 6405499
Add fields_around() function to object
alpinus4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pytest==7.2.0 | ||
transitions==0.9.0 | ||
pytest==7.2.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import List | ||
|
||
from src.common.point import Point | ||
from src.actions.action import Action | ||
from src.actions.deal_dmg import DealDmg | ||
from src.objects.object import Object | ||
from src.objects.npc import NPC | ||
from src.objects.agent import Agent | ||
|
||
SLAM_DMG = 10 | ||
|
||
|
||
class Slam(Action): | ||
""" | ||
Simple action for attacking. | ||
""" | ||
object: Object | ||
|
||
def __init__(self, object: Object, points: List[Point]) -> None: | ||
super().__init__() | ||
self.properties.object_id = object.properties.id | ||
self.object = object | ||
self.properties.points = points | ||
|
||
def execute(self) -> None: | ||
self.log() | ||
for target_position in self.properties.points: | ||
if not self.object.scene._objects_position_map.get(target_position): | ||
continue | ||
for target in self.object.scene.get_objects_by_position(target_position): | ||
if isinstance(target, NPC) or isinstance(target, Agent): | ||
DealDmg(target, SLAM_DMG).execute() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from src.actions.action import Action | ||
from src.common.serializable import Properties | ||
from src.objects.object import Object | ||
|
||
|
||
class PrepareToSlam(Action): | ||
""" | ||
Simple action for idling. | ||
""" | ||
object: Object | ||
|
||
def __init__(self, object: Object) -> None: | ||
super().__init__() | ||
self.properties.object_id = object.properties.id | ||
self.object = object | ||
|
||
def execute(self) -> None: | ||
self.log() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Union | ||
from enum import Enum | ||
from typing import Callable | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Transition: | ||
def __init__(self, from_state: Union[str, Enum], to_state: Union[str, Enum], condition_func: Callable[[], bool]): | ||
self.from_state = from_state | ||
self.to_state = to_state | ||
self.condition_func = condition_func | ||
|
||
|
||
class StateMachine(ABC): | ||
""" | ||
Simple state machine. | ||
It checks transitions, and moves one state at a time (if possible) during tick() method. | ||
States can be virtually anything, but best kept as either strings or enums. | ||
""" | ||
|
||
def __init__(self): | ||
self._state = None | ||
self._previous_state = None | ||
self._states = {} | ||
self._transitions = {} | ||
|
||
@abstractmethod | ||
def _state_logic(self): | ||
pass | ||
|
||
def _get_transition(self): | ||
for transition in self._transitions[self._state]: | ||
if transition.condition_func(): | ||
return transition.to_state | ||
return None | ||
alpinus4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@abstractmethod | ||
def _enter_state(self, new_state: Union[str, Enum], old_state: Union[str, Enum]): | ||
pass | ||
|
||
@abstractmethod | ||
def _exit_state(self, old_state: Union[str, Enum], new_state: Union[str, Enum]): | ||
pass | ||
|
||
def set_state(self, new_state: Union[str, Enum]): | ||
self._previous_state = self._state | ||
self._state = new_state | ||
if self._previous_state: | ||
self._exit_state(self._previous_state, new_state) | ||
if new_state: | ||
self._enter_state(new_state, self._previous_state) | ||
|
||
def add_state(self, state: Union[str, Enum]): | ||
self._states[state] = state | ||
self._transitions[state] = [] | ||
return self | ||
|
||
def add_transition(self, from_state: Union[str, Enum], to_state: Union[str, Enum], condition_func: Callable[[], bool]): | ||
self._transitions[from_state].append(Transition(from_state, to_state, condition_func)) | ||
return self | ||
|
||
def tick(self): | ||
if self._state: | ||
self._state_logic() | ||
transition = self._get_transition() | ||
if transition: | ||
self.set_state(transition) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from typing import List | ||
from enum import Enum | ||
|
||
from src.common.state_machine import StateMachine | ||
from src.common.enums import Direction | ||
from src.objects.npcs.enemy import Enemy | ||
from src.objects.agent import Agent | ||
from src.common.point import Point | ||
from src.actions.prepare_to_slam import PrepareToSlam | ||
from src.actions.attacks.slam import Slam | ||
|
||
HP = 100 | ||
|
||
|
||
class Bear(Enemy): | ||
""" | ||
Bear boss. | ||
""" | ||
|
||
def __init__(self, scene: 'Scene', position: Point) -> None: | ||
super().__init__(scene, position, HP, Bear.BearStateMachine(self)) | ||
|
||
def occupied_fields(self, current_position: Point = None) -> List[Point]: | ||
if not current_position: | ||
current_position = self.properties.position | ||
return [current_position, | ||
current_position + Point(0, 1), | ||
current_position + Point(1, 1), | ||
current_position + Point(1, 0)] | ||
|
||
def tick(self) -> None: | ||
self.machine.tick() | ||
|
||
def slam(self): | ||
Slam(self, self.fields_around()).execute() | ||
|
||
def prepare_to_slam(self): | ||
PrepareToSlam(self).execute() | ||
|
||
def is_there_agent_to_slam(self): | ||
for target_position in self.fields_around(): | ||
if not self.scene._objects_position_map.get(target_position): | ||
continue | ||
for target in self.scene.get_objects_by_position(target_position): | ||
if isinstance(target, Agent): | ||
return True | ||
return False | ||
|
||
class State(Enum): | ||
IDLE = 0 | ||
MOVE = 1 | ||
PREPARE_TO_SLAM = 2 | ||
SLAM = 3 | ||
|
||
class BearStateMachine(StateMachine): | ||
|
||
def __init__(self, bear: 'Bear'): | ||
super().__init__() | ||
self.bear = bear | ||
self.add_state(Bear.State.IDLE)\ | ||
.add_state(Bear.State.MOVE)\ | ||
.add_state(Bear.State.PREPARE_TO_SLAM)\ | ||
.add_state(Bear.State.SLAM)\ | ||
.add_transition(Bear.State.IDLE, Bear.State.PREPARE_TO_SLAM, lambda: self.bear.is_there_agent_to_slam())\ | ||
.add_transition(Bear.State.IDLE, Bear.State.MOVE, lambda: True)\ | ||
.add_transition(Bear.State.MOVE, Bear.State.PREPARE_TO_SLAM, lambda: self.bear.is_there_agent_to_slam())\ | ||
.add_transition(Bear.State.PREPARE_TO_SLAM, Bear.State.SLAM, lambda: True)\ | ||
.add_transition(Bear.State.SLAM, Bear.State.IDLE, lambda: True)\ | ||
.set_state(Bear.State.IDLE) | ||
|
||
def _state_logic(self): | ||
match self._state: | ||
case Bear.State.IDLE: | ||
self.bear.idle() | ||
case Bear.State.MOVE: | ||
self.bear.move() | ||
case Bear.State.PREPARE_TO_SLAM: | ||
self.bear.prepare_to_slam() | ||
case Bear.State.SLAM: | ||
self.bear.slam() | ||
|
||
def _enter_state(self, new_state, old_state): | ||
pass | ||
|
||
def _exit_state(self, old_state, new_state): | ||
pass |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This thing is a little tricky. Right now front-end does not support deserialization of lists but I think it should not be a problem to add something like that. I guess it will be enough to have some helper function in
Deserializer
to callstrtok
.