Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial work on tiled integration #117

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions modules/tiled/example/assets/maps/test_desert/desert.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="40" height="40" tilewidth="32" tileheight="32" nextobjectid="1">
<tileset firstgid="1" name="Desert" tilewidth="32" tileheight="32" spacing="1" margin="1" tilecount="48" columns="8">
<image source="tmw_desert_spacing.png" width="265" height="199"/>
<terraintypes>
<terrain name="Desert" tile="29"/>
<terrain name="Brick" tile="9"/>
<terrain name="Cobblestone" tile="33"/>
<terrain name="Dirt" tile="14"/>
</terraintypes>
<tile id="0" terrain="0,0,0,1"/>
<tile id="1" terrain="0,0,1,1"/>
<tile id="2" terrain="0,0,1,0"/>
<tile id="3" terrain="3,3,3,0"/>
<tile id="4" terrain="3,3,0,3"/>
<tile id="5" terrain="0,0,0,3"/>
<tile id="6" terrain="0,0,3,3"/>
<tile id="7" terrain="0,0,3,0"/>
<tile id="8" terrain="0,1,0,1"/>
<tile id="9" terrain="1,1,1,1"/>
<tile id="10" terrain="1,0,1,0"/>
<tile id="11" terrain="3,0,3,3"/>
<tile id="12" terrain="0,3,3,3"/>
<tile id="13" terrain="0,3,0,3"/>
<tile id="14" terrain="3,3,3,3"/>
<tile id="15" terrain="3,0,3,0"/>
<tile id="16" terrain="0,1,0,0"/>
<tile id="17" terrain="1,1,0,0"/>
<tile id="18" terrain="1,0,0,0"/>
<tile id="19" terrain="1,1,1,0"/>
<tile id="20" terrain="1,1,0,1"/>
<tile id="21" terrain="0,3,0,0"/>
<tile id="22" terrain="3,3,0,0"/>
<tile id="23" terrain="3,0,0,0"/>
<tile id="24" terrain="0,0,0,2"/>
<tile id="25" terrain="0,0,2,2"/>
<tile id="26" terrain="0,0,2,0"/>
<tile id="27" terrain="1,0,1,1"/>
<tile id="28" terrain="0,1,1,1"/>
<tile id="29" terrain="0,0,0,0"/>
<tile id="30" terrain="0,0,0,0" probability="0.01"/>
<tile id="31" terrain="0,0,0,0" probability="0.01"/>
<tile id="32" terrain="0,2,0,2"/>
<tile id="33" terrain="2,2,2,2"/>
<tile id="34" terrain="2,0,2,0"/>
<tile id="35" terrain="2,2,2,0"/>
<tile id="36" terrain="2,2,0,2"/>
<tile id="37" terrain="0,0,0,0" probability="0.01"/>
<tile id="38" terrain="0,0,0,0" probability="0.01"/>
<tile id="39" terrain="0,0,0,0" probability="0.01"/>
<tile id="40" terrain="0,2,0,0"/>
<tile id="41" terrain="2,2,0,0"/>
<tile id="42" terrain="2,0,0,0"/>
<tile id="43" terrain="2,0,2,2"/>
<tile id="44" terrain="0,2,2,2"/>
<tile id="45" terrain="0,0,0,0" probability="0"/>
<tile id="46" terrain="0,0,0,0" probability="0.01">
<animation>
<frame tileid="37" duration="1000"/>
<frame tileid="38" duration="200"/>
</animation>
</tile>
<tile id="47" terrain="0,0,0,0" probability="0.01"/>
</tileset>
<layer name="Ground" width="40" height="40">
<data encoding="base64" compression="zlib">
eJztmNkOgjAQRas8uCWuiWtEFPeV//86x9gGMmlKqXQcl4cTICnp4XagE0IhROiRNtABup7nceGq8Vsw8PqU/B5ZDYERMGbgo6MCVIEAefuYawJMCzADGkATaBFksQRWBYg9+u0NfmvgCGze6GfKL7bMk8Jvy9AvQV7Y7yye9X8h9sM1h6/z8sJ1WYaf7j3Yo/zUeZ4fzrWs/HboWvkUzc/3+mJPzEGuny2nEvxqkrpI9yITgeU4xfxFv45I+wv1rCZaluN097n4jUTaX/SAfg4Dy3G6+3zWHjWR0Pc5nDwjQh+X3ozC6yqPNt/O7P4TlzS/LTZ+D6dEjv11P1zX3Pxc8sv2F9jvxsAPZ8ktP91ac/ajzM+lN6P0+xYiBg6fAs6K27/B/xrT4Xt/+/Me7lpst88=
</data>
</layer>
</map>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions modules/tiled/example/assets/sharders/positionshader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---VERTEX SHADER---
#ifdef GL_ES
precision highp float;
#endif

/* Outputs to the fragment shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* vertex attributes */
attribute vec2 pos;
attribute vec2 uvs;

/* uniform variables */
uniform mat4 modelview_mat;
uniform mat4 projection_mat;
uniform vec4 color;
uniform float opacity;

void main (void) {
frag_color = color * vec4(1.0, 1., 1.0, opacity);
tex_coord0 = uvs;
vec4 new_pos = vec4(pos.xy, 0.0, 1.0);
gl_Position = projection_mat * modelview_mat * new_pos;

}


---FRAGMENT SHADER---
#ifdef GL_ES
precision highp float;
#endif

/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* uniform texture samplers */
uniform sampler2D texture0;

void main (void){

gl_FragColor = frag_color * texture2D(texture0, tex_coord0);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions modules/tiled/example/assets/textures/testatlas.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"testatlas-0.png": {"desert-38": [172, 410, 32, 32], "desert-39": [2, 444, 32, 32], "desert-34": [36, 478, 32, 32], "desert-35": [444, 478, 32, 32], "desert-36": [104, 410, 32, 32], "desert-37": [342, 478, 32, 32], "desert-30": [410, 478, 32, 32], "desert-31": [206, 410, 32, 32], "desert-32": [410, 444, 32, 32], "desert-33": [444, 410, 32, 32], "desert-16": [36, 410, 32, 32], "desert-17": [138, 444, 32, 32], "desert-14": [206, 444, 32, 32], "desert-15": [172, 444, 32, 32], "desert-12": [478, 444, 32, 32], "desert-13": [274, 478, 32, 32], "desert-10": [104, 444, 32, 32], "desert-11": [308, 478, 32, 32], "desert-18": [376, 410, 32, 32], "desert-19": [444, 444, 32, 32], "desert-8": [138, 410, 32, 32], "desert-9": [240, 444, 32, 32], "desert-4": [308, 444, 32, 32], "desert-5": [2, 478, 32, 32], "desert-6": [342, 444, 32, 32], "desert-7": [240, 410, 32, 32], "desert-1": [104, 478, 32, 32], "desert-2": [342, 410, 32, 32], "desert-3": [308, 410, 32, 32], "desert-29": [36, 376, 32, 32], "desert-28": [2, 410, 32, 32], "desert-23": [172, 478, 32, 32], "desert-22": [138, 478, 32, 32], "desert-21": [478, 478, 32, 32], "desert-20": [206, 478, 32, 32], "desert-27": [36, 444, 32, 32], "desert-26": [410, 410, 32, 32], "desert-25": [240, 478, 32, 32], "desert-24": [478, 410, 32, 32], "desert-48": [70, 376, 32, 32], "desert-41": [376, 478, 32, 32], "desert-40": [70, 444, 32, 32], "desert-43": [70, 478, 32, 32], "desert-42": [274, 444, 32, 32], "desert-45": [376, 444, 32, 32], "desert-44": [70, 410, 32, 32], "desert-47": [2, 376, 32, 32], "desert-46": [274, 410, 32, 32]}}
86 changes: 86 additions & 0 deletions modules/tiled/example/example.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#:kivy 1.9.0
#:import main __main__
#:import path os.path
#:import dirname os.path.dirname

#:import GameWorld kivent_core.gameworld.GameWorld
#:import Renderer kivent_core.systems.renderers.Renderer
#:import TiledGameMap kivent_tiles.systems.gamemap.TiledGameMap
#:import TileAnimator kivent_tiles.systems.animation.TiledAnimator
#:import PositionSystem2D kivent_core.systems.position_systems.PositionSystem2D


TestGame:
<TestGame>:
gameworld: gameworld
app: app
GameWorld:
id: gameworld
gamescreenmanager: gamescreenmanager
size_of_gameworld: 20 * 1024
zones: {'general': 20 * 1024}
PositionSystem2D:
system_id: 'position'
gameworld: gameworld
zones: ['general']
Renderer:
id: map_renderer
shader_source: path.join(dirname(path.abspath(main.__file__)), 'assets/sharders/positionshader.glsl')
system_id: 'map_renderer'
gameworld: gameworld
gameview: 'camera'
zones: ['general']
system_names: ['map_renderer', 'position']
frame_count: 1
force_update: True
static_rendering: True
GameView:
id: gameview
currentmap: tilemap
system_id: 'camera'
gameworld: gameworld
size: root.size
pos: root.pos
do_touch_zoom: True
updateable: True
TiledAnimator:
id: map_animator
system_id: 'map_animator'
renderer_obj: map_renderer
gameworld: gameworld
updateable: True
TiledGameMap:
id: tilemap
system_id: 'tilemap'
map_source: path.join(dirname(path.abspath(main.__file__)), 'assets/maps/test_desert/desert.tmx')
atlas_source: path.join(dirname(path.abspath(main.__file__)), 'assets/textures/testatlas.atlas')
gameworld: gameworld
gameview: 'camera'
renderer: 'map_renderer'
animator_obj: map_animator
GameScreenManager:
id: gamescreenmanager
size: root.size
pos: root.pos
gameworld: gameworld

<GameScreenManager>:
MainScreen:
id: main_screen

<MainScreen@GameScreen>:
name: 'main'
FloatLayout:
DebugPanel:
size_hint: (0.2, 0.1)
pos_hint: {'x': 0.225, 'y': 0.025}

<DebugPanel>:
Label:
pos: root.pos
size: root.size
font_size: root.size[1] * 0.5
halign: 'center'
valign: 'middle'
color: (1, 1, 1, 1)
text: 'FPS: ' + root.fps if root.fps != None else 'FPS:'
55 changes: 55 additions & 0 deletions modules/tiled/example/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.properties import StringProperty

import kivent_core


class TestGame(Widget):
def __init__(self, **kwargs):
super(TestGame, self).__init__(**kwargs)
self.gameworld.init_gameworld(
['position', 'map_renderer', 'camera', 'map_animator', 'tilemap'],
callback=self.init_game)

def init_game(self):
self.setup_states()
self.set_state()
self.ids.tilemap.init()

def update(self, dt):
self.gameworld.update(dt)

def setup_states(self):
self.gameworld.add_state(
state_name='main',
systems_added=['map_renderer', 'map_animator'],
systems_removed=[],
systems_paused=[],
systems_unpaused=['map_renderer', 'map_animator'],
screenmanager_screen='main')

def set_state(self):
self.gameworld.state = 'main'


class DebugPanel(Widget):
fps = StringProperty(None)

def __init__(self, **kwargs):
super(DebugPanel, self).__init__(**kwargs)
Clock.schedule_once(self.update_fps)

def update_fps(self, dt):
self.fps = str(int(Clock.get_fps()))
Clock.schedule_once(self.update_fps, .05)


class ExampleApp(App):
def build(self):
pass


if __name__ == '__main__':
ExampleApp().run()
Empty file.
Empty file.
48 changes: 48 additions & 0 deletions modules/tiled/kivent_tiled/systems/animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from fractions import gcd
from itertools import ifilter

from kivy.factory import Factory
from kivent_core.systems.gamesystem import GameSystem
from kivy.properties import ListProperty, ObjectProperty, BooleanProperty


class AnimationSystem(GameSystem):
dirty = BooleanProperty(False)
renderer_obj = ObjectProperty(None)

def update(self, dt):
system_id = self.system_id
renderer = self.renderer_obj
renderer_id = renderer.system_id
entities = self.gameworld.entities
for component in ifilter(None, self.components):
entity = entities[component.entity_id]
animation_comp = getattr(entity, system_id)
frame = animation_comp.current_frame
animation_comp.timer += dt
if animation_comp.timer >= frame.duration_seconds:
animation_comp.timer = 0
animation_comp.current_frame = frame = next(animation_comp.frames)
render_comp = getattr(entity, renderer_id)
render_comp.texture_key = frame.image
if not self.dirty:
self.dirty = True

if self.dirty:
renderer.update_trigger()
self.dirty = False


class TiledAnimationSystem(AnimationSystem):
durations = ListProperty()

@property
def has_animations(self):
return bool(self.durations)

def set_update_time(self):
self.update_time = float(reduce(gcd, self.durations)) / 1000


Factory.register('AnimationSystem', cls=AnimationSystem)
Factory.register('TiledAnimationSystem', cls=TiledAnimationSystem)
Loading