app.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import datetime
  2. import pyglet as pyg
  3. import pyglet.window.key as key
  4. import pyglet.window.mouse as mouse
  5. import cocos
  6. from cocos.director import director
  7. from .camera import camera
  8. from .verkolst import verkolst
  9. from . import keyboard_state, mouse_state
  10. class CocostratUI(cocos.layer.Layer):
  11. is_event_handler = True
  12. def __init__(self):
  13. from .ui.status_bar import StatusBar
  14. from .ui.province_info import ProvinceInfo
  15. import app.ui.map_overlay as overlays
  16. super().__init__()
  17. self.province_info = ProvinceInfo()
  18. self.province_info.position = (director.window.width+450, director.window.height)
  19. self.add(self.province_info)
  20. # self.add_step_button()
  21. self.status_bar = StatusBar()
  22. self.add(self.status_bar)
  23. self.hl_province = None
  24. self.hl_province_sprite = cocos.sprite.Sprite('highlight_hex.png',
  25. position=(100, 100))
  26. self.hl_province_sprite.visible = False
  27. from app.ui.overlay_layer import HexMapLayerWithOutlines
  28. self.overlay_layer = HexMapLayerWithOutlines('overlay', verkolst['overlay'])
  29. camera.add(self.overlay_layer)
  30. self.overlays = [
  31. overlays.FoodOverlay(self, self.overlay_layer),
  32. overlays.PopsOverlay(self, self.overlay_layer, 'lc'),
  33. overlays.PopsOverlay(self, self.overlay_layer, 'mc'),
  34. overlays.PopsOverlay(self, self.overlay_layer, 'uc'),
  35. overlays.TradeDistanceOverlay(self, self.overlay_layer),
  36. ]
  37. self.click_sound = pyg.media.load('assets/sound_effects/tap1.wav', streaming=False)
  38. self.knock_sound = pyg.media.load('assets/sound_effects/tap2.wav', streaming=False)
  39. self.on_turn_start()
  40. def on_turn_start(self):
  41. self.status_bar.on_turn_start()
  42. self.province_info.on_turn_start()
  43. for overlay in self.overlays:
  44. overlay.update()
  45. def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
  46. if buttons & mouse.MIDDLE:
  47. x1, y1 = camera.screen_to_world(x-dx, y-dy)
  48. x2, y2 = camera.screen_to_world(x, y)
  49. d_world = (x2-x1, y2-y1)
  50. cam = camera.cam_position
  51. camera.cam_position = (cam[0] - d_world[0], cam[1] - d_world[1])
  52. def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
  53. if abs(scroll_y) == 1:
  54. camera.zoom(scroll_y)
  55. def on_mouse_press(self, x, y, buttons, modifiers):
  56. if buttons & mouse.LEFT:
  57. x, y = camera.screen_to_world(x, y)
  58. self.hl_province = verkolst['terrain'].get_at_pixel(x, y)
  59. if self.hl_province:
  60. self.click_sound.play()
  61. self.hl_province_sprite.position = self.hl_province.center
  62. self.hl_province_sprite.visible = True
  63. self.province_info.set_province(self.hl_province)
  64. for overlay in self.overlays:
  65. overlay.update()
  66. elif buttons & mouse.RIGHT:
  67. if self.hl_province is not None:
  68. self.knock_sound.play()
  69. self.hl_province = None
  70. self.hl_province_sprite.visible = False
  71. self.province_info.set_province(None)
  72. for overlay in self.overlays:
  73. overlay.update()
  74. def toggle_overlay(self, overlay_idx):
  75. for idx, overlay in enumerate(self.overlays):
  76. if idx == overlay_idx:
  77. overlay.toggle()
  78. else:
  79. overlay.disable()
  80. self.overlay_layer.visible = any(overlay.enabled for overlay in self.overlays)
  81. def on_key_press(self, symbol, modifiers):
  82. if symbol == key.F1:
  83. verkolst.process_turn()
  84. self.on_turn_start()
  85. elif symbol == key.F2:
  86. self.toggle_overlay(0)
  87. elif symbol == key.F3:
  88. self.toggle_overlay(1)
  89. elif symbol == key.F4:
  90. self.toggle_overlay(2)
  91. elif symbol == key.F5:
  92. self.toggle_overlay(3)
  93. elif symbol == key.F6:
  94. self.toggle_overlay(4)
  95. def main():
  96. from os.path import dirname, abspath, split, join
  97. import pyglet.image
  98. from pyglet import gl
  99. pyglet.image.Texture.default_mag_filter = gl.GL_NEAREST
  100. pyglet.image.Texture.default_min_filter = gl.GL_LINEAR
  101. # Must specify full path for Pycharm debugger to work... ugh
  102. base_dir = split(dirname(abspath(__file__)))[0]
  103. pyg.resource.path = [
  104. join(base_dir, 'assets/map/'),
  105. join(base_dir, 'assets/sprites/'),
  106. join(base_dir, 'assets/gui/'),
  107. join(base_dir, 'assets/sound_effects/')
  108. ]
  109. pyg.resource.reindex()
  110. # print(pyg.resource._default_loader._index)
  111. director.init(width=1920, height=1080, autoscale=True, resizable=True,
  112. audio_backend='pyglet')
  113. director.window.push_handlers(keyboard_state)
  114. director.window.push_handlers(mouse_state)
  115. verkolst.init('Verkolst.tmx')
  116. camera.init()
  117. camera.add(verkolst['terrain'])
  118. camera.add(verkolst['rivers'])
  119. camera.add(verkolst['buildings'])
  120. # camera.add(verkolst['nations'])
  121. ui = CocostratUI()
  122. camera.overlay_layer.add(ui.hl_province_sprite)
  123. main_scene = cocos.scene.Scene(camera, ui)
  124. director.run(main_scene)
  125. if __name__ == '__main__':
  126. main()