base.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # coding: utf-8
  2. from pyglet.window import key
  3. from cocos.actions import MoveTo as BaseMoveTo
  4. from sandbox.tile.gui.move import MoveActorInteraction
  5. from sandbox.tile.gui.move import MoveFastActorInteraction
  6. from sandbox.tile.gui.move import MoveCrawlActorInteraction
  7. from synergine2_cocos2d.layer import LayerManager
  8. from synergine2_xyz.move.simulation import FinishMoveEvent
  9. from synergine2_xyz.move.simulation import StartMoveEvent
  10. from synergine2_xyz.utils import get_angle
  11. from synergine2.config import Config
  12. from synergine2.log import SynergineLogger
  13. from synergine2.terminals import Terminal
  14. from synergine2_cocos2d.gui import TMXGui
  15. from synergine2_cocos2d.gui import EditLayer as BaseEditLayer
  16. from synergine2_cocos2d.actions import MoveTo
  17. from synergine2_cocos2d.animation import Animate
  18. # TODO NOW: MOVE
  19. from synergine2_cocos2d.animation import ANIMATION_CRAWL
  20. from synergine2_cocos2d.animation import ANIMATION_WALK
  21. from sandbox.tile.user_action import UserAction
  22. class EditLayer(BaseEditLayer):
  23. def _on_key_press(self, k, m):
  24. if self.selection:
  25. if k == key.M:
  26. self.user_action_pending = UserAction.ORDER_MOVE
  27. if k == key.R:
  28. self.user_action_pending = UserAction.ORDER_MOVE_FAST
  29. if k == key.C:
  30. self.user_action_pending = UserAction.ORDER_MOVE_CRAWL
  31. class TileLayerManager(LayerManager):
  32. edit_layer_class = EditLayer
  33. class Game(TMXGui):
  34. layer_manager_class = TileLayerManager
  35. def __init__(
  36. self,
  37. config: Config,
  38. logger: SynergineLogger,
  39. terminal: Terminal,
  40. read_queue_interval: float = 1 / 60.0,
  41. map_dir_path: str=None,
  42. ):
  43. super().__init__(
  44. config,
  45. logger,
  46. terminal,
  47. read_queue_interval,
  48. map_dir_path,
  49. )
  50. self.terminal.register_event_handler(
  51. FinishMoveEvent,
  52. self.set_subject_position,
  53. )
  54. self.terminal.register_event_handler(
  55. StartMoveEvent,
  56. self.start_move_subject,
  57. )
  58. # configs
  59. self.move_duration_ref = float(self.config.resolve('game.move.walk_ref_time'))
  60. self.move_fast_duration_ref = float(self.config.resolve('game.move.run_ref_time'))
  61. self.move_crawl_duration_ref = float(self.config.resolve('game.move.crawl_ref_time'))
  62. def before_run(self) -> None:
  63. self.layer_manager.interaction_manager.register(MoveActorInteraction, self.layer_manager)
  64. self.layer_manager.interaction_manager.register(MoveFastActorInteraction, self.layer_manager)
  65. self.layer_manager.interaction_manager.register(MoveCrawlActorInteraction, self.layer_manager)
  66. def set_subject_position(self, event: FinishMoveEvent):
  67. actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
  68. new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
  69. actor.stop_actions((BaseMoveTo,))
  70. actor.set_position(*new_world_position)
  71. def start_move_subject(self, event: StartMoveEvent):
  72. actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
  73. new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
  74. if event.gui_action == UserAction.ORDER_MOVE:
  75. animation = ANIMATION_WALK
  76. cycle_duration = 2
  77. move_duration = self.move_duration_ref
  78. elif event.gui_action == UserAction.ORDER_MOVE_FAST:
  79. animation = ANIMATION_WALK
  80. cycle_duration = 0.5
  81. move_duration = self.move_fast_duration_ref
  82. elif event.gui_action == UserAction.ORDER_MOVE_CRAWL:
  83. animation = ANIMATION_CRAWL
  84. cycle_duration = 2
  85. move_duration = self.move_crawl_duration_ref
  86. else:
  87. raise NotImplementedError()
  88. move_action = MoveTo(new_world_position, move_duration)
  89. actor.do(move_action)
  90. actor.do(Animate(animation, duration=move_duration, cycle_duration=cycle_duration))
  91. actor.rotation = get_angle(event.from_position, event.to_position)