gui.py 5.2KB

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