gui.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # coding: utf-8
  2. from random import randint
  3. import cocos
  4. from cocos.actions import MoveTo, Repeat, ScaleBy, Reverse, RotateTo
  5. from cocos.sprite import Sprite
  6. from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, MoveTo as MoveToEvent, EatEvent, AttackEvent, EatenEvent
  7. from sandbox.engulf.subject import Cell, Grass, PreyCell, PredatorCell
  8. from synergine2.terminals import TerminalPackage
  9. from synergine2_cocos2d.gui import Gui, GridLayerMixin
  10. from synergine2_cocos2d.gui import MainLayer as BaseMainLayer
  11. cell_scale = ScaleBy(1.1, duration=0.25)
  12. class CellsLayer(GridLayerMixin, BaseMainLayer):
  13. def __init__(self, game: 'Game', *args, **kwargs):
  14. super().__init__(*args, **kwargs)
  15. self.game = game
  16. self.cell_positions = {}
  17. self.cell_ids = {}
  18. @property
  19. def move_duration(self):
  20. return self.game.cycle_duration
  21. @property
  22. def fake_move_rotate_duration(self):
  23. return self.move_duration / 3
  24. def born(self, subject_id: int, grid_position, cell_type):
  25. png = 'resources/cell.png' if cell_type is PreyCell else 'resources/cellp.png'
  26. cell = Sprite(png)
  27. cell.rotation = randint(0, 360)
  28. self.grid_manager.scale_sprite(cell)
  29. self.grid_manager.position_sprite(cell, grid_position)
  30. self.cell_positions[grid_position] = cell
  31. self.cell_ids[subject_id] = cell
  32. cell.do(Repeat(cell_scale + Reverse(cell_scale)))
  33. self.add(cell)
  34. def move(self, subject_id: int, position: tuple):
  35. if subject_id not in self.cell_ids:
  36. return # Cell eaten before
  37. cell = self.cell_ids[subject_id]
  38. window_position = self.grid_manager.get_window_position(position[0], position[1])
  39. move_action = MoveTo(window_position, self.move_duration)
  40. fake_rotate = RotateTo(randint(0, 360), self.fake_move_rotate_duration)
  41. cell.do(move_action)
  42. cell.do(fake_rotate)
  43. def attack(self, attacker_id: int, attacked_id: int):
  44. attacker = self.cell_ids[attacker_id]
  45. attacker.do(ScaleBy(1.7, duration=0.25))
  46. def eaten(self, eaten_id: int):
  47. eaten = self.cell_ids[eaten_id]
  48. self.remove(eaten)
  49. # TODO: refact: On a pas nettoyer self.cell_positions par exemple
  50. del self.cell_ids[eaten_id]
  51. class GrassLayer(GridLayerMixin, BaseMainLayer):
  52. def __init__(self, game: 'Game', *args, **kwargs):
  53. super().__init__(*args, **kwargs)
  54. self.game = game
  55. self.grasses = {}
  56. def born(self, subject_id, grid_position, opacity=100):
  57. grass = Sprite('resources/grass.png')
  58. grass.rotation = randint(0, 360)
  59. grass.opacity = opacity
  60. self.grid_manager.scale_sprite(grass)
  61. self.grid_manager.position_sprite(grass, grid_position)
  62. self.grasses[subject_id] = grass
  63. self.add(grass)
  64. def set_density(self, subject_id, density):
  65. self.grasses[subject_id].opacity = density
  66. class MainLayer(GridLayerMixin, BaseMainLayer):
  67. def __init__(self, game: 'Game', terminal, *args, **kwargs):
  68. super().__init__(terminal, *args, **kwargs)
  69. self.game = game
  70. self.cells = CellsLayer(game=game, terminal=terminal)
  71. self.add(self.cells)
  72. self.grasses = GrassLayer(game=game, terminal=terminal)
  73. self.add(self.grasses)
  74. class Game(Gui):
  75. def __init__(self, *args, **kwargs):
  76. super().__init__(*args, **kwargs)
  77. self.main_layer = MainLayer(game=self, terminal=self.terminal)
  78. self.main_scene = cocos.scene.Scene(self.main_layer)
  79. # Event registering
  80. self.terminal.register_event_handler(
  81. GrassGrownUp,
  82. self.on_grass_grown_up,
  83. )
  84. self.terminal.register_event_handler(
  85. GrassSpawn,
  86. self.on_grass_spawn,
  87. )
  88. self.terminal.register_event_handler(
  89. MoveToEvent,
  90. self.on_move_to,
  91. )
  92. self.terminal.register_event_handler(
  93. EatEvent,
  94. self.on_eat,
  95. )
  96. self.terminal.register_event_handler(
  97. AttackEvent,
  98. self.on_attack,
  99. )
  100. self.terminal.register_event_handler(
  101. EatenEvent,
  102. self.on_eaten,
  103. )
  104. def get_main_scene(self):
  105. return self.main_scene
  106. def before_received(self, package: TerminalPackage):
  107. if package.subjects: # It's thirst package
  108. for subject in package.subjects:
  109. if isinstance(subject, PreyCell):
  110. self.main_layer.cells.born(subject.id, subject.position, PreyCell)
  111. if isinstance(subject, PredatorCell):
  112. self.main_layer.cells.born(subject.id, subject.position, PredatorCell)
  113. if isinstance(subject, Grass):
  114. self.main_layer.grasses.born(
  115. subject.id,
  116. subject.position,
  117. subject.density,
  118. )
  119. def on_grass_spawn(self, event: GrassSpawn):
  120. self.main_layer.grasses.born(
  121. event.subject_id,
  122. event.position,
  123. event.density,
  124. )
  125. def on_grass_grown_up(self, event: GrassGrownUp):
  126. self.main_layer.grasses.set_density(
  127. event.subject_id,
  128. event.density, # TODO: Recupe ces données depuis local plutôt que event ?
  129. )
  130. def on_move_to(self, event: MoveToEvent):
  131. self.main_layer.cells.move(
  132. event.subject_id,
  133. event.position,
  134. )
  135. def on_eat(self, event: EatEvent):
  136. self.main_layer.grasses.set_density(
  137. event.eaten_id,
  138. event.eaten_new_density,
  139. )
  140. def on_attack(self, event: AttackEvent):
  141. self.main_layer.cells.attack(
  142. event.attacker_id,
  143. event.attacked_id,
  144. )
  145. def on_eaten(self, event: EatenEvent):
  146. self.main_layer.cells.eaten(
  147. event.eaten_id,
  148. )