all repos — Legends-RPG @ c34273ecf367cfde3fa95f9181f1e4ca13095502

A fantasy mini-RPG built with Python and Pygame.

data/states/level_state.py (view raw)

  1__author__ = 'justinarmstrong'
  2"""
  3This is the base class all level states (i.e. states
  4where the player can move around the screen) inherit
  5from.  This class inherits from the generic state class
  6found in the tools.py module.
  7"""
  8
  9import pygame as pg
 10from .. import tools, collision
 11from .. import tilemap as tm
 12from .. components import person, textbox
 13
 14
 15class LevelState(tools._State):
 16    def __init__(self, name):
 17        super(LevelState, self).__init__(name)
 18
 19
 20    def startup(self, current_time, persist):
 21        """Called when the State object is created"""
 22        self.persist = persist
 23        self.current_time = current_time
 24        self.state = 'normal'
 25        self.town_map = tm.create_town_map(self.name)
 26        self.viewport = tm.create_viewport(self.town_map)
 27        self.blockers = tm.create_blockers(self.name)
 28        self.level_surface = tm.create_level_surface(self.town_map)
 29        self.level_rect = self.level_surface.get_rect()
 30        self.player = person.Player('up')
 31        self.town_sprites = pg.sprite.Group()
 32        self.start_positions = tm.set_sprite_positions(self.player,
 33                                                       self.town_sprites,
 34                                                       self.name)
 35        self.set_sprite_dialogue()
 36        self.collision_handler = collision.CollisionHandler(self.player,
 37                                                            self.blockers,
 38                                                            self.town_sprites)
 39        self.dialogue_handler = textbox.DialogueHandler(self.player,
 40                                                        self.town_sprites,
 41                                                        self)
 42        self.state_dict = self.make_state_dict()
 43        self.portals = tm.make_level_portals(self.name)
 44
 45
 46    def set_sprite_dialogue(self):
 47        """Sets unique dialogue for each sprite"""
 48        raise NotImplementedError
 49
 50
 51    def make_state_dict(self):
 52        """Make a dictionary of states the level can be in"""
 53        state_dict = {'normal': self.running_normally,
 54                      'dialogue': self.handling_dialogue}
 55
 56        return state_dict
 57
 58
 59    def running_normally(self, surface, keys, current_time):
 60        """Update level normally"""
 61        self.check_for_dialogue()
 62        self.check_for_portals()
 63        self.player.update(keys, current_time)
 64        self.town_sprites.update(current_time)
 65        self.collision_handler.update(keys, current_time)
 66        self.dialogue_handler.update(keys, current_time)
 67        self.viewport_update()
 68
 69        self.draw_level(surface)
 70
 71
 72    def check_for_portals(self):
 73        """Check if the player walks into a door, requiring a level change"""
 74        portal = pg.sprite.spritecollideany(self.player, self.portals)
 75
 76        if portal and self.player.state == 'resting':
 77            self.next = portal.name
 78            self.done = True
 79
 80
 81    def handling_dialogue(self, surface, keys, current_time):
 82        """Update only dialogue boxes"""
 83        self.dialogue_handler.update(keys, current_time)
 84        self.draw_level(surface)
 85
 86
 87    def check_for_dialogue(self):
 88        """Check if the level needs to freeze"""
 89        if self.dialogue_handler.textbox:
 90            self.state = 'dialogue'
 91
 92
 93    def update(self, surface, keys, current_time):
 94        """Updates state"""
 95        state_function = self.state_dict[self.state]
 96        state_function(surface, keys, current_time)
 97
 98
 99    def viewport_update(self):
100        """Viewport stays centered on character, unless at edge of map"""
101        self.viewport.center = self.player.rect.center
102        self.viewport.clamp_ip(self.level_rect)
103
104
105    def draw_level(self, surface):
106        """Blits all images to screen"""
107        self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
108        self.level_surface.blit(self.player.image, self.player.rect)
109        self.town_sprites.draw(self.level_surface)
110
111        surface.blit(self.level_surface, (0, 0), self.viewport)
112        self.dialogue_handler.draw(surface)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127