gui.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import cocos
  2. from cocos.actions import ScaleBy, Repeat, Reverse, RotateBy
  3. from cocos.director import director
  4. from cocos.layer import ScrollableLayer, Layer
  5. from cocos.sprite import Sprite
  6. from pyglet.window import key as wkey
  7. from random import randint
  8. from sandbox.life_game.simulation import CellBornEvent
  9. from sandbox.life_game.simulation import CellDieEvent, Cell, InvertCellStateBehaviour, \
  10. EmptyPositionWithLotOfCellAroundEvent
  11. from synergine2.terminals import Terminal
  12. from synergine2.terminals import TerminalPackage
  13. from synergine2_cocos2d.gui import Gui, GridLayerMixin, MainLayer as BaseMainLayer
  14. cell_scale = ScaleBy(1.1, duration=0.25)
  15. cell_rotate = RotateBy(360, duration=30)
  16. flash_flash = ScaleBy(8, duration=0.5)
  17. flash_rotate = RotateBy(360, duration=6)
  18. class Cells(GridLayerMixin, Layer):
  19. def __init__(self):
  20. super().__init__()
  21. self.cells = {}
  22. self.flashs = []
  23. def born(self, grid_position):
  24. if grid_position in self.cells:
  25. return # cell can be added by gui
  26. cell = Sprite('resources/cells_l.png')
  27. cell.rotation = randint(0, 360)
  28. self.grid_manager.scale_sprite(cell)
  29. self.grid_manager.position_sprite(cell, grid_position)
  30. cell.do(Repeat(cell_scale + Reverse(cell_scale)))
  31. cell.do(Repeat(cell_rotate + Reverse(cell_rotate)))
  32. self.cells[grid_position] = cell
  33. self.add(cell)
  34. def die(self, grid_position):
  35. try:
  36. self.remove(self.cells[grid_position])
  37. del self.cells[grid_position]
  38. except KeyError:
  39. pass # Cell can be removed by gui
  40. def flash(self, position):
  41. flash = Sprite('resources/flash.png')
  42. flash.opacity = 40
  43. flash.scale = 0.1
  44. flash.rotation = randint(0, 360)
  45. flash.do(flash_flash + Reverse(flash_flash))
  46. flash.do(Repeat(flash_rotate + Reverse(flash_rotate)))
  47. self.grid_manager.position_sprite(flash, position)
  48. self.flashs.append(flash)
  49. self.add(flash)
  50. class MainLayer(GridLayerMixin, BaseMainLayer):
  51. is_event_handler = True
  52. def __init__(self, *args, **kwargs):
  53. super().__init__(*args, **kwargs)
  54. self.background = Sprite('resources/banner-1711735_640.jpg')
  55. self.background.position = 0, 0
  56. self.background.opacity = 70
  57. self.background.scale = 5
  58. self.add(self.background, z=1)
  59. self.cells = Cells()
  60. self.add(self.cells)
  61. self.cross = Sprite('resources/cross31x31.png')
  62. self.cross.position = 0, 0
  63. self.cross.opacity = 50
  64. self.add(self.cross)
  65. # Set scene center on center of screen
  66. window_size = director.get_window_size()
  67. self.position = window_size[0] // 2, window_size[1] // 2
  68. def on_mouse_press(self, x, y, buttons, modifiers):
  69. x, y = director.get_virtual_coordinates(x, y)
  70. grid_position = self.grid_manager.get_grid_position(x, y)
  71. self.terminal.send(TerminalPackage(
  72. simulation_actions=[(InvertCellStateBehaviour, {'position': grid_position})],
  73. ))
  74. def on_mouse_motion(self, x, y, dx, dy):
  75. x, y = director.get_virtual_coordinates(x, y)
  76. grid_position = self.grid_manager.get_grid_position(x, y)
  77. window_position = self.grid_manager.get_window_position(grid_position[0], grid_position[1])
  78. self.cross.position = window_position
  79. class LifeGameGui(Gui):
  80. def __init__(
  81. self,
  82. terminal: Terminal,
  83. read_queue_interval: float=1 / 60.0,
  84. ):
  85. super().__init__(terminal, read_queue_interval)
  86. self.main_layer = MainLayer(terminal=self.terminal)
  87. self.main_scene = cocos.scene.Scene(self.main_layer)
  88. self.positions = {}
  89. self.terminal.register_event_handler(CellDieEvent, self.on_cell_die)
  90. self.terminal.register_event_handler(CellBornEvent, self.on_cell_born)
  91. self.terminal.register_event_handler(
  92. EmptyPositionWithLotOfCellAroundEvent,
  93. self.on_empty_cell_with_lot_of_cell_around,
  94. )
  95. def get_main_scene(self):
  96. return self.main_scene
  97. def before_received(self, package: TerminalPackage):
  98. if package.subjects: # It's thirst package
  99. for subject in package.subjects:
  100. if isinstance(subject, Cell):
  101. self.positions[subject.id] = subject.position
  102. self.main_layer.cells.born(subject.position)
  103. for flash in self.main_layer.cells.flashs[:]:
  104. self.main_layer.cells.flashs.remove(flash)
  105. self.main_layer.cells.remove(flash)
  106. def on_cell_die(self, event: CellDieEvent):
  107. try:
  108. self.main_layer.cells.die(self.positions[event.subject_id])
  109. except KeyError:
  110. pass
  111. def on_cell_born(self, event: CellBornEvent):
  112. if event.subject_id not in self.terminal.subjects:
  113. return
  114. subject = self.terminal.subjects.get(event.subject_id)
  115. self.positions[event.subject_id] = subject.position
  116. self.main_layer.cells.born(subject.position)
  117. def on_empty_cell_with_lot_of_cell_around(self, event: EmptyPositionWithLotOfCellAroundEvent):
  118. self.main_layer.cells.flash(event.position)