Ignore:
Timestamp:
Sep 6, 2013, 11:04:01 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
cfdbf911c96183a66f8127a1ff9fcbbe16ebc497
Message:

Use Result to handle firing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/enemies.py

    r391 r393  
    99from nagslang.constants import (COLLISION_TYPE_ENEMY, COLLISION_TYPE_FURNITURE,
    1010                                ACID_SPEED, ACID_DAMAGE, ZORDER_MID)
    11 from nagslang.events import FireEvent, EnemyDeathEvent
     11from nagslang.events import EnemyDeathEvent
    1212from nagslang.game_object import (GameObject, SingleShapePhysicser, Result,
    13                                   make_body)
     13                                  Bullet, make_body)
    1414from nagslang.mutators import FLIP_H
    1515from nagslang.resources import resources
     
    8686        self.lose_health(claw_attack.damage)
    8787
    88     def ranged_attack(self, range_, speed, damage, type_, reload_time):
     88    def ranged_attack(self, range_, speed, damage, type_, reload_time,
     89                      result=None):
     90        if result is None:
     91            result = Result()
     92
    8993        pos = self.physicser.position
    9094        target = self.world.protagonist.get_shape().body.position
     
    96100                    or shape.sensor):
    97101                continue
    98             return
     102            return result
    99103
    100104        if not self.check_timer('reload_time'):
     
    103107            if vec.length < range_:
    104108                vec.length = speed
    105                 FireEvent.post(pos, vec, damage, type_,
    106                                COLLISION_TYPE_ENEMY)
     109                result.add += (Bullet(self.get_space(), pos, vec, damage,
     110                                      type_, COLLISION_TYPE_ENEMY),)
     111        return result
    107112
    108113    def greedy_move(self, target):
     
    128133    def update(self, dt):
    129134        super(Enemy, self).update(dt)
     135        result = Result()
    130136        if self.health <= 0:
    131             return Result(
    132                 add=[DeadEnemy(self.get_space(), self.world,
    133                                self.physicser.position, self.enemy_type)],
    134                 remove=[self])
     137            result.remove += (self,)
     138            result.add += (DeadEnemy(self.get_space(), self.world,
     139                                     self.physicser.position,
     140                                     self.enemy_type),)
     141        return result
    135142
    136143
     
    202209            self._switch_direction()
    203210        self.set_direction(x_step, y_step)
    204         self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
    205         return super(PatrollingAlien, self).update(dt)
     211        result = self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
     212        return result.merge(super(PatrollingAlien, self).update(dt))
    206213
    207214    @classmethod
     
    256263        # Calculate the step every frame
    257264        # Distance to the protagonist
    258         self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
     265        result = self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
    259266        dx, dy = self._calc_movement()
    260267        self.set_direction(dx, dy)
    261         return super(ChargingAlien, self).update(dt)
     268        return result.merge(super(ChargingAlien, self).update(dt))
    262269
    263270    @classmethod
Note: See TracChangeset for help on using the changeset viewer.