fire.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # coding: utf-8
  2. import typing
  3. from opencombat.game.actor import BaseActor
  4. from opencombat.simulation.event import DEFAULT_WEAPON_TYPE
  5. from opencombat.simulation.fire import RequestFireBehaviour
  6. from synergine2_cocos2d.interaction import BaseActorInteraction
  7. from opencombat.user_action import UserAction
  8. from synergine2.simulation import SimulationBehaviour
  9. from synergine2_cocos2d.actor import Actor
  10. from synergine2_cocos2d.gl import draw_line
  11. class BaseFireActorInteraction(BaseActorInteraction):
  12. gui_action = None
  13. color = None
  14. not_visible_color = (0, 0, 0)
  15. request_move_behaviour_class = RequestFireBehaviour
  16. def draw_pending(self) -> None:
  17. for actor in self.layer_manager.edit_layer.selection:
  18. actor_grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
  19. actor_pixel_position = self.layer_manager.grid_manager.get_world_position_of_grid_position(
  20. actor_grid_position,
  21. )
  22. mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
  23. self.layer_manager.scrolling_manager.screen_to_world(
  24. *self.layer_manager.edit_layer.screen_mouse,
  25. )
  26. )
  27. draw_to_pixel = self.layer_manager.edit_layer.screen_mouse
  28. obstacle_grid_position = self.layer_manager.gui.physics.get_visibility_obstacle(
  29. subject=actor.subject,
  30. to_position=mouse_grid_position,
  31. matrix_name='visibility',
  32. opacity_property_name='opacity',
  33. )
  34. # DEBUG
  35. if self.layer_manager.debug:
  36. grid_paths = self.layer_manager.gui.physics.matrixes.get_path_positions(
  37. from_=actor_grid_position,
  38. to=mouse_grid_position,
  39. )
  40. previous_grid_path = None
  41. for grid_path in grid_paths:
  42. if previous_grid_path:
  43. previous_grid_path_pixel = self.layer_manager.grid_manager.get_world_position_of_grid_position(
  44. previous_grid_path,
  45. )
  46. current_grid_pixel = self.layer_manager.grid_manager.get_world_position_of_grid_position(
  47. grid_path,
  48. )
  49. draw_line(
  50. self.layer_manager.scrolling_manager.world_to_screen(*previous_grid_path_pixel),
  51. self.layer_manager.scrolling_manager.world_to_screen(*current_grid_pixel),
  52. (25, 125, 25),
  53. )
  54. previous_grid_path = grid_path
  55. if obstacle_grid_position:
  56. obstacle_pixel = self.layer_manager.grid_manager.get_world_position_of_grid_position(
  57. obstacle_grid_position,
  58. )
  59. draw_to_pixel = self.layer_manager.scrolling_manager.world_to_screen(*obstacle_pixel)
  60. draw_line(
  61. self.layer_manager.scrolling_manager.world_to_screen(*obstacle_pixel),
  62. self.layer_manager.edit_layer.screen_mouse,
  63. self.not_visible_color,
  64. )
  65. draw_line(
  66. self.layer_manager.scrolling_manager.world_to_screen(*actor_pixel_position),
  67. draw_to_pixel,
  68. self.color,
  69. )
  70. def get_behaviour(self, actor: Actor, mouse_grid_position) -> typing.Tuple[typing.Type[SimulationBehaviour], dict]:
  71. raise NotImplementedError()
  72. return self.request_move_behaviour_class, {
  73. 'subject_id': actor.subject.id,
  74. 'move_to': mouse_grid_position,
  75. 'gui_action': self.gui_action,
  76. }
  77. class FireActorInteraction(BaseFireActorInteraction):
  78. gui_action = UserAction.ORDER_FIRE
  79. color = (255, 0, 0)
  80. class GuiFiringEvent(object):
  81. def __init__(
  82. self,
  83. actor: BaseActor,
  84. weapon: str,
  85. ) -> None:
  86. self.actor = actor
  87. self.weapon = weapon
  88. self._animation_index = -1
  89. if weapon == DEFAULT_WEAPON_TYPE:
  90. self.weapon = self.actor.weapons[0]
  91. @property
  92. def animation_index(self) -> int:
  93. return self._animation_index
  94. def increment_animation_index(self) -> None:
  95. self._animation_index += 1
  96. def reset_animation_index(self) -> None:
  97. self._animation_index = -1