use of ilargia.egdx.logicbricks.gen.sensor.SensorEntity in project Entitas-Java by Rubentxu.
the class Indexed method initialize.
public static void initialize(Entitas entitas) {
_entitas = entitas;
index = new KeyIndex(-1, null);
// GameEntity contains Sensors entities
_entitas.sensor.addEntityIndex(Indexed.SensorsEntityIndex, new PrimaryEntityIndex<SensorEntity, KeyIndex>(Indexed.SensorsEntityIndex, ((e, c) -> {
if (c != null) {
ilargia.egdx.logicbricks.component.sensor.Link link = (ilargia.egdx.logicbricks.component.sensor.Link) c;
return new KeyIndex(link.ownerEntity, link.sensorReference);
}
return new KeyIndex(e.getLink().ownerEntity, e.getLink().sensorReference);
}), _entitas.sensor.getGroup(SensorMatcher.Link())));
// GameEntity contains Actuator entities
_entitas.actuator.addEntityIndex(Indexed.ActuatorsEntityIndex, new PrimaryEntityIndex<ActuatorEntity, KeyIndex>(Indexed.ActuatorsEntityIndex, ((e, c) -> {
if (c != null) {
Link link = (Link) c;
return new KeyIndex(link.ownerEntity, link.actuatorReference);
}
return new KeyIndex(e.getLink().ownerEntity, e.getLink().actuatorReference);
}), _entitas.actuator.getGroup(ActuatorMatcher.Link())));
// Interactive GameEntity index
_entitas.game.addEntityIndex(Indexed.InteractiveEntityIndex, new PrimaryEntityIndex<GameEntity, Integer>(Indexed.InteractiveEntityIndex, ((e, c) -> e.getCreationIndex()), _entitas.game.getGroup(GameMatcher.Interactive())));
// Tags GameEntity index
_entitas.game.addEntityIndex(Indexed.TagEntityIndex, new EntityIndex<GameEntity, String>(Indexed.TagEntityIndex, _entitas.game.getGroup(Matcher.AllOf(GameMatcher.Tags(), GameMatcher.Interactive())), ((e, c) -> e.getTags().values.toArray(new String[0]))));
// Sensors context GameEntities
_entitas.game.addEntityIndex(Indexed.GameEntitiesInSensorIndex, new EntityIndex<GameEntity, Integer>(Indexed.GameEntitiesInSensorIndex, _entitas.game.getGroup(GameMatcher.Interactive()), ((e, c) -> new Integer[0])));
}
use of ilargia.egdx.logicbricks.gen.sensor.SensorEntity in project Entitas-Java by Rubentxu.
the class CreateNearSensorSystem method execute.
@Override
protected void execute(List<SensorEntity> entities) {
for (SensorEntity e : entities) {
GameEntity gameEntity = Indexed.getInteractiveEntity(e.getLink().ownerEntity);
RigidBody rigidBody = gameEntity.getRigidBody();
NearSensor nearSensor = e.getNearSensor();
if (nearSensor.distance > 0) {
FixtureDef nearFixture = bodyBuilder.fixtureDefBuilder().circleShape(nearSensor.distance).sensor().build();
rigidBody.body.createFixture(nearFixture).setUserData("NearSensor");
if (nearSensor.resetDistance > nearSensor.distance) {
FixtureDef nearResetFixture = bodyBuilder.fixtureDefBuilder().circleShape(nearSensor.resetDistance).sensor().build();
rigidBody.body.createFixture(nearResetFixture).setUserData("ResetNearSensor");
}
}
}
}
use of ilargia.egdx.logicbricks.gen.sensor.SensorEntity 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 ilargia.egdx.logicbricks.gen.sensor.SensorEntity in project Entitas-Java by Rubentxu.
the class CollisionSensorSystem method processCollision.
@Override
public void processCollision(Fixture colliderA, Fixture colliderB, boolean collisionSignal) {
Integer indexEntityA = (Integer) colliderA.getBody().getUserData();
Integer indexEntityB = (Integer) colliderB.getBody().getUserData();
if (indexEntityA != null && indexEntityB != null) {
GameEntity entityA = Indexed.getInteractiveEntity(indexEntityA);
GameEntity entityB = Indexed.getInteractiveEntity(indexEntityB);
if (entityA != null && entityB != null) {
for (SensorEntity entity : sensorGroup.getEntities()) {
CollisionSensor collision = entity.getCollisionSensor();
if (entityB.getTags().values.contains(collision.targetTag)) {
if (collisionSignal) {
Indexed.addEntityInSensor(entity, entityB);
} else {
Indexed.removeEntityInSensor(entity, entityB);
}
collision.collisionSignal = collisionSignal;
}
}
}
}
}
use of ilargia.egdx.logicbricks.gen.sensor.SensorEntity in project Entitas-Java by Rubentxu.
the class CreateRadarSensorSystem method execute.
@Override
protected void execute(List<SensorEntity> entities) {
Vector2[] vertices = new Vector2[8];
vertices[0] = new Vector2();
for (SensorEntity e : entities) {
GameEntity gameEntity = Indexed.getInteractiveEntity(e.getLink().ownerEntity);
RigidBody rigidBody = gameEntity.getRigidBody();
RadarSensor radarSensor = e.getRadarSensor();
if (radarSensor.angle < 180) {
for (int i = 0; i < 7; i++) {
float angle = (float) (i / 6.0 * radarSensor.angle) - (radarSensor.angle / 2) + (radarSensor.axis2D.ordinal() * 90);
vertices[i + 1] = new Vector2(radarSensor.distance * MathUtils.cosDeg(angle), radarSensor.distance * MathUtils.sinDeg(angle));
}
FixtureDef radarFixture = bodyBuilder.fixtureDefBuilder().polygonShape(vertices).sensor().build();
rigidBody.body.createFixture(radarFixture).setUserData("RadarSensor");
}
}
}
Aggregations