gui.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  7. from sandbox.engulf.subject import Cell, Grass
  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):
  25. cell = Sprite('resources/cell.png')
  26. cell.rotation = randint(0, 360)
  27. self.grid_manager.scale_sprite(cell)
  28. self.grid_manager.position_sprite(cell, grid_position)
  29. self.cell_positions[grid_position] = cell
  30. self.cell_ids[subject_id] = cell
  31. cell.do(Repeat(cell_scale + Reverse(cell_scale)))
  32. self.add(cell)
  33. def move(self, subject_id: int, position: tuple):
  34. cell = self.cell_ids[subject_id]
  35. window_position = self.grid_manager.get_window_position(position[0], position[1])
  36. move_action = MoveTo(window_position, self.move_duration)
  37. fake_rotate = RotateTo(randint(0, 360), self.fake_move_rotate_duration)
  38. cell.do(move_action)
  39. cell.do(fake_rotate)
  40. class GrassLayer(GridLayerMixin, BaseMainLayer):
  41. def __init__(self, game: 'Game', *args, **kwargs):
  42. super().__init__(*args, **kwargs)
  43. self.game = game
  44. self.grasses = {}
  45. def born(self, subject_id, grid_position, opacity=100):
  46. grass = Sprite('resources/grass.png')
  47. grass.rotation = randint(0, 360)
  48. grass.opacity = opacity
  49. self.grid_manager.scale_sprite(grass)
  50. self.grid_manager.position_sprite(grass, grid_position)
  51. self.grasses[subject_id] = grass
  52. self.add(grass)
  53. def set_density(self, subject_id, density):
  54. self.grasses[subject_id].opacity = density
  55. class MainLayer(GridLayerMixin, BaseMainLayer):
  56. def __init__(self, game: 'Game', terminal, *args, **kwargs):
  57. super().__init__(terminal, *args, **kwargs)
  58. self.game = game
  59. self.cells = CellsLayer(game=game, terminal=terminal)
  60. self.add(self.cells)
  61. self.grasses = GrassLayer(game=game, terminal=terminal)
  62. self.add(self.grasses)
  63. class Game(Gui):
  64. def __init__(self, *args, **kwargs):
  65. super().__init__(*args, **kwargs)
  66. self.main_layer = MainLayer(game=self, terminal=self.terminal)
  67. self.main_scene = cocos.scene.Scene(self.main_layer)
  68. # Event registering
  69. self.terminal.register_event_handler(
  70. GrassGrownUp,
  71. self.on_grass_grown_up,
  72. )
  73. self.terminal.register_event_handler(
  74. GrassSpawn,
  75. self.on_grass_spawn,
  76. )
  77. self.terminal.register_event_handler(
  78. MoveToEvent,
  79. self.on_move_to,
  80. )
  81. self.terminal.register_event_handler(
  82. EatEvent,
  83. self.on_eat,
  84. )
  85. def get_main_scene(self):
  86. return self.main_scene
  87. def before_received(self, package: TerminalPackage):
  88. if package.subjects: # It's thirst package
  89. for subject in package.subjects:
  90. if isinstance(subject, Cell):
  91. self.main_layer.cells.born(subject.id, subject.position)
  92. if isinstance(subject, Grass):
  93. self.main_layer.grasses.born(
  94. subject.id,
  95. subject.position,
  96. subject.density,
  97. )
  98. def on_grass_spawn(self, event: GrassSpawn):
  99. self.main_layer.grasses.born(
  100. event.subject_id,
  101. event.position,
  102. event.density,
  103. )
  104. def on_grass_grown_up(self, event: GrassGrownUp):
  105. self.main_layer.grasses.set_density(
  106. event.subject_id,
  107. event.density, # TODO: Recupe ces données depuis local plutôt que event ?
  108. )
  109. def on_move_to(self, event: MoveToEvent):
  110. self.main_layer.cells.move(
  111. event.subject_id,
  112. event.position,
  113. )
  114. def on_eat(self, event: EatEvent):
  115. self.main_layer.grasses.set_density(
  116. event.eaten_id,
  117. event.eaten_new_density,
  118. )