import java.util.ArrayList; import java.util.Random; public class World { public static final int SHOT_HIT_GROUND = -3; public static final int SHOT_OUT_OF_BOUNDS = -2; public static final int SHOT_IN_FLIGHT = -1; public static final int MAX_SLOPE = 3; public static final double GRAVITY = 9.8; private Player[] players; private int width; private int height; private int playerSize; private Terrain terrain; private Shot lastShot; private int[][] shotToBePrinted; public World(int numberOfPlayers, int worldWidth, int worldHeight, int playerSize) { players = new Player[numberOfPlayers]; this.width = worldWidth; this.height = worldHeight; this.playerSize = playerSize; init(); } private void init() { // Randomly generate terrain. terrain = new Terrain(width, height); // Randomly place players. Random random = new Random(); int spacePerPlayer = width / players.length; int currentSlotStart = 0; for (int i = 0; i < players.length; i++) { int location = Math.abs(random.nextInt()) % spacePerPlayer; if (location < playerSize) { location += playerSize * 2; } else if (location > spacePerPlayer - playerSize) { location -= playerSize * 2; } location += currentSlotStart; players[i] = new Player(playerSize, playerSize, location, 100); currentSlotStart += spacePerPlayer; } resetLastShot(); } public void reset() { init(); } public int getPlayerLocation(int index) { return players[index].getXCoordinate(); } public int getAltitude(int x) { return terrain.getAltitude(x); } // For a given x coordinate, get the y for the shot. public int getLastShotHeight(int x) { if (lastShot == null) { return -1; } else { return lastShot.getYForX(x); } } private void resetLastShot() { lastShot = null; } // Return constant representing the outcome of the shot (or the index of the player that was killed). public int doShot(int playerIndex, int velocity, int angle) { resetLastShot(); int[] result = new int[] {SHOT_IN_FLIGHT, -1}; int initialX = players[playerIndex].getXCoordinate(); int initialY = terrain.getAltitude(initialX) + players[playerIndex].getHeight(); int x = initialX; int adder = 1; angle = Math.abs(angle % 181); if (angle > 90) { adder = -1; } else if (angle == 90) { return playerIndex; } int[][] buffer = new int[2][width]; int count = 0; while (result[0] == SHOT_IN_FLIGHT) { x += adder; result = getTrajectoryY(playerIndex, x, initialX, initialY, velocity, angle); if (result[0] == SHOT_IN_FLIGHT) { buffer[0][count] = x; buffer[1][count] = height - result[1]; count++; } } shotToBePrinted = new int[2][count]; for (int i = 0; i < count; i++) { shotToBePrinted[0][i] = buffer[0][i]; shotToBePrinted[1][i] = buffer[1][i]; } return result[0]; } // Returns an integer array with the y value and the status of the shot. private int[] getTrajectoryY(int playerIndex, int x, int initialX, int initialY, int velocity, int angle) { if (x >= width || x < 0) { return new int[] {SHOT_OUT_OF_BOUNDS, -1}; } if (lastShot == null) { lastShot = new Shot(initialX, initialY, velocity, angle, 100); } int y = lastShot.getYForX(x); for (int i = 0; i < players.length; i++) { // Shooting player cannot be killed by own bullet. if (i == playerIndex) { continue; } // Check to see if shot hit player. if (players[i].getXCoordinate() - playerSize < x && players[i].getXCoordinate() + playerSize > x && terrain.getAltitude(x) + 2 * playerSize > y && y > terrain.getAltitude(x)) { return new int[] {i, y}; } } if (y <= terrain.getAltitude(x)) { return new int[] {SHOT_HIT_GROUND, y}; } return new int[] {SHOT_IN_FLIGHT, y}; } public void setDead(int playerIndex) { players[playerIndex].setHealth(0); } public boolean isDead(int playerIndex) { return !(players[playerIndex].isAlive()); } // Get terrain for printing purposes. public int[][] getLandPolygon() { int[][] land = new int[2][width + 2]; for (int i = 0; i < width; i++) { land[0][i] = i; land[1][i] = height - terrain.getAltitude(i); } land[0][width] = width - 1; land[1][width] = height; land[0][width + 1] = 0; land[1][width + 1] = height; return land; } // Get last shot for printing purposes. public int[][] getLastShot() { return shotToBePrinted; } // Since these three variables are passed in, probably not necessary. public int getNumPlayers() {return players.length;} public int getWidth() {return width;} public int getHeight() {return height;} public int getPlayerSize() {return playerSize;} }