diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-05-02 04:31:23 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-05-02 04:31:23 (GMT) |
commit | 6d9cbdc157e0b7d0a0fdf807ec8dfb2d510f2edb (patch) | |
tree | 68d50bd790e210614e222c234811edf7ce997623 /character.py | |
parent | 86a0f2454fa3a9b255cabdcd3f7ab362746925d0 (diff) |
Start movement of character in play mode
Now the character does not walk all time left and right,
but wait to a click, and go to the place,
if the click is over a lateral wall or over a door, the character walk and later
the map change to the new selected vision.
Is not working all right now. When the character go to the left
the svg is not rendered. A posible solution using to ImageSurfaces
to cache te rendering is commented in character.py
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
Diffstat (limited to 'character.py')
-rw-r--r-- | character.py | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/character.py b/character.py index cf3c2ce..a4daa5c 100644 --- a/character.py +++ b/character.py @@ -1,3 +1,5 @@ +import logging + import gtk from gtk import gdk import gobject @@ -10,7 +12,7 @@ class Sprite(object): self.svg_file = svg_file self.cel_width, self.cel_height = cel_width, cel_height self.current_animation = None - self.flip_x = None + self.direction = 1 self._animation_data = animation_data self._current_data = None @@ -20,13 +22,30 @@ class Sprite(object): def load_svg(self): self._svg = rsvg.Handle(file=self.svg_file) - - def change_animation(self, animation_name, flip_x=1): + self._svg_width = self._svg.props.width + """ + # create a cache with the image rendered + self.cache = cairo.ImageSurface(cairo.FORMAT_ARGB32, + self._svg.props.width, self._svg.props.height) + self.cache_context = cairo.Context(self.cache) + self._svg.render_cairo(self.cache_context) + self.cache.flush() + + # and another with a flipped render + self.cache_inv = cairo.ImageSurface(cairo.FORMAT_ARGB32, + self._svg.props.width, self._svg.props.height) + self.cache_inv_context = cairo.Context(self.cache_inv) + self.cache_inv_context.scale(-1, 1) + self._svg.render_cairo(self.cache_inv_context) + self.cache_inv.flush() + """ + + def change_animation(self, animation_name, direction=1): self.current_animation = animation_name self._current_data = self._animation_data[animation_name] self._animation_index = 0 - self.flip_x = flip_x - if flip_x == -1: + #self.direction = direction + if self.direction == -1: self._current_data = list(reversed(self._current_data)) def next_frame(self): @@ -37,18 +56,22 @@ class Sprite(object): def draw(self, context, dx, dy): cel_x, cel_y = self._current_data[self._animation_index] - context.scale(self.flip_x, 1) - + context.scale(self.direction, 1) + #if self.direction == 1: + # context.set_source_surface(self.cache) + #else: + # context.set_source_surface(self.cache_inv) context.translate(dx, dy) - context.translate(-cel_x * self.cel_width * self.flip_x, + context.translate(-cel_x * self.cel_width * self.direction, -cel_y * self.cel_height) - context.rectangle(cel_x * self.cel_width * self.flip_x, + context.rectangle(cel_x * self.cel_width * self.direction, cel_y * self.cel_height, self.cel_width, self.cel_height) - if self.flip_x == -1: - context.translate(-900, 0) + if self.direction == -1: + context.translate(-self._svg_width, 0) context.clip() + #context.paint() self._svg.render_cairo(context) @@ -67,11 +90,12 @@ class Character(object): self.sprite.change_animation('walk') self.pos = [0, 0] - self.direction = -1 # -1 for left, 1 for right + self.direction = 1 # -1 for left, 1 for right def update(self): + self.sprite.direction = self.direction self.sprite.next_frame() - + """ if self.direction == 1 and self.pos[0] > 600: self.pos[0] += self.sprite.cel_width self.direction = -1 @@ -81,18 +105,19 @@ class Character(object): self.direction = 1 self.sprite.change_animation('walk') else: - self.pos[0] += self.speed * self.direction + """ + self.pos[0] += self.speed * self.direction return (self.pos[0], self.pos[1] - self.sprite.cel_height + 10, self.sprite.cel_width, self.sprite.cel_height) def draw(self, context): # draw char - dx, dy = self.pos[0], \ - self.pos[1] - self.sprite.cel_height + 10 + dx, dy = self.pos[0], self.pos[1] - self.sprite.cel_height + 10 # for debug write a rectangle around - context.rectangle(dx, dy, self.sprite.cel_width, self.sprite.cel_height) + context.rectangle(dx, dy, self.sprite.cel_width, + self.sprite.cel_height) stroke = (0, 0, 0) context.set_source_rgb(*stroke) context.stroke() |