Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. import tmx
  4. from cymunk.cymunk import Vec2d
  5. from kivent_maps.map_utils import _load_obj_models, _load_tile_map, _load_tile_properties, parse_tmx
  6. from kivent_maps.map_utils import _load_tilesets
  7. from kivy.app import App, platform
  8. from kivy.core.window import Window, Keyboard
  9. from kivy.clock import Clock
  10. from kivy.uix.widget import Widget
  11. from kivy.properties import StringProperty
  12. from kivent_core.managers.resource_managers import texture_manager
  13. from os.path import dirname, join, abspath, basename
  14. from kivent_maps import map_utils
  15. import kivent_cymunk
  16. from kivent_maps.map_system import MapSystem
  17.  
  18. Window.size = (1600, 900)
  19.  
  20.  
  21. def get_asset_path(asset, asset_loc):
  22. return join(dirname(dirname(abspath(__file__))), asset_loc, asset)
  23.  
  24.  
  25.  
  26. keys = defaultdict(lambda: False)
  27. texture_manager.load_atlas(join(dirname(dirname(abspath(__file__))), 'assets',
  28. 'background_objects.atlas'))
  29.  
  30.  
  31. class Game(Widget):
  32. def __init__(self, **kwargs):
  33. super(Game, self).__init__(**kwargs)
  34. map_render_args = {'zones': ['general'], 'frame_count': 2, 'gameview': 'camera1',
  35. 'shader_source': get_asset_path('positionshader.glsl', 'assets/glsl')}
  36. map_anim_args = {'zones': ['general'], }
  37. map_poly_args = {'zones': ['general'], 'frame_count': 2, 'gameview': 'camera1',
  38. 'shader_source': 'poscolorshader.glsl'}
  39. self.map_layers, self.map_layer_animators = map_utils.load_map_systems(
  40. 4, self.gameworld, map_render_args, map_anim_args, map_poly_args)
  41. self.moving = False
  42. self.gameworld.init_gameworld(
  43. ['cymunk_physics', 'position', 'color', 'camera1', 'tile_map']
  44. + self.map_layers + self.map_layer_animators, callback=self.init_game)
  45.  
  46. def init_game(self):
  47. self.move_to = None
  48. self.setup_states()
  49. self.set_state()
  50. self.setup_tile_map()
  51. self.add_player()
  52. self.camera1.render_system_order = reversed(self.map_layers)
  53. self.camera1.entity_to_focus = self.player_id
  54. self.camera1.focus_entity = True
  55. Clock.schedule_interval(self.update, 1 / 60)
  56.  
  57. def add_player(self):
  58. pos = self.current_map.get_tile_position(*self.player_tile)
  59. print('starting position', pos)
  60. renderer_name = 'map_layer%d' % self.current_map.z_index_map[1]
  61. shape_dict = {'inner_radius': 0, 'outer_radius': 0, 'mass': 0, 'offset': (0, 0)}
  62. col_shape = {'shape_type': 'circle', 'elasticity': .5, 'collision_type': 1, 'shape_info': shape_dict,
  63. 'friction': 1.0}
  64. col_shapes = [col_shape]
  65. physics_component = {'main_shape': 'circle',
  66. 'velocity': (0, 0), 'position': pos, 'angle': 0,
  67. 'angular_velocity': 0, 'vel_limit': 0,
  68. 'ang_vel_limit': 0, 'mass': 0, 'col_shapes': col_shapes}
  69. component_data = {'position': pos,
  70. renderer_name: {'model': 'star2', 'texture': 'star2', 'size': [20, 20]},
  71. 'rotate': 0,
  72. 'cymunk_physics': physics_component}
  73. systems = ['position', renderer_name, 'rotate', 'cymunk_physics']
  74. self.player_id = self.gameworld.init_entity(component_data, systems)
  75. self.player = self.gameworld.entities[self.player_id]
  76.  
  77. def setup_tile_map(self):
  78. filename = get_asset_path('orthogonal.tmx', 'assets/maps/')
  79. map_manager = self.gameworld.managers['map_manager']
  80. map_name = parse_tmx(filename, self.gameworld)
  81. map_utils.init_entities_from_map(map_manager.maps[map_name], self.gameworld.init_entity)
  82. self.current_map = map_manager.maps[map_name]
  83. # print(self.current_map.width)
  84. self.player_tile = (0, 0)
  85. print(self.current_map.size)
  86.  
  87. def on_touch_up(self, touch):
  88. print(self.camera1.convert_from_screen_to_world(touch.pos))
  89.  
  90. def setup_states(self):
  91. self.gameworld.add_state(state_name='main', systems_added=self.map_layers,
  92. systems_unpaused=self.map_layer_animators + self.map_layers)
  93.  
  94. def set_state(self):
  95. self.gameworld.state = 'main'
  96.  
  97. def update(self, dt):
  98. direction = None
  99. if keys.get(Keyboard.keycodes['right']):
  100. direction = (1, 0)
  101. keys[Keyboard.keycodes['right']] = False
  102. elif keys.get(Keyboard.keycodes['down']):
  103. direction = (0, 1)
  104. keys[Keyboard.keycodes['down']] = False
  105. elif keys.get(Keyboard.keycodes['left']):
  106. direction = (-1, 0)
  107. keys[Keyboard.keycodes['left']] = False
  108. elif keys.get(Keyboard.keycodes['up']):
  109. direction = (0, -1)
  110. keys[Keyboard.keycodes['up']] = False
  111. if direction:
  112. x = direction[0] + self.player_tile[0]
  113. y = direction[1] + self.player_tile[1]
  114. go = True
  115. go = False if (x < 0 or y < 0) else go
  116. go = False if (x > self.current_map.size[0]-1 or y > self.current_map.size[1]-1) else go
  117. go = False if (x == self.player_tile[0] and y == self.player_tile[1]) else go
  118. if go:
  119. print('moving from', self.player_tile)
  120. entity = self.gameworld.entities[self.player_id]
  121. coords = self.current_map.get_tile_position(x, y)
  122. self.player_tile = (x, y)
  123. print('moving to', coords, self.player_tile)
  124. entity.cymunk_physics.body.position = Vec2d(coords)
  125. else:
  126. print('too far or same tile', x, y)
  127. self.gameworld.update(dt)
  128.  
  129.  
  130. class DebugPanel(Widget):
  131. fps = StringProperty(None)
  132.  
  133. def __init__(self, **kwargs):
  134. super(DebugPanel, self).__init__(**kwargs)
  135. Clock.schedule_once(self.update_fps)
  136.  
  137. def update_fps(self, dt):
  138. self.fps = str(int(Clock.get_fps()))
  139. Clock.schedule_once(self.update_fps, .1)
  140.  
  141.  
  142. class YourAppNameApp(App):
  143. pass
  144.  
  145.  
  146. if __name__ == '__main__':
  147. if platform != 'android':
  148. def on_key_down(window, keycode, *rest):
  149. keys[keycode] = True
  150.  
  151. def on_key_up(window, keycode, *rest):
  152. keys[keycode] = False
  153.  
  154. Window.bind(on_key_down=on_key_down, on_key_up=on_key_up)
  155.  
  156. YourAppNameApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement