changeset 440:b9d2db93972f

Add a base Element class for every object in pytouhou.game.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 30 Aug 2013 14:16:08 +0200
parents 723a3e67a223
children e8dc95a2a287
files pytouhou/game/bullet.pyx pytouhou/game/effect.py pytouhou/game/element.pxd pytouhou/game/element.py pytouhou/game/enemy.py pytouhou/game/face.py pytouhou/game/item.py pytouhou/game/laser.py pytouhou/game/orb.py pytouhou/game/player.py pytouhou/game/text.py
diffstat 11 files changed, 87 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/bullet.pyx
+++ b/pytouhou/game/bullet.pyx
@@ -17,30 +17,28 @@ from math import cos, sin, atan2, pi
 from pytouhou.utils.interpolator import Interpolator
 from pytouhou.vm.anmrunner import ANMRunner
 from pytouhou.game.sprite import Sprite
+from pytouhou.game.element cimport Element
 
 
 LAUNCHING, LAUNCHED, CANCELLED = range(3)
 
-cdef class Bullet(object):
+cdef class Bullet(Element):
     cdef public unsigned int state, flags, frame, sprite_idx_offset
     cdef public double dx, dy, angle, speed #TODO
     cdef public object player_bullet, target
     cdef public object _game, _bullet_type
-    cdef public object sprite, anmrunner, removed, was_visible, objects
+    cdef public object was_visible
     cdef public object attributes, damage, hitbox, speed_interpolator, grazed
-    cdef public object x, y #TODO
 
     def __init__(self, pos, bullet_type, sprite_idx_offset,
                        angle, speed, attributes, flags, target, game,
                        player_bullet=False, damage=0, hitbox=None):
+        Element.__init__(self, pos)
+
         self._game = game
         self._bullet_type = bullet_type
         self.state = LAUNCHING
-        self.sprite = None
-        self.anmrunner = None
-        self.removed = False
         self.was_visible = True
-        self.objects = [self]
 
         if hitbox:
             self.hitbox = (hitbox[0], hitbox[1])
@@ -58,7 +56,6 @@ cdef class Bullet(object):
         self.flags = flags
         self.attributes = list(attributes)
 
-        self.x, self.y = pos
         self.angle = angle
         self.speed = speed
         self.dx, self.dy = cos(angle) * speed, sin(angle) * speed
--- a/pytouhou/game/effect.py
+++ b/pytouhou/game/effect.py
@@ -13,20 +13,18 @@
 ##
 
 
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
 from pytouhou.utils.interpolator import Interpolator
 
 
 
-class Effect(object):
+class Effect(Element):
     def __init__(self, pos, index, anm):
+        Element.__init__(self, pos)
         self.sprite = Sprite()
         self.anmrunner = ANMRunner(anm, index, self.sprite)
-        self.removed = False
-        self.objects = [self]
-
-        self.x, self.y = pos
 
 
     def update(self):
new file mode 100644
--- /dev/null
+++ b/pytouhou/game/element.pxd
@@ -0,0 +1,8 @@
+from pytouhou.game.sprite cimport Sprite
+
+cdef class Element:
+    cdef public double x, y
+    cdef public bint removed
+    cdef public Sprite sprite
+    cdef public list objects
+    cdef public object anmrunner
new file mode 100644
--- /dev/null
+++ b/pytouhou/game/element.py
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+##
+## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published
+## by the Free Software Foundation; version 3 only.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+
+class Element(object):
+    def __init__(self, pos=None):
+        self.sprite = None
+        self.anmrunner = None
+        self.removed = False
+        self.objects = [self]
+
+        if pos is not None:
+            self.x, self.y = pos
--- a/pytouhou/game/enemy.py
+++ b/pytouhou/game/enemy.py
@@ -15,6 +15,7 @@
 
 from pytouhou.utils.interpolator import Interpolator
 from pytouhou.vm.anmrunner import ANMRunner
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.game.bullet import Bullet
 from pytouhou.game.laser import Laser
@@ -23,16 +24,15 @@ from math import cos, sin, atan2, pi
 from pytouhou.game.bullet import LAUNCHED
 
 
-class Enemy(object):
+class Enemy(Element):
     def __init__(self, pos, life, _type, bonus_dropped, die_score, anms, game):
+        Element.__init__(self)
+
         self._game = game
         self._anms = anms
         self._type = _type
 
         self.process = None
