Changeset 385:51deb78cae52 for nagslang


Ignore:
Timestamp:
Sep 6, 2013, 9:58:15 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
5077a64a868295383cfa281f1f69c605a3d6437c
Message:

Use a result object to get new drawables back to the area

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/collectable.py

    r378 r385  
    44from nagslang import render
    55from nagslang.constants import ZORDER_LOW
    6 from nagslang.game_object import GameObject, SingleShapePhysicser, make_body
     6from nagslang.game_object import (GameObject, SingleShapePhysicser, Result,
     7                                  make_body)
    78from nagslang.resources import resources
    89
     
    1314    def __init__(self, space, name, shape, renderer):
    1415        self._name = name
     16        self.collected = False
    1517        shape.sensor = True
    1618        super(CollectibleGameObject, self).__init__(
     
    2426    def _collect(self, protagonist):
    2527        protagonist.add_item(self._name)
    26         # TODO: Make this less hacky.
    2728        self.physicser.remove_from_space()
    28         self.renderer = render.NullRenderer()
     29        self.collected = True
     30
     31    def update(self, dt):
     32        if self.collected:
     33            return Result(remove=[self])
    2934
    3035
  • nagslang/enemies.py

    r372 r385  
    1010                                ACID_SPEED, ACID_DAMAGE, ZORDER_MID)
    1111from nagslang.events import EnemyDeathEvent, FireEvent
    12 from nagslang.game_object import GameObject, SingleShapePhysicser, make_body
     12from nagslang.game_object import (GameObject, SingleShapePhysicser, Result,
     13                                  make_body)
    1314from nagslang.mutators import FLIP_H
    1415from nagslang.resources import resources
     
    6970    def lose_health(self, amount):
    7071        self.health -= amount
    71         if self.health < 0:
     72        if self.health <= 0:
    7273            self.physicser.remove_from_space()
    73             self.remove = True
    7474            EnemyDeathEvent.post(self.physicser.position, self.enemy_type)
    7575
     
    126126        return x_step, y_step
    127127
     128    def update(self, dt):
     129        super(Enemy, self).update(dt)
     130        if self.health <= 0:
     131            return Result(remove=[self])
     132
    128133
    129134class DeadEnemy(GameObject):
     
    195200        self.set_direction(x_step, y_step)
    196201        self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
    197         super(PatrollingAlien, self).update(dt)
     202        return super(PatrollingAlien, self).update(dt)
    198203
    199204    @classmethod
     
    251256        dx, dy = self._calc_movement()
    252257        self.set_direction(dx, dy)
    253         super(ChargingAlien, self).update(dt)
     258        return super(ChargingAlien, self).update(dt)
    254259
    255260    @classmethod
     
    291296
    292297    def update(self, dt):
    293         super(RunAndGunAlien, self).update(dt)
    294298        self.count += 1
     299        return super(RunAndGunAlien, self).update(dt)
    295300
    296301    @classmethod
  • nagslang/game_object.py

    r384 r385  
    1414from nagslang.resources import resources
    1515from nagslang.events import DoorEvent
     16
     17
     18class Result(object):
     19    '''
     20    Return from an update() function, to add new objects to the world, and/or
     21    remove old objects.
     22    '''
     23    def __init__(self, add=(), remove=()):
     24        self.add = add
     25        self.remove = remove
    1626
    1727
     
    126136        if interactible is not None:
    127137            self.interactible.set_game_object(self)
    128         self.remove = False  # If true, will be removed from drawables
    129138        self._timers = {}
    130139        self._active_timers = {}
     
    483492                shape.physicser.game_object.hit(self)
    484493            self.physicser.remove_from_space()
    485             self.remove = True
    486             break
     494            return Result(remove=[self])
    487495
    488496
     
    508516        if self.lifetime > 0.1:
    509517            self.physicser.remove_from_space()
    510             self.remove = True
     518            return Result(remove=[self])
    511519
    512520
  • nagslang/screens/area.py

    r371 r385  
    244244        self.tick_protagonist()
    245245        for drawable in self._drawables:
    246             drawable.update(seconds)
    247             if drawable.remove:
    248                 self._drawables.remove(drawable)
     246            result = drawable.update(seconds)
     247            if result is not None:
     248                for new_drawable in result.add:
     249                    self._drawables.add(new_drawable)
     250                for old_drawable in result.remove:
     251                    self._drawables.remove(old_drawable)
    249252
    250253    def render_health_bar(self, surface, damage_experienced=None):
Note: See TracChangeset for help on using the changeset viewer.