__author__ = 'justinarmstrong' import pygame as pg from .. import tools, collision from .. import tilemap as tm from .. components import person, textbox from .. import constants as c class Town(tools._State): def __init__(self): super(Town, self).__init__() def startup(self, current_time, persist): """Called when the State object is created""" self.persist = persist self.current_time = current_time self.state = 'normal' self.town_map = tm.create_town_map() self.viewport = tm.create_viewport(self.town_map) self.blockers = tm.create_blockers() self.level_surface = tm.create_level_surface(self.town_map) self.level_rect = self.level_surface.get_rect() self.player = person.Player('up') self.town_sprites = pg.sprite.Group() self.start_positions = tm.set_sprite_positions(self.player, self.town_sprites) self.set_sprite_dialogue() self.collision_handler = collision.CollisionHandler(self.player, self.blockers, self.town_sprites) self.dialogue_handler = textbox.DialogueHandler(self.player, self.town_sprites, self) self.state_dict = self.make_state_dict() self.portals = tm.make_level_portals() def set_sprite_dialogue(self): """Sets unique dialogue for each sprite""" for sprite in self.town_sprites: if sprite.location == (10, 47): sprite.dialogue = ['Welcome to our town, Mr. Traveller!', 'The King is loved by all!', 'You should go visit him in his castle.'] elif sprite.location == (16, 42): sprite.dialogue = ['You seem tired, why not rest at our Inn?'] sprite.begin_auto_resting() elif sprite.location == (14, 14): sprite.dialogue = ['Be careful. There are monsters surrounding our town.', 'Make sure to equip sufficient armour and weapons.', 'Spells and potions are useful too.'] elif sprite.location == (11, 14): sprite.dialogue = ['I have heard rumours that the King has lost something...', 'Perhaps you should pay him a visit.'] elif sprite.location == (11, 8): sprite.dialogue = ['Welcome to the castle, citizen.'] elif sprite.location == (14, 8): sprite.dialogue = ['Move along.'] def make_state_dict(self): """Make a dictionary of states the level can be in""" state_dict = {'normal': self.running_normally, 'dialogue': self.handling_dialogue} return state_dict def running_normally(self, surface, keys, current_time): """Update level normally""" self.check_for_dialogue() self.check_for_portals() self.player.update(keys, current_time) self.town_sprites.update(current_time) self.collision_handler.update(keys, current_time) self.dialogue_handler.update(keys, current_time) self.viewport_update() self.draw_level(surface) def check_for_portals(self): """Check if the player walks into a door, requiring a level change""" portal = pg.sprite.spritecollideany(self.player, self.portals) if portal and self.player.state == 'resting': #self.next = portal.name self.next = c.TOWN self.done = True def handling_dialogue(self, surface, keys, current_time): """Update only dialogue boxes""" self.dialogue_handler.update(keys, current_time) self.draw_level(surface) def check_for_dialogue(self): """Check if the level needs to freeze""" if self.dialogue_handler.textbox: self.state = 'dialogue' def update(self, surface, keys, current_time): """Updates state""" state_function = self.state_dict[self.state] state_function(surface, keys, current_time) def viewport_update(self): """Viewport stays centered on character, unless at edge of map""" self.viewport.center = self.player.rect.center self.viewport.clamp_ip(self.level_rect) def draw_level(self, surface): """Blits all images to screen""" self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport) self.level_surface.blit(self.player.image, self.player.rect) self.town_sprites.draw(self.level_surface) surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface)