code1
liqwidice
0 Likes0 Commentsjava
package ca.liqwidice.mirror.state;
import java.awt.Font;
import java.io.BufferedReader;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import ca.liqwidice.mirror.Button;
import ca.liqwidice.mirror.Game;
import ca.liqwidice.mirror.Level;
import ca.liqwidice.mirror.Squares;
public class GameState extends BasicGameState {
Button btnMainMenu;
Button btnReset;
Button btnLevelSelect;
Button btnQuit;
BufferedReader levelReader;
public static TrueTypeFont font;
public static TrueTypeFont titleFont;
/** Current Level (Modified by the player) */
public static Level curLev;
@Override
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
Game.imgBlank = new Image("res/blank.png");
Game.imgMirrorNW = new Image("res/mirrorNW.png");
Game.imgMirrorNE = Game.imgMirrorNW.copy();
Game.imgMirrorNE.rotate(90F);
Game.imgPointerE = new Image("res/laser_pointerE.png");
Game.imgPointerN = Game.imgPointerE.copy();
Game.imgPointerN.rotate(270F);
Game.imgPointerS = Game.imgPointerE.copy();
Game.imgPointerS.rotate(90F);
Game.imgPointerW = Game.imgPointerE.copy();
Game.imgPointerW.rotate(180F);
Game.imgReceptor = new Image("res/receptor.png");
Game.imgLaser_redNS = new Image("res/laser_red.png");
Game.imgLaser_redNS_s = Game.imgLaser_redNS.getSubImage(0, 0, 7, 25);
Game.imgLaser_redEW = Game.imgLaser_redNS.copy();
Game.imgLaser_redEW.rotate(90F);
Game.imgLaser_redEW_s = Game.imgLaser_redNS.getSubImage(0, 0, 7, 25);
Game.imgLaser_redEW_s.rotate(90f);
//Game.imgLaser_greenNS = new Image("res/laser_green.png");
//Game.imgLaser_greenEW = Game.imgLaser_greenNS.copy();
//Game.imgLaser_greenEW.rotate(90F);
//Game.laser_blueNS = new Image("res/laser_blue.png");
//Game.laser_blueEW = Game.laser_blueNS.copy();
//Game.laser_blueEW.rotate(90F);
Game.imgBuffer = new Image("res/buffer.png");
Game.imgLouder = new Image("res/louder.png");
Game.imgQuieter = new Image("res/quieter.png");
Game.sndSelect = new Sound("res/select.wav");
font = new TrueTypeFont(new Font("System", Font.PLAIN, 21), true);
titleFont = new TrueTypeFont(new Font("System", Font.BOLD, 36), true);
curLev = new Level(1);
btnMainMenu = new Button(10, 35, 125, 40);
btnLevelSelect = new Button(10, 90, 135, 40);
btnReset = new Button(10, 145, 80, 40);
btnQuit = new Button(10, 200, 65, 40);
}
@SuppressWarnings("incomplete-switch")
@Override
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
Input input = container.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
if (input.isKeyPressed(Input.KEY_ESCAPE)) {
if (input.isKeyDown(Input.KEY_ESCAPE)) {
Game.sndSelect.play(Game.pitch, Game.volume);
sbg.enterState(States.MENUSTATE.ordinal());
}
}
//Click check in game grid
if (Mouse.isButtonDown(0) && !Game.clicked) //Mouse is clicked
if (Game.mouseInBounds(mouseX, mouseY, new Button(216, 0, Game.WIDTH - 216, Game.HEIGHT))) { //Mouse is somewhere in grid
int row, column;
if (((row = getRow(mouseY, curLev)) != -1) && ((column = getCollumn(mouseY, curLev)) != -1)) { //We know what row / column the click is in
switch (curLev.squares[row][column]) {
case NWMIRROR:
case NWMIRROR_A:
curLev.squares[0][0] = Squares.NEMIRROR; //rotate
break;
case NEMIRROR:
case NEMIRROR_A:
curLev.squares[0][0] = Squares.NWMIRROR; //rotate
break;
}
updateRow(row, 0, true);
}
}
//Update game grid
for (int x = 0; x < curLev.squares[0].length; x++) {
for (int y = 0; y < curLev.squares.length; y++) {
//check if square is a laser pointer or laser
switch (curLev.squares[x][y]) {
case NPOINTER:
//check column up
updateCol(y, x, false);
break;
case EPOINTER:
//check row to the right
updateRow(x, y, true);
break;
case SPOINTER:
//check column down
updateCol(y, x, true);
break;
case WPOINTER:
//check row to the left
updateRow(x, y, false);
break;
}
}
}
//btnMainMenu
if (Game.mouseInBounds(mouseX, mouseY, btnMainMenu)) {
if (Mouse.isButtonDown(0) && !Game.clicked) { //Mouse is clicking
Game.sndSelect.play(Game.pitch, Game.volume);
sbg.enterState(States.MAINMENUSTATE.ordinal());
}
btnMainMenu.hover = true; //Mouse in bounds but not down (Hovering)
} else btnMainMenu.hover = false;
//btnReset
if (Game.mouseInBounds(mouseX, mouseY, btnReset)) {
if (Mouse.isButtonDown(0) && !Game.clicked) {
Game.sndSelect.play(0.6f, Game.volume);
try {
Thread.sleep(80);
} catch (InterruptedException e) { //Fancy warning sounds
e.printStackTrace();
}
Game.sndSelect.play(0.9f, Game.volume);
curLev = new Level(curLev.currrentLevel);
}
btnReset.hover = true;
} else btnReset.hover = false;
//btnLevelSelect
if (Game.mouseInBounds(mouseX, mouseY, btnLevelSelect)) {
if (Mouse.isButtonDown(0) && !Game.clicked) {
Game.sndSelect.play(Game.pitch, Game.volume);
sbg.enterState(States.LEVELSELECTSTATE.ordinal());
}
btnLevelSelect.hover = true;
} else btnLevelSelect.hover = false;
//btnQuit
if (Game.mouseInBounds(mouseX, mouseY, btnQuit)) {
if (Mouse.isButtonDown(0) && !Game.clicked) {
Game.sndSelect.play(Game.pitch, Game.volume);
sbg.enterState(States.CONFIRMQUITSTATE.ordinal());
}
btnQuit.hover = true;
} else btnQuit.hover = false;
//if laser is hitting receptor: next level? You Win! screen? currentLevel++;
Game.clicked = Mouse.isButtonDown(0); //Must be last line of method
}
/** @param row 0 = top row
* @param start starting location
* @param dir true = right, false = left */
@SuppressWarnings("incomplete-switch")
public void updateRow(int row, int start, boolean dir) {
//1. Either start at the next square, or the previous
//2. Either loop until i = 0, or i = the row's length
//3. Either increment or decrement i
for (int i = start + (dir ? 1 : -1); dir ? i > 0 : i < curLev.squares[row].length; i += (dir ? (1) : (-1))) { //start at next square and go right 'till you hit the right side
switch (curLev.squares[row][i]) {
case BLANK:
curLev.squares[row][i] = Squares.LASER_RED_EW;
case NEMIRROR:
curLev.squares[row][i] = Squares.LASER_RED_EW;
break;
case NEMIRROR_A:
curLev.squares[row][i] = Squares.LASER_RED_EW;
break;
case NWMIRROR:
curLev.squares[row][i] = Squares.LASER_RED_EW;
break;
case NWMIRROR_A:
curLev.squares[row][i] = Squares.LASER_RED_EW;
break;
case RECEPTOR:
curLev.squares[row][i] = Squares.LASER_RED_EW;
break;
}
}
}
/** @param row 0 = top row
* @param start starting location
* @param dir true = down, false = up */
@SuppressWarnings("incomplete-switch")
public void updateCol(int col, int start, boolean dir) {
if (dir) {
for (int i = start + 1; i < curLev.squares[col].length; i++) { //start at square below and go down 'till you hit the bottom
switch (curLev.squares[i][start]) {
case BLANK:
curLev.squares[i][start] = Squares.LASER_RED_NS;
break;
case NEMIRROR:
case NEMIRROR_A:
updateRow(i, start, !dir);
break;
case NWMIRROR:
case NWMIRROR_A:
updateRow(i, start, dir);
break;
case RECEPTOR:
break;
}
//if mirror -> update row
}
} else {
for (int i = start - 1; i > 0; i--) { //start at square above and go up 'till you hit the top
//if mirror -> update row
}
}
}
@SuppressWarnings("incomplete-switch")
@Override
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
//Sidebar Graphic
Game.imgBuffer.draw(); //(0, 0)
//Game Grid
int buffer = Game.imgBuffer.getWidth();
for (int y = 0; y < curLev.squares.length; y++) { //Rows
for (int x = 0; x < curLev.squares[0].length; x++) { //Columns
Game.imgBlank.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH); //Fill grid with blanks
switch (curLev.squares[x][y]) {
case BLANK:
//0's are just place holders (Blanks)
break;
case NPOINTER:
Game.imgPointerN.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redNS_s.draw(x * Game.TILE_WIDTH + buffer + 50, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 62);
break;
case EPOINTER:
Game.imgPointerE.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redEW_s.draw(x * Game.TILE_WIDTH + buffer + 100, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 12);
break;
case SPOINTER:
Game.imgPointerS.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redNS_s.draw(x * Game.TILE_WIDTH + buffer + 50, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) + 37);
break;
case WPOINTER:
Game.imgPointerW.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redEW_s.draw(x * Game.TILE_WIDTH + buffer + 1, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 12);
break;
case NWMIRROR:
Game.imgMirrorNW.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
break;
case NWMIRROR_A:
Game.imgMirrorNW.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redNS_s.draw(x * Game.TILE_WIDTH + buffer + 50, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 62);
Game.imgLaser_redEW_s.draw(x * Game.TILE_WIDTH + buffer + 1, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 12);
break;
case NEMIRROR:
Game.imgMirrorNE.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
break;
case NEMIRROR_A:
Game.imgMirrorNE.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
Game.imgLaser_redNS_s.draw(x * Game.TILE_WIDTH + buffer + 50, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 62);
Game.imgLaser_redEW_s.draw(x * Game.TILE_WIDTH + buffer + 1, y * Game.TILE_WIDTH + (Game.TILE_WIDTH / 2) - 12);
break;
case RECEPTOR:
Game.imgReceptor.draw(x * Game.TILE_WIDTH + buffer, y * Game.TILE_WIDTH);
break;
case LASER_RED_NS:
Game.imgLaser_redNS.draw(x * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2), y * Game.TILE_WIDTH);
break;
case LASER_RED_EW:
Game.imgLaser_redEW.draw(x * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2) - 2, y * Game.TILE_WIDTH);
break;
// case LASER_BLUE_EW:
//Unimplemented
//Game.laser_blueEW.draw(j * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2) - 2, i * Game.TILE_WIDTH);
// break;
// case LASER_BLUE_NS:
//Unimplemented
//Game.laser_blueNS.draw(j * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2), i * Game.TILE_WIDTH);
// break;
// case LASER_GREEN_EW:
//Unimplemented
//Game.imgLaser_greenEW.draw(j * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2) - 2, i * Game.TILE_WIDTH);
// break;
// case LASER_GREEN_NS:
//Unimplemented
//Game.imgLaser_greenNS.draw(j * Game.TILE_WIDTH + buffer + (Game.TILE_WIDTH / 2), i * Game.TILE_WIDTH);
// break;
}
}
g.setFont(GameState.font);
Game.drawButton(g, btnMainMenu, "Main Menu", 10, 5);
Game.drawButton(g, btnReset, "Reset", 10, 5);
Game.drawButton(g, btnLevelSelect, "Level Select", 10, 5);
Game.drawButton(g, btnQuit, "Quit", 10, 5);
}
}
/** @returns game grid row (0 being uppermost) or -1 for out of bounds */
private int getRow(int y, Level level) {
if (y > Game.HEIGHT || y < 0) return -1;
int imgHeight = Game.imgBlank.getHeight();
for (int i = 0; i < level.squares[0].length; i++)
//For every square in the row (# of squares in array)
if (y <= imgHeight * (i + 1)) return i;
return -1;
}
/** @returns game grid column (0 being leftmost) or -1 for out of bounds */
private int getCollumn(int x, Level level) {
if (x > Game.WIDTH || x < 0) return -1;
int buffer = Game.imgBuffer.getWidth(); //left side where there is no grid
int imgWidth = Game.imgBlank.getWidth();
if (x <= buffer) return -1; //mouse is in the buffer area
for (int i = 0; i < level.squares.length; i++)
//For every square in the column (# of arrays)
if (x <= buffer + (imgWidth * (i + 1))) return i;
return -1;
}
@Override
public int getID() {
return States.GAMESTATE.ordinal();
}
}
You can change the width to suit the size area that you will be embedding to