-        self.sprite = None
-        self.anmrunner = None
-        self.removed = False
         self.visible = True
         self.was_visible = False
         self.bonus_dropped = bonus_dropped
--- a/pytouhou/game/face.py
+++ b/pytouhou/game/face.py
@@ -13,25 +13,25 @@
 ##
 
 
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
 
 
-class Face(object):
-    __slots__ = ('_anms', 'sprite', 'anmrunner', 'side', 'x', 'y', 'objects')
+class Face(Element):
+    __slots__ = ('_anms', 'side')
 
     def __init__(self, anms, effect, side):
+        Element.__init__(self, (-32, -16))
+
         self._anms = anms
         self.sprite = Sprite()
         self.anmrunner = ANMRunner(self._anms[0][0][0], side * 2, self.sprite)
         self.side = side
         self.load(0)
         self.animate(effect)
-        self.objects = [self]
 
         #FIXME: the same as game.effect.
-        self.x = -32
-        self.y = -16
         self.sprite.allow_dest_offset = True
 
 
--- a/pytouhou/game/item.py
+++ b/pytouhou/game/item.py
@@ -15,15 +15,17 @@
 
 from math import cos, sin, atan2, pi
 
+from pytouhou.game.element import Element
 from pytouhou.utils.interpolator import Interpolator
 
 
-class Indicator(object):
+class Indicator(Element):
     def __init__(self, item):
+        Element.__init__(self)
+
         self._item = item
 
         self.sprite = item._item_type.indicator_sprite.copy()
-        self.removed = False
 
         self.frame = 0
         self.x = self._item.x
@@ -37,16 +39,16 @@ class Indicator(object):
 
 
 
-class Item(object):
+class Item(Element):
     def __init__(self, start_pos, _type, item_type, game, angle=pi/2, player=None, end_pos=None):
+        Element.__init__(self, start_pos)
+
         self._game = game
         self._type = _type
         self._item_type = item_type
         self.sprite = item_type.sprite
-        self.removed = False
 
         self.frame = 0
-        self.x, self.y = start_pos
         self.angle = angle
         self.indicator = None
 
@@ -155,7 +157,7 @@ class Item(object):
 
     @property
     def objects(self):
-        if self.indicator:
+        if self.indicator is not None:
             return [self.indicator]
         return [self]
 
--- a/pytouhou/game/laser.py
+++ b/pytouhou/game/laser.py
@@ -14,6 +14,7 @@
 
 from math import cos, sin, pi
 
+from pytouhou.game.element import Element
 from pytouhou.vm.anmrunner import ANMRunner
 from pytouhou.game.sprite import Sprite
 
@@ -21,16 +22,15 @@ from pytouhou.game.sprite import Sprite
 STARTING, STARTED, STOPPING = range(3)
 
 
-class LaserLaunchAnim(object):
+class LaserLaunchAnim(Element):
     def __init__(self, laser, anm, index):
+        Element.__init__(self, (0, 0))
+
         self._laser = laser
         self.sprite = Sprite()
         self.sprite.anm = anm
         self.sprite.texcoords = anm.sprites[index]
         self.sprite.blendfunc = 1
-        self.removed = False
-        self.objects = [self]
-        self.x, self.y = 0, 0
 
 
     def update(self):
@@ -51,12 +51,14 @@ class LaserLaunchAnim(object):
 
 
 
-class Laser(object):
+class Laser(Element):
     def __init__(self, base_pos, laser_type, sprite_idx_offset,
                        angle, speed, start_offset, end_offset, max_length, width,
                        start_duration, duration, stop_duration,
                        grazing_delay, grazing_extra_duration,
                        game):
