Like the previous part for animations heere we will look into collision and camera logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
private void tryMove () { bounds.x += vel.x; fetchCollidableRects(); for (int i = 0; i < collisionRectangles.length; i++) { Rectangle rect = collisionRectangles[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 < collisionRectangles.length; i++) { Rectangle rect = collisionRectangles[i]; if (bounds.overlaps(rect)) { if (vel.y < 0) { bounds.y = rect.y + rect.height + 0.01f; grounded = true; if (state != DYING && state != SPAWN) state = Math.abs(accel.x) > 0.1f ? RUN : IDLE; } else bounds.y = rect.y - bounds.height - 0.01f; vel.y = 0; } } pos.x = bounds.x - 0.2f; pos.y = bounds.y; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
private void fetchCollidableRects () { int p1x = (int)bounds.x; int p1y = (int)Math.floor(bounds.y); int p2x = (int)(bounds.x + bounds.width); int p2y = (int)Math.floor(bounds.y); int p3x = (int)(bounds.x + bounds.width); int p3y = (int)(bounds.y + bounds.height); int p4x = (int)bounds.x; int p4y = (int)(bounds.y + bounds.height); int[][] tiles = map.tiles; int tile1 = tiles[p1x][map.tiles[0].length - 1 - p1y]; int tile2 = tiles[p2x][map.tiles[0].length - 1 - p2y]; int tile3 = tiles[p3x][map.tiles[0].length - 1 - p3y]; int tile4 = tiles[p4x][map.tiles[0].length - 1 - p4y]; if (state != DYING && (map.isDeadly(tile1) || map.isDeadly(tile2) || map.isDeadly(tile3) || map.isDeadly(tile4))) { state = DYING; stateTime = 0; } if (tile1 == Map.TILE) { collisionRectangles[0].set(p1x, p1y, 1, 1); } else { collisionRectangles[0].set(-1, -1, 0, 0); } if (tile2 == Map.TILE) { collisionRectangles[1].set(p2x, p2y, 1, 1); } else { collisionRectangles[1].set(-1, -1, 0, 0); } if (tile3 == Map.TILE) { collisionRectangles[2].set(p3x, p3y, 1, 1); } else { collisionRectangles[2].set(-1, -1, 0, 0); } if (tile4 == Map.TILE) { collisionRectangles[3].set(p4x, p4y, 1, 1); } else { collisionRectangles[3].set(-1, -1, 0, 0); } if (map.cube.state == Cube.FIXED) { collisionRectangles[4].x = map.cube.bounds.x; collisionRectangles[4].y = map.cube.bounds.y; collisionRectangles[4].width = map.cube.bounds.width; collisionRectangles[4].height = map.cube.bounds.height; } else collisionRectangles[4].set(-1, -1, 0, 0); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public void render (float deltaTime) { if (map.cube.state != Cube.CONTROLLED){ cam.position.lerp(lerpTarget.set(map.bob.pos.x, map.bob.pos.y, 0), 2f * deltaTime); }else{ cam.position.lerp(lerpTarget.set(map.cube.pos.x, map.cube.pos.y, 0), 2f * deltaTime); } cam.update(); cache.setProjectionMatrix(cam.combined); Gdx.gl.glDisable(GL20.GL_BLEND); cache.begin(); for (int blockY = 0; blockY < 4; blockY++) { for (int blockX = 0; blockX < 6; blockX++) { cache.draw(blocks[blockX][blockY]); } } cache.end(); stateTime += deltaTime; batch.setProjectionMatrix(cam.combined); batch.begin(); renderBob(); renderCube(); renderDispensers(); batch.end(); } |