test_move.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # coding: utf-8
  2. import time
  3. from freezegun import freeze_time
  4. from synergine2.config import Config
  5. from synergine2.simulation import Simulation, Subject
  6. from synergine2_cocos2d.user_action import UserAction as BaseUserAction
  7. from synergine2_xyz.move.intention import MoveToIntention
  8. from synergine2_xyz.move.simulation import MoveToMechanism
  9. from synergine2_xyz.move.simulation import StartMoveEvent
  10. from synergine2_xyz.move.simulation import FinishMoveEvent
  11. from synergine2_xyz.simulation import XYZSimulation
  12. from synergine2_xyz.subjects import XYZSubject
  13. from tests import BaseTest
  14. from synergine2_xyz.move.simulation import MoveToBehaviour as BaseMoveToBehaviour
  15. class MyMoveToBehaviour(BaseMoveToBehaviour):
  16. def __init__(
  17. self,
  18. config: Config,
  19. simulation: Simulation,
  20. subject: Subject,
  21. ) -> None:
  22. super().__init__(config, simulation, subject)
  23. self._walk_duration = float(self.config.resolve('game.move.walk_ref_time'))
  24. def _can_move_to_next_step(self, move_to_data: dict) -> bool:
  25. if move_to_data['gui_action'] == UserAction.ORDER_MOVE:
  26. return time.time() - move_to_data['last_intention_time'] >= self._walk_duration
  27. class UserAction(BaseUserAction):
  28. ORDER_MOVE = 'ORDER_MOVE'
  29. class MySubject(XYZSubject):
  30. pass
  31. class MySimulation(XYZSimulation):
  32. pass
  33. class TestMove(BaseTest):
  34. def test_behaviour_cycle(self):
  35. config = Config({
  36. 'game': {
  37. 'move': {
  38. 'walk_ref_time': 2,
  39. }
  40. }
  41. })
  42. simulation = MySimulation(config)
  43. subject = MySubject(config, simulation)
  44. behaviour = MyMoveToBehaviour(config, simulation, subject)
  45. with freeze_time("2000-01-01 00:00:00"):
  46. move_intention = MoveToIntention((0, 3), time.time(), gui_action=UserAction.ORDER_MOVE)
  47. assert move_intention.path_progression == -1
  48. assert move_intention.just_reach is False
  49. assert move_intention.initial is True
  50. subject.intentions.set(move_intention)
  51. move_data = {
  52. 'new_path': [(0, 1), (0, 2)],
  53. 'last_intention_time': time.time(),
  54. 'just_reach': False,
  55. 'initial': True,
  56. 'gui_action': UserAction.ORDER_MOVE,
  57. }
  58. data = {
  59. MoveToMechanism: move_data,
  60. }
  61. run_data = behaviour.run(data)
  62. assert move_data == run_data
  63. events = behaviour.action(run_data)
  64. assert events
  65. assert 1 == len(events)
  66. assert isinstance(events[0], StartMoveEvent)
  67. assert move_intention.path_progression == -1
  68. assert move_intention.just_reach is False
  69. assert move_intention.initial is False
  70. # Update data like mechanism do it
  71. move_data['last_intention_time'] = move_intention.last_intention_time
  72. move_data['just_reach'] = move_intention.just_reach
  73. move_data['initial'] = move_intention.initial
  74. # Only one second, no reach
  75. with freeze_time("2000-01-01 00:00:01"):
  76. run_data = behaviour.run(data)
  77. assert run_data is False
  78. # Two second, step reach
  79. with freeze_time("2000-01-01 00:00:02"):
  80. run_data = behaviour.run(data)
  81. assert {
  82. 'new_path': [(0, 1), (0, 2)],
  83. 'initial': False,
  84. 'just_reach': False,
  85. 'last_intention_time': 946684800.0,
  86. 'reach_next': True,
  87. 'gui_action': UserAction.ORDER_MOVE,
  88. } == run_data
  89. events = behaviour.action(run_data)
  90. assert events
  91. assert 1 == len(events)
  92. assert isinstance(events[0], FinishMoveEvent)
  93. # Update data like mechanism do it
  94. move_data['last_intention_time'] = move_intention.last_intention_time
  95. move_data['just_reach'] = move_intention.just_reach
  96. move_data['initial'] = move_intention.initial
  97. # Three seconds, start a new move
  98. with freeze_time("2000-01-01 00:00:03"):
  99. run_data = behaviour.run(data)
  100. assert {
  101. 'new_path': [(0, 1), (0, 2)],
  102. 'initial': False,
  103. 'just_reach': True,
  104. 'last_intention_time': 946684802.0,
  105. 'reach_next': False,
  106. 'gui_action': UserAction.ORDER_MOVE,
  107. } == run_data
  108. events = behaviour.action(run_data)
  109. assert events
  110. assert 1 == len(events)
  111. assert isinstance(events[0], StartMoveEvent)
  112. # Update data like mechanism do it
  113. move_data['last_intention_time'] = move_intention.last_intention_time
  114. move_data['just_reach'] = move_intention.just_reach
  115. move_data['initial'] = move_intention.initial
  116. # Four seconds, start a new move
  117. with freeze_time("2000-01-01 00:00:04"):
  118. run_data = behaviour.run(data)
  119. assert {
  120. 'new_path': [(0, 1), (0, 2)],
  121. 'initial': False,
  122. 'just_reach': False,
  123. 'last_intention_time': 946684802.0,
  124. 'reach_next': True,
  125. 'gui_action': UserAction.ORDER_MOVE,
  126. } == run_data
  127. events = behaviour.action(run_data)
  128. assert events
  129. assert 1 == len(events)
  130. assert isinstance(events[0], FinishMoveEvent)