all repos — Legends-RPG @ 042b912afd55ebeecab540dc4a44dd070f886b6a

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 copy
 10import pygame as pg
 11from .. import tools, collision
 12from .. import tilemap as tm
 13from .. components import person, textbox
 14
 15
 16class LevelState(tools._State):
 17    def __init__(self):
 18        super(LevelState, self).__init__()
 19        self.name = None
 20        self.map_width = None
 21        self.map_height = None
 22
 23
 24
 25    def startup(self, current_time, game_data):
 26        """Called when the State object is created"""
 27        self.game_data = game_data
 28        print(game_data['player inventory'])
 29        self.current_time = current_time
 30        self.state = 'normal'
 31        self.allow_input = False
 32        self.background = tm.create_map_layer1(self.name,
 33                                               self.map_width,
 34                                               self.map_height)
 35        self.foreground = tm.create_map_layer2(self.name,
 36                                               self.map_width,
 37                                               self.map_height)
 38        self.viewport = tm.create_viewport(self.background)
 39        self.blockers = tm.create_blockers(self.name)
 40        self.level_surface = tm.make_level_surface(self.background)
 41        self.level_rect = self.level_surface.get_rect()
 42        self.player = person.Player(game_data['last direction'])
 43        self.sprites = pg.sprite.Group()
 44        self.start_positions = tm.set_sprite_positions(self.player,
 45                                                       self.sprites,
 46                                                       self.name,
 47                                                       self.game_data)
 48        self.set_sprite_dialogue()
 49        self.collision_handler = collision.CollisionHandler(self.player,
 50                                                            self.blockers,
 51                                                            self.sprites)
 52        self.dialogue_handler = textbox.TextHandler(self)
 53        self.state_dict = self.make_state_dict()
 54        self.portals = tm.make_level_portals(self.name)
 55
 56
 57    def set_sprite_dialogue(self):
 58        """Sets unique dialogue for each sprite"""
 59        raise NotImplementedError
 60
 61
 62    def make_state_dict(self):
 63        """Make a dictionary of states the level can be in"""
 64        state_dict = {'normal': self.running_normally,
 65                      'dialogue': self.handling_dialogue}
 66
 67        return state_dict
 68
 69
 70    def running_normally(self, surface, keys, current_time):
 71        """Update level normally"""
 72        self.check_for_dialogue()
 73        self.check_for_portals()
 74        self.player.update(keys, current_time)
 75        self.sprites.update(current_time)
 76        self.collision_handler.update(keys, current_time)
 77        self.dialogue_handler.update(keys, current_time)
 78        self.check_for_menu(keys)
 79        self.viewport_update()
 80
 81        self.draw_level(surface)
 82
 83
 84    def check_for_portals(self):
 85        """Check if the player walks into a door, requiring a level change"""
 86        portal = pg.sprite.spritecollideany(self.player, self.portals)
 87
 88        if portal and self.player.state == 'resting':
 89            self.player.location = self.player.get_tile_location()
 90            self.next = portal.name
 91            self.update_game_data()
 92            self.done = True
 93
 94
 95    def check_for_menu(self, keys):
 96        """Check if player hits enter to go to menu"""
 97        if keys[pg.K_RETURN] and self.allow_input:
 98            if self.player.state == 'resting':
 99                self.player.location = self.player.get_tile_location()
100                self.next = 'player menu'
101                self.update_game_data()
102                self.done = True
103
104
105        if not keys[pg.K_RETURN]:
106            self.allow_input = True
107
108
109    def update_game_data(self):
110        """Update the persistant game data dictionary"""
111        self.game_data['last location'] = self.player.location
112        self.game_data['last direction'] = self.player.direction
113        self.game_data['last state'] = self.name
114
115        self.set_new_start_pos()
116
117
118    def set_new_start_pos(self):
119        """Set new start position based on previous state"""
120        location = copy.deepcopy(self.game_data['last location'])
121        direction = self.game_data['last direction']
122        state = self.game_data['last state']
123
124        if self.next == 'player menu':
125            pass
126        elif direction == 'up':
127            location[1] += 1
128        elif direction == 'down':
129            location[1] -= 1
130        elif direction == 'left':
131            location[0] += 1
132        elif direction == 'right':
133            location[0] -= 1
134
135        self.game_data[state + ' start pos'] = location
136
137
138    def handling_dialogue(self, surface, keys, current_time):
139        """Update only dialogue boxes"""
140        self.dialogue_handler.update(keys, current_time)
141        self.draw_level(surface)
142
143
144    def check_for_dialogue(self):
145        """Check if the level needs to freeze"""
146        if self.dialogue_handler.textbox:
147            self.state = 'dialogue'
148
149
150    def update(self, surface, keys, current_time):
151        """Updates state"""
152        state_function = self.state_dict[self.state]
153        state_function(surface, keys, current_time)
154
155
156    def viewport_update(self):
157        """Viewport stays centered on character, unless at edge of map"""
158        self.viewport.center = self.player.rect.center
159        self.viewport.clamp_ip(self.level_rect)
160
161
162    def draw_level(self, surface):
163        """Blits all images to screen"""
164        self.level_surface.blit(self.background, self.viewport, self.viewport)
165        self.level_surface.blit(self.player.image, self.player.rect)
166        self.sprites.draw(self.level_surface)
167        self.level_surface.blit(self.foreground, self.viewport, self.viewport)
168
169
170        surface.blit(self.level_surface, (0, 0), self.viewport)
171        self.dialogue_handler.draw(surface)
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186