class Level(Controller): """docstring for LevelController""" def __init__(self, window, keys, levelname): super(Level, self).__init__(window, keys) self.tiled_map = load_pyglet(levelname, invert_y=True) self.view = LevelView(self.window.width, self.window.height, self.tiled_map) self.collition_layer = self.tiled_map.get_layer_by_name('collision') self.startLocation = self.tiled_map.get_object_by_name('Starting Location') self.exitLocation = self.tiled_map.get_object_by_name('Exit Location') self.setupPlayer(self.startLocation) def draw(self): self.view.draw() def setupPlayer(self, startLocation): self.mainCharacter = Entity(PlayerInputComponent(self.keys), PhysicsComponent(self), GraphicsComponent(self.view, "test.png")) self.mainCharacter.x = startLocation.x self.mainCharacter.y = self.window.height - startLocation.y - startLocation.height + 96 self.mainCharacter.width = 32 self.mainCharacter.height = 32 def update(self): self.mainCharacter.update() def handleKeys(self): pass def handleMouse(self, x, y, button, modifier): pass
def get_collision_direction(entity: DynamicEntity, other: Entity): """Get the direction where from which a collision event occurred. Parameters: entity (DynamicEntity): Colliding entity. other (Entity): The entity with which the colliding entity collided. Returns: (str): The direction the collision occurred in. "A" for Above "B" for Below "R" for Right "L" for Left """ bb = entity.get_shape().bb cx, cy = bb.center() lx = cx - (cx - bb.left) / 2 rx = cx + (cx - bb.left) / 2 # direction mapping directions = [((cx, bb.top), ABOVE), ((lx, bb.top), ABOVE), ((rx, bb.top), ABOVE), ((cx, bb.bottom), BELOW), ((lx, bb.bottom), BELOW), ((rx, bb.bottom), BELOW), ((bb.left, cy), RIGHT), ((bb.right, cy), LEFT)] for pos, result in directions: if other.get_shape().point_query(pos)[0] < 0: return result
def create_weapon(self, weapon: Weapons): self.db = get_transaction() equippable_component = Equippable(EQUIPMENT_SLOTS.WEAPONS) weapon_component = Weapon(weapon_type=weapon.weapon_enum, dmg_quantity=weapon.damage_quantity, weapon_dmg=weapon.weapon_damage, dmg_type=weapon.id_weapon_damage_type, light=weapon.light, heavy=weapon.heavy, two_hand=weapon.two_hand, reach=weapon.reach, finesse=weapon.finesse, thrown=weapon.thrown, ammunition=weapon.ammunition, range_from=weapon.range_from, range_to=weapon.range_to, versatile=weapon.versatile, versatile_value=weapon.versatile_value, loading=weapon.loading) return Entity(0, 0, SKY, weapon.weapon_name, equippable=equippable_component, weapon=weapon_component)
def add_thing(self, thing: Entity, x: float, y: float, size: Tuple[float, float], collision_type=None, categories=None, mass: float = 1, friction: float = 1): """Adds a thing to the game world centred at the position ('x', 'y') Parameters: thing (Entity): The entity to add to the game world x (float): The x-coordinate at which to place the thing y (float): The y-coordinate at which to place the thing size (tuple<float, float>): The (x, y) size of the thing collision_type (int): The collision type of the thing; should be a value of self._collision_types categories (int): The query categories of the thing; should be a bitwise combination of the value of self._physical_thing_categories mass (float): The mass of the thing friction (float): The friction of the thing """ width, height = size left = -width // 2 right = left + width top = -height // 2 bottom = top + height body = pymunk.Body(mass, pymunk.inf) body.position = x, y shape = pymunk.Poly(body, [(left, top), (left, bottom), (right, bottom), (right, top)]) shape.object = thing if collision_type is not None: shape.collision_type = collision_type if categories is not None: shape.filter = pymunk.ShapeFilter(categories=categories) shape.friction = friction thing.set_shape(shape) self._space.add(body, shape)
def new_instance(self, boost): multiplier = LEVEL_MULTIPLIER[boost] nickname = NICKNAMES[boost] hmsmead = [0.0 for _ in range(DEX + 1)] for stat in range(DEX + 1): hmsmead[stat] = uniform(self._min_hsmead[stat] * multiplier, self._max_hsmead[stat] * multiplier) entity = Entity(self._id, f'{nickname}{self._name}', self._skel_path, hmsmead[HEALTH], 0, hmsmead[STR], hmsmead[MAG], hmsmead[END], hmsmead[STR], hmsmead[MAG], hmsmead[END], hmsmead[AGI], hmsmead[DEX], self._element, self._skills) return BattleEnemy(entity, boost, self._skill_probabilities, self._sub_element, self._id + str(randint(0, 1000000)))
def create_monster(self, monster_name, x, y): self.db = get_transaction() _monster: Monsters = self.db.query(Monsters).filter( Monsters.monster_name == monster_name).one() ai_component = BasicMonster() ability_component = Ability(strenght=_monster.strenght, dexterity=_monster.dexterity, constitution=_monster.constitution, intelligence=_monster.intelligence, wisdom=_monster.wisdom, charisma=_monster.charisma) playable_component = Playable(hp=_monster.hit_points, ac=_monster.armor_class, xp=_monster.exp) inventory_component = Inventory(2) equipment_component = Equipment() monster = Entity(x, y, DESATURED_GREEN, _monster.monster_name, blocks=True, render_order=RenderOrder.ACTOR, playable=playable_component, ai=ai_component, inventory=inventory_component, equipment=equipment_component, ability=ability_component, image_name="{}_right".format( _monster.monster_name.lower())) if len(_monster.monsters_weapons) > 0: random = randint(0, 100) _weapon = None if random > 75: _weapon = self.weapon_factory.create_weapon( _monster.monsters_weapons[1]) else: _weapon = self.weapon_factory.create_weapon( _monster.monsters_weapons[0]) if _weapon != None: monster.inventory.add_item(_weapon) monster.equipment.toggle_equip_main_hand(_weapon) return monster
def setupPlayer(self, startLocation): self.mainCharacter = Entity(PlayerInputComponent(self.keys), PhysicsComponent(self), GraphicsComponent(self.view, "test.png")) self.mainCharacter.x = startLocation.x self.mainCharacter.y = self.window.height - startLocation.y - startLocation.height + 96 self.mainCharacter.width = 32 self.mainCharacter.height = 32
def create_unknown(world: World, entity_id: str, x: int, y: int, *args): """Create an unknown entity.""" world.add_thing(Entity(), x * BLOCK_SIZE, y * BLOCK_SIZE, size=(BLOCK_SIZE, BLOCK_SIZE))
def remove_thing(self, thing: Entity): """Removes a thing from the world""" self._space.remove(thing.get_shape())
import pygame from game.level import Level from game.entity import Entity from game.exitzone import ExitZone from game.pokezone import PokeZone pygame.init() # level selection # TREE WIDTH = 65 + 5, 75 + 5 level_list = { "hometown": Level( pygame.image.load("./assets/bgs/hometown.png"), "./assets/sounds/music/pallet_town.mp3", [ Entity([0, 0], [430, 75]), # level walls Entity([0, 0], [220, 720]), Entity([0, 560], [1080, 720]), Entity([1080 - 224, 0], [224, 720]), Entity([1080 - 434, 0], [434, 75]), Entity([315, 90], [140, 80]), # topleft house Entity([315, 90], [35, 110]), Entity([400, 90], [55, 110]), Entity([620, 90], [140, 80]), # topright house Entity([620, 90], [35, 110]), Entity([705, 90], [55, 110]), Entity([620, 310], [140, 80]), # bottomright house Entity([620, 310], [35, 110]), Entity([705, 310], [55, 110]), Entity([300, 310], [180, 80]), # oakslab Entity([300, 310], [65, 110]),
def set_player(self, name="Player", weapon_main=None, weapon_off=None, armor=None, items=None, race=None, class_name=None, strenght=None, dexterity=None, constitution=None, intelligence=None, wisdom=None, charisma=None): db = get_transaction() db.query() ability_component = Ability(strenght=strenght, dexterity=dexterity, constitution=constitution, intelligence=intelligence, wisdom=wisdom, charisma=charisma) hit_points = 1 armor_class = 1 preficientcies_component = None if class_name != None: _class: Classes = db.query(Classes).filter( Classes.class_name == class_name).one() hit_points = 8 + ability_component.modifaier_constitution preficientcies_component = Preficiencies( weapons=_class.preficiencies_weapons) if armor != None: _armor = db.query(Armors).filter(Armors.armor_name == armor).one() armor_class = _armor.armor_class + (ability_component.dexterity * _armor.dex_modifier) playable_component = Playable(hp=hit_points, ac=armor_class, xp=0) inventory_component = Inventory(26) level_component = Level() equipment_component = Equipment() self.temp_player = Entity(int(self.width / 2), int(self.height / 2), BLUE, name, blocks=True, render_order=RenderOrder.ACTOR, playable=playable_component, inventory=inventory_component, level=level_component, equipment=equipment_component, ability=ability_component, preficiencies=preficientcies_component, image_name="player_left") if weapon_main: w_main = db.query(Weapons).filter( Weapons.weapon_name == weapon_main).one() item = self.weapon_factory.create_weapon(w_main) self.temp_player.inventory.add_item(item) self.temp_player.equipment.toggle_equip_main_hand(item) if weapon_off: w_off = db.query(Weapons).filter( Weapons.weapon_name == weapon_off).one() item = self.weapon_factory.create_weapon(w_main) self.temp_player.inventory.add_item(item) self.temp_player.equipment.toggle_equip_off_hand(item)
def make_map(self, max_rooms, room_min_size, room_max_size, player, entities): rooms = [] num_rooms = 0 center_of_last_room_x = None center_of_last_room_y = None for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, self.map_width - w - 1) y = randint(0, self.map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: #did not break, means no intersect self.create_room(new_room) (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.place_entities(new_room, entities) rooms.append(new_room) num_rooms += 1 for x in self.tiles: for y in x: y.check_neighbors(self.tiles) for x in self.tiles: for y in x: y.set_wall_type() y.clean_neighbors() stairs_component = Stairs(self.dungeon_level + 1) down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, STAIRS, "Stairs", render_order=RenderOrder.STAIRS, stairs=stairs_component, image_name='flore_stars') entities.append(down_stairs)
def place_entities(self, room, entities): db = get_transaction() max_monsers_per_room = from_dungeon_level([[2, 1], [3, 4], [5, 6]], self.dungeon_level) max_items_per_room = from_dungeon_level([[1, 1], [2, 4]], self.dungeon_level) number_of_monsters = randint(0, max_monsers_per_room) number_of_items = randint(0, max_items_per_room) monster_factory = MonsterFactory() monster_chances = {} _monsters = db.query(MonstersChances).filter( MonstersChances.id_dungeon_level <= self.dungeon_level) for _monster in _monsters: monster_chances[_monster.monster.monster_name] = _monster.chances item_chances = { "healing_potion": 70, "lightinh_scroll": from_dungeon_level([[25, 4]], self.dungeon_level), "fireball_scroll": from_dungeon_level([[25, 6]], self.dungeon_level), "confusion_scroll": from_dungeon_level([[26, 2]], self.dungeon_level), "sword": from_dungeon_level([[5, 4]], self.dungeon_level), "shield": from_dungeon_level([[15, 8]], self.dungeon_level) } for i in range(number_of_monsters): x = randint(room.x1 + 1, room.x2 - 1) y = randint(room.y1 + 1, room.y2 - 1) if not any([ entity for entity in entities if entity.x == x and entity.y == y ]): monster_choice = random_choice_from_dict(monster_chances) entities.append( monster_factory.create_monster(monster_choice, x, y)) for i in range(number_of_items): x = randint(room.x1 + 1, room.x2 - 1) y = randint(room.y1 + 1, room.y2 - 1) if not any([ entity for entity in entities if entity.x == x and entity.y == y ]): item_choice = random_choice_from_dict(item_chances) if item_choice == "healing_potion": item_component = Item(use_funtion=heal, amount=3) item = Entity(x, y, VIOLET, "Healing Potion", render_order=RenderOrder.ITEM, item=item_component, image_name="potion") elif item_choice == "fireball_scroll": item_component = Item( use_funtion=cast_fireball, targeting=True, targeting_message=Message( "Left-click a target tile for the fireball, or right-click to cancel", libtcod.light_cyan), damage=25, radius=3) item = Entity(x, y, RED, "Fireball Scroll", render_order=RenderOrder.ITEM, item=item_component) elif item_choice == "confusion_scroll": item_component = Item( use_funtion=cast_confuse, targeting=True, targeting_message=Message( "Left-click an enemy to confuse it, or right-click to cancel", libtcod.light_cyan), damage=25, radius=3) item = Entity(x, y, ORANGE, "Confuion Scroll", render_order=RenderOrder.ITEM, item=item_component) elif item_choice == "sword": equippable_component = Equippable(EQUIPMENT_SLOTS.WEAPONS, power_bonus=3) weapon_component = Weapon(3, 5, 20, WEAPON_TYPE.SHORTSWORD) item = Entity(x, y, SKY, "Sword", render_order=RenderOrder.ITEM, equippable=equippable_component, weapon=weapon_component) elif item_choice == "shield": equippable_component = Equippable(EQUIPMENT_SLOTS.WEAPONS, defense_bonus=1) weapon_component = Weapon(0, 1, 50, WEAPON_TYPE.SHIELD) item = Entity(x, y, ORANGE, "Shield", render_order=RenderOrder.ITEM, equippable=equippable_component, weapon=weapon_component) else: item_component = Item(use_funtion=cast_lightning, damage=20, maximum_range=5) item = Entity(x, y, YELLOW, "Lightning Scroll", render_order=RenderOrder.ITEM, item=item_component) entities.append(item)