Changeset 180:026297a03963


Ignore:
Timestamp:
Sep 3, 2013, 2:58:45 PM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
6acd60ff0197cad6bc4cbd52053cdf77bbacd0bd
Message:

Add DoorEvent? and tweak ScreenChange? to keep more state when the player goes through a door

Location:
nagslang
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • nagslang/engine.py

    r175 r180  
    2323            'level2': AreaScreen,
    2424        }
    25         self.change_screen('menu')
     25        self.change_screen('menu', None)
    2626        # Dummy resize event, to force us to realise our real size
    2727        # http://stackoverflow.com/q/16442573/8629
     
    2929                                             size=(0, 0), w=0, h=0))
    3030
    31     def change_screen(self, new_screen):
     31    def change_screen(self, new_screen, player):
    3232        if self._current_screen is not None:
    3333            self._current_screen.teardown()
    3434        screen_cls = self._screens[new_screen]
    35         self._current_screen = screen_cls(new_screen, self._world)
     35        self._current_screen = screen_cls(new_screen, player, self._world)
    3636        self._current_screen.setup()
    3737
     
    4848                elif ScreenChange.matches(ev):
    4949                    self._surface.fill(pygame.color.Color(0, 0, 0))
    50                     self.change_screen(ev.screen)
     50                    self.change_screen(ev.screen, ev.player)
    5151                else:
    5252                    self._current_screen.handle_event(ev)
  • nagslang/events.py

    r20 r180  
    3636class ScreenChange(UserEvent):
    3737    @classmethod
    38     def post(cls, new_screen):
    39         super(ScreenChange, cls).post(screen=new_screen)
     38    def post(cls, new_screen, player=None):
     39        super(ScreenChange, cls).post(screen=new_screen, player=player)
     40
     41
     42class DoorEvent(UserEvent):
     43    @classmethod
     44    def post(cls, destination, dest_pos):
     45        super(DoorEvent, cls).post(destination=destination, dest_pos=dest_pos)
  • nagslang/game_object.py

    r176 r180  
    1010from nagslang.options import options
    1111from nagslang.resources import resources
    12 from nagslang.events import ScreenChange
     12from nagslang.events import DoorEvent
    1313
    1414
     
    407407            if shape.collision_type == COLLISION_TYPE_PLAYER:
    408408                # Force to new position
    409                 shape.body.position = self.dest_pos
    410                 ScreenChange.post(self.destination)
     409                DoorEvent.post(self.destination, self.dest_pos)
  • nagslang/screens/area.py

    r147 r180  
    55import pymunk.pygame_util
    66
    7 from nagslang.events import ScreenChange
     7from nagslang.events import ScreenChange, DoorEvent
    88from nagslang.level import Level
    99from nagslang.protagonist import Protagonist
     
    6969        self._drawables = Drawables()
    7070        self.add_walls()
    71         self.add_protagonist()
     71        if self.protagonist is not None:
     72            # We do things this way to avoid extra pymunk
     73            # juggling to move objects between spaces
     74            old_protagonist = self.protagonist
     75            self.add_protagonist()
     76            self.protagonist.copy_state(old_protagonist)
     77        else:
     78            self.add_protagonist()
    7279        self.add_game_objects()
    7380
     
    101108            if ev.key == pygame.locals.K_c:
    102109                self.protagonist.toggle_form()
     110        elif DoorEvent.matches(ev):
     111            self.protagonist.set_position(ev.dest_pos)
     112            if ev.destination != self.name:
     113                # Go to anther screen
     114                ScreenChange.post(ev.destination, self.protagonist)
     115            # else we're teleporting within the screen, and just the
     116            # position change is enough
    103117        self.keys.handle_event(ev)
    104118
  • nagslang/screens/base.py

    r37 r180  
    77class Screen(object):
    88
    9     def __init__(self, name, world):
     9    def __init__(self, name, player, world):
    1010        self.name = name
    1111        self.world = world
     12        self.protagonist = player
    1213        self.space = pymunk.Space()
    1314
  • nagslang/screens/menu.py

    r29 r180  
    1515                QuitEvent.post()
    1616            elif ev.key == pygame.locals.K_1:
    17                 ScreenChange.post('level1')
     17                ScreenChange.post('level1', None)
    1818
    1919    def render(self, surface):
Note: See TracChangeset for help on using the changeset viewer.