Created
November 13, 2012 04:52
-
-
Save chintanparikh/4063974 to your computer and use it in GitHub Desktop.
Dungeon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.event.KeyEvent; | |
| import java.util.Random; | |
| import java.awt.Point; | |
| import javax.swing.JOptionPane; | |
| import java.util.Timer; | |
| import java.util.TimerTask; | |
| /** | |
| * In order to help learn course concepts, I worked on this homework with Chris Lewis, | |
| * discussed homework topics and issues with Chris Lewis | |
| * | |
| * A class to represent a Dungeon | |
| * | |
| * @author Chintan Parikh | |
| * @version 1 | |
| */ | |
| public class Dungeon extends TimerTask | |
| { | |
| private Player player; | |
| private Creature[] creatures = new Creature[2]; | |
| private char[][] map; | |
| private Tile[][] tileMap = new Tile[17][17]; | |
| private Timer timer; | |
| /** | |
| * A constructor method to create a new Dungeon | |
| */ | |
| public Dungeon() | |
| { | |
| // generate the map | |
| map = MapGenerator.generateMap(5, 5, 2, true, '|', 'x'); | |
| // iterate over the map and create the tileMap | |
| int i = 0; | |
| int j = 0; | |
| for (char[] row : map) | |
| { | |
| for (char tile : row) | |
| { | |
| if (tile == '|') | |
| { | |
| tileMap[i][j] = new WallTile(this, i, j); | |
| } | |
| if (tile == 'x') | |
| { | |
| tileMap[i][j] = new FloorTile(this, i, j); | |
| } | |
| j++; | |
| } | |
| i++; | |
| j = 0; | |
| } | |
| // Insert a trap tile, exit tile and the various creatures | |
| Random r = new Random(); | |
| boolean exit = false, trap = false, player_ = false, enemy1 = false, enemy2 = false; | |
| int x, y = 0; | |
| while (!exit) | |
| { | |
| x = (int)r.nextInt(16) + 1; | |
| y = (int)r.nextInt(16) + 1; | |
| if (tileMap[x][y] instanceof FloorTile) | |
| { | |
| tileMap[x][y] = new ExitTile(this, x, y); | |
| exit = true; | |
| } | |
| } | |
| while (!trap) | |
| { | |
| x = (int)r.nextInt(16) + 1; | |
| y = (int)r.nextInt(16) + 1; | |
| if (tileMap[x][y] instanceof FloorTile) | |
| { | |
| tileMap[x][y] = new TrapTile(this, x, y); | |
| trap = true; | |
| } | |
| } | |
| while (!player_) | |
| { | |
| x = (int)r.nextInt(16) + 1; | |
| y = (int)r.nextInt(16) + 1; | |
| if (tileMap[x][y] instanceof FloorTile) | |
| { | |
| player = new Player(this, x, y); | |
| player_ = true; | |
| } | |
| } | |
| while (!enemy1) | |
| { | |
| x = (int)r.nextInt(16) + 1; | |
| y = (int)r.nextInt(16) + 1; | |
| if (tileMap[x][y] instanceof FloorTile) | |
| { | |
| creatures[0] = new FirstEnemy(this, x, y); | |
| enemy1 = true; | |
| } | |
| } | |
| while (!enemy2) | |
| { | |
| x = (int)r.nextInt(16) + 1; | |
| y = (int)r.nextInt(16) + 1; | |
| if (tileMap[x][y] instanceof FloorTile) | |
| { | |
| creatures[1] = new SecondEnemy(this, x, y); | |
| enemy2 = true; | |
| } | |
| } | |
| timer.schedule(this, 1000, 2000); | |
| } | |
| /** | |
| * A getter method for the player | |
| * | |
| * @return The player instance | |
| */ | |
| public Player getPlayer() | |
| { | |
| return player; | |
| } | |
| /** | |
| * A getter method for a tile | |
| * | |
| * @param tileIndex A point to look up to find the Tile | |
| * @return A Tile | |
| */ | |
| public Tile getTile(Point tileIndex) | |
| { | |
| return tileMap[tileIndex.x][tileIndex.y]; | |
| } | |
| /** | |
| * A getter method for the entire tileMap | |
| * | |
| * @return The tileMap | |
| */ | |
| public Tile[][] getTileMap() | |
| { | |
| return tileMap; | |
| } | |
| /** | |
| * A getter method for the creatures | |
| * | |
| * @return The creatures array | |
| */ | |
| public Creature[] getCreatures() | |
| { | |
| return creatures; | |
| } | |
| /** | |
| * A method to kill a creature | |
| * @param i The index of the creature to kill | |
| */ | |
| public void killCreature(int i) | |
| { | |
| getTile(creatures[i].getPosition()).setOccupant(null); | |
| creatures[i].setPosition(0, 0); | |
| System.out.println(creatures[i].getName() + " was killed!"); | |
| creatures[i] = null; | |
| } | |
| /** | |
| * A method to kill the player | |
| */ | |
| public void killPlayer() | |
| { | |
| getTile(player.getPosition()).setOccupant(null); | |
| player.setPosition(0, 0); | |
| player = null; | |
| System.out.println("Player was killed!\n Game Over!"); | |
| JOptionPane.showMessageDialog(null, "You are dead. Game Over.", "Dead", JOptionPane.INFORMATION_MESSAGE); | |
| } | |
| /** | |
| * The response to a keypress | |
| * @param key The keycode of the key that was pressed | |
| */ | |
| public void press(int key) | |
| { | |
| Point playerPos = player.getPosition(); | |
| int x = playerPos.x; | |
| int y = playerPos.y; | |
| Tile newTile; | |
| if (player != null && player instanceof Player) | |
| { | |
| player.update(key); | |
| } | |
| for (Creature c : creatures) | |
| { | |
| if (c != null && c instanceof Creature) | |
| { | |
| c.update(); | |
| } | |
| } | |
| for (Tile[] row : tileMap) | |
| { | |
| for (Tile tile : row) | |
| { | |
| if (tile instanceof Tile) | |
| { | |
| tile.update(); | |
| } | |
| } | |
| } | |
| } | |
| public void run() | |
| { | |
| for (Creature c : creatures) | |
| { | |
| if (c != null && c instanceof Creature) | |
| { | |
| c.update(); | |
| } | |
| } | |
| for (Tile[] row : tileMap) | |
| { | |
| for (Tile tile : row) | |
| { | |
| if (tile instanceof Tile) | |
| { | |
| tile.update(); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment