Permalink
Browse files

Let's get this party started. INITIAL COMMIT

You can walk around as a cagroo.  Exciting!
  • Loading branch information...
0 parents commit 66b6e6291a0a04b8b5e9bd12c7adee89fb87f782 @eevee committed May 28, 2012
@@ -0,0 +1,13 @@
+.*.swp
+*.pyc
+*.pyo
+__pycache__
+
+# DE cruft, just in case
+.directory
+desktop.ini
+.DS_Store
+Thumbs.db
+
+# Just in case; rarely do we want to include psds
+*.psd
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
No changes.
@@ -0,0 +1,202 @@
+# encoding: utf8
+from __future__ import division
+
+import cocos
+import pyglet
+
+TIC = 1 / 24
+
+from flora.spriteset import PosableSprite, UP, DOWN, LEFT, RIGHT
+class Actor(cocos.sprite.Sprite):
+ # TODO this is already aching to be componentized, oh no
+
+ def __init__(self, spriteset, *args, **kwargs):
+ self.spriteset = spriteset
+ kwargs.setdefault('anchor', (0, 0))
+ super(Actor, self).__init__(spriteset._pick_image(), *args, **kwargs)
+
+ self.current_frame = 0
+ self.frames = []#imagelist
+
+ self.set_state()
+
+ walk_action = None
+
+ def walk(self, dx, dy):
+ self.set_state(pose='walking')
+
+ step_size = 64
+
+ if self.walk_action:
+ self.remove_action(self.walk_action)
+
+ action = cocos.actions.MoveBy((dx * step_size, dy * step_size), 1)
+ action = cocos.actions.Repeat(action)
+ self.walk_action = self.do(action)
+
+ def stop_walking(self):
+ if self.walk_action:
+ self.remove_action(self.walk_action)
+ self.walk_action = None
+ self.set_state(pose='standing')
+
+
+ def set_state(self, pose=None, angle=None):
+ if pose:
+ self.spriteset.pose = pose
+ if angle:
+ self.spriteset.angle = angle
+
+ self.image = self.spriteset._pick_image()
+ self.image_anchor = self.spriteset._pick_anchor()
+
+
+ def dont_draw(self):
+ super(Actor, self).draw()
+
+ from pyglet import gl
+
+ # TODO this is all almost certainly not the nicest way to do any of
+ # this.
+
+ gl.glColor3f(1, 1, 0)
+
+ # NOTE: a vertex list has a .draw() but it does its own thang that
+ # includes setting the color
+ v = list(self._vertex_list.vertices)
+ gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
+ gl.glLineStipple(1, 0x5555)
+ gl.glEnable(gl.GL_LINE_STIPPLE)
+ gl.glBegin(gl.GL_QUADS)
+ gl.glVertex2f(v[0], v[1])
+ gl.glVertex2f(v[2], v[3])
+ gl.glVertex2f(v[4], v[5])
+ gl.glVertex2f(v[6], v[7])
+ gl.glEnd()
+ gl.glDisable(gl.GL_LINE_STIPPLE)
+ gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
+
+ # Draw a dot at the anchor point
+ gl.glPushMatrix()
+ self.transform()
+ n = 2 / self.scale
+ gl.glBegin(gl.GL_QUADS)
+ gl.glVertex2f(n, n)
+ gl.glVertex2f(n, -n)
+ gl.glVertex2f(-n, -n)
+ gl.glVertex2f(-n, n)
+ gl.glEnd()
+ gl.glPopMatrix()
+
+ gl.glColor3f(1, 1, 1)
+
+
+class Background(cocos.layer.Layer):
+ def __init__(self):
+ super(Background, self).__init__()
+
+ self.add(cocos.layer.ColorLayer(0x6b, 0x87, 0x48, 0xff))
+
+
+class Midground(cocos.layer.Layer):
+ is_event_handler = True
+
+ def __init__(self):
+ super(Midground, self).__init__()
+
+ cagroo_sprite = PosableSprite()
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/side3.png',
+ ],
+ 'standing',
+ LEFT,
+ anchor=(385, 530 - 456))
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/front3.png',
+ ],
+ 'standing',
+ DOWN,
+ anchor=(116, 581 - 528))
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/back3.png',
+ ],
+ 'standing',
+ UP,
+ anchor=(141, 743 - 546))
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/side4.png',
+ 'assets/characters/cagroo/side5.png',
+ 'assets/characters/cagroo/side6.png',
+ 'assets/characters/cagroo/side2.png',
+ 'assets/characters/cagroo/side1.png',
+ 'assets/characters/cagroo/side3.png',
+ ],
+ 'walking',
+ LEFT,
+ anchor=(385, 530 - 456))
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/front4.png',
+ 'assets/characters/cagroo/front5.png',
+ 'assets/characters/cagroo/front6.png',
+ 'assets/characters/cagroo/front2.png',
+ 'assets/characters/cagroo/front1.png',
+ 'assets/characters/cagroo/front3.png',
+ ],
+ 'walking',
+ DOWN,
+ anchor=(116, 581 - 528))
+ cagroo_sprite.add_pose([
+ 'assets/characters/cagroo/back4.png',
+ 'assets/characters/cagroo/back5.png',
+ 'assets/characters/cagroo/back6.png',
+ 'assets/characters/cagroo/back2.png',
+ 'assets/characters/cagroo/back1.png',
+ 'assets/characters/cagroo/back3.png',
+ ],
+ 'walking',
+ UP,
+ anchor=(141, 743 - 546))
+
+ cagroo_sprite.flip_pose('standing', LEFT, RIGHT)
+ cagroo_sprite.flip_pose('walking', LEFT, RIGHT)
+
+ cagroo_sprite.pose = 'standing'
+ cagroo_sprite.angle = UP
+ self.cagroo = Actor(cagroo_sprite, position=(400, 300), scale=0.25)
+ self.add(self.cagroo)
+
+ current_direction = None
+
+ def on_key_press(self, key, mod):
+ if key in (pyglet.window.key.RIGHT, pyglet.window.key.LEFT, pyglet.window.key.UP, pyglet.window.key.DOWN):
+ self.current_direction = key
+
+ if key == pyglet.window.key.RIGHT:
+ self.cagroo.spriteset.angle = RIGHT
+ self.cagroo.walk(1, 0)
+ elif key == pyglet.window.key.LEFT:
+ self.cagroo.spriteset.angle = LEFT
+ self.cagroo.walk(-1, 0)
+ elif key == pyglet.window.key.DOWN:
+ self.cagroo.spriteset.angle = DOWN
+ self.cagroo.walk(0, -1)
+ elif key == pyglet.window.key.UP:
+ self.cagroo.spriteset.angle = UP
+ self.cagroo.walk(0, 1)
+
+ def on_key_release(self, key, mod):
+ if key == self.current_direction:
+ self.cagroo.stop_walking()
+
+
+def main():
+ dx = cocos.director.director
+
+ dx.init(width=800, height=600, caption=u'❀ flora ✿')
+
+ scene = cocos.scene.Scene(Background(), Midground())
+
+ dx.run(scene)
+
+if __name__ == '__main__':
+ main()
@@ -0,0 +1,87 @@
+from __future__ import absolute_import
+
+import cocos
+import pyglet.image
+
+
+UP = object()
+DOWN = object()
+LEFT = object()
+RIGHT = object()
+ALL = object()
+
+
+
+class PosableSprite(object):
+ """I represent a single sprite with an arbitrary number of poses at
+ multiple angles. For example, an actor may have both running and walking
+ animations. "Running" and "walking" are two different poses, and both are
+ depicted differently depending on the angle the actor is facing.
+ """
+
+ _sprite = None
+
+ def __init__(self, *args, **kwargs):
+ self._views = {}
+ self._txbin = pyglet.image.atlas.TextureBin(texture_width=1024, texture_height=1024)
+ self._current_pose = None
+ self._current_angle = DOWN
+
+ def add_pose(self, filenames, view_name, angle, anchor=None):
+ # TODO support angle=ALL, and make it default?
+ # TODO support reflecting left/right
+ view = self._views.setdefault(view_name, {})
+ if angle in view:
+ raise ValueError
+
+ animation = pyglet.image.Animation.from_image_sequence(
+ sequence=(pyglet.image.load(fn) for fn in filenames),
+ period=0.1,
+ )
+ animation.add_to_texture_bin(self._txbin)
+
+ if not anchor:
+ anchor = animation.get_max_width() / 2, animation.get_max_height() / 2
+
+ view[angle] = animation, anchor
+
+ def flip_pose(self, view_name, angle_from, angle_to):
+ # copies with a horizontal flip
+ view = self._views[view_name]
+ if angle_to in view:
+ raise ValueError
+
+ animation, anchor = view[angle_from]
+ new_animation = animation.get_transform(flip_x=True)
+ new_anchor = new_animation.get_max_width() - anchor[0], anchor[1]
+ view[angle_to] = new_animation, new_anchor
+
+
+ @property
+ def pose(self):
+ return self._current_pose
+
+ @pose.setter
+ def pose(self, value):
+ if value not in self._views:
+ raise KeyError
+
+ self._current_pose = value
+
+ @property
+ def angle(self):
+ return self._current_angle
+
+ @angle.setter
+ def angle(self, value):
+ if value not in (UP, DOWN, LEFT, RIGHT):
+ raise KeyError
+
+ self._current_angle = value
+
+
+ def _pick_image(self):
+ return self._views[self._current_pose][self._current_angle][0]
+
+ def _pick_anchor(self):
+ return self._views[self._current_pose][self._current_angle][1]

0 comments on commit 66b6e62

Please sign in to comment.