5
from cocos.director import director
6
from cocos.sprite import Sprite
9
from pyglet.gl import *
10
from pyglet.window import key
14
from skeleton import Bone, Skeleton, Skin, Animation, Animate
16
class Player(cocos.layer.Layer):
17
""" Skeletal animation player example
19
we take a skeleton and a list of animations and let the player
20
choose what animation to play and how
22
is_event_handler = True
24
def __init__(self, sk, skin, *anims):
25
super(Player, self).__init__()
27
self.anims = [ cPickle.load(open(a)) for a in anims ]
29
# we create a skin. Skins are what are rendered.
30
# skins also are cocos nodes, so we add it to ourselves
31
self.skin = animator.BitmapSkin(self.skeleton, skin)
34
x, y = director.get_window_size()
35
self.skin.position = x/2, y/2
36
self.translate = False
39
def on_key_press(self, k, mod):
40
numbers = [key._1, key._2, key._3, key._4, key._5,
41
key._6, key._7, key._8, key._9, key._0 ]
43
# track if the user wants to translate origin to the current
45
# if you run two walk left animations without translation
46
# you will see a player move left, go to the origin and move
48
# if you use translation, the player will just move left twice
50
self.translate = not self.translate
52
# track if the user wants to run the animation normal or flipped
53
# if the animation is a guy walking left, when flipped it will
55
self.flipped = not self.flipped
59
# find which animation the user wants to run
61
if n < len(self.anims):
62
# kill current animations
65
# if we want to run the animation flipped, we create
69
# we run the animation on the skin using the Animate action.
70
# remember that Animate is a cocos action, so you can do
71
# any action stuff you want with them.
72
# you just have to say which animation you want to use
73
# and what kind of translation
74
self.skin.do( Animate( anim , recenter_x=self.translate ) )
78
if __name__ == "__main__":
80
p = os.path.abspath(os.path.normpath(
81
os.path.join(os.path.dirname(__file__).replace("\\", "/"), "../data")
83
pyglet.resource.path.append(p)
84
pyglet.resource.reindex()
89
"python animator.py skeleton animation.anim+ \n" +\
90
" skeleton is a python file with a skeleton variable inside \n"+\
91
" which has the skeleton you will want to animate\n"+\
92
" animation.anim+ means a list of animation file names \n"+\
93
" each of this files will be asigned to a number 1-0"
99
skin_data = imp.load_source("skin", sys.argv[2]).skin
100
sk_file = imp.load_source("skeleton", sys.argv[1])
101
player = Player(sk_file.skeleton, skin_data, *sys.argv[3:])
102
director.run(cocos.scene.Scene(player))