use of com.b3dgs.lionengine.game.feature.Transformable in project lionengine by b3dgs.
the class SceneRasterable method add.
private void add(SetupSurfaceRastered setup, int offsetX) {
final SpriteAnimated surface = Drawable.loadSpriteAnimated(setup.getSurface(), 4, 4);
final Featurable featurable = new FeaturableModel(services, setup);
featurable.addFeature(new MirrorableModel(services, setup));
featurable.addFeature(new AnimatableModel(services, setup, surface));
final Transformable transformable = featurable.addFeatureAndGet(new TransformableModel(services, setup));
final Rasterable rasterable = new RasterableModel(services, setup);
rasterable.setOrigin(Origin.MIDDLE);
rasterable.setFrameOffsets(1, 2);
featurable.addFeature(rasterable);
featurable.addFeature(new RefreshableModel(extrp -> {
transformable.setLocationY(UtilMath.sin(count) * 240);
surface.setLocation(camera, transformable);
rasterable.update(extrp);
surface.update(extrp);
}));
featurable.addFeature(new DisplayableModel(g -> rasterable.render(g)));
transformable.setLocationX(120 + offsetX);
handler.add(featurable);
}
use of com.b3dgs.lionengine.game.feature.Transformable in project lionengine by b3dgs.
the class LauncherModel method computeVector.
/**
* Compute the force vector depending of the target.
*
* @param vector The initial vector used for launch.
* @param target The target reference.
* @return The computed force to reach target.
*/
private Force computeVector(Force vector, Localizable target) {
final double sx = transformable.getX();
final double sy = transformable.getY();
double dx = target.getX();
double dy = target.getY();
if (extrapolate && target instanceof Transformable) {
final Transformable targetTransformable = (Transformable) target;
final double ray = UtilMath.getDistance(transformable.getX(), transformable.getY(), target.getX(), target.getY());
dx += (int) ((target.getX() - targetTransformable.getOldX()) / vector.getDirectionHorizontal() * ray);
dy += (int) ((target.getY() - targetTransformable.getOldY()) / vector.getDirectionVertical() * ray);
}
final double dist = Math.max(Math.abs(sx - dx), Math.abs(sy - dy));
final double vecX = (dx - sx) / dist * vector.getDirectionHorizontal();
final double vecY = (dy - sy) / dist * vector.getDirectionVertical();
final Force force = new Force(vector);
force.setDestination(vecX, vecY);
return force;
}
use of com.b3dgs.lionengine.game.feature.Transformable in project lionengine by b3dgs.
the class ProducerModel method startProduction.
/**
* Start production of this element. Get its corresponding instance and add it to the handler.
* Featurable will be removed from handler if production is cancelled.
*
* @param featurable The element to produce.
*/
private void startProduction(Featurable featurable) {
final Transformable transformable = featurable.getFeature(Transformable.class);
final Producible producible = featurable.getFeature(Producible.class);
transformable.teleport(producible.getX(), producible.getY());
handler.add(featurable);
steps = current.getFeature(Producible.class).getSteps();
progress = 0.0;
for (int i = 0; i < listenable.size(); i++) {
listenable.get(i).notifyStartProduction(featurable);
}
for (final ProducibleListener listener : featurable.getFeature(Producible.class).getListeners()) {
listener.notifyProductionStarted(this);
}
}
use of com.b3dgs.lionengine.game.feature.Transformable in project lionengine by b3dgs.
the class ComponentCollision method notifyHandlableRemoved.
@Override
public void notifyHandlableRemoved(Featurable featurable) {
if (featurable.hasFeature(Collidable.class)) {
final Transformable transformable = featurable.getFeature(Transformable.class);
final Collidable collidable = transformable.getFeature(Collidable.class);
removePoints(transformable, collidable);
transformable.removeListener(this);
}
}
use of com.b3dgs.lionengine.game.feature.Transformable in project lionengine by b3dgs.
the class RasterableModelTest method testModel.
/**
* Test the model.
*/
@Test
void testModel() {
services.add(new ViewerMock());
final SetupSurfaceRastered setup = new SetupSurfaceRastered(Medias.create(OBJECT_XML));
final Featurable featurable = new FeaturableModel(services, setup);
final Transformable transformable = featurable.addFeatureAndGet(new TransformableModel(services, setup));
final Animatable animatable = featurable.addFeatureAndGet(new AnimatableModel(services, setup));
featurable.addFeature(new MirrorableModel(services, setup));
final Rasterable rasterable = new RasterableModel(services, setup);
rasterable.setFrameOffsets(1, 2);
rasterable.prepare(featurable);
rasterable.setOrigin(Origin.TOP_LEFT);
rasterable.update(1.0);
rasterable.setEnabled(false);
rasterable.update(1.0);
rasterable.render(g);
assertTrue(rasterable.isVisible());
rasterable.setVisibility(false);
rasterable.update(1.0);
rasterable.render(g);
assertFalse(rasterable.isVisible());
assertEquals(1, rasterable.getRasterIndex(0));
assertEquals(RasterImage.MAX_RASTERS, rasterable.getRasterIndex(240));
assertNotNull(rasterable.getRasterAnim(0));
transformable.teleportY(-100);
rasterable.update(1.0);
assertEquals(1, rasterable.getRasterIndex(0));
assertEquals(RasterImage.MAX_RASTERS, rasterable.getRasterIndex(240));
assertNotNull(rasterable.getRasterAnim(0));
animatable.play(new Animation("default", 1, 5, 1.0, false, false));
animatable.update(1.0);
rasterable.update(1.0);
assertEquals(1, rasterable.getRasterAnim(0).getFrame());
rasterable.setAnimOffset(1);
rasterable.update(1.0);
assertEquals(1, rasterable.getRasterAnim(0).getFrame());
}
Aggregations