private void tryMove() { bounds.x += vel.x; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.x < 0) bounds.x = rect.x + rect.width + 0.01f; else bounds.x = rect.x - bounds.width - 0.01f; vel.x = 0; } } bounds.y += vel.y; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.y < 0) { bounds.y = rect.y + rect.height + 0.01f; } else bounds.y = rect.y - bounds.height - 0.01f; vel.y = 0; } } pos.x = bounds.x - 0.2f; pos.y = bounds.y - 0.2f; }
@Override public void render() { // clear the screen with a dark blue color. The // arguments to glClearColor are the red, green // blue and alpha component in the range [0,1] // of the color to be used to clear the screen. Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // tell the camera to update its matrices. camera.update(); // tell the SpriteBatch to render in the // coordinate system specified by the camera. batch.setProjectionMatrix(camera.combined); // begin a new batch and draw the bucket and // all drops batch.begin(); batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { batch.draw(dropImage, raindrop.x, raindrop.y); } batch.end(); // // process user input if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 48 / 2; } if (Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 48) bucket.x = 800 - 48; // commit // check if we need to create a new raindrop if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop(); // move the raindrops, remove any that are beneath the bottom edge of // the screen or that hit the bucket. In the later case we play back // a sound effect as well. Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 48 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropSound.play(); iter.remove(); } } }
public void update(Array<BaseTile> tiles, Rectangle playerRect) { intersectingPlayer = false; if (rect.overlaps(playerRect)) { intersectingPlayer = true; } item1.update(tiles, playerRect); item2.update(tiles, playerRect); item3.update(tiles, playerRect); }
public boolean collidesWithTile(Entity entity, float posX, float posY) { Rectangle box = new Rectangle( posX, posY, entity.getBoundingBox().getWidth(), entity.getBoundingBox().getHeight()); for (Rectangle rectangle : collisions) { if (rectangle.overlaps(box)) { return true; } } return false; }
public boolean collide(Rectangle body) { boolean collide = false; if (body.overlaps(AABB)) { collide = true; float bodyMidX = body.x + (body.width / 2); float bodyMidY = body.y + (body.height / 2); float AABBMidX = AABB.x + (AABB.width / 2); float AABBMidY = AABB.y + (AABB.height / 2); AABB.x -= deltaPos.x; AABB.y -= deltaPos.y; AABB.x += deltaPos.x; if (AABB.overlaps(body)) { if (AABBMidX - deltaPos.x > bodyMidX) { AABB.x = (int) body.x + (int) body.width; } else { AABB.x = body.x - AABB.width; } } AABB.y += deltaPos.y; if (AABB.overlaps(body)) { if (AABBMidY - deltaPos.y > bodyMidY) { AABB.y = body.y + body.height; } else { AABB.y = body.y - AABB.height; } } position.x = AABB.x - 14; position.y = AABB.y - 14; } return collide; }
private void handleCollision(Rectangle col) { float temp = position.x; collisionArea.x = oldPosition.x; // just x if (collisionArea.overlaps(col) == false) { position.x = oldPosition.x; return; } collisionArea.x = temp; temp = position.y; collisionArea.y = oldPosition.y; // just y if (collisionArea.overlaps(col) == false) { position.y = oldPosition.y; return; } // both x and y collisionArea.x = oldPosition.y; position.x = oldPosition.x; position.y = oldPosition.y; if (collisionArea.overlaps(col)) { System.out.println("ERROR! collision"); } }
@Override public void render() { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { batch.draw(dropImage, raindrop.x, raindrop.y); } batch.end(); if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 64 / 2; } if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 64) bucket.x = 800 - 64; if (TimeUtils.nanoTime() - lastDropTime > 1000000000) { spawnRaindrop(); } Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 64 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropSound.play(); iter.remove(); } } }
private LinkedList<Unit> selectCharacters() { LinkedList<Unit> units = new LinkedList<>(); for (Actor actor : getStage().getActors()) { if (!(actor instanceof Unit)) continue; Rectangle boundingBox = new Rectangle(actor.getX(), actor.getY(), actor.getWidth(), actor.getHeight()); if (selectionBox.overlaps(boundingBox)) { units.add((Unit) actor); } } return units; }
public void updateCreateCharacterButton() { if (Gdx.input.justTouched()) { Rectangle mousePos = Utils.getMenuMousePos(); Ent btnAdd = this.getEntByName("btnAddCharacter"); if (mousePos.overlaps(btnAdd.getPosBox())) { this.setCurrentSubMenu(this.getSubMenuByName("createcharacter")); Ent btnSelect = this.getEntByName("btnSelect"); for (Ent btn : this.getEnts()) { if (btn.getName().contains("Tile") && btn.getName().contains("btn")) { if (btn.getName().equals("btnDeleteTile")) { btnSelect.setPosBox(new Rectangle(btn.getPosBox())); } btn.setState(State.NORMAL); } } } } }
public void updateButtonClick() { if (Gdx.input.justTouched()) { Rectangle mousePos = new Rectangle(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), 1, 1); for (Ent e : getEnts()) { if (e.getName().contains("btn") && mousePos.overlaps(e.getPosBox())) { Aosa.getGlobal().getSoundByName("click").getSound().play(); if (e.getName().equals("btnExit")) { Gdx.app.exit(); } else if (e.getName().equals("btnPlay")) { Aosa.getGlobal().setCurrentMenu(Aosa.getGlobal().getMenuByName("load")); LoadGameInstance.load("sandbox"); } } } } }
public void selectTile() { if (Gdx.input.justTouched()) { Rectangle mousePos = Utils.getMenuMousePos(); Ent btnSelect = this.getEntByName("btnSelect"); Ent selectedTile = null; for (Ent e : this.getEnts()) { if (e.getName().contains("btn") && e.getName().contains("Tile")) { if (mousePos.overlaps(e.getPosBox())) { e.setState(State.SELECTED); btnSelect.setPosBox(new Rectangle(e.getPosBox())); selectedTile = e; } } } if (selectedTile != null) { for (Ent e : this.getEnts()) { if (e.getName() != selectedTile.getName()) { e.setState(State.NORMAL); } } } } }
private void tryToMovePlayer() { boolean sticked = player.abilities.contains(Ability.SLICK); if (player.jumpCommand || sticked) { int nextBlock = (player.gravityDirection < 0) ? -1 : 2; Tile tile1 = map[(int) player.x / BLOCK_SIZE][(int) player.y / BLOCK_SIZE + nextBlock]; Tile tile2 = map[(int) player.x / BLOCK_SIZE + 1][(int) player.y / BLOCK_SIZE + nextBlock]; boolean onTheGround = player.state != State.SWIM && (tile1 == Tile.BLOCK || tile1 == Tile.GLASS || tile1 == Tile.TUBE_UP || tile1 == Tile.TUBE_RIGHT || tile1 == Tile.TUBE_DOWN || tile1 == Tile.TUBE_LEFT || tile2 == Tile.BLOCK || tile2 == Tile.GLASS || tile2 == Tile.TUBE_UP || tile2 == Tile.TUBE_RIGHT || tile2 == Tile.TUBE_DOWN || tile2 == Tile.TUBE_LEFT); if (!player.isJumping && onTheGround && !sticked) { // Sounds.get().play(Sounds.JUMP); player.vy -= player.gravityDirection * Player.JUMPING; player.isJumping = true; } else if (!onTheGround && sticked) { player.clearSlick(); } player.jumpCommand = false; } Rectangle pr = new Rectangle( (int) player.x / BLOCK_SIZE + (((int) player.x % BLOCK_SIZE == 0) ? 0 : 1), (int) Math.floor(player.y / BLOCK_SIZE) + (((int) player.y % BLOCK_SIZE == 0) ? 0 : 1), 1, 1); Rectangle[] rs; boolean collided = false; player.x += player.vx; pr.setX((int) player.x / BLOCK_SIZE); rs = checkCollisions(); for (Rectangle r : rs) { if (pr.overlaps(r)) { if (player.vx < 0) player.x = (r.x + 1) * BLOCK_SIZE + 0.01f; else player.x = (r.x - 1) * BLOCK_SIZE - 0.01f; collided = true; } } if (collided) { player.vx = 0; /*player.ax = 0;*/ } collided = false; player.y += player.vy; pr.setX((int) player.x / BLOCK_SIZE); pr.setY((int) Math.floor(player.y / BLOCK_SIZE)); rs = checkCollisions(); for (Rectangle r : rs) { if (pr.overlaps(r)) { if (player.vy < 0) player.y = (r.y + 1) * BLOCK_SIZE + 0.01f; else player.y = (r.y - 1) * BLOCK_SIZE - 0.01f; collided = true; } } if (collided) { if (player.isJumping && player.vy * player.gravityDirection > 0) player.isJumping = false; if (player.gravityDirection > 0 && player.vy > 0) { // if (! player.abilities.contains(Ability.SLICK)) { // wasCollided = true; // collidedCount = 50; // } if (!sticked) player.gravityDirection = -1; player.abilities.remove(Ability.GAS); } player.vy = 0; player.ay = 0; // if (! player.gravityAffection) { // wasCollided = true; // collidedCount++; // } } /*else if (! player.gravityAffection*/ /* && wasCollided*/ /*) { // if (collidedCount > 5) { // player.clearSlick(); // wasCollided = false; // } }*/ }
public boolean collides(Rectangle player) { return player.overlaps(boundsBottom) || player.overlaps(boundsTop); }
public void update(float delta, Array<Platform> platforms) { lastFramePosition.set(position); velocity.y -= Constants.GRAVITY; position.mulAdd(velocity, delta); if (position.y < Constants.KILL_PLANE) { init(); } // Land on/fall off platforms if (jumpState != Enums.JumpState.JUMPING) { if (jumpState != JumpState.RECOILING) { jumpState = Enums.JumpState.FALLING; } for (Platform platform : platforms) { if (landedOnPlatform(platform)) { jumpState = Enums.JumpState.GROUNDED; velocity.y = 0; velocity.x = 0; position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT; } } } // Collide with enemies Rectangle gigaGalBounds = new Rectangle( position.x - Constants.GIGAGAL_STANCE_WIDTH / 2, position.y - Constants.GIGAGAL_EYE_HEIGHT, Constants.GIGAGAL_STANCE_WIDTH, Constants.GIGAGAL_HEIGHT); for (Enemy enemy : level.getEnemies()) { Rectangle enemyBounds = new Rectangle( enemy.position.x - Constants.ENEMY_COLLISION_RADIUS, enemy.position.y - Constants.ENEMY_COLLISION_RADIUS, 2 * Constants.ENEMY_COLLISION_RADIUS, 2 * Constants.ENEMY_COLLISION_RADIUS); if (gigaGalBounds.overlaps(enemyBounds)) { if (position.x < enemy.position.x) { recoilFromEnemy(Direction.LEFT); } else { recoilFromEnemy(Direction.RIGHT); } } } // Move left/right if (jumpState != JumpState.RECOILING) { if (Gdx.input.isKeyPressed(Keys.LEFT)) { moveLeft(delta); } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) { moveRight(delta); } else { walkState = Enums.WalkState.NOT_WALKING; } } // Jump if (Gdx.input.isKeyPressed(Keys.Z)) { switch (jumpState) { case GROUNDED: startJump(); break; case JUMPING: continueJump(); } } else { endJump(); } // TODO: Check if GigaGal should pick up a powerup // This is a tough one. Check for overlaps similar to how we detect collisions with enemies, // then remove any picked up powerups (and update GigaGal's ammo count) // Remember to check out the solution project if you run into trouble! DelayedRemovalArray<Powerup> powerups = level.getPowerups(); powerups.begin(); for (int i = 0; i < powerups.size; i++) { Powerup powerup = powerups.get(i); Rectangle powerupBounds = new Rectangle( powerup.position.x - Constants.POWERUP_CENTER.x, powerup.position.y - Constants.POWERUP_CENTER.y, Assets.instance.powerupAssets.powerup.getRegionWidth(), Assets.instance.powerupAssets.powerup.getRegionHeight()); if (gigaGalBounds.overlaps(powerupBounds)) { ammo += Constants.POWERUP_AMMO; powerups.removeIndex(i); } } powerups.end(); // Shoot // TODO: Check if GigaGal has any ammo left if (Gdx.input.isKeyJustPressed(Keys.X) && ammo > 0) { // TODO: Decrement ammo ammo--; Vector2 bulletPosition; if (facing == Direction.RIGHT) { bulletPosition = new Vector2( position.x + Constants.GIGAGAL_CANNON_OFFSET.x, position.y + Constants.GIGAGAL_CANNON_OFFSET.y); } else { bulletPosition = new Vector2( position.x - Constants.GIGAGAL_CANNON_OFFSET.x, position.y + Constants.GIGAGAL_CANNON_OFFSET.y); } level.spawnBullet(bulletPosition, facing); } }
/** * Draw the edges defined by {@link #setVertices(Array)}. If {@link * OutlineMerger#mergeOutlines(Array)} has been used on this {@link OutlinePolygon} then the draw * method may just be redirected to this outlinePolygon's parents. * * @param batch Accumulates data and sends it in large portions to the gpu, instead of sending * small portions more often. * @return this for chaining. */ public OutlinePolygon draw(PrettyPolygonBatch batch) { if (myParents.size > 0) { // if i have a parent i will not be drawn for (OutlinePolygon outlinePolygon : myParents) { outlinePolygon.draw(batch); } return this; } drawInvocations++; if (myChildren.size == 0 || drawInvocations >= myChildren.size) { if (!visible) return this; debugRenderer.queueIfEnabled(batch); timeOfLastDrawCall = System.currentTimeMillis(); // if i don't have any children i will just attempt to draw right away // also if all my children has had their draw method called i will attempt to draw drawInvocations = 0; if (halfWidth <= 0) return this; if (color.a <= 0) return this; if (weight <= 0) return this; if (scale <= 0) return this; if (!drawInside && !drawOutside) return this; Rectangle frustum = this.frustum.set(batch.frustum); tmpColor.set(color).a *= opacity; updateStripAndCulling(); for (BoundingBox box : boundingBoxes) { Rectangle cullingArea = getCullingArea(tmpRectangle, box.rectangle, angleRad, position, scale); if (frustum.overlaps(cullingArea)) { // if we reached here we can draw the vertices within this particular BoundingBox if (drawInside) { batch.drawOutline( vertexDataArray, closedPolygon, true, box.begin, box.begin + box.count + 1, box.dataCount, tmpColor, scale, angleRad, position.x, position.y, weight); } if (drawOutside) { batch.drawOutline( vertexDataArray, closedPolygon, false, box.begin, box.begin + box.count + 1, box.dataCount, tmpColor, scale, angleRad, position.x, position.y, weight); } } } } return this; }
private void updateWorld() { float deltaTime = Gdx.graphics.getDeltaTime(); planeStateTime += deltaTime; if (Gdx.input.justTouched()) { if (gameState == GameState.Start) { gameState = GameState.Running; } if (gameState == GameState.Running) { planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); } if (gameState == GameState.GameOver) { gameState = GameState.Start; resetWorld(); } } if (gameState != GameState.Start) planeVelocity.add(gravity); planePosition.mulAdd(planeVelocity, deltaTime); camera.position.x = planePosition.x + 350; if (camera.position.x - groundOffsetX > ground.getRegionWidth() + 400) { groundOffsetX += ground.getRegionWidth(); } rect1.set( planePosition.x + 20, planePosition.y, plane.getKeyFrames()[0].getRegionWidth() - 20, plane.getKeyFrames()[0].getRegionHeight()); for (Rock r : rocks) { if (camera.position.x - r.position.x > 400 + r.image.getRegionWidth()) { boolean isDown = MathUtils.randomBoolean(); r.position.x += 5 * 200; r.position.y = isDown ? 480 - rock.getRegionHeight() : 0; r.image = isDown ? rockDown : rock; r.counted = false; } rect2.set( r.position.x + (r.image.getRegionWidth() - 30) / 2 + 20, r.position.y, 20, r.image.getRegionHeight() - 10); if (rect1.overlaps(rect2)) { if (gameState != GameState.GameOver) explode.play(); gameState = GameState.GameOver; planeVelocity.x = 0; } if (r.position.x < planePosition.x && !r.counted) { score++; r.counted = true; } } if (planePosition.y < ground.getRegionHeight() - 20 || planePosition.y + plane.getKeyFrames()[0].getRegionHeight() > 480 - ground.getRegionHeight() + 20) { if (gameState != GameState.GameOver) explode.play(); gameState = GameState.GameOver; planeVelocity.x = 0; } }
@Override public void render(float delta) { // clear the screen with a dark blue color. The // arguments to glClearColor are the red, green // blue and alpha component in the range [0,1] // of the color to be used to clear the screen. Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // tell the camera to update its matrices. camera.update(); // tell the SpriteBatch to render in the // coordinate system specified by the camera. game.batch.setProjectionMatrix(camera.combined); // Implement pause/resume if (Gdx.input.isKeyJustPressed(Keys.ESCAPE)) { if (state == State.RUN) { setGameState(State.PAUSE); } else { setGameState(State.RUN); } } // begin a new batch and draw the bucket and // all drops game.batch.begin(); game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480); game.batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { game.batch.draw(dropImage, raindrop.x, raindrop.y); } game.batch.end(); // Implement pause/resume switch (state) { case RUN: // process user input if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 64 / 2; } if (Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 64) bucket.x = 800 - 64; // check if we need to create a new raindrop if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop(); // move the raindrops, remove any that are beneath the bottom edge of // the screen or that hit the bucket. In the later case we increase the // value our drops counter and add a sound effect. Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 64 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropsGathered++; dropSound.play(); iter.remove(); } } break; case PAUSE: break; } // end switch }
public boolean collides(Rectangle player) { if (player.overlaps(boundsTop) || player.overlaps(boundsBot)) { return true; } return false; }