use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.
the class Mariano method create.
@Override
public GameEntity create(Engine engine, Entitas entitas) {
PhysicsManagerGDX physics = engine.getManager(PhysicsManagerGDX.class);
BodyBuilder bodyBuilder = physics.getBodyBuilder();
TextureAtlas textureAtlas = assetsManager.getTextureAtlas(atlas);
ParticleEffect dustEffect = assetsManager.get(effect, ParticleEffect.class);
Array<TextureAtlas.AtlasRegion> heroWalking = textureAtlas.findRegions("Andando");
Array<TextureAtlas.AtlasRegion> heroJump = textureAtlas.findRegions("Saltando");
Array<TextureAtlas.AtlasRegion> heroFall = textureAtlas.findRegions("Cayendo");
Array<TextureAtlas.AtlasRegion> heroIdle = textureAtlas.findRegions("Parado");
Map<String, Animation<TextureRegion>> animationStates = EntitasCollections.createMap(String.class, Animation.class);
animationStates.put("walking", new Animation(0.02f, heroWalking, Animation.PlayMode.LOOP));
animationStates.put("jump", new Animation(0.02f * 7, heroJump, Animation.PlayMode.NORMAL));
animationStates.put("fall", new Animation(0.02f * 5, heroFall, Animation.PlayMode.NORMAL));
animationStates.put("idle", new Animation(0.02f * 4, heroIdle, Animation.PlayMode.LOOP));
Body bodyPlayer = bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder().boxShape(0.35f, 1).density(1)).type(BodyDef.BodyType.DynamicBody).fixedRotation().position(0, 5).mass(1).build();
GameEntity entity = entitas.game.createEntity();
entity.addRigidBody(bodyPlayer).addAnimations(animationStates, animationStates.get("idle"), 0).addTags("Mariano").setInteractive(true).addTextureView(null, new Bounds(0.9f, 1.15f), false, false, 1, 1, Color.WHITE).addInputController((inputManager, context) -> {
boolean isGround = false;
SensorEntity sensor = Indexed.getSensorsEntity(entity, "CollisionGround");
if (sensor.getCollisionSensor().collisionSignal)
isGround = true;
Vector2 impulse = new Vector2();
if (inputManager.isKeyDown(Input.Keys.D)) {
impulse.x = 2;
if (isGround)
entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("walking");
entity.getTextureView().flipX = false;
} else if (inputManager.isKeyDown(Input.Keys.A)) {
impulse.x = -2;
if (isGround)
entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("walking");
entity.getTextureView().flipX = true;
}
if (inputManager.isKeyDown(Input.Keys.W)) {
if (isGround)
impulse.y = 4;
entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("jump");
}
Vector2 vel = bodyPlayer.getLinearVelocity();
if (!inputManager.isKeyDown(Input.Keys.A) && !inputManager.isKeyDown(Input.Keys.D) && isGround) {
bodyPlayer.setLinearVelocity(new Vector2(0, vel.y));
entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("idle");
}
if (Math.abs(vel.x) > 7) {
vel.x = Math.signum(vel.x) * 7;
bodyPlayer.setLinearVelocity(new Vector2(vel.x, vel.y));
}
if (!isGround && vel.y < 0) {
entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("fall");
}
bodyPlayer.applyLinearImpulse(impulse, bodyPlayer.getWorldCenter(), false);
});
entitas.sensor.createEntity().addCollisionSensor("Ground").addLink(entity.getCreationIndex(), "CollisionGround");
entitas.actuator.createEntity().addCameraActuator(((EngineGDX) engine).getCamera(), (short) 0.3f, 0.08f, 6, 1, "Mariano").addLink(entity.getCreationIndex(), "CameraActuator", true);
entitas.actuator.createEntity().addParticleEffectActuator(dustEffect, true, -1, -1).addLink(entity.getCreationIndex(), "EffectActuator", true);
return entity;
}
use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.
the class SplashState method initialize.
@Override
public void initialize() {
// Input
Camera camera = engine.getManager(BaseSceneManager.class).getDefaultCamera();
Batch batch = engine.getManager(BaseSceneManager.class).getBatch();
BitmapFont font = engine.getManager(BaseGUIManager.class).getDefaultFont();
systems.add(new DelaySystem(context.core)).add(new RendererSystem(context.core, engine.sr, camera, batch, font));
Texture texture = assetsManager.getTexture(splash);
context.core.createEntity().addTextureView("Pong", new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()), new Vector2(), 0, Pong.SCREEN_HEIGHT, Pong.SCREEN_WIDTH).addDelay(3);
}
use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.
the class Examples method create.
@Override
public void create() {
engine = new ExamplesEngine();
entitas = new Entitas();
preferencesManager.LOG_LEVEL = LogManager.LOG_DEBUG;
AssetManager assetsManager = new AssetManager(new TestFileHandleResolver());
engine.addManager(new AssetsManagerGDX(assetsManager, preferencesManager));
engine.addManager(new PhysicsManagerGDX(new Vector2(0, -9.8f)));
engine.addManager(new GUIManagerGDX(new ScreenViewport(), new BitmapFont(), engine));
engine.addManager(new SceneManagerGDX(engine, entitas));
engine.addManager(new LogManagerGDX(preferencesManager));
engine.addManager(new InputManagerGDX(entitas, engine));
engine.addManager(preferencesManager);
game = new ExamplesGame(engine, new EventBusGDX(new MBassador()));
game.init();
game.pushState(new PlatformExampleState(engine, entitas));
}
use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.
the class AnimatePositionSystem method execute.
@Override
public void execute(List<GameEntity> entities) {
for (GameEntity e : entities) {
Position pos = e.getPosition();
//e.getTextureView().body.setTransform(new Vector2(0, -12), new Vector2(pos.x, pos.y - 1), true);
moveFocused(new Vector2(pos.x, pos.y - 1), e.getTextureView().body);
}
}
use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.
the class AnimatePositionSystem method moveFocused.
public void moveFocused(Vector2 center, Body body) {
MoveData moveData = new MoveData();
body.setLinearVelocity(new Vector2());
moveData.start = body.getWorldCenter().cpy();
moveData.direction = new Vector2(center.x - moveData.start.x, center.y - moveData.start.y);
moveData.current = 1;
moveData.total = 10;
if (moveData.current == moveData.total + 1) {
body.setLinearVelocity(new Vector2());
return;
}
int t = easeInOut(moveData.current, 0, 1, moveData.total);
Vector2 sEnd = moveData.direction.cpy();
sEnd.scl(t);
sEnd.add(moveData.start.cpy());
Vector2 sStart = body.getWorldCenter();
sEnd.sub(sStart);
sEnd.scl(60);
moveData.current++;
body.setLinearVelocity(sEnd);
}
Aggregations