def test_add_entity__full__returns_False(): em = EntityManager(capacity=1) e = Entity(name="fleeb", item="item") f = Entity(name="floop", item="item") em.add_entity(e) assert em.is_full() assert em.add_entity(f) is False
def get_random_enemy(): global enemy_types idx = random.randint(0, len(enemy_types) - 1) etype = enemy_types[idx] if etype == enemy_types[0]: return Entity(0, 0, 'G', libtcod.yellow, con, "enemy", etype, [], { "health": 25, "damage": 5, "defense": 0.0, "nourishment": 100 }) elif etype == enemy_types[1]: return Entity(0, 0, 'N', libtcod.white, con, "enemy", etype, [], { "health": 25, "damage": 5, "defense": 0.0, "nourishment": 100 }) elif etype == enemy_types[2]: return Entity(0, 0, 'B', libtcod.orange, con, "enemy", etype, [], { "health": 25, "damage": 5, "defense": 0.0, "nourishment": 100 }) elif etype == enemy_types[3]: return Entity(0, 0, 'L', libtcod.cyan, con, "enemy", etype, [], { "health": 25, "damage": 5, "defense": 0.0, "nourishment": 100 })
def test_add_entities__iterable(em): e = Entity(name="fleeb") f = Entity(name="fleeb2", x=0) list_of_entities = [e, f] em.add_entities(*list_of_entities) assert e in em.entities assert f in em.entities
def test_create_unhappy_paths(self): # Most things that go wrong here are type mismatches (which are # handled by explicit typing) and invalid values of size with self.assertRaises(Exception): test1 = Entity(size=34, sigil=Sigil("x")) # Size > 10 with self.assertRaises(Exception): test2 = Entity(size=-2, sigil=Sigil("z")) # Size < 0
def __init__(self, game): super().__init__(game) self.name = 'Play Scene' self.tag = 'Play Scene' self.entities_manager = EntitiesManager() self.data = Data() self.player = Entity() self.wall2 = Entity() self.sokoban_spritesheet = load_sprite_data( '*****@*****.**') self.all_sprites = pygame.sprite.Group() self.camera = Camera(self.data.play_map.width, self.data.play_map.height)
def get_game_variables(constants): fighter_component = Fighter(hp=100, defense=1, power=4) inventory_component = Inventory(26) level_component = Level() player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component) entities = [player] game_map = GameMap(constants['map_width'], constants['map_height']) game_map.make_map(constants['max_rooms'], constants['room_min_size'], constants['room_max_size'], constants['map_width'], constants['map_height'], player, entities) message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) game_state = GameStates.PLAYERS_TURN return player, entities, game_map, message_log, game_state
def setUp(self): self.dummy = Entity(name="Test", health=150, mana=70) self.weapon = Weapon(name="Gorehowl", damage=20) self.spell = Spell(name="Pyroblast", damage=40, manaCost=30, castRange=1)
def test_playfield(self): ent = Entity(size=5, sigil=Sigil("4")) pf = PlayField(4, 4) ent.introduce_at(2, 2, pf) assert isinstance(ent.playfield, PlayField) assert ent in pf.entities
def test_passable(self): """Test getter and setter for passable""" ent = Entity(size=6, sigil=Sigil("r")) assert ent.passable # Entities are passable by default ent.passable = False assert not ent.passable ent2 = Entity(size=3, sigil=Sigil("q")) pf = PlayField(3, 3) # Test that movement is blocked when destination contains an impassable entity ent.introduce_at(1, 2, pf) ent2.introduce_at(2, 2, pf) ent2.move_to(1, 2) assert ent2.position == (2, 2)
def __init__(self, game): super().__init__(game) self.name = "Test Scene 2" self.tag = "Test Scene 2" self.entities_manager = EntitiesManager() self.entity = Entity() self.data = Data()
def test_drawables(self): ent1 = Static(4, Sigil("X", priority=4)) ent2 = Entity(3, Sigil("y")) ent3 = Entity(3, Sigil("z")) pf = PlayField(3, 3) ent1.introduce_at(1, 1, pf) ent2.introduce_at(1, 1, pf) ent3.introduce_at(2, 0, pf) # Extract the characters from the resulting dicts for comparison chars = [row["character"] for row in pf.drawables()] # The high priority X should render, and not the lower priority y assert "X" in chars assert "y" not in chars
def test_cell(self): """Test getter and setter for parent cell""" ent = Entity(size=5, sigil=Sigil("4")) pf = PlayField(4, 4) ent.introduce_at(2, 3, pf) assert isinstance(ent.cell, Cell) assert ent in ent._parent_cell.contents
def test_sigils(self): # Test that .sigils returns the highest priority sigils in a cell pf = PlayField(4, 4) ent = Entity(4, Sigil("A")) ent2 = Entity(4, Sigil("B")) ent3 = Entity(4, Sigil("C", priority=2)) # Introduce three passable entities, one with a lower priority ent.introduce_at(2, 2, pf) ent2.introduce_at(2, 2, pf) ent3.introduce_at(2, 2, pf) sigs: List[Sigil] = pf.get_cell(2, 2).sigils # Assert that the two highest, but not the one lowest, are returned by .sigil assert ent.sigil in sigs assert ent2.sigil in sigs assert ent3.sigil not in sigs
def __init__(self): self.player = GamePlayer() self.world = Entity(Vector2(0, 0), Vector2(Config.WIDTH, Config.HEIGHT)) self.world.append_child(Flag(Vector2(450, 300), Vector2(2, 2))) self.world.append_child(SwingMeter(Vector2(300, 400), Vector2(100, 10))) self.world.append_child(Golfer(Vector2(250, 250), Vector2(10, 10)))
def test_init_entity(self): name = 'entity' pos = (3, 2) sprite = 'imgs/dungeon_crawl/monster/angel.png' entity = Entity(name, pos, sprite) self.assertEqual(name, entity.name) self.assertEqual(pos, entity.pos) self.assertEqual('Entity', str(entity)) self.assertTrue(entity.is_on_pos(pos))
def init_el_based_clusters(self, entity_json, cluster_json): """ create Entity instance for each entity and put in self.entities ; create Cluster instance for each external link and put entities with elink to corresponding ta2_clusters; go over ta1 clusters and put every no-elink entity to self.no_link, record siblings' elinks or ta1 cluster uri. :param entity_json: raw info {entity_uri: [name_or_{translation:[tran1,tran2]}, type, external_link], ... } :param cluster_json: raw info {cluster_uri: [[member1, member2], [prototype1]], ... } :return: None """ # init all entities # init ta2 clusters, group by external links(skip 'others') for ent, attr in entity_json.items(): name, _type, link = attr names = self.parse_name(name) _type = _type.rsplit('#', 1)[-1] link = self.parse_link(link) self.entities[ent] = Entity(ent, names, _type, link) if link != OTHERS: if link not in self.ta2_clusters: self.ta2_clusters[link] = Cluster([]) self.ta2_clusters[link].add_member(self.entities[ent]) else: self.no_link[ent] = [set(), set()] ''' now we have: self.entities - dict, each key is an entity uri, each value is the corresponding Entity object self.ta2_clusters - dict, each key is a real external link, each value is the corresponding Cluster object self.no_link - dict, each key is an entity uri, each value is two sets: one to store elinks related to the entity, the other to store the ta1 cluster uri then all the entities are either in ta2_clusters's Clusters, or in no_link's keys ''' # process ta1 clusters for cluster, mems in cluster_json.items(): self.cluster_to_ent[cluster] = set() cur = Cluster([]) members, prototypes = mems cur_no_link = set() for m in members: cur.add_member(self.entities[m]) if self.entities[m].link == OTHERS: cur_no_link.add(m) for m in prototypes: if m in self.entities: cur.add_member(self.entities[m]) if self.entities[m].link == OTHERS: cur_no_link.add(m) for elink in cur.links: if elink == OTHERS: for m in cur_no_link: self.cluster_to_ent[cluster].add(m) self.no_link[m][1].add(cluster) else: for m in cur_no_link: self.cluster_to_ent[cluster].add(m) self.no_link[m][0].add(elink)
def makeBulletWithUUID(self, uuid, x, y, damage, deltaYConstant, ownerUUID): id = uuid entityType = "bullet" health = 1 width = 5 height = 15 velocity = 12 return Entity(id, entityType, health, x, y, width, height, damage, velocity, 0, deltaYConstant=deltaYConstant, ownerUUID=ownerUUID)
def testitem(): e = Entity( x=0, y=0, name="fleepgork", item=ItemComponent(), stackable=StackableComponent(), ) e.item.size = 10 return e
def test_name_format(self): pos = random_pos() sprite = 'imgs/dungeon_crawl/monster/angel.png' name = 'test' entity = Entity(name, pos, sprite) self.assertEqual('Test', str(entity)) name = 'Test' entity = Entity(name, pos, sprite) self.assertEqual('Test', str(entity)) name = 'entity_test' entity = Entity(name, pos, sprite) self.assertEqual('Entity Test', str(entity)) name = '5entity_test_01' entity = Entity(name, pos, sprite) self.assertEqual('Entity Test', str(entity))
def test_passable(self): """Should be passable if no entity in the cell has .passable=False""" ent1 = Entity(4, Sigil("A")) ent2 = Entity(3, Sigil("B")) ent3 = Entity(3, Sigil("C")) pf = PlayField(3, 3) ent1.introduce_at(2, 2, pf) ent2.introduce_at(2, 2, pf) ent3.introduce_at(2, 2, pf) c: Cell = pf.get_cell(2, 2) assert c.passable # Making any of the contents impassible should make the cell impassable ent2.passable = False assert not c.passable c.remove_entity(ent2) assert c.passable
def __init__(self, game): super().__init__(game) self.name = 'Main Menu ' self.tag = 'Main Menu' self.entities_manager = EntitiesManager() self.play_button = Entity() self.data = Data() self.kenny_future_narrow_font = load_font_data( 'Kenney Future Narrow.ttf', 20) self.green_sheet = load_sprite_data('greenSheet.png') self.all_sprites = pygame.sprite.Group()
def get_game_variables(constants): fighter_component = Fighter(hp=100, defense=1, power=2) inventory_component = Inventory(26) level_component = Level() equipment_component = Equipment() player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component, equipment=equipment_component) entities = [player] equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=2) dagger = Entity(0, 0, '-', tcod.sky, 'Dagger', equippable=equippable_component) player.inventory.add_item(dagger) player.equipment.toggle_equip(dagger) game_map = GameMap(constants['map_width'], constants['map_height']) game_map.make_map(constants['max_rooms'], constants['room_min_size'], constants['room_max_size'], constants['map_width'], constants['map_height'], player, entities) message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) game_state = GameStates.PLAYERS_TURN return player, entities, game_map, message_log, game_state
def make_map(): global map map = [[Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) new_room = Rect(x, y, w, h) intersected = False for room in rooms: if new_room.overlap(room): intersected = True break if not intersected: create_room(new_room) (newx, newy) = new_room.center() if num_rooms == 0: player.x = newx player.y = newy else: # Not player spawn! (prevx, prevy) = rooms[num_rooms - 1].center() if libtcod.random_get_int(0, 0, 1) == 1: create_horz_tunnel(prevx, newx, prevy) create_vert_tunnel(prevy, newy, newx) else: create_vert_tunnel(prevy, newy, prevx) create_horz_tunnel(prevx, newx, newy) rooms.append(new_room) num_rooms += 1 stairs_index = random.randint(1, num_rooms - 1) (stairsx, stairsy) = rooms[stairs_index].center() global stairs stairs.clear() del entities[0] stairs = Entity(stairsx, stairsy, '=', libtcod.blue, con, 'object', 'stairs', [], {}) entities.insert(0, stairs) #Generate monsters! for i in range(0, num_rooms): randy = random.randint(1, num_rooms - 1) randMonst = get_random_enemy() (centx, centy) = rooms[randy].center() randMonst.x = centx randMonst.y = centy entities.append(randMonst)
def makeEntityFromJSON(self, json): id = json["id"] entityType = json["entityType"] health = json["health"] x = json["x"] y = json["y"] width = json["width"] height = json["height"] damage = json["damage"] velocity = json["velocity"] shootDelay = json["shootDelay"] return Entity(id, entityType, health, x, y, width, height, damage, velocity, shootDelay)
def makeEnemyWithUUID(self, uuid): id = uuid entityType = "enemy" health = 10 x = randint(0, 860) y = 0 width = 75 height = 60 damage = 1 velocity = 1 shootDelay = 90 return Entity(id, entityType, health, x, y, width, height, damage, velocity, shootDelay, deltaYConstant=1)
def makePlayerWithUUID(self, uuid): id = uuid entityType = "player" health = 10 x = randint(0, 780) y = randint(320, 580) width = 75 height = 55 damage = 3 velocity = 6 shootDelay = 10 return Entity(id, entityType, health, x, y, width, height, damage, velocity, shootDelay)
def kill_weak(self): body_count = 0 for index in range(self.population_size): if random.random() * index > self.die_chance: self.population[index] = Entity("%s_%s" % (self.index + 1, index)) body_count += 1 else: self.population[index].mutate_chromosome() self.population[index].chromosome_score = 0 return body_count
def test_position(self): name = 'test' sprite = 'imgs/dungeon_crawl/monster/angel.png' pos = random_pos() entity = Entity(name, pos, sprite) self.assertTrue(entity.is_on_pos(pos)) self.assertTrue( entity.is_on_pos((pos[0] + rd.randint(0, TILE_SIZE), pos[1] + rd.randint(0, TILE_SIZE)))) self.assertFalse( entity.is_on_pos((pos[0] - rd.randint(0, MAIN_WIN_WIDTH), pos[1]))) self.assertFalse( entity.is_on_pos( (pos[0], pos[1] - rd.randint(0, MAIN_WIN_HEIGHT))))
def test_statics(self): """Create three entities, of which only one is a static should appear alone in .statics.""" ent1 = Static(2, Sigil("A")) ent2 = Mobile(3, Sigil("B")) ent3 = Entity(4, Sigil("C")) pf = PlayField(3, 3) ent1.introduce_at(0, 2, pf) ent2.introduce_at(2, 1, pf) ent3.introduce_at(1, 1, pf) assert ent1 in pf.statics assert ent2 not in pf.statics assert ent3 not in pf.statics with self.assertRaises(Exception): # Reject assignment pf.statics = [ent1]
def test_entities(self): """Tests .entities, which returns a list of all entities in the field.""" e1 = Mobile(4, Sigil("A")) e2 = Entity(4, Sigil("B")) e3 = Static(4, Sigil("C")) pf = PlayField(10, 10) e1.introduce_at(1, 2, pf) e2.introduce_at(5, 3, pf) e3.introduce_at(9, 5, pf) assert e1 in pf.entities assert e2 in pf.entities assert e3 in pf.entities with self.assertRaises(Exception): # Rejects assignment. pf.entities = [e2]