PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/skeleton/anim_player.py

http://los-cocos.googlecode.com/
Python | 142 lines | 68 code | 14 blank | 60 comment | 10 complexity | c6fb5f8fa6b80a5995a3652b268428be MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-SA-3.0, LGPL-2.0, BSD-3-Clause
  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. from __future__ import division, print_function, unicode_literals
  36. import math
  37. try:
  38. import cPickle as pickle
  39. except ImportError:
  40. import pickle
  41. import cocos
  42. from cocos.director import director
  43. from cocos.sprite import Sprite
  44. import pyglet
  45. from pyglet.gl import *
  46. from pyglet.window import key
  47. import ui
  48. import animator
  49. from skeleton import Bone, Skeleton, Skin, Animation, Animate
  50. class Player(cocos.layer.Layer):
  51. """ Skeletal animation player example
  52. we take a skeleton and a list of animations and let the player
  53. choose what animation to play and how
  54. """
  55. is_event_handler = True
  56. def __init__(self, sk, skin, *anims):
  57. super(Player, self).__init__()
  58. self.skeleton = sk
  59. self.anims = [ pickle.load(open(a, "rb")) for a in anims ]
  60. # we create a skin. Skins are what are rendered.
  61. # skins also are cocos nodes, so we add it to ourselves
  62. self.skin = animator.BitmapSkin(self.skeleton, skin)
  63. self.add( self.skin )
  64. x, y = director.get_window_size()
  65. self.skin.position = x // 2, y // 2
  66. self.translate = False
  67. self.flipped = False
  68. def on_key_press(self, k, mod):
  69. numbers = [key._1, key._2, key._3, key._4, key._5,
  70. key._6, key._7, key._8, key._9, key._0 ]
  71. if k == key.T:
  72. # track if the user wants to translate origin to the current
  73. # skeleton position
  74. # if you run two walk left animations without translation
  75. # you will see a player move left, go to the origin and move
  76. # left again.
  77. # if you use translation, the player will just move left twice
  78. # as far
  79. self.translate = not self.translate
  80. if k == key.F:
  81. # track if the user wants to run the animation normal or flipped
  82. # if the animation is a guy walking left, when flipped it will
  83. # walk right
  84. self.flipped = not self.flipped
  85. self.skin.flip()
  86. if k in numbers:
  87. # find which animation the user wants to run
  88. n = numbers.index(k)
  89. if n < len(self.anims):
  90. # kill current animations
  91. self.skin.stop()
  92. anim = self.anims[n]
  93. # if we want to run the animation flipped, we create
  94. # the flipped version
  95. if self.flipped:
  96. anim = anim.flipped()
  97. # we run the animation on the skin using the Animate action.
  98. # remember that Animate is a cocos action, so you can do
  99. # any action stuff you want with them.
  100. # you just have to say which animation you want to use
  101. # and what kind of translation
  102. self.skin.do( Animate( anim , recenter_x=self.translate ) )
  103. if __name__ == "__main__":
  104. import sys, imp, os
  105. p = os.path.abspath(os.path.normpath(
  106. os.path.join(os.path.dirname(__file__).replace("\\", "/"), "../data")
  107. ))
  108. pyglet.resource.path.append(p)
  109. pyglet.resource.reindex()
  110. director.init()
  111. def usage():
  112. return "USAGE:\n"+\
  113. "python animator.py skeleton animation.anim+ \n" +\
  114. " skeleton is a python file with a skeleton variable inside \n"+\
  115. " which has the skeleton you will want to animate\n"+\
  116. " animation.anim+ means a list of animation file names \n"+\
  117. " each of this files will be asigned to a number 1-0"
  118. if len(sys.argv)<3:
  119. print(usage())
  120. sys.exit()
  121. skin_data = imp.load_source("skin", sys.argv[2]).skin
  122. sk_file = imp.load_source("skeleton", sys.argv[1])
  123. player = Player(sk_file.skeleton, skin_data, *sys.argv[3:])
  124. director.run(cocos.scene.Scene(player))