+        Element.__init__(self, (0, 0))
+
         self._game = game
         launch_anim = LaserLaunchAnim(self, laser_type.anm,
                                       laser_type.launch_anim_offsets[sprite_idx_offset]
@@ -64,10 +66,6 @@ class Laser(object):
         self._game.effects.append(launch_anim)
         self._laser_type = laser_type
         self.state = STARTING
-        self.sprite = None
-        self.anmrunner = None
-        self.removed = False
-        self.objects = [self]
 
         #TODO: hitbox
 
@@ -80,7 +78,6 @@ class Laser(object):
 
         self.sprite_idx_offset = sprite_idx_offset
         self.base_pos = base_pos
-        self.x, self.y = 0, 0
         self.angle = angle
         self.speed = speed
         self.start_offset = start_offset
@@ -195,15 +192,13 @@ class Laser(object):
         self.frame += 1
 
 
-class PlayerLaser(object):
+class PlayerLaser(Element):
     def __init__(self, laser_type, sprite_idx_offset, hitbox, damage,
                  angle, offset, duration, origin):
-        self.sprite = None
-        self.anmrunner = None
-        self.removed = False
+        Element.__init__(self)
+
         self._laser_type = laser_type
         self.origin = origin
-        self.objects = [self]
 
         self.hitbox = hitbox[0], hitbox[1]
 
--- a/pytouhou/game/orb.py
+++ b/pytouhou/game/orb.py
@@ -13,15 +13,17 @@
 ##
 
 
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
 
 
-class Orb(object):
-    __slots__ = ('sprite', 'anmrunner', 'offset_x', 'offset_y', 'player_state',
-                 'fire')
+class Orb(Element):
+    __slots__ = ('offset_x', 'offset_y', 'player_state', 'fire')
 
     def __init__(self, anm, index, player_state, fire_func):
+        Element.__init__(self)
+
         self.sprite = Sprite()
         self.anmrunner = ANMRunner(anm, index, self.sprite)
 
--- a/pytouhou/game/player.py
+++ b/pytouhou/game/player.py
@@ -13,6 +13,7 @@
 ##
 
 
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
 from pytouhou.game.bullettype import BulletType
@@ -52,11 +53,11 @@ class PlayerState(object):
                            self.power, self.lives, self.bombs)
 
 
-class Player(object):
+class Player(Element):
     def __init__(self, state, game, anm):
+        Element.__init__(self)
+
         self._game = game
-        self.sprite = None
-        self.anmrunner = None
         self.anm = anm
 
         self.speeds = (self.sht.horizontal_vertical_speed,
--- a/pytouhou/game/text.py
+++ b/pytouhou/game/text.py
@@ -12,25 +12,22 @@
 ## GNU General Public License for more details.
 ##
 
+from pytouhou.game.element import Element
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
 from pytouhou.utils.interpolator import Interpolator
 
 
-class Glyph(object):
+class Glyph(Element):
     def __init__(self, sprite, pos):
+        Element.__init__(self, pos)
         self.sprite = sprite
-        self.removed = False
-
-        self.x, self.y = pos
 
 
-class Widget(object):
+class Widget(Element):
     def __init__(self, pos, back_anm=None, back_script=22):
-        self.sprite = None
-        self.removed = False
+        Element.__init__(self, pos)
         self.changed = True
-        self.anmrunner = None
         self.frame = 0
 
         # Set up the backround sprite
@@ -39,8 +36,6 @@ class Widget(object):
             self.sprite = Sprite()
             self.anmrunner = ANMRunner(back_anm, back_script, self.sprite)
 
-        self.x, self.y = pos
-
     def update(self):
         self.frame += 1
         if self.changed:
@@ -65,6 +60,11 @@ class GlyphCollection(Widget):
         self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right
 
 
+    @property
+    def objects(self):
+        return [self] + self.glyphes
+
+
     def set_length(self, length):
         current_length = len(self.glyphes)
         if length > current_length:
@@ -119,11 +119,6 @@ class Text(GlyphCollection):
         self.set_text(text)
 
 
-    @property
-    def objects(self):
-        return self.glyphes + [self]
-
-
     def set_text(self, text):
         if text == self.text:
             return
@@ -200,11 +195,6 @@ class Counter(GlyphCollection):
         self.set_value(value)
 
 
-    @property
-    def objects(self):
-        return [self] + self.glyphes
-
-
     def set_value(self, value):
         if value < 0:
             value = 0
@@ -217,15 +207,13 @@ class Counter(GlyphCollection):
 
 
 
-class Gauge(object):
+class Gauge(Element):
     def __init__(self, pos, anm, max_length=280, maximum=1, value=0):
+        Element.__init__(self, pos)
         self.sprite = Sprite()
         self.anmrunner = ANMRunner(anm, 21, self.sprite)
-        self.removed = False
         self.sprite.corner_relative_placement = True #TODO: perhaps not right
-        self.objects = [self]
 
-        self.x, self.y = pos
         self.max_length = max_length
         self.maximum = maximum