Changeset 440:9eee96966d88


Ignore:
Timestamp:
Sep 7, 2013, 12:36:09 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

Add Death event to reduce death code duplication

Location:
nagslang
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • nagslang/events.py

    r406 r440  
    4141
    4242
     43class DeathEvent(UserEvent):
     44    @classmethod
     45    def post(cls):
     46        super(DeathEvent, cls).post()
     47
     48
    4349class DoorEvent(UserEvent):
    4450    @classmethod
  • nagslang/protagonist.py

    r437 r440  
    1212from nagslang.game_object import (
    1313    GameObject, Physicser, Result, Bullet, ClawAttack, make_body)
    14 from nagslang.level import Level
    1514from nagslang.mutators import FLIP_H
    1615from nagslang.resources import resources
    17 from nagslang.events import ScreenChange
     16from nagslang.events import DeathEvent
    1817from nagslang.utils import vec_from_angle, vec_with_length
    1918
     
    340339        # Handle player death - may be called due to other reasons
    341340        # than zero health
    342         self.world.load()
    343         self.world.deaths += 1
    344         level, pos = Level.game_starting_point()
    345         self.set_position(pos)
    346         ScreenChange.post(level)
     341        self.reset()
     342        DeathEvent.post()
    347343
    348344    def lose_health(self, amount):
  • nagslang/screens/area.py

    r437 r440  
    99    COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK,
    1010    CMD_TOGGLE_FORM, CMD_ACTION)
    11 from nagslang.events import ScreenChange, DoorEvent, QuitEvent
     11from nagslang.events import ScreenChange, DoorEvent, QuitEvent, DeathEvent
    1212from nagslang.level import Level
    1313from nagslang.screens.base import Screen
     
    166166                    QuitEvent.post()
    167167                    return
    168                 room, pos = self.game_starting_point
    169                 self.world.load()
    170                 self.world.deaths += 1
    171                 ScreenChange.post(room)
    172                 self.protagonist.set_position(pos)
    173                 self._disable_render = True
     168                self.protagonist.die()
    174169                return
    175170            cmd_key = self.keys.get_command_key(ev.key)
     
    188183            # else we're teleporting within the screen, and just the
    189184            # position change is enough
     185        elif DeathEvent.matches(ev):
     186            self._disable_render = True
     187            self.world.load()
     188            self.world.deaths += 1
     189            level, pos = Level.game_starting_point()
     190            self.protagonist.set_position(pos)
     191            ScreenChange.post(level)
     192
    190193        self.keys.handle_event(ev)
    191194
Note: See TracChangeset for help on using the changeset viewer.