Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game;
- import java.awt.Color;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseEvent;
- import javax.swing.JButton;
- import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
- import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
- import acm.graphics.GCompound;
- import acm.graphics.GLabel;
- import acm.graphics.GPoint;
- import acm.program.GraphicsProgram;
- import game.compounds.PlayField;
- import game.compounds.TileBottomLeft;
- import game.compounds.TileBottomRight;
- import game.compounds.TileCenter;
- import game.compounds.TileTopLeft;
- import game.compounds.TileTopRight;
- import game.controller.TileController;
- import game.utils.Constants;
- import game.utils.MoveDirections;
- public class playfield extends GraphicsProgram {
- private enum Gamestate {
- STARTING, GENERATING, RUNNING, EXITING
- }
- PlayField playField;
- TileTopLeft topLeft;
- TileTopRight topRight;
- TileBottomLeft botLeft;
- TileBottomRight botRight;
- TileCenter center;
- GLabel label;
- JButton button;
- Gamestate gamestate = Gamestate.STARTING;
- GCompound selected;
- MoveDirections moveDirection = MoveDirections.IDLE;
- boolean test = false;
- public static final Color compoundColor = Constants.compoundColor;
- GPoint collisionPoint1 = new GPoint();
- GPoint collisionPoint2 = new GPoint();
- public double collision_x = 0;
- public double collision_y = 0;
- public double one_compound_tile = Constants.TILE_DIAMETER - Constants.COMPOUND_OFFSET;
- @Override
- public void run() {
- setSize(800, 800);
- setUpBoard();
- setupStart();
- // initGame();
- while (gamestate != Gamestate.EXITING) {
- // System.out.println(selectedTile);
- checkSelected();
- // debugTesting();
- moveTile();
- pause(50);
- }
- System.exit(0);
- }
- /**
- * Just a method to test some stuff. TODO: Remove upon release.
- */
- public void debugTesting() {
- TileController controller = new TileController(selected);
- controller.moveDown();
- }
- /**
- * Method to initialize a few things upon starting the game.
- */
- public void init() {
- super.init();
- addKeyListeners();
- addMouseListeners();
- }
- // TODO: Fix, this is just for testing purpose
- public void initGame() {
- button = new JButton("START");
- button.setLocation(40, 40);
- button.setSize(50, 50);
- add(button, SOUTH);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- super.keyPressed(e);
- switch (e.getKeyCode()) {
- case KeyEvent.VK_Q:
- gamestate = Gamestate.EXITING;
- break;
- case KeyEvent.VK_SPACE:
- selectNextTile();
- pause(20);
- break;
- case KeyEvent.VK_UP:
- moveDirection = MoveDirections.UP;
- break;
- case KeyEvent.VK_DOWN:
- moveDirection = MoveDirections.DOWN;
- break;
- case KeyEvent.VK_LEFT:
- moveDirection = MoveDirections.LEFT;
- break;
- case KeyEvent.VK_RIGHT:
- moveDirection = MoveDirections.RIGHT;
- break;
- // nur zum testen
- case KeyEvent.VK_1:
- collision();
- break;
- }
- }
- /**
- * TO get our mouseclicks
- */
- @Override
- public void mouseClicked(MouseEvent e) {
- super.mouseClicked(e);
- }
- /**
- * Selects the next Tile after pressing tab.
- */
- public void selectNextTile() {
- if (selected == null) {
- selected = topLeft;
- } else if (selected.equals(topLeft)) {
- selected = topRight;
- } else if (selected.equals(topRight)) {
- selected = center;
- } else if (selected.equals(center)) {
- selected = botLeft;
- } else if (selected.equals(botLeft)) {
- selected = botRight;
- } else if (selected.equals(botRight)) {
- selected = null;
- }
- }
- /**
- * Gives a color to the currently selected tile and sets all other tiles to
- * their default color.
- */
- public void checkSelected() {
- if (selected == null) {
- clearColor();
- return;
- }
- if (selected.equals(topLeft)) {
- clearColor();
- topLeft.setColor(Color.RED);
- } else if (selected.equals(topRight)) {
- clearColor();
- topRight.setColor(Color.RED);
- } else if (selected.equals(center)) {
- clearColor();
- center.setColor(Color.RED);
- } else if (selected.equals(botLeft)) {
- clearColor();
- botLeft.setColor(Color.RED);
- } else if (selected.equals(botRight)) {
- clearColor();
- botRight.setColor(Color.RED);
- }
- }
- /**
- * Moves the selected Tile to a given Direction.
- */
- public void moveTile() {
- TileController controller = new TileController(selected);
- switch (moveDirection) {
- case UP:
- controller.moveUp();
- moveDirection = MoveDirections.IDLE;
- break;
- case DOWN:
- if (collision()) {
- controller.moveDown();
- moveDirection = MoveDirections.IDLE;
- break;
- } else {
- System.out.println("HALT");
- break;
- }
- case LEFT:
- if (collision()) {
- controller.moveLeft();
- moveDirection = MoveDirections.IDLE;
- break;
- } else {
- System.out.println("HALT");
- break;
- }
- case RIGHT:
- if (collision()) {
- controller.moveRight();
- moveDirection = MoveDirections.IDLE;
- break;
- } else {
- System.out.println("HALT");
- break;
- }
- case IDLE:
- break;
- }
- }
- /**
- * Clears the color of all tiles on the board, to select the next Tile.
- */
- public void clearColor() {
- topLeft.setColor(compoundColor);
- topRight.setColor(compoundColor);
- center.setColor(compoundColor);
- botLeft.setColor(compoundColor);
- botRight.setColor(compoundColor);
- }
- public void setupStart() {
- // TODO: Add some crazy ass shit. Don´t know if we still need this tbh.
- }
- /**
- * Sets up the Boards default state.
- */
- public void setUpBoard() {
- playField = new PlayField(0, 0);
- topLeft = new TileTopLeft(0, 0);
- topRight = new TileTopRight(200, 0);
- botLeft = new TileBottomLeft(0, 200);
- botRight = new TileBottomRight(200, 200);
- center = new TileCenter(100, 100);
- add(playField);
- add(topLeft);
- add(topRight);
- add(botLeft);
- add(botRight);
- add(center);
- }
- public boolean collision() {
- if (selected == null) {
- }
- collision_x = Constants.BOARD_OFFSET_X + selected.getLocation().getX() + Constants.COMPOUND_OFFSET;
- collision_y = Constants.BOARD_OFFSET_Y + selected.getLocation().getY() + Constants.COMPOUND_OFFSET;
- if (selected.equals(topLeft)) {
- switch (moveDirection) {
- case UP:
- break;
- case DOWN:
- collisionPoint1.setLocation(collision_x, collision_y + 2 * Constants.TILE_DIAMETER + one_compound_tile);
- collisionPoint2.setLocation(collision_x + 2 * Constants.TILE_DIAMETER + one_compound_tile, collision_y);
- if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
- System.out.println("The targeted tile is out of bounds");
- return false;
- } else {
- if (getElementAt(collisionPoint1).equals(playField)
- && getElementAt(collisionPoint2).equals(playField)) {
- return true;
- } else {
- return false;
- }
- }
- case LEFT:
- collisionPoint1.setLocation(collision_x - Constants.TILE_DIAMETER, collision_y);
- collisionPoint2.setLocation(collision_x - Constants.TILE_DIAMETER,
- collision_y + Constants.TILE_DIAMETER + one_compound_tile);
- if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
- System.out.println("The targeted tile is out of bounds");
- return false;
- } else {
- if (getElementAt(collisionPoint1).equals(playField)
- && getElementAt(collisionPoint2).equals(playField)) {
- return true;
- } else {
- return false;
- }
- }
- case RIGHT:
- collisionPoint1.setLocation(collision_x, collision_y);
- collisionPoint2.setLocation(collision_x, collision_y + Constants.TILE_DIAMETER);
- if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
- System.out.println("The targeted tile is out of bounds");
- return false;
- } else {
- if (getElementAt(collisionPoint1).equals(playField)
- && getElementAt(collisionPoint2).equals(playField)) {
- return true;
- } else {
- return false;
- }
- }
- case IDLE:
- break;
- }
- } else if (selected.equals(topRight)) {
- } else if (selected.equals(center)) {
- } else if (selected.equals(botLeft)) {
- } else if (selected.equals(botRight)) {
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement