1
################################################################################
2
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com> #
4
# This file is part of UltimateSmashFriends #
6
# UltimateSmashFriends is free software: you can redistribute it and/or modify #
7
# it under the terms of the GNU General Public License as published by #
8
# the Free Software Foundation, either version 3 of the License, or #
9
# (at your option) any later version. #
11
# UltimateSmashFriends is distributed in the hope that it will be useful, #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14
# GNU General Public License for more details. #
16
# You should have received a copy of the GNU General Public License #
17
# along with UltimateSmashFriends. If not, see <http://www.gnu.org/licenses/>.#
18
################################################################################
23
from animations import Frame, PreciseTimedAnimation
27
from debug_utils import LOG
28
from config import config
30
# different in python 2.4 and 2.5
31
if sys.version_info[0] == 2 and sys.version_info[1] >= 5:
32
from xml.etree import ElementTree
34
from elementtree import ElementTree
39
class Entity_skin (object):
41
An Entity_skin contains all information about a player or an item, which is
42
mainly animations frames, with their timings and vectors, and less
43
importants information as the character/item name and such details.
46
def __init__( self, dir_name="characters"+os.sep+"stick-tiny",
47
server=False, xml_from_str=None):
49
The dir_name is the relative path of the directory where the item/player
50
is defined, the class search for an xml file of the same name as the
56
if xml_from_str is not None:
57
a = ElementTree.ElementTree()
59
f = open('/tmp/erf','w')
66
config['MEDIA_DIRECTORY'],
68
dir_name.split(os.sep)[-1]
72
a = ElementTree.ElementTree(
77
attribs = a.getroot().attrib
79
self.filename = dir_name
80
self.name = attribs['name']
83
self.image = loaders.image(
85
config['MEDIA_DIRECTORY'],
91
self.weigh = attribs['weight']
92
if 'armor' in attribs:
93
self.armor = int(attribs['armor'])
96
self.action_events = {}
100
self.hardshape = pygame.Rect(
103
in attribs['hardshape'].split(' ')
107
for movement in a.findall('movement'):
113
for event in movement.findall('vector'):
114
#LOG().log('vector found')
115
x,y = [int(n) for n in event.attrib['vector'].split(',')]
119
int(event.attrib['time'])
123
self.vectors[movement.attrib['name']] = vectors
125
for event in movement.findall('event'):
128
event.attrib['action'],
131
for i in event.attrib['period'].split(',')
136
self.action_events[movement.attrib['name']] = events
138
for sound in movement.findall('sound'):
142
config['MEDIA_DIRECTORY'],
144
sound.attrib['filename']
149
self.sounds[movement.attrib['name']] = sounds
151
for frame in movement.findall('frame'):
152
image = os.path.join(
153
config['MEDIA_DIRECTORY'],
155
frame.attrib['image']
161
frame.attrib['time'],
162
('hardshape' in frame.attrib
163
and frame.attrib ['hardshape']
164
or loaders.image(image)[1]),
165
name=frame.attrib['image']
169
for agressiv in frame.findall( 'agressiv-point' ):
170
coords = agressiv.attrib[ 'coords' ].split( ',' )
171
vector = agressiv.attrib[ 'vector' ].split( ',' )
172
frames[-1].addAgressivPoint([int(i) for i in coords],
173
[int(i) for i in vector])
175
self.animations[movement.attrib['name']] = PreciseTimedAnimation(
181
self.current_animation = "static"
182
self.animation = self.animations[self.current_animation]
183
self.animation_change = 1
189
def change_animation( self, anim_name, game, params={}):
191
Change animation of the entity skin, updating hardshape and agressiv
192
points. Add associated events to game.
196
if ( anim_name in self.animations
197
and ( anim_name == "static"
198
or anim_name != self.current_animation )):
199
if 'entity' in params and params['entity'].upgraded:
200
if anim_name+'_upgraded' in self.animations:
201
self.current_animation = anim_name+'_upgraded'
203
LOG().log(self.name+' character has no upgraded \
204
version of '+anim_name+' falling back to normal version')
205
self.current_animation = anim_name
207
self.current_animation = anim_name
209
self.animation_change = 1
210
params['world'] = game
211
params['gametime'] = game.gametime
212
for event in self.action_events[anim_name]:
216
p1 = game.gametime+(event[1][0]/1000.0)
221
p2 = game.gametime+(event[1][1]/1000.0)
225
timed_event.event_names[event[0]]
232
except AttributeError:
233
LOG().log((self.name, game), 3)
236
#LOG().log(self.vectors[anim_name])
237
for vector in self.vectors[anim_name]:
238
#LOG().log('vector added')
239
params2 = params.copy() # because we want to change some of
240
# them for this time.
241
params2['vector'] = vector[0]
242
params2['anim_name'] = anim_name
244
timed_event.VectorEvent(
245
period=(None, game.gametime+vector[1]/1000.0),
250
if self.sounds[anim_name] != []:
251
random.choice(self.sounds[anim_name]).play()
253
#LOG().log( "entity_skin "+self.name+" has no "+anim_name+"\
257
def update(self, t, reversed=False):
259
Update the skin's animation if necessary.
262
if self.animation.playing == 0:
263
self.current_animation = "static"
264
self.animation_change = True
265
if self.animation_change:
266
self.animation = self.animations[self.current_animation]
267
self.animation_change = False
268
self.animation.start(t)
269
self.animation.update(t, reversed)