Changeset 393:8d961e05b7b6


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

Location:
nagslang
Files:
5 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
  • nagslang/events.py

    r391 r393  
    4747
    4848
    49 class FireEvent(UserEvent):
    50     @classmethod
    51     def post(cls, source, impulse, damage, bullet_type, source_collision_type):
    52         super(FireEvent, cls).post(source=source, impulse=impulse,
    53                                    damage=damage, bullet_type=bullet_type,
    54                                    source_collision_type=source_collision_type)
    55 
    56 
    5749class EnemyDeathEvent(UserEvent):
    5850    @classmethod
  • nagslang/game_object.py

    r390 r393  
    2424        self.add = add
    2525        self.remove = remove
     26
     27    def merge(self, result):
     28        if result is not None:
     29            self.add += result.add
     30            self.remove += result.remove
     31        return self
    2632
    2733
  • nagslang/protagonist.py

    r375 r393  
    1010    NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, BULLET_SPEED, CLAW_DAMAGE,
    1111    CMD_TOGGLE_FORM, CMD_ACTION)
    12 from nagslang.events import FireEvent, ClawEvent
    13 from nagslang.game_object import GameObject, Physicser, make_body
     12from nagslang.events import ClawEvent
     13from nagslang.game_object import (
     14    GameObject, Physicser, Result, Bullet, make_body)
    1415from nagslang.mutators import FLIP_H
    1516from nagslang.resources import resources
     
    193194        self.start_timer('attack_cooldown')
    194195        self.world.attacks += 1
    195         self.attack()
     196        return self.attack()
    196197
    197198    def handle_keypress(self, key_command):
     
    295296        """
    296297        if self.in_wolf_form():
    297             self.claw()
     298            return self.claw()
    298299        else:
    299             self.shoot()
     300            return self.shoot()
    300301
    301302    def shoot(self):
     
    303304            return
    304305        vec = vec_from_angle(self.angle, BULLET_SPEED)
    305         FireEvent.post(
    306             self.physicser.position, vec, BULLET_DAMAGE, 'bullet',
    307             COLLISION_TYPE_PLAYER)
     306        return Result(add=(Bullet(self.get_space(), self.physicser.position,
     307                                  vec, BULLET_DAMAGE, 'bullet',
     308                                  COLLISION_TYPE_PLAYER),))
    308309
    309310    def claw(self):
  • nagslang/screens/area.py

    r391 r393  
    99    COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK,
    1010    CMD_TOGGLE_FORM, CMD_ACTION)
    11 from nagslang.events import (
    12     ScreenChange, DoorEvent, EnemyDeathEvent, FireEvent, ClawEvent)
     11from nagslang.events import ScreenChange, DoorEvent, EnemyDeathEvent, ClawEvent
    1312from nagslang.level import Level
    1413from nagslang.screens.base import Screen
    15 from nagslang.game_object import Bullet, ClawAttack
     14from nagslang.game_object import ClawAttack
    1615from nagslang.sound import sound
    1716
     
    179178            # else we're teleporting within the screen, and just the
    180179            # position change is enough
    181         elif FireEvent.matches(ev):
    182             bullet = Bullet(self.space, ev.source, ev.impulse, ev.damage,
    183                             ev.bullet_type, ev.source_collision_type)
    184             self._drawables.add(bullet)
    185180        elif EnemyDeathEvent.matches(ev):
    186181            self.world.kills += 1
     
    234229        self.protagonist.set_direction(dx, dy)
    235230        if self.keys.is_attacking():
    236             self.protagonist.handle_attack_key_down()
     231            self._handle_result(self.protagonist.handle_attack_key_down())
    237232
    238233    def tick(self, seconds):
     
    240235        self.tick_protagonist()
    241236        for drawable in self._drawables:
    242             result = drawable.update(seconds)
    243             if result is not None:
    244                 for new_drawable in result.add:
    245                     self._drawables.add(new_drawable)
    246                 for old_drawable in result.remove:
    247                     self._drawables.remove(old_drawable)
     237            self._handle_result(drawable.update(seconds))
     238
     239    def _handle_result(self, result):
     240        if result is not None:
     241            for drawable in result.add:
     242                self._drawables.add(drawable)
     243            for drawable in result.remove:
     244                self._drawables.remove(drawable)
    248245
    249246    def render_health_bar(self, surface, damage_experienced=None):
Note: See TracChangeset for help on using the changeset viewer.