public static void createTrigger( PooledEngine engine, PhysixSystem physixSystem, float x, float y, float width, float height, Consumer<Entity> consumer) { Entity entity = engine.createEntity(); PhysixModifierComponent modifyComponent = engine.createComponent(PhysixModifierComponent.class); entity.add(modifyComponent); TriggerComponent triggerComponent = engine.createComponent(TriggerComponent.class); triggerComponent.consumer = consumer; entity.add(triggerComponent); modifyComponent.schedule( () -> { PhysixBodyComponent bodyComponent = engine.createComponent(PhysixBodyComponent.class); PhysixBodyDef bodyDef = new PhysixBodyDef(BodyDef.BodyType.StaticBody, physixSystem) .position(x + width / 2, y + height / 2); bodyComponent.init(bodyDef, physixSystem, entity); PhysixFixtureDef fixtureDef = new PhysixFixtureDef(physixSystem).sensor(true).shapeBox(width, height); bodyComponent.createFixture(fixtureDef); entity.add(bodyComponent); }); engine.addEntity(entity); }
public static Entity makePlayerControlled(Entity entity) { entity.add(GameManager.getGame().engine.createComponent(PlayerOwnedComponent.class)); entity.add(GameManager.getGame().engine.createComponent(PlayerControllerComponent.class)); entity.add(GameManager.getGame().engine.createComponent(JoystickControllerComponent.class)); return entity; }
private static Entity createBaseTile(Level level, int x, int y, Color color, boolean isWall) { x *= Tiles.SIZE; y *= Tiles.SIZE; final Entity tile = createWithPosition(level, x, y); tile.add(level.createComponent(MiniMapObject.class).setColor(color)); if (isWall) { tile.add(new BoundingBox().set(x, y, Tiles.SIZE)); } return tile; }
public static Entity createPlayer(float x, float y, float width, float height) { Entity entity = createBasicMovingEntity(x, y, width, height); makePlayerControlled(entity); makeVisual(entity, width, height, "player_ship1.png"); entity.add(GameManager.getGame().engine.createComponent(ShootingComponent.class)); entity.add(GameManager.getGame().engine.createComponent(CameraFocusComponent.class)); GameManager.getGame().engine.addEntity(entity); return entity; }
// TODO: Refactor into multiple functions @Override public Entity buildEntity() { Entity player = new Entity(); // Define the body BodyDef playerBody = new BodyDef(); playerBody.type = BodyDef.BodyType.DynamicBody; playerBody.position.set(0, 0); // Create the components PhysicsComponent p = new PhysicsComponent(physicsWorld.createBody(playerBody), player); RenderComponent r = new RenderComponent(); KeyboardInputComponent i = new KeyboardInputComponent(); MoveAction m = new MoveAction(); // Properties for the components Polygon sprite = new Polygon( new float[] { 0, 5, 3, -3, 0, -2, -3, -3 }); PolygonShape fixShape = new PolygonShape(); fixShape.set(sprite.getVertices()); FixtureDef def = new FixtureDef(); def.shape = fixShape; def.density = 1.2f; def.friction = 0f; def.restitution = 0.1f; // Apply properties p.physicsBody.createFixture(def); r.renderColor = Color.BLUE; r.sprite = sprite; m.lin_v = 5f; m.rot_v = 2f; // Add to player player.add(p); player.add(r); player.add(i); player.add(m); return player; }
public static Entity createPlayer(Level level, TextureRegion[][] spriteSheet) { final Entity player = level.createEntity(); player.add( level .createComponent(Position.class) .set(level.getNextSpawnLocation())); // TODO: This can return null! player.add(level.createComponent(Velocity.class).reset()); player.add( level .createComponent(Health.class) .reset(20)); // TODO change default based on selected character player.add(level.createComponent(Sprite.class).set(spriteSheet[0][0])); return player; }
public void addContact(float delta, Contact contact) { for (Entity e : entities) { if (contact.getFixtureA() == bm.get(e).body.getFixtureList().first()) { for (Entity player : players) { if (contact.getFixtureB() == sensorm.get(player).sensor) { e.add(new CollisionComponent(player.getId(), contact.getWorldManifold())); player.add(new CollisionComponent(e.getId(), contact.getWorldManifold())); System.out.println("hit a player"); return; } } } } }
public static Entity makeHoming(Entity entity, Entity homingTarget) { HomingComponent homing = GameManager.getGame().engine.createComponent(HomingComponent.class); homing.target = homingTarget; entity.add(homing); return entity; }
@Override public void run( Entity entity, SafeProperties meta, SafeProperties properties, EntityFactoryParam param) { PositionComponent component = engine.createComponent(PositionComponent.class); component.x = param.x; component.y = param.y; entity.add(component); }
public static Entity createMap() { Entity map = GameManager.getGame().engine.createEntity(); MapComponent mapComponent = GameManager.getGame().engine.createComponent(MapComponent.class); mapComponent.init(256, 256); mapComponent.rectangle.x = -128; mapComponent.rectangle.y = -128; map.add(mapComponent); GameManager.getGame().engine.addEntity(map); return map; }
public static Entity makeVisual(Entity entity, float width, float height, String image) { VisualComponent visual = GameManager.getGame().engine.createComponent(VisualComponent.class); final Texture texture = new Texture(Gdx.files.internal(image != null ? image : images.random())); visual.region = new TextureRegion(texture, texture.getWidth(), texture.getHeight()); visual.width = width; visual.height = height; entity.add(visual); return entity; }
public static Entity createEntity(PooledEngine engine, String name, float x, float y, Team team) { factoryParam.x = x; factoryParam.y = y; factoryParam.team = team; Entity entity = entityFactory.createEntity(name, factoryParam); SetupComponent setup = engine.createComponent(SetupComponent.class); setup.name = name; setup.team = team; entity.add(setup); engine.addEntity(entity); return entity; }
public static Entity createBullet(float x, float y, float width, float height) { Entity bullet = createBasicMovingEntity(x, y, width, height); makeVisual(bullet, width, height, null); bullet.add(GameManager.getGame().engine.createComponent(DamageOnCollisionComponent.class)); // // bullet.add(GameManager.getGame().engine.createComponent(DestroyOnMapCollisionComponent.class)); GameManager.getGame().engine.addEntity(bullet); return bullet; }
@Override public void run( Entity entity, SafeProperties meta, SafeProperties properties, EntityFactoryParam param) { if (param.game instanceof GameLocal) { EatableComponent component = engine.createComponent(EatableComponent.class); component.energy = properties.getFloat("energy", 0); component.sound = properties.getString("sound"); String powerupName = properties.getString("powerup"); if (powerupName != null) { component.powerup = powerupSystem.createPowerup(powerupName); } entity.add(component); } }
public void init(final Area area) { /* Dialogues */ DialogueComponent dc = DialogueComponent.read("scroll"); dc.events.put( "end", new Runnable() { @Override public void run() { Event addAttack = new Event() { public void run(Context context) { Attack attack = new Attack(Attack.prototypes.get("nmScroll")); Player.addInventory(attack); Player.addAttack(attack); Player.addAttribute("nmScroll"); Player.save((Area) context); } }; Event hideScroll = new SetEntityVisibility("scroll", false); area.events.add(addAttack); area.events.add(hideScroll); } }); /* Entities */ Entity scroll = new Entity(); scroll.add(new NameComponent("scroll")); scroll.add(new AreaComponent(area)); scroll.add(new ActorComponent(new Image(Main.getTexture("scroll")))); scroll.add(new PositionComponent(5 * Constants.TILE_SIZE, 5 * Constants.TILE_SIZE)); if (!Player.getAttribute("nmScroll")) scroll.add(new VisibleComponent()); CollisionComponent cc = new CollisionComponent(0, 0, 16, 8); cc.events.add(new StartDialogue(dc)); scroll.add(cc); Entity leave = new Entity(); EnterZoneComponent ec = new EnterZoneComponent(area); ec.bounds.set(0, -Constants.TILE_SIZE, mapSize.x * Constants.TILE_SIZE, Constants.TILE_SIZE); ec.events.add( new ChangeContext( "puzzle1", new Vector2(17.5f * Constants.TILE_SIZE, 34 * Constants.TILE_SIZE), new Vector2(17.5f * Constants.TILE_SIZE, 32 * Constants.TILE_SIZE))); leave.add(ec); /* Adding things to Area */ entities = new Entity[] {scroll, leave}; new ParticleEffectActor.EnvironmentParticleEffect("scroll", area); }
public static Entity createChainLight( PooledEngine engine, float x, float y, Color color, float distance, int rayDirection, ArrayList<Point> points) { Entity e = engine.createEntity(); PositionComponent pc = engine.createComponent(PositionComponent.class); pc.x = x; pc.y = y; ChainLightComponent clc = engine.createComponent(ChainLightComponent.class); float chain[] = new float[points.size() * 2]; int i = 0; for (Point point : points) { chain[i++] = (x + point.x) / GameConstants.BOX2D_SCALE; chain[i++] = (y + point.y) / GameConstants.BOX2D_SCALE; } final RayHandler rayHandler = engine.getSystem(LightRenderSystem.class).getRayHandler(); clc.chainLight = new ChainLight( rayHandler, GameConstants.LIGHT_RAYS, color, distance / GameConstants.BOX2D_SCALE, rayDirection, chain); clc.chainLight.setStaticLight(false); e.add(pc); e.add(clc); engine.addEntity(e); return e; }
private Entity buildPuffin(World world) { Entity e = engine.createEntity(); e.add(new PuffinComponent()); AnimationComponent a = new AnimationComponent(); a.animations.put( "DEFAULT", new Animation(1f / 16f, Assets.getPuffinArray(), Animation.PlayMode.LOOP)); a.animations.put( "RUNNING", new Animation(1f / 16f, Assets.getPuffinRunArray(), Animation.PlayMode.LOOP)); e.add(a); StateComponent state = new StateComponent(); state.set("DEFAULT"); e.add(state); TextureComponent tc = new TextureComponent(); e.add(tc); TransformComponent tfc = new TransformComponent(); tfc.position.set(10f, 10f, 1f); tfc.rotation = 15f; tfc.scale.set(0.25f, 0.25f); e.add(tfc); BodyComponent bc = new BodyComponent(); BodyDef bodyDef = new BodyDef(); // We set our body to dynamic, for something like ground which doesn't move we would set it to // StaticBody bodyDef.type = BodyDef.BodyType.DynamicBody; // Set our body's starting position in the world bodyDef.position.set(10f, 23f); // Create our body in the world using our body definition bc.body = world.createBody(bodyDef); bc.body.applyAngularImpulse(50f, true); // Create a circle shape and set its radius to 6 CircleShape circle = new CircleShape(); circle.setRadius(2f); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = circle; fixtureDef.density = 20f; fixtureDef.friction = 0.4f; fixtureDef.restitution = 0.6f; // Make it bounce a little bit // Create our fixture and attach it to the body bc.body.createFixture(fixtureDef); // Remember to dispose of any shapes after you're done with them! // BodyDef and FixtureDef don't need disposing, but shapes do. circle.dispose(); e.add(bc); return e; }
public static Entity createBasicMovingEntity(float x, float y, float width, float height) { Entity entity = GameManager.getGame().engine.createEntity(); PositionComponent position = GameManager.getGame().engine.createComponent(PositionComponent.class); position.x = x; position.y = y; entity.add(position); CollisionComponent collision = GameManager.getGame().engine.createComponent(CollisionComponent.class); collision.init(width, height); entity.add(collision); entity.add(GameManager.getGame().engine.createComponent(AccelerationComponent.class)); entity.add(GameManager.getGame().engine.createComponent(DragComponent.class)); entity.add(GameManager.getGame().engine.createComponent(VelocityComponent.class)); entity.add(GameManager.getGame().engine.createComponent(AngleComponent.class)); entity.add(GameManager.getGame().engine.createComponent(PropelComponent.class)); entity.add( GameManager.getGame().engine.createComponent(StopMovementOnCollisionComponent.class)); return entity; }
private void processPlayerBeginCollision(Entity playerEntity, Entity otherEntity) { TypeComponent typeComponent = ComponentMappers.TYPE.get(otherEntity); CapsuleControlComponent capsuleControlComponent = ComponentMappers.CAPSULE_CONTROL.get(playerEntity); StateComponent stateComponent = ComponentMappers.STATE.get(otherEntity); if (typeComponent.type == GameObjectType.TYPE_SAFE_CAPSULE && capsuleControlComponent == null && stateComponent.state == States.STATE_FREE) { stateComponent.state = States.STATE_BUSY; Body playerBody = ComponentMappers.PHYSIC.get(playerEntity).body; Body safeCapsuleBody = ComponentMappers.PHYSIC.get(otherEntity).body; playerBody.getFixtureList().get(0).setSensor(true); Vector2 posDiff = playerBody.getWorldCenter().sub(playerBody.getPosition()).cpy(); playerBody.setTransform(safeCapsuleBody.getWorldCenter().sub(posDiff), playerBody.getAngle()); WeldJointDef jointDef = new WeldJointDef(); jointDef.initialize(playerBody, safeCapsuleBody, safeCapsuleBody.getWorldCenter()); safeCapsuleBody.getWorld().createJoint(jointDef); JetpackControlComponent jetpackControlComponent = ComponentMappers.JETPACK.get(playerEntity); jetpackControlComponent.isActive = false; TurnControlComponent turnControlComponent = ComponentMappers.CONTROL.get(playerEntity); turnControlComponent.isActive = false; PooledEngine engine = (PooledEngine) this.getEngine(); playerEntity.add( engine .createComponent(CapsuleControlComponent.class) .init(jetpackControlComponent.jetpackActivateKey)); } if (typeComponent.type == GameObjectType.TYPE_ASTEROID) { Body playerBody = ComponentMappers.PHYSIC.get(playerEntity).body; if (!playerBody.getFixtureList().get(0).isSensor()) { OxygenComponent oxygenComponent = ComponentMappers.OXYGEN.get(playerEntity); if (oxygenComponent != null) oxygenComponent.addModificator(OxygenModificator.createInstantAsteroidModificator()); } } }
private Entity buildFloorEntity(World world) { Entity e = engine.createEntity(); Vector2 screenMeters = RenderingSystem.getScreenSizeInMeters(); Gdx.app.log("Splash Screen", "Screen Meters:" + screenMeters.x + " x " + screenMeters.y); Vector2 screenPixels = RenderingSystem.getScreenSizeInPixesl(); Gdx.app.log("Splash Screen", "Screen Pixels:" + screenPixels.x + " x " + screenPixels.y); // Create our body definition BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; // Set its world position groundBodyDef.position.set(new Vector2(6f, 1f)); groundBodyDef.angle = 45f; BodyComponent bc = new BodyComponent(); // Create a body from the defintion and add it to the world bc.body = world.createBody(groundBodyDef); // Create a polygon shape PolygonShape groundBox = new PolygonShape(); // Set the polygon shape as a box which is twice the size of our view port and 20 high // (setAsBox takes half-width and half-height as arguments) groundBox.setAsBox(5f, 1f); // Create a fixture from our polygon shape and add it to our ground body bc.body.createFixture(groundBox, 0.0f); // Clean up after ourselves groundBox.dispose(); e.add(bc); return e; }
public static Entity createTile( Level level, int x, int y, Color color, TextureRegion texture, boolean isWall) { final Entity tile = createBaseTile(level, x, y, color, isWall); tile.add(level.createComponent(Sprite.class).set(texture)); return tile; }
public static Entity createWithPosition(Level level, int x, int y) { final Entity tile = level.createEntity(); tile.add(level.createComponent(Position.class).set(x, y)); return tile; }