PageRenderTime 4ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/skeleton/anim_player.py

http://los-cocos.googlecode.com/
Python | 142 lines | 68 code | 14 blank | 60 comment | 11 complexity | c6fb5f8fa6b80a5995a3652b268428be MD5 | raw file
  1# ----------------------------------------------------------------------------
  2# cocos2d
  3# Copyright (c) 2008-2012 Daniel Moisset, Ricardo Quesada, Rayentray Tappa,
  4# Lucio Torre
  5# Copyright (c) 2009-2014  Richard Jones, Claudio Canepa
  6# All rights reserved.
  7#
  8# Redistribution and use in source and binary forms, with or without
  9# modification, are permitted provided that the following conditions are met:
 10#
 11#   * Redistributions of source code must retain the above copyright
 12#     notice, this list of conditions and the following disclaimer.
 13#   * Redistributions in binary form must reproduce the above copyright
 14#     notice, this list of conditions and the following disclaimer in
 15#     the documentation and/or other materials provided with the
 16#     distribution.
 17#   * Neither the name of cocos2d nor the names of its
 18#     contributors may be used to endorse or promote products
 19#     derived from this software without specific prior written
 20#     permission.
 21#
 22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 26# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 29# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 30# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 31# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 32# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 33# POSSIBILITY OF SUCH DAMAGE.
 34# ----------------------------------------------------------------------------
 35
 36from __future__ import division, print_function, unicode_literals
 37
 38import math
 39try:
 40    import cPickle as pickle
 41except ImportError:
 42    import pickle
 43
 44import cocos
 45from cocos.director import director
 46from cocos.sprite import Sprite
 47
 48import pyglet
 49from pyglet.gl import *
 50from pyglet.window import key
 51
 52import ui
 53import animator
 54from skeleton import Bone, Skeleton, Skin, Animation, Animate
 55
 56class Player(cocos.layer.Layer):
 57    """ Skeletal animation player example
 58
 59    we take a skeleton and a list of animations and let the player
 60    choose what animation to play and how
 61    """
 62    is_event_handler = True
 63
 64    def __init__(self, sk, skin, *anims):
 65        super(Player, self).__init__()
 66        self.skeleton = sk
 67        self.anims = [ pickle.load(open(a, "rb")) for a in anims ]
 68
 69        # we create a skin. Skins are what are rendered.
 70        # skins also are cocos nodes, so we add it to ourselves
 71        self.skin = animator.BitmapSkin(self.skeleton, skin)
 72        self.add( self.skin )
 73
 74        x, y = director.get_window_size()
 75        self.skin.position = x // 2, y // 2
 76        self.translate = False
 77        self.flipped = False
 78
 79    def on_key_press(self, k, mod):
 80        numbers = [key._1, key._2, key._3, key._4, key._5,
 81                   key._6, key._7, key._8, key._9, key._0 ]
 82        if k == key.T:
 83            # track if the user wants to translate origin to the current
 84            # skeleton position
 85            # if you run two walk left animations without translation
 86            # you will see a player move left, go to the origin and move
 87            # left again.
 88            # if you use translation, the player will just move left twice
 89            # as far
 90            self.translate = not self.translate
 91        if k == key.F:
 92            # track if the user wants to run the animation normal or flipped
 93            # if the animation is a guy walking left, when flipped it will
 94            # walk right
 95            self.flipped = not self.flipped
 96            self.skin.flip()
 97
 98        if k in numbers:
 99            # find which animation the user wants to run
100            n = numbers.index(k)
101            if n < len(self.anims):
102                # kill current animations
103                self.skin.stop()
104                anim = self.anims[n]
105                # if we want to run the animation flipped, we create
106                # the flipped version
107                if self.flipped:
108                    anim = anim.flipped()
109                # we run the animation on the skin using the Animate action.
110                # remember that Animate is a cocos action, so you can do
111                # any action stuff you want with them.
112                # you just have to say which animation you want to use
113                # and what kind of translation
114                self.skin.do( Animate( anim , recenter_x=self.translate ) )
115
116
117
118if __name__ == "__main__":
119    import sys, imp, os
120    p = os.path.abspath(os.path.normpath(
121            os.path.join(os.path.dirname(__file__).replace("\\", "/"), "../data")
122            ))
123    pyglet.resource.path.append(p)
124    pyglet.resource.reindex()
125    director.init()
126
127    def usage():
128        return "USAGE:\n"+\
129               "python animator.py skeleton animation.anim+ \n" +\
130               "   skeleton is a python file with a skeleton variable inside \n"+\
131               "       which has the skeleton you will want to animate\n"+\
132               "   animation.anim+ means a list of animation file names \n"+\
133               "       each of this files will be asigned to a number 1-0"
134
135    if len(sys.argv)<3:
136        print(usage())
137        sys.exit()
138
139    skin_data = imp.load_source("skin", sys.argv[2]).skin
140    sk_file = imp.load_source("skeleton", sys.argv[1])
141    player = Player(sk_file.skeleton, skin_data, *sys.argv[3:])
142    director.run(cocos.scene.Scene(player))