def __init__(self, uid: str, width, height, world_width: int, world_height: int, start_pos): super().__init__(uid, width, height) keyl = runners.KeyListener() # adding pause button keyl.add_key(key=32, func=func.toggle_pause) # restart on F10 keyl.add_key(key=299, func=func.restart) self.add_runner(keyl) # self.keyl.addKey(key=264, func = checkWarp) main = Entity(tag='main') #main.camera = camera.OrthoCamera(world_width=world_width * vars.tile_size, # world_height=world_height * vars.tile_size, # cam_width=width, cam_height=height, viewport=[0, 0, width, height], # tag='maincam') main.camera = camera.PerspectiveCamera(viewport=[0, 0, width, height], tag='maincam') main.camera.pos = (0, 1, 0) self.main = main self.add(main) # create the collision engine ce = runners.CollisionEngine(85, 85) self.add_runner(ce) self.add_runner(runners.Scheduler()) self.dw = runners.DynamicWorld(256, 256, 'maincam') self.add_runner(self.dw) diag = Entity(tag='diag') diag.camera = camera.OrthoCamera(world_width=width, world_height=height, cam_width=width, cam_height=height, viewport=[0, 0, width, height], tag='diagcam') fps_count = Text('main', 8, '0', [255, 255, 255, 255], align=TextAlignment.top_left, tag='fps', pos=(0, 256, 2)) fps_count.add_component(comp.FPSCounter()) diag.add(fps_count) self.add(diag) e = Entity() e.add_component(Road()) e.add_component(comp.KeyInput()) main.add(e)
def create_player(self): pos_x = 200 pos_y = 200 tag = PlayerObject() position = PositionComponent(pos_x, pos_y) velocity = VelocityComponent() health = HealthComponent() canfly = FlyingComponent() sprite = SpriteComponent((20, 20), pg.Color("blue")) player = Entity(self) player.add_component("sprite", sprite) player.image = player.components["sprite"].image player.rect = player.components["sprite"].image.get_rect() player.add_component("position", position) player.add_component("velocity", velocity) player.add_component("health", health) player.add_component("player", tag) player.add_component("can_fly", canfly) self.entities.add(player)
def create_enemy(self): pos_x = random.randint(1, 700) pos_y = random.randint(100, 400) position = PositionComponent(pos_x, pos_y) velocity = VelocityComponent() health = HealthComponent() sprite = SpriteComponent((10, 10), pg.Color("red")) enemy = Entity(self) enemy.add_component("sprite", sprite) enemy.image = enemy.components["sprite"].image enemy.rect = enemy.components["sprite"].image.get_rect() enemy.add_component("position", position) enemy.add_component("velocity", velocity) enemy.add_component("health", health) self.entities.add(enemy)
def create_entity(self, entity_type, pos=[0, 0], rot=0, vel=[0, 0], avel=0, radius=0, player_id=-1): """Create an entity of a specific type and with specific settings""" try: e = self.unused_entities.pop() self.log('Reusing entity') e.initialize(pos, rot, vel, avel, radius, self.dispatch.get_id(), entity_type) except IndexError: self.log('No unused entities free, creating a new one') e = Entity(pos, rot, vel, avel, radius, self.dispatch.get_id(), entity_type) # Construct some of the data we'll send to clients about this new entity msg_content = [(MSGCONTENT.ENTITY_ID, e.entity_id), (MSGCONTENT.ENTITY_TYPE, e.entity_type), (MSGCONTENT.X_POS, e.position[0]), (MSGCONTENT.Y_POS, e.position[1]), (MSGCONTENT.ROTATION, e.rotation)] # Asteroids if (entity_type == GAME.ENTITY_ASTEROID_BIG or entity_type == GAME.ENTITY_ASTEROID_MED or entity_type == GAME.ENTITY_ASTEROID_SMALL): if entity_type == GAME.ENTITY_ASTEROID_BIG: # These radii are roughly half the width/height of the sprite that represents # the entity e.radius = 58 elif entity_type == GAME.ENTITY_ASTEROID_MED: e.radius = 21 elif entity_type == GAME.ENTITY_ASTEROID_SMALL: e.radius = 14 e.add_component(components.AsteroidComponent()) self.asteroids.append(e) # Bullets fired from a ship elif entity_type == GAME.ENTITY_BULLET: e.add_component(components.BulletComponent()) e.radius = 5 e.lifetime = 1 # player_id_and_addr = self.dispatch.concat_id_and_addr(player_id, player_addr) self.bullets[player_id].append(e) # Explosion that spawns after an asteroid or the player blows up elif entity_type == GAME.ENTITY_EXPLOSION: e.add_component(components.ExplosionComponent(0.5)) # Player ship elif entity_type == GAME.ENTITY_PLAYERSHIP: e.add_component(components.PlayerComponent()) e.radius = 30 msg_content.append((MSGCONTENT.PLAYER_ID, player_id)) e.player_id = player_id e.visible = True e.active = True self.entities[e.entity_id] = e self.send_global_msg(MESSAGES.CREATE_ENTITY, *msg_content) return e
def add_entities(self): base_stats = Stats("stats", 100, 80, 5, 6, 7, 10) monster_stats = Stats("stats", 50, 50, 3, 5, 5, 5) player_x, player_y = self.world.empty_location(0) player = Entity(player_x, player_y, 0, '@', "player") player.add_component(MovementController("controller")) player.add_component(PlayerAi("ai")) player.add_component(copy.deepcopy(base_stats)) player.add_component(FovComponent("fov")) player.add_component(AttackComponent("attack", 15)) player.add_component(DestructibleComponent("destructible", 8)) player.add_component(Inventory("inventory", 10)) self.world.add_entity(player, is_player=True) # must be done in this order! player.get_component("fov").initialize() monster_count = 4 for x in xrange(0, monster_count): m = Entity(random.randint(0, self.game.screen_width), random.randint(0, self.game.screen_height), 0, 'm', 'monster', lbt.yellow) m.add_component(MovementController("controller")) m.add_component(SimpleFollowAi("ai")) m.add_component(copy.deepcopy(monster_stats)) m.add_component(AttackComponent("attack", 10)) m.add_component(DestructibleComponent("destructible", 10)) self.world.add_entity(m)
def create_test_scene(self): self.loader = mesh_loader.MeshLoader(self) self.mesh_pool = [] self.mesh_pool.append( self.loader.get_meshs("data/models/objs/cube.obj")) self.mesh_pool.append( self.loader.get_meshs("data/models/objs/ground_box.obj")) #self.mesh_pool.append(self.loader.get_meshs("data/models/iqms/mrfixit/mrfixit.iqm")) ent = Entity(self) ent.add_component(mesh_renderer.MeshRenderer(self.mesh_pool[0])) ent.add_component(mesh_debug_renderer.MeshDebugRenderer()) b_size = self.mesh_pool[0][0].get_aabb().get_size() ent.add_component( rigid_body.RigidBody( box_collision_shape.BoxCollisionShape(b_size.x, b_size.y, b_size.z))) self.entities.append(ent) ent2 = Entity(self) ent2.add_component(mesh_renderer.MeshRenderer(self.mesh_pool[0])) ent2.add_component(mesh_debug_renderer.MeshDebugRenderer()) b_size = self.mesh_pool[0][0].get_aabb().get_size() ent2.add_component( static_body.StaticBody( box_collision_shape.BoxCollisionShape(b_size.x, b_size.y, b_size.z))) self.entities.append(ent2) ent3 = Entity(self) ent3.add_component(mesh_renderer.MeshRenderer(self.mesh_pool[1])) ent3.add_component(mesh_debug_renderer.MeshDebugRenderer()) b_size = self.mesh_pool[1][0].get_aabb().get_size() ent3.add_component( static_body.StaticBody( box_collision_shape.BoxCollisionShape(b_size.x, -b_size.y, b_size.z))) self.entities.append(ent3) ent4 = Entity(self) ent4.add_component( primitive_render.PrimitiveRender( DebugShapes.create_box_shape(b_size.x * 0.5, b_size.y, b_size.z))) self.entities.append(ent4) #for testing we say the mesh is at the origin for now quat = quaternion.Quaternion(vector3.Vector3(0.0, 0.0, -1.0), 3.141 * 0.5).get_axis_quaternion() quat = quat * quaternion.Quaternion(vector3.Vector3(0.0, -1.0, 0.0), 3.141 * 0.5).get_axis_quaternion() self.t1_position = vector3.Vector3(0.0, 5.0, -10.0) self.entities[0].get_transform().set_local_rotation( quaternion.Quaternion.from_axis(vector3.Vector3(), 1.0)) self.entities[0].get_transform().set_local_position(self.t1_position) self.entities[1].get_transform().set_local_rotation( quat.get_normalized()) self.entities[1].get_transform().set_local_position( vector3.Vector3(5.0, -5.0, -10.0)) self.entities[0].get_component("MeshRenderer").play_animation( "idle", 1.0) self.entities[1].get_component("MeshRenderer").play_animation( "idle", 1.0) self.entities[2].get_transform().set_local_position( vector3.Vector3(0.0, -8, 0.0)) self.entities[2].get_component( "MeshRenderer").get_materials()[0].assign_material( "mesh_color", [0.5, 0.5, 0.5, 1.0]) self.entities[3].get_transform().set_local_position( vector3.Vector3(0.0, -8, 0.0))