def move(game, x=0, y=0): if game.state == game.STATE_NORMAL: ''' Move the player towards the target location. ''' target_x = game.player.x + x target_y = game.player.y + y tile = game.map.get_tile(target_x, target_y) if tile is not None and tile.is_passable(): game.execute_action(actions.action.ActionInstance( source = game.current_entity, action = actions.misc.Move(), target = (target_x, target_y), )) game.end_turn(100) elif game.state == game.STATE_TARGET: ''' Choose the given location as the target for the current targeting state. ''' game.selection = (game.selection[0] + x, game.selection[1] + y, game.selection[2]) game.do_update('cursor') elif game.state == game.STATE_EXPLORE: ''' Move the user's view towards the given location. ''' hotspot = game.focus if hotspot.is_game_layer(): x = hotspot.mouse_x + x*(hotspot.width / game.map.width) y = hotspot.mouse_y + y*(hotspot.height / game.map.height) State.window().set_mouse_position(x, y) hotspot.on_hover(game, x, y) game.do_update('bounds')
def update_shadows(self): model = State.model() for x in range(max(0, self.grid_x), min(self.grid_x + self.grid_width, model.map.width)): for y in range(max(0, self.grid_y), min(self.grid_y + self.grid_height, model.map.height)): tile = model.map.get_tile(x, y) if tile is not None: if tile.entity is None: sprite = tile.ascii else: sprite = tile.entity.ascii player = State.model().player if not tile.explored: sprite.color = [0, 0, 0] elif model.map.has_line_of_sight(player.x, player.y, x, y): if tile.is_opaque(): light_x = x light_y = y distance_x = x - player.x distance_y = y - player.y if distance_x == 0: light_y = light_y - int(math.copysign(1, distance_y)) elif distance_y == 0: light_x = light_x - int(math.copysign(1, distance_x)) else: angle = math.radians(distance_y / distance_y) delta_x = math.cos(angle) delta_y = math.sin(angle) if delta_x >= delta_y: light_x -= int(math.copysign(1, distance_x)) if delta_x <= delta_y: light_y -= int(math.copysign(1, distance_y)) light_source = model.map.get_tile(light_x, light_y) if light_source is not None: shadow = light_source.shadow else: shadow = 100 else: shadow = tile.shadow if shadow > 0: sprite.opacity = (255 - GameView.MIN_LIGHT) / (shadow + 1) + GameView.MIN_LIGHT else: sprite.opacity = 255 else: sprite.opacity = 128 sprite.color = [128, 128, 128] if model.state == model.STATE_TARGET: valid = model.is_valid_target(x, y) if valid is None: sprite.opacity *= 0.25 elif valid is False: sprite.opacity *= 0.55
def set_state(game, state): ''' Change the program's current state. Specifically, this means switching to a different view, so that the player can view a menu, or return to the game view. ''' State.set_current(state)
def on_click(self, model, button, modifiers): player = State.model().player index = self.selection_y functions.set_state(model, 'game') State.model().execute_action(action.ActionInstance( action = player.actions[index], source = model.player, ))
def init_states(window): game_model = model.game.Game() global_controller = controller.controller.Controller() print("Initializing Game States...") State('game', window = window, model = game_model, view = view.game.GameView(window), controller = global_controller, commands = controller.game.commands, ) State('properties', window = window, model = game_model, view = view.properties.PropertiesView(window), controller = global_controller, commands = controller.properties.commands, ) State('traits', window = window, model = game_model, view = view.traits.TraitsView(window), controller = global_controller, commands = controller.properties.commands, ) State('actions', window = window, model = game_model, view = view.actions.ActionsView(window), controller = global_controller, commands = controller.properties.commands, ) State('inventory', window = window, model = game_model, view = view.inventory.InventoryView(window), controller = global_controller, commands = controller.properties.commands, ) State('world', window = window, model = game_model, view = view.world.WorldView(window), controller = global_controller, commands = controller.properties.commands, ) State.set_current('game') game_model.next_turn()
def get_target_types(self): target_types = set(self.type) if self.entity is not None: target_types.add(Tile.TARGET_ENTITY) if State.model() is not None and self.entity == State.model().current_entity: target_types.add(Tile.TARGET_SELF) if self.items != []: target_types.add(Tile.TARGET_ITEM) return target_types
def update_list(self): ItemListLayer.update_list(self) player = State.model().player i = 0 for item in player.inventory: if item not in player.equipment[item.equip_slot]: x = i / self.rows y = i - self.rows*x text = "" text += " " * (InventoryLayer.NAME_COLUMN - len(text)) text += item.name text += " " * (InventoryLayer.TYPE_COLUMN - len(text)) text += item.type text += " " * (InventoryLayer.SLOT_COLUMN - len(text)) text += item.equip_slot.name text += " " * (InventoryLayer.SIZE_COLUMN - len(text)) text += str(item.equip_size) text += " " * (InventoryLayer.WEIGHT_COLUMN - len(text)) text += str(item.weight) self.items[x][y].text = text self.items[x][y].item = item if player.could_equip(item): self.items[x][y].disabled = False self.items[x][y].color = (255, 255, 255, 255) else: self.items[x][y].disabled = True self.items[x][y].color = (188, 143, 143, 255) i += 1
def update_list(self): ItemListLayer.update_list(self) player = State.model().player i = 0 for equip_slot, items in player.equipment.iteritems(): n = 0 for item in items: x = i / self.rows y = i - self.columns*x text = "" text += " " * (EquipmentLayer.NAME_COLUMN - len(text)) text += item.name text += " " * (EquipmentLayer.TYPE_COLUMN - len(text)) text += item.type text += " " * (EquipmentLayer.SLOT_COLUMN - len(text)) text += str(item.equip_slot.get_slot_description(n)) text += " " * (EquipmentLayer.WEIGHT_COLUMN - len(text)) text += str(item.weight) self.items[x][y].text = text self.items[x][y].item = item i += 1 n += 1
def update_shadows(self): print('update shadows') model = State.model() for x in range(self.grid_width): for y in range(self.grid_height): tile = model.map.get_tile(self.grid_x + x, self.grid_y + y) if tile is not None: if True: #TODO: Change this check to be opacity vs. pixel shading. via some kind of options class. sprite = self.shadows[x][y] sprite.image = visual.Tile.get_shadow(7) sprite.batch = self.shadow_layer else: if tile.shadow > 0: sprite.opacity = (255 - GameView.MIN_LIGHT) / (tile.shadow + 1) + GameView.MIN_LIGHT else: sprite.opacity = 255 if model.state == model.STATE_TARGET: valid = model.is_valid_target(x, y) if valid is None: sprite.opacity *= 0.25 elif valid is False: sprite.opacity *= 0.55 elif True: #TODO: Change this check to be opacity vs. pixel shading. via some kind of options class. self.shadows[x][y].batch = None
def update_description(self, tile): title = "" text = "" if tile.text != "": prop = tile.prop player = State.model().player modifiers = player.get_property_modifiers(prop) title = prop.name+" "+str(player.get(prop)) modifier = str(player.get(prop, True)) spacing = " "*(2 - len(modifier)) text += " "+spacing+modifier+"\n" if type(prop.base) == properties.Property: modifier = str(player.get(prop.base)) spacing = " "*(2 - len(modifier)) name = prop.base.name text += " +"+spacing+modifier+", "+name+"\n" for modifier in modifiers: value = modifier[0] if value < 0: sign = "-" value = str(abs(value)) else: sign = "+" value = str(value) spacing = " "*(2 - len(value)) source = modifier[1] text += " "+sign+spacing+value+", "+source+"\n" self.title.text = title self.modifiers.text = text
def on_mouse_press(self, x, y, button, modifiers): ''' An event intended to be triggered by the window object. Triggers every time the user moves the mouse. ''' for hotspot in State.view().hotspots: if hotspot.contains(x, y): self.dispatch_event('on_hotspot_click', hotspot, button, modifiers)
def update_description(self, tile): ItemListLayer.update_description(self, tile) #TODO: the following isn't working atm if State.model().player.could_equip(tile.item) is False: text = self.description.text text += "You cannot equip this item, because your "+tile.item.equip_slot.name+" slot does not have "+str(tile.item.equip_size)+" space." self.description.text = text
def trigger_command(self, key, key_type): ''' A key has been pressed. Check if it is mapped to a command, and send the command activation event if so. ''' command = State.commands().get_action(key, key_type) if command is not None: self.dispatch_event('activate_command', command)
def on_click(self, model, button, modifiers): if button == mouse.LEFT: player = State.model().player index = self.rows - self.selection_y - 1 model.execute_action(action.ActionInstance( action = player.actions[index], source = model.player, )) elif button == mouse.RIGHT: functions.set_state(model, 'actions')
def on_mouse_motion(self, x, y, dx, dy): ''' An event intended to be triggered by the window object. Triggers every time the user moves the mouse. ''' for hotspot in State.view().hotspots: if hotspot.contains(x, y): self.dispatch_event('on_mouse_hover', hotspot, x, y) return # If the mouse is not over any hotspot, send an event to clear the hotspots. self.dispatch_event('on_mouse_hover', None)
def update(self, components = None): ActiveListLayer.update(self, components) if components is None or 'stance' in components: player = State.model().player for i in range(min(len(player.actions), self.rows)): action = player.actions[i] x = i / self.rows y = i self.items[x][y].text = action.name self.items[x][y].action = action
def set(self, prop, value): Entity.set(self, prop, value) model = State.model() max_hp = self.get(properties.max_hp) if value <= 3 and prop.type == properties.Property.TYPE_ATTR: model.log_message("LOW ATTRIBUTE WARNING: " + prop.name) model.do_update("log") elif prop == properties.hp and self.get(properties.hp) <= max_hp * 0.4: model.log_message("LOW HP WARNING: " + str(self.get(properties.hp))) model.do_update("log")
def update(self, components = None): model = State.model() if components is None or 'log' in components: if model.info: for i in range(min(len(model.log), len(self.log))): self.log[i].text = "" self.info.text = Log.HTML_FONT + model.info + "</font>" else: for i in range(min(len(model.log), len(self.log))): self.log[i].text = model.log[i+model.log_offset] self.info.text = ""
def update(self, components = None): world = State.model().game Layer.update(self, components) Hotspot.__init__(self, self.x, self.y, self.width, self.height, world.rows, world.columns) if components is None or 'cursor' in components: self.update_cursor(); elif components == None: y = 0 for depth in xrange(-world.depth, world.height): floor = World.FLOORS[depth] self.items[0][y].text = floor['name'] self.items[0][y].depth = depth y += 1
def update(self, components = None): if components is None or 'playercard' in components: player = State.model().player self.portrait.image = player.portrait self.name.text = player.name self.hp.text = "HP: "+str(player.get(properties.hp))+" / "+str(player.get(properties.max_hp)) self.mana.text = "MN: "+str(player.get(properties.mana))+" / "+str(player.get(properties.max_mana)) self.attributes.text = '' self.attributes.text += "Dexterity "+str(player.get(properties.dexterity))+"\n" self.attributes.text += "Agility "+str(player.get(properties.agility))+"\n" self.attributes.text += "Mobility "+str(player.get(properties.mobility))+"\n" self.attributes.text += "Wits "+str(player.get(properties.wits))+"\n" self.attributes.text += "Perception "+str(player.get(properties.perception)) '''
def update(self, components = None): if components is None or 'effects' in components: player = State.model().player for i in range(min(len(player.effects), len(self.items))): effect = player.effects[i] index = self.line_quantity - i - 1 name = effect.name spacing = " " * (12 - len(name)) duration = effect.duration self.items[index].text = name + spacing + str(duration) self.items[index].effect = effect if components is None or 'cursor' in components: self.update_cursor();
def update_tiles(self): if self.tile_layer is not None: self.batches.remove(self.tile_layer) self.tile_layer = graphics.Batch() self.batches.append(self.tile_layer) model = State.model() grid = model.map.grid for x in range(max(0, self.grid_x), min(self.grid_x + self.grid_width, len(grid))): for y in range(max(0, self.grid_y), min(self.grid_y + self.grid_height, len(grid[x]))): tile = grid[x][y] if tile is not None: sprite = tile.sprite sprite.batch = self.tile_layer sprite.x = x*self.tile_width - self.grid_x*self.tile_width + self.x sprite.y = y*self.tile_height - self.grid_y*self.tile_width + self.y
def update(self, components = None): ActiveListLayer.update(self, components) player = State.model().player slots = self.rows * self.columns i = 0 for trait in player.traits: if (i < slots): x = i / self.rows y = i - self.columns*x self.items[x][y].text = trait.name self.items[x][y].trait = trait i += 1 else: break;
def update(self, components = None): ActiveListLayer.update(self, components) player = State.model().player slots = self.rows * self.columns i = 0 for action in player.actions: if i < slots: x = i / self.rows y = i - self.columns*x self.items[x][y].text = action.name self.items[x][y].action = action i += 1 else: break;
def update(self, components = None): ActiveListLayer.update(self, components) if components is None or 'inventory' in components: player = State.model().player for i in range(min(len(player.inventory), self.rows)): x = i / self.rows y = i item = player.inventory[i] text = "" if item in player.equipment[item.equip_slot]: text += "e " else: text += " " text += item.name self.items[x][y].text = text self.items[x][y].item = item
def update(self, components = None): ActiveListLayer.update(self, components) player = State.model().player slots = self.rows * self.columns i = 0 for prop in properties.Property.all: if i < slots: x = i / self.rows y = i - self.rows*x name = prop.name value = str(player.get(prop)) spacing = " "*(19 - len(prop.name) + 3 - len(value)) self.items[x][y].text = name + spacing + value self.items[x][y].prop = prop i += 1 else: break;
def update_cursor(self): model = State.model() if self.has_focus: self.update_game_bounds() if model.state == model.STATE_EXPLORE: x = self.columns / 2 y = self.rows / 2 else: x = self.selection_x y = self.selection_y absolute_x = self.grid_x + x absolute_y = self.grid_y + y - 1 valid = model.is_valid_target(absolute_x, absolute_y) if valid is None: self.cursor.image = visual.Cursor.get(visual.Cursor.TARGET) elif valid: self.cursor.image = visual.Cursor.get(visual.Cursor.YELLOW) else: self.cursor.image = visual.Cursor.get(visual.Cursor.SELECT) if valid is not None and (absolute_x != model.player.x or absolute_y != model.player.y): path = model.map.find_path(model.map.ALGORITHM_ASTAR, model.player.x, model.player.y, absolute_x, absolute_y) if path is not None: for path_x, path_y in path: self.highlight_tile(path_x, path_y) self.highlight_tile(model.player.x, model.player.y) self.highlight_tile(absolute_x, absolute_y) self.cursor.x = x * self.tile_width + self.x self.cursor.y = (self.rows - y - 1) * self.tile_height + self.y self.show_cursor = True else: self.show_cursor = False
def update_grid(self): self.batches = graphics.Batch() model = State.model() for x in range(max(0, self.grid_x), min(self.grid_x + self.grid_width, model.map.width)): for y in range(max(0, self.grid_y), min(self.grid_y + self.grid_height, model.map.height)): tile = model.map.get_tile(x, y) if tile is not None: if tile.entity is None: sprite = tile.ascii if tile.is_opaque(): sprite.color = [255, 100, 100] else: sprite.color = [255, 255, 255] else: sprite = tile.entity.ascii sprite.color = [100, 100, 255] sprite.batch = self.batches sprite.x = self.x + (x*self.tile_width - self.grid_x*self.tile_width) sprite.y = self.y + (y*self.tile_height - self.grid_y*self.tile_height)
def draw(self): Layer.draw(self) game = State.model() world = game.world cell_width = self.width / world.columns cell_height = self.height / world.rows depth = 0 x = self.x for col in xrange(world.columns): y = self.y for row in xrange(world.rows): floor = world.get_floor(col, row, depth) color = (1.0, 1.0, 1.0) if game.current_floor_coords == (col, row, depth): color = (0.7, 0.7, 1.0) if floor is None: opacity = 0 elif floor.known: opacity = 0.5 elif floor.explored: opacity = 1.0 else: opacity = 0 if opacity > 0: self.set_color(*color, opacity=opacity) gl.glRectf(x, y, x + cell_width, y + cell_height) y += cell_height x += cell_width
def init_states(window): game_model = model.game.Game() global_controller = controller.controller.Controller() print("Initializing Game States...") State( 'game', window=window, model=game_model, view=view.game.GameView(window), controller=global_controller, commands=controller.game.commands, ) State( 'properties', window=window, model=game_model, view=view.properties.PropertiesView(window), controller=global_controller, commands=controller.properties.commands, ) State( 'traits', window=window, model=game_model, view=view.traits.TraitsView(window), controller=global_controller, commands=controller.properties.commands, ) State( 'actions', window=window, model=game_model, view=view.actions.ActionsView(window), controller=global_controller, commands=controller.properties.commands, ) State( 'inventory', window=window, model=game_model, view=view.inventory.InventoryView(window), controller=global_controller, commands=controller.properties.commands, ) State( 'world', window=window, model=game_model, view=view.world.WorldView(window), controller=global_controller, commands=controller.properties.commands, ) State.set_current('game') game_model.next_turn()
def on_click(self, model, button, modifiers): action = State.commands().get(CommandBar.ACTIONS[self.selection_x]).action if action is not None: action(model)