Search in sources :

Example 31 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project Entitas-Java by Rubentxu.

the class MoveSystem method execute.

@Override
public void execute(float deltatime) {
    for (CoreEntity e : _group.getEntities()) {
        Motion motion = e.getMotion();
        View view = e.getView();
        if (view.shape instanceof Rectangle) {
            Rectangle ret = (Rectangle) view.shape;
            ret.setPosition(ret.x + motion.velocity.x * Gdx.graphics.getDeltaTime(), ret.y + motion.velocity.y * Gdx.graphics.getDeltaTime());
        } else {
            Circle circle = (Circle) view.shape;
            circle.setPosition(circle.x + motion.velocity.x * Gdx.graphics.getDeltaTime(), circle.y + motion.velocity.y * Gdx.graphics.getDeltaTime());
        }
    }
}
Also used : CoreEntity(com.ilargia.games.entitas.core.CoreEntity) Motion(com.ilargia.games.logicbrick.component.Motion) Circle(com.badlogic.gdx.math.Circle) Rectangle(com.badlogic.gdx.math.Rectangle) View(com.ilargia.games.logicbrick.component.View)

Example 32 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project AmazingMaze by TheVirtualMachine.

the class MazeScreen method createBoundingBoxes.

/** Create the bounding boxes for collision detection. */
private void createBoundingBoxes() {
    obstacleBoxes = new Array<Rectangle>(false, 16);
    TiledMapTileLayer objects = (TiledMapTileLayer) map.getLayers().get(MapFactory.OBJECT_LAYER);
    for (int r = 0; r < mapHeight; r++) {
        for (int c = 0; c < mapWidth; c++) {
            Cell cell = objects.getCell(c, r);
            if (cell != null) {
                obstacleBoxes.add(new Rectangle(c, r, 1, 1));
            }
        }
    }
    wireBoxes = new Array<Rectangle>(false, 16);
    TiledMapTileLayer wires = (TiledMapTileLayer) map.getLayers().get(MapFactory.WIRE_LAYER);
    for (int r = 0; r < mapHeight; r++) {
        for (int c = 0; c < mapWidth; c++) {
            WireCell wire = (WireCell) wires.getCell(c, r);
            if (wire != null && wire.isOn()) {
                wireBoxes.add(new Rectangle(c + 5f / 16f, r, 6f / 16f, 1));
            }
        }
    }
    fishBoxes = new Array<Rectangle>(false, 16);
    cheeseBoxes = new Array<Rectangle>(false, 16);
    TiledMapTileLayer items = (TiledMapTileLayer) map.getLayers().get(MapFactory.ITEM_LAYER);
    for (int r = 0; r < mapHeight; r++) {
        for (int c = 0; c < mapWidth; c++) {
            Cell item = items.getCell(c, r);
            if (item != null) {
                if (item.getClass() == FishCell.class) {
                    fishBoxes.add(new Rectangle(c, r, 1, 1));
                } else {
                    cheeseBoxes.add(new Rectangle(c, r, 1, 1));
                }
            }
        }
    }
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Rectangle(com.badlogic.gdx.math.Rectangle) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) Point(java.awt.Point)

Example 33 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project AmazingMaze by TheVirtualMachine.

the class Player method collectFish.

/** Handle the player collecting fish. */
private void collectFish() {
    Rectangle thisBox = getBoundingRectangle();
    for (int i = 0; i < maze.fishBoxes.size; i++) {
        if (thisBox.overlaps(maze.fishBoxes.get(i))) {
            TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER);
            int x = (int) maze.fishBoxes.get(i).x;
            int y = (int) maze.fishBoxes.get(i).y;
            FishColour colour = ((FishCell) layer.getCell(x, y)).getColour();
            layer.setCell(x, y, null);
            maze.fishBoxes.removeIndex(i);
            switch(colour) {
                case BLUE:
                    blueCollected++;
                    break;
                case PURPLE:
                    purpleCollected++;
                    break;
                case GREEN:
                    greenCollected++;
                    break;
                case RED:
                    redCollected++;
                    break;
                case ORANGE:
                    orangeCollected++;
                    break;
            }
            break;
        }
    }
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Rectangle(com.badlogic.gdx.math.Rectangle) FishColour(ca.hiphiparray.amazingmaze.FishCell.FishColour)

Example 34 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project AmazingMaze by TheVirtualMachine.

the class Player method collectCheese.

/** Handle the player collecting cheese. */
private void collectCheese() {
    Rectangle thisBox = getBoundingRectangle();
    for (int i = 0; i < maze.cheeseBoxes.size; i++) {
        if (thisBox.overlaps(maze.cheeseBoxes.get(i))) {
            TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER);
            int x = (int) maze.cheeseBoxes.get(i).x;
            int y = (int) maze.cheeseBoxes.get(i).y;
            layer.setCell(x, y, null);
            maze.cheeseBoxes.removeIndex(i);
            lives++;
            maze.updateLives(-1);
            break;
        }
    }
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Rectangle(com.badlogic.gdx.math.Rectangle)

Example 35 with Rectangle

use of com.badlogic.gdx.math.Rectangle in project AmazingMaze by TheVirtualMachine.

the class Player method doObjectCollision.

/**
	 * Return where the player should be after handling object collision.
	 *
	 * @param deltaPos the change in position since the last position update.
	 * @return the new position, as a {@link Point2D.Float}.
	 */
private Point2D.Float doObjectCollision(Vector2 deltaPos) {
    float newX = getX() + deltaPos.x;
    float newY = getY() + deltaPos.y;
    Point2D.Float nextTilePos = new Point2D.Float(newX, newY);
    Rectangle nextBoundingBox = new Rectangle(nextTilePos.x, nextTilePos.y, PLAYER_SIZE, PLAYER_SIZE);
    float nextStartX = nextBoundingBox.getX();
    float nextEndX = nextStartX + nextBoundingBox.getWidth();
    float nextStartY = nextBoundingBox.getY();
    float nextEndY = nextStartY + nextBoundingBox.getHeight();
    for (Rectangle obstacle : maze.obstacleBoxes) {
        if (nextBoundingBox.overlaps(obstacle)) {
            float objectStartX = obstacle.getX();
            float objectEndX = objectStartX + obstacle.getWidth();
            float objectStartY = obstacle.getY();
            float objectEndY = objectStartY + obstacle.getHeight();
            if (deltaPos.x != 0) {
                if (nextStartX > objectStartX && nextStartX < objectEndX) {
                    // Collided on right.
                    newX = objectEndX;
                } else if (nextEndX > objectStartX && nextEndX < objectEndX) {
                    // Collided on left.
                    newX = objectStartX - PLAYER_SIZE;
                }
            } else if (deltaPos.y != 0) {
                if (nextStartY > objectStartY && nextStartY < objectEndY) {
                    // Collided on bottom.
                    newY = objectEndY;
                } else if (nextEndY > objectStartY && nextEndY < objectEndY) {
                    // Collided on top.
                    newY = objectStartY - PLAYER_SIZE;
                }
            }
        }
    }
    newX = Math.max(newX, 0);
    newX = Math.min(newX, maze.mapWidth - PLAYER_SIZE);
    nextTilePos.setLocation(newX, newY);
    return nextTilePos;
}
Also used : Point2D(java.awt.geom.Point2D) 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