Search in sources :

Example 6 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.

the class TiledMapObjectLoadingTest method render.

@Override
public void render() {
    Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    shapeRenderer.setProjectionMatrix(camera.combined);
    batch.setProjectionMatrix(camera.combined);
    shapeRenderer.setColor(Color.BLUE);
    Gdx.gl20.glLineWidth(2);
    MapLayer layer = map.getLayers().get("Objects");
    AnimatedTiledMapTile.updateAnimationBaseTime();
    for (MapObject mapObject : layer.getObjects()) {
        if (mapObject instanceof TiledMapTileMapObject) {
            batch.begin();
            TiledMapTileMapObject tmtObject = (TiledMapTileMapObject) mapObject;
            TextureRegion textureRegion = tmtObject.getTile().getTextureRegion();
            // TilEd rotation is clockwise, we need counter-clockwise.
            float rotation = -tmtObject.getRotation();
            float scaleX = tmtObject.getScaleX();
            float scaleY = tmtObject.getScaleY();
            float xPos = tmtObject.getX();
            float yPos = tmtObject.getY();
            textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
            batch.draw(textureRegion, xPos, yPos, tmtObject.getOriginX() * scaleX, tmtObject.getOriginY() * scaleY, textureRegion.getRegionWidth() * scaleX, textureRegion.getRegionHeight() * scaleY, 1f, 1f, rotation);
            // We flip back to the original state.
            textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
            batch.end();
        } else if (mapObject instanceof EllipseMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            shapeRenderer.ellipse(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
            shapeRenderer.end();
        } else if (mapObject instanceof RectangleMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
            shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
            shapeRenderer.end();
        } else if (mapObject instanceof PolygonMapObject) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
            Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
            shapeRenderer.polygon(polygon.getTransformedVertices());
            shapeRenderer.end();
        }
    }
    batch.begin();
    font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
    batch.end();
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) Ellipse(com.badlogic.gdx.math.Ellipse) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) MapLayer(com.badlogic.gdx.maps.MapLayer) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) Rectangle(com.badlogic.gdx.math.Rectangle) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) TextureMapObject(com.badlogic.gdx.maps.objects.TextureMapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject)

Example 7 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.

the class SuperKoalio method updateKoala.

private void updateKoala(float deltaTime) {
    if (deltaTime == 0)
        return;
    if (deltaTime > 0.1f)
        deltaTime = 0.1f;
    koala.stateTime += deltaTime;
    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) {
        koala.velocity.y += Koala.JUMP_VELOCITY;
        koala.state = Koala.State.Jumping;
        koala.grounded = false;
    }
    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
        koala.velocity.x = -Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = false;
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
        koala.velocity.x = Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = true;
    }
    if (Gdx.input.isKeyJustPressed(Keys.B))
        debug = !debug;
    // apply gravity if we are falling
    koala.velocity.add(0, GRAVITY);
    // clamp the velocity to the maximum, x-axis only
    koala.velocity.x = MathUtils.clamp(koala.velocity.x, -Koala.MAX_VELOCITY, Koala.MAX_VELOCITY);
    // If the velocity is < 1, set it to 0 and set state to Standing
    if (Math.abs(koala.velocity.x) < 1) {
        koala.velocity.x = 0;
        if (koala.grounded)
            koala.state = Koala.State.Standing;
    }
    // multiply by delta time so we know how far we go
    // in this frame
    koala.velocity.scl(deltaTime);
    // perform collision detection & response, on each axis, separately
    // if the koala is moving right, check the tiles to the right of it's
    // right bounding box edge, otherwise check the ones to the left
    Rectangle koalaRect = rectPool.obtain();
    koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    int startX, startY, endX, endY;
    if (koala.velocity.x > 0) {
        startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
    } else {
        startX = endX = (int) (koala.position.x + koala.velocity.x);
    }
    startY = (int) (koala.position.y);
    endY = (int) (koala.position.y + Koala.HEIGHT);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.x += koala.velocity.x;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            koala.velocity.x = 0;
            break;
        }
    }
    koalaRect.x = koala.position.x;
    // top bounding box edge, otherwise check the ones to the bottom
    if (koala.velocity.y > 0) {
        startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
    } else {
        startY = endY = (int) (koala.position.y + koala.velocity.y);
    }
    startX = (int) (koala.position.x);
    endX = (int) (koala.position.x + Koala.WIDTH);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.y += koala.velocity.y;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            // this removes bouncing :)
            if (koala.velocity.y > 0) {
                koala.position.y = tile.y - Koala.HEIGHT;
                // we hit a block jumping upwards, let's destroy it!
                TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
                layer.setCell((int) tile.x, (int) tile.y, null);
            } else {
                koala.position.y = tile.y + tile.height;
                // if we hit the ground, mark us as grounded so we can jump
                koala.grounded = true;
            }
            koala.velocity.y = 0;
            break;
        }
    }
    rectPool.free(koalaRect);
    // unscale the velocity by the inverse delta time and set
    // the latest position
    koala.position.add(koala.velocity);
    koala.velocity.scl(1 / deltaTime);
    // Apply damping to the velocity on the x-axis so we don't
    // walk infinitely once a key was pressed
    koala.velocity.x *= Koala.DAMPING;
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Rectangle(com.badlogic.gdx.math.Rectangle)

