gui.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  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. def get_main_scene(self):
  82. return self.main_scene
  83. def before_received(self, package: TerminalPackage):
  84. if package.subjects: # It's thirst package
  85. for subject in package.subjects:
  86. if isinstance(subject, Cell):
  87. self.main_layer.cells.born(subject.id, subject.position)
  88. if isinstance(subject, Grass):
  89. self.main_layer.grasses.born(
  90. subject.id,
  91. subject.position,
  92. subject.density,
  93. )
  94. def on_grass_spawn(self, event: GrassSpawn):
  95. self.main_layer.grasses.born(
  96. event.subject_id,
  97. event.position,
  98. event.density,
  99. )
  100. def on_grass_grown_up(self, event: GrassGrownUp):
  101. self.main_layer.grasses.set_density(
  102. event.subject_id,
  103. event.density, # TODO: Recupe ces données depuis local plutôt que event ?
  104. )
  105. def on_move_to(self, event: MoveToEvent):
  106. self.main_layer.cells.move(
  107. event.subject_id,
  108. event.position,
  109. )