Here we will see three types of hazards. If bob collides with them he will die and respawn on the last dispenser/checkpoint.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; public class Laser { static final int FORWARD = 1; static final int BACKWARD = -1; static final float FORWARD_VEL = 10; static final float BACKWARD_VEL = 4; int state = FORWARD; Map map; Vector2 pos = new Vector2(); Vector2 endPoint = new Vector2(); Vector2 cappedEndPoint = new Vector2(); float angle = 0; public Laser (Map map, float x, float y) { this.map = map; pos.x = x; pos.y = y; } public void init () { int ix = (int)pos.x; int iy = map.tiles[0].length - 1 - (int)pos.y; int left = map.tiles[ix - 1][iy]; int right = map.tiles[ix + 1][iy]; int top = map.tiles[ix][iy - 1]; int bottom = map.tiles[ix][iy + 1]; if (left == Map.TILE) { angle = -90; for (int x = ix; x < map.tiles.length; x++) { if (map.tiles[x][iy] == Map.TILE) { endPoint.set(x, pos.y); break; } } } if (right == Map.TILE) { angle = 90; for (int x = ix; x >= 0; x--) { if (map.tiles[x][iy] == Map.TILE) { endPoint.set(x, pos.y); break; } } } if (top == Map.TILE) { angle = 180; for (int y = iy; y < map.tiles[0].length; y++) { if (map.tiles[ix][y] == Map.TILE) { endPoint.set(pos.x, map.tiles[0].length - y - 1); break; } } } if (bottom == Map.TILE) { angle = 0; for (int y = iy; y >= 0; y--) { if (map.tiles[ix][y] == Map.TILE) { endPoint.set(pos.x, map.tiles[0].length - y - 1); break; } } } } Vector2 startPoint = new Vector2(); public void update () { startPoint.set(pos).add(0.5f, 0.5f); cappedEndPoint.set(endPoint).add(0.5f, 0.5f); Rectangle cbounds = map.cube.bounds; Rectangle bbounds = map.bob.bounds; boolean kill = false; if (angle == -90) { if (startPoint.x < cbounds.x && endPoint.x > cbounds.x) { if (cbounds.y < startPoint.y && cbounds.y + cbounds.height > startPoint.y) { cappedEndPoint.x = cbounds.x; } } } if (angle == 90) { if (startPoint.x > cbounds.x && endPoint.x < cbounds.x) { if (cbounds.y < startPoint.y && cbounds.y + cbounds.height > startPoint.y) { cappedEndPoint.x = cbounds.x + cbounds.width; } } } if (angle == 0) { if (startPoint.y < cbounds.y && endPoint.y > cbounds.y) { if (cbounds.x < startPoint.x && cbounds.x + cbounds.width > startPoint.x) { cappedEndPoint.y = cbounds.y; } } } if (angle == 180) { if (startPoint.y > cbounds.y && endPoint.y < cbounds.y) { if (cbounds.x < startPoint.x && cbounds.x + cbounds.width > startPoint.x) { cappedEndPoint.y = cbounds.y + cbounds.height; } } } if (angle == -90) { if (startPoint.x < bbounds.x) { if (bbounds.y < startPoint.y && bbounds.y + bbounds.height > startPoint.y) { if (cappedEndPoint.x > bbounds.x) kill = true; } } } if (angle == 90) { if (startPoint.x > bbounds.x) { if (bbounds.y < startPoint.y && bbounds.y + bbounds.height > startPoint.y) { if (cappedEndPoint.x < bbounds.x + bbounds.width) kill = true; } } } if (angle == 0) { if (pos.y < bbounds.y) { if (bbounds.x < startPoint.x && bbounds.x + bbounds.width > startPoint.x) { if (cappedEndPoint.y > bbounds.y) kill = true; } } } if (angle == 180) { if (pos.y > bbounds.y) { if (bbounds.x < startPoint.x && bbounds.x + bbounds.width > startPoint.x) { if (cappedEndPoint.y < bbounds.y + bbounds.height) kill = true; } } } if (kill && map.bob.state != Bob.DYING) { map.bob.state = Bob.DYING; map.bob.stateTime = 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 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; public class MovingSpikes { static final int FORWARD = 1; static final int BACKWARD = -1; static final float FORWARD_VEL = 10; static final float BACKWARD_VEL = 4; int state = FORWARD; Map map; Rectangle bounds = new Rectangle(); Vector2 vel = new Vector2(); Vector2 pos = new Vector2(); float angle = 0; int fx = 0; int fy = 0; int bx = 0; int by = 0; public MovingSpikes (Map map, float x, float y) { this.map = map; pos.x = x; pos.y = y; bounds.x = x; bounds.y = y; bounds.width = bounds.height = 1; } public void init () { int ix = (int)pos.x; int iy = (int)pos.y; int left = map.tiles[ix - 1][map.tiles[0].length - 1 - iy]; int right = map.tiles[ix + 1][map.tiles[0].length - 1 - iy]; int top = map.tiles[ix][map.tiles[0].length - 1 - iy - 1]; int bottom = map.tiles[ix][map.tiles[0].length - 1 - iy + 1]; if (left == Map.TILE) { vel.x = FORWARD_VEL; angle = -90; fx = 1; } if (right == Map.TILE) { vel.x = -FORWARD_VEL; angle = 90; bx = 1; } if (top == Map.TILE) { vel.y = -FORWARD_VEL; angle = 180; by = -1; } if (bottom == Map.TILE) { vel.y = FORWARD_VEL; angle = 0; fy = -1; } } public void update (float deltaTime) { pos.add(vel.x * deltaTime, vel.y * deltaTime); boolean change = false; if (state == FORWARD) { change = map.tiles[(int)pos.x + fx][map.tiles[0].length - 1 - (int)pos.y + fy] == Map.TILE; } else { change = map.tiles[(int)pos.x + bx][map.tiles[0].length - 1 - (int)pos.y + by] == Map.TILE; } if (change) { pos.x -= vel.x * deltaTime; pos.y -= vel.y * deltaTime; state = -state; vel.scl(-1); if (state == FORWARD) vel.nor().scl(FORWARD_VEL); if (state == BACKWARD) vel.nor().scl(BACKWARD_VEL); } bounds.x = pos.x; bounds.y = pos.y; if (map.bob.bounds.overlaps(bounds)) { if (map.bob.state != Bob.DYING) { map.bob.state = Bob.DYING; map.bob.stateTime = 0; } } if (map.cube.bounds.overlaps(bounds)) { map.cube.state = Cube.DEAD; map.cube.stateTime = 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 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; public class Rocket { static final int FLYING = 0; static final int EXPLODING = 1; static final int DEAD = 2; static final float VELOCITY = 6; Map map; float stateTime = 0; int state = FLYING; Vector2 startPos = new Vector2(); Vector2 pos = new Vector2(); Vector2 vel = new Vector2(); Rectangle bounds = new Rectangle(); Rectangle[] collisionRectangles = {new Rectangle(), new Rectangle(), new Rectangle(), new Rectangle()}; public Rocket (Map map, float x, float y) { this.map = map; this.startPos.set(x, y); this.pos.set(x, y); this.bounds.x = x + 0.2f; this.bounds.y = y + 0.2f; this.bounds.width = 0.6f; this.bounds.height = 0.6f; this.vel.set(-VELOCITY, 0); } public void update (float deltaTime) { if (state == FLYING) { vel.set(map.bob.pos); vel.sub(pos).nor().scl(VELOCITY); pos.add(vel.x * deltaTime, vel.y * deltaTime); bounds.x = pos.x + 0.2f; bounds.y = pos.y + 0.2f; if (checkHit()) { state = EXPLODING; stateTime = 0; } } if (state == EXPLODING) { if (stateTime > 0.6f) { state = FLYING; stateTime = 0; pos.set(startPos); bounds.x = pos.x + 0.2f; bounds.y = pos.y + 0.2f; } } stateTime += deltaTime; } private boolean checkHit () { fetchCollidableRects(); for (int i = 0; i < collisionRectangles.length; i++) { if (bounds.overlaps(collisionRectangles[i])) { return true; } } if (bounds.overlaps(map.bob.bounds)) { if (map.bob.state != Bob.DYING) { map.bob.state = Bob.DYING; map.bob.stateTime = 0; } return true; } if (bounds.overlaps(map.cube.bounds)) { return true; } return false; } 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 (tile1 != Map.EMPTY) collisionRectangles[0].set(p1x, p1y, 1, 1); else collisionRectangles[0].set(-1, -1, 0, 0); if (tile2 != Map.EMPTY) collisionRectangles[1].set(p2x, p2y, 1, 1); else collisionRectangles[1].set(-1, -1, 0, 0); if (tile3 != Map.EMPTY) collisionRectangles[2].set(p3x, p3y, 1, 1); else collisionRectangles[2].set(-1, -1, 0, 0); if (tile4 != Map.EMPTY) collisionRectangles[3].set(p4x, p4y, 1, 1); else collisionRectangles[3].set(-1, -1, 0, 0); } } |