Example 8 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.

the class ScissorStack method popScissors.

/** Pops the current scissor rectangle from the stack and sets the new scissor area to the new top of stack rectangle. In case
	 * no more rectangles are on the stack, {@link GL20#GL_SCISSOR_TEST} is disabled.
	 * <p>
	 * Any drawing should be flushed before popping scissors. */
public static Rectangle popScissors() {
    Rectangle old = scissors.pop();
    if (scissors.size == 0)
        Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
    else {
        Rectangle scissor = scissors.peek();
        HdpiUtils.glScissor((int) scissor.x, (int) scissor.y, (int) scissor.width, (int) scissor.height);
    }
    return old;
}
Also used : Rectangle(com.badlogic.gdx.math.Rectangle)

Example 9 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.

the class ScissorStack method getViewport.

/** @return the current viewport in OpenGL ES window coordinates based on the currently applied scissor */
public static Rectangle getViewport() {
    if (scissors.size == 0) {
        viewport.set(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        return viewport;
    } else {
        Rectangle scissor = scissors.peek();
        viewport.set(scissor);
        return viewport;
    }
}
Also used : Rectangle(com.badlogic.gdx.math.Rectangle)

Example 10 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.

the class Group method drawChildren.

/** Draws all children. {@link #applyTransform(Batch, Matrix4)} should be called before and {@link #resetTransform(Batch)}
	 * after this method if {@link #setTransform(boolean) transform} is true. If {@link #setTransform(boolean) transform} is false
	 * these methods don't need to be called, children positions are temporarily offset by the group position when drawn. This
	 * method avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area}, if set. */
protected void drawChildren(Batch batch, float parentAlpha) {
    parentAlpha *= this.color.a;
    SnapshotArray<Actor> children = this.children;
    Actor[] actors = children.begin();
    Rectangle cullingArea = this.cullingArea;
    if (cullingArea != null) {
        // Draw children only if inside culling area.
        float cullLeft = cullingArea.x;
        float cullRight = cullLeft + cullingArea.width;
        float cullBottom = cullingArea.y;
        float cullTop = cullBottom + cullingArea.height;
        if (transform) {
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible())
                    continue;
                float cx = child.x, cy = child.y;
                if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom)
                    child.draw(batch, parentAlpha);
            }
        } else {
            // No transform for this group, offset each child.
            float offsetX = x, offsetY = y;
            x = 0;
            y = 0;
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible())
                    continue;
                float cx = child.x, cy = child.y;
                if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom) {
                    child.x = cx + offsetX;
                    child.y = cy + offsetY;
                    child.draw(batch, parentAlpha);
                    child.x = cx;
                    child.y = cy;
                }
            }
            x = offsetX;
            y = offsetY;
        }
    } else {
        // No culling, draw all children.
        if (transform) {
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible())
                    continue;
                child.draw(batch, parentAlpha);
            }
        } else {
            // No transform for this group, offset each child.
            float offsetX = x, offsetY = y;
            x = 0;
            y = 0;
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible())
                    continue;
                float cx = child.x, cy = child.y;
                child.x = cx + offsetX;
                child.y = cy + offsetY;
                child.draw(batch, parentAlpha);
                child.x = cx;
                child.y = cy;
            }
            x = offsetX;
            y = offsetY;
        }
    }
    children.end();
}
Also used : Rectangle(com.badlogic.gdx.math.Rectangle)

Aggregations

Rectangle (com.badlogic.gdx.math.Rectangle)35 Test (org.junit.Test)6 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)5 MockPointer (com.gemserk.commons.gdx.input.MockPointer)4 Circle (com.badlogic.gdx.math.Circle)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 CoreEntity (com.ilargia.games.entitas.core.CoreEntity)2 View (com.ilargia.games.logicbrick.component.View)2 PlayerSoldier (me.dumfing.multiplayerTools.PlayerSoldier)2 FishColour (ca.hiphiparray.amazingmaze.FishCell.FishColour)1 AssetManager (com.badlogic.gdx.assets.AssetManager)1 FileHandle (com.badlogic.gdx.files.FileHandle)1 Camera (com.badlogic.gdx.graphics.Camera)1 Color (com.badlogic.gdx.graphics.Color)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Batch (com.badlogic.gdx.graphics.g2d.Batch)1 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)1 TextBounds (com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds)1