Skip to content

Commit

Permalink
Merge pull request #169 from arnauddupuis/arnauddupuis/issue121
Browse files Browse the repository at this point in the history
[BUG] Actuators needs to return NO_DIR if there is no next_move available.
  • Loading branch information
arnauddupuis committed Oct 22, 2021
2 parents 6f18989 + 678c5cb commit 572bf3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 3 additions & 0 deletions breaking-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ Sprites are not describing the same thing as before.

1.2.0 -> 1.3.0

META: We need some sort of code to show the severity of the breaking change. It should reflect the pain to update.

BoardItem: type in constructor is now item_type. This breaks a lot of things...
BoardItem: they are now all able to set/get overlappable, restorable, pickable and can_move
BoardItem: value has been moved to BoardItem.
BoardItem: change of behavior: now if both model and sprixel are given to the constructor, model is ignored (sprixel has priority). Need an example in the release note.
BoardItem: there was a conflict with inventory_space. It was defined both as a property and a method. The method has been removed. Concretely: you might have to remove parenthesis when using any_item.inventory_space (vs the old any_item.inventory_space()).
pygamelib.actuators: all the next_move() methods now returns pygamelib.constants.NO_DIR instead of None if there's no more move to use.

[non breaking][important] Void cells are now initialized with knowing their position.
[non breaking][important] The observer system has some limitations. One being that notifications are not passed down by attributes.
Expand Down
32 changes: 18 additions & 14 deletions pygamelib/actuators.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def next_move(self):
"""Return a randomly selected movement
The movement is randomly selected from moveset if state is RUNNING,
otherwise it should return None.
otherwise it returns NO_DIR from the :py:mod:`~pygamelib.constants` module.
:return: The next movement
:rtype: int | None
:rtype: int | :py:const:`pygamelib.constants.NO_DIR`
Example::
Expand Down Expand Up @@ -226,12 +226,12 @@ def next_move(self):
"""Return the movement based on current index
The movement is selected from path if state is RUNNING, otherwise
it should return None. When state is RUNNING, the movement is selected
before incrementing the index by 1. When the index equal the length of
path, the index should return back to 0.
it returns NO_DIR from the :py:mod:`~pygamelib.constants` module. When state is
RUNNING, the movement is selected before incrementing the index by 1. When the
index equal the length of path, the index should return back to 0.
:return: The next movement
:rtype: int | None
:rtype: int | :py:const:`pygamelib.constants.NO_DIR`
Example::
Expand All @@ -243,6 +243,8 @@ def next_move(self):
if self.index == len(self.path):
self.index = 0
return move
else:
return constants.NO_DIR

def set_path(self, path):
"""Defines a new path
Expand Down Expand Up @@ -307,14 +309,14 @@ class PatrolActuator(PathActuator):
def next_move(self):
"""Return the movement based on current index
The movement is selected from path if state is RUNNING, otherwise it
should return None. When state is RUNNING, the movement is selected
before incrementing the index by 1. When the index equals the length
of path, the index should return back to 0 and the path list should be
reversed before the next call.
The movement is selected from path if state is RUNNING, otherwise it returns
NO_DIR from the :py:mod:`~pygamelib.constants` module. When state is RUNNING,
the movement is selected before incrementing the index by 1. When the index
equals the length of path, the index should return back to 0 and the path list
should be reversed before the next call.
:return: The next movement
:rtype: int | None
:rtype: int | :py:const:`pygamelib.constants.NO_DIR`
Example::
Expand Down Expand Up @@ -344,6 +346,8 @@ def next_move(self):
elif self.path[i] == constants.DRUP:
self.path[i] = constants.DLDOWN
return move
else:
return constants.NO_DIR

def serialize(self) -> dict:
"""Return a dictionary with all the attributes of this object.
Expand Down Expand Up @@ -398,10 +402,10 @@ def next_move(self):
"""Return the direction.
The movement is always direction if state is RUNNING,
otherwise it returns None.
otherwise it returns NO_DIR from the :py:mod:`~pygamelib.constants` module.
:return: The next movement
:rtype: int | None
:rtype: int | :py:const:`pygamelib.constants.NO_DIR`
Example::
Expand Down
5 changes: 4 additions & 1 deletion tests/test_actuators.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_random_empty(self):
def test_path(self):
a = actuators.PathActuator([constants.UP])
a.pause()
self.assertIsNone(a.next_move())
self.assertEqual(a.next_move(), constants.NO_DIR)
a.start()
self.assertEqual(a.next_move(), constants.UP)
a.set_path([constants.DOWN])
Expand All @@ -69,6 +69,9 @@ def test_patrol(self):
al = actuators.PatrolActuator.load(data)
self.assertEqual(al.path, [constants.UP, constants.DOWN])
self.assertEqual(a.state, al.state)
a.pause()
self.assertEqual(a.next_move(), constants.NO_DIR)
a.start()
a.next_move()
self.assertEqual(a.next_move(), constants.DOWN)
self.assertEqual(a.next_move(), constants.UP)
Expand Down

0 comments on commit 572bf3c

Please sign in to comment.