test_move.py 4.8KB

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