private void createFallTriggerSensor() { obstacleSensor = new SensorObstacle( world, new Vector2( converter.pixToMeter(world.getScreenWidth() / 2.0f), initLoc.y - converter.pixToMeter(world.getScreenHeight() / 2.0f)), converter.pixToMeter(world.getScreenWidth()), converter.pixToMeter(20), this); }
protected void createRigidBody(World simWorld) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; float texHalfWidthMeters = converter.pixToMeter(texHalfWidth); float texHalfHeightMeters = converter.pixToMeter(texHalfHeight); bodyDef.position.set(initLoc.x, initLoc.y); PolygonShape shape = new PolygonShape(); shape.setAsBox(texHalfWidthMeters, texHalfHeightMeters); body = simWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; body.createFixture(fixtureDef); body.getFixtureList().get(0).setSensor(true); body.getFixtureList().get(0).setUserData(this); body.setTransform(body.getPosition().x, body.getPosition().y, 0); }
public abstract class ObstacleFalling extends EnvironObject implements IInteractive { protected Body body; protected float texHalfWidth, texHalfHeight; private SensorObstacle obstacleSensor; protected boolean activated; protected Sprite sprite; // ANIMATION variables protected static int FRAME_COLS = 1; protected static int FRAME_ROWS = 1; protected static final float TIME_PER_FRAME = 1f / 20f; protected float stateTime; protected SpriteAnim anim; protected Vector2 initLoc; protected UnitConverter converter = UnitConverter.Instance(); /** * @param _world * @param texture * @param location * @param _length in PIXELS * @param isHoriz */ public ObstacleFalling(GameWorld _world, Vector2 location) { initLoc = location; world = _world; idLabel = GC.ObjectLabels.INTERACTIVE; stateTime = 0f; setupAnimations(); createFallTriggerSensor(); createRigidBody(world.getSimWorld()); } protected abstract void setupAnimations(); protected void createRigidBody(World simWorld) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; float texHalfWidthMeters = converter.pixToMeter(texHalfWidth); float texHalfHeightMeters = converter.pixToMeter(texHalfHeight); bodyDef.position.set(initLoc.x, initLoc.y); PolygonShape shape = new PolygonShape(); shape.setAsBox(texHalfWidthMeters, texHalfHeightMeters); body = simWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; body.createFixture(fixtureDef); body.getFixtureList().get(0).setSensor(true); body.getFixtureList().get(0).setUserData(this); body.setTransform(body.getPosition().x, body.getPosition().y, 0); } private void createFallTriggerSensor() { obstacleSensor = new SensorObstacle( world, new Vector2( converter.pixToMeter(world.getScreenWidth() / 2.0f), initLoc.y - converter.pixToMeter(world.getScreenHeight() / 2.0f)), converter.pixToMeter(world.getScreenWidth()), converter.pixToMeter(20), this); } // @Override // public void render(SpriteBatch spriteBatch) { // if(activated){ // updateTransform(); // } // renderAnim(spriteBatch); // } // // protected void renderAnim(SpriteBatch spriteBatch){ // TextureRegion curFrame; // stateTime += Gdx.graphics.getDeltaTime(); // curFrame = anim.getKeyFrame(stateTime, true); // drawSprite(spriteBatch, curFrame); // } // // protected void drawSprite(SpriteBatch spriteBatch, TextureRegion currentFrame){ // sprite.setRegion(currentFrame); // sprite.draw(spriteBatch); // } protected void updateTransform() { Vector2 bodyLoc = body.getPosition(); sprite.setPosition( converter.meterToPix(bodyLoc.x) - texHalfWidth, converter.meterToPix(bodyLoc.y) - texHalfHeight); } @Override public void destroyBody() { world.getSimWorld().destroyBody(body); obstacleSensor.destroyBody(); } public abstract void activate(); public void handleHit() { world.reportLossByContact(); } @Override public InteractiveType getType() { return InteractiveType.OBSTACLE; } @Override public GC.RenderLayer getRenderLayer() { return GC.RenderLayer.FG_4; } }
protected void updateTransform() { Vector2 bodyLoc = body.getPosition(); sprite.setPosition( converter.meterToPix(bodyLoc.x) - texHalfWidth, converter.meterToPix(bodyLoc.y) - texHalfHeight); }