Skip to content

Commit

Permalink
Merge pull request #193 from arnauddupuis/arnauddupuis/issue189
Browse files Browse the repository at this point in the history
Issue #189: Improve RandomActuator
  • Loading branch information
arnauddupuis committed Jun 11, 2022
2 parents c0c8979 + 777668f commit 87587d1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
70 changes: 64 additions & 6 deletions pygamelib/actuators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from queue import PriorityQueue


class Actuator:
class Actuator(base.PglBaseObject):
"""
Actuator is the base class for all Actuators. It is mainly a contract class with
some utility methods.
Expand All @@ -40,6 +40,7 @@ def __init__(self, parent):
actuator to be in a different state (PAUSED for example), you have to do it
yourself.
"""
super().__init__()
self.type = None
self.state = constants.RUNNING
self.parent = parent
Expand Down Expand Up @@ -150,8 +151,48 @@ def __init__(self, moveset=None, parent=None):
if moveset is None:
moveset = []
super().__init__(parent)
self.__moveset = []
self._vector_moveset = []
self.__current_direction = None
self.__current_dir_move_left = None
# We'll use that to check if the moving board item is stuck.
self.__projected_position_cache = None
self.moveset = moveset

@property
def moveset(self):
"""
Return the moveset.
:return: The moveset.
:rtype: list
"""
return self.__moveset

@moveset.setter
def moveset(self, moveset):
"""
Set the moveset.
:param moveset: The moveset.
:type moveset: list
"""
self.__moveset = moveset
self._vector_moveset = []
if len(self.moveset) > 0:
# let's build a cache of directions to avoid creating vectors at each
# next_move() call.
for m in self.moveset:
if isinstance(m, base.Vector2D):
self._vector_moveset.append(m)
else:
# Here we consider that in moveset, there's either Vector2D or
# directions from the constants module. If it is not the case,
# result will be funky...
self._vector_moveset.append(base.Vector2D.from_direction(m, 1))
self.__current_direction = random.randrange(0, len(self.moveset))
self.__current_dir_move_left = random.randint(1, 10)

def next_move(self):
"""Return a randomly selected movement
Expand All @@ -163,10 +204,27 @@ def next_move(self):
Example::
randomactuator.next_move()
random_actuator.next_move()
"""
if self.state == constants.RUNNING and self.moveset:
return random.choice(self.moveset)
ppav = None
if isinstance(self.parent, board_items.Movable):
ppav = self.parent.position_as_vector()
if (
self.__current_dir_move_left <= 0
or ppav != self.__projected_position_cache
):

self.__current_direction = random.randrange(0, len(self.moveset))
self.__current_dir_move_left = random.randint(1, 10)
self.__current_dir_move_left -= 1
if ppav is not None:
self.__projected_position_cache = (
ppav + self._vector_moveset[self.__current_direction]
)
return self.moveset[self.__current_direction]

# return random.choice(self.moveset)
else:
return constants.NO_DIR

Expand Down Expand Up @@ -235,7 +293,7 @@ def next_move(self):
Example::
pathactuator.next_move()
path_actuator.next_move()
"""
if self.state == constants.RUNNING:
move = self.path[self.index]
Expand All @@ -256,7 +314,7 @@ def set_path(self, path):
Example::
pathactuator.set_path([constants.UP,constants.DOWN,constants.LEFT,constants.RIGHT])
path_actuator.set_path([constants.UP,constants.DOWN,constants.LEFT,constants.RIGHT])
"""
self.path = path
self.index = 0
Expand Down Expand Up @@ -320,7 +378,7 @@ def next_move(self):
Example::
patrolactuator.next_move()
patrol_actuator.next_move()
"""
if self.state == constants.RUNNING:
move = self.path[self.index]
Expand Down
3 changes: 2 additions & 1 deletion pygamelib/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,8 @@ def add_npc(
constants.DOWN,
constants.LEFT,
constants.RIGHT,
]
],
parent=npc,
)
if npc.step is None:
npc.step = 1
Expand Down
8 changes: 4 additions & 4 deletions tests/test_actuators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from pygamelib import actuators
from pygamelib import constants
from pygamelib import board_items
from pygamelib import engine
from pygamelib import actuators, constants, board_items, engine, base
import unittest

# Test cases for all classes in pygamelib.gfx.particles.
Expand Down Expand Up @@ -40,6 +37,9 @@ def test_random(self):
al = actuators.RandomActuator.load(data)
self.assertEqual(al.moveset, [constants.UP])
self.assertEqual(al.moveset, a.moveset)
# Test with a vector based move set
a = actuators.RandomActuator([base.Vector2D(1, 1)])
self.assertEqual(a.moveset, [base.Vector2D(1, 1)])

def test_random_empty(self):
a = actuators.RandomActuator()
Expand Down

0 comments on commit 87587d1

Please sign in to comment.