Advertisement
IronTomman

Untitled

Feb 27th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. package game;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.MouseEvent;
  6.  
  7. import javax.swing.JButton;
  8.  
  9. import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
  10. import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
  11.  
  12. import acm.graphics.GCompound;
  13. import acm.graphics.GLabel;
  14. import acm.graphics.GPoint;
  15. import acm.program.GraphicsProgram;
  16. import game.compounds.PlayField;
  17. import game.compounds.TileBottomLeft;
  18. import game.compounds.TileBottomRight;
  19. import game.compounds.TileCenter;
  20. import game.compounds.TileTopLeft;
  21. import game.compounds.TileTopRight;
  22. import game.controller.TileController;
  23. import game.utils.Constants;
  24. import game.utils.MoveDirections;
  25.  
  26. public class playfield extends GraphicsProgram {
  27.  
  28.     private enum Gamestate {
  29.  
  30.         STARTING, GENERATING, RUNNING, EXITING
  31.  
  32.     }
  33.  
  34.     PlayField playField;
  35.     TileTopLeft topLeft;
  36.     TileTopRight topRight;
  37.     TileBottomLeft botLeft;
  38.     TileBottomRight botRight;
  39.     TileCenter center;
  40.     GLabel label;
  41.     JButton button;
  42.     Gamestate gamestate = Gamestate.STARTING;
  43.     GCompound selected;
  44.     MoveDirections moveDirection = MoveDirections.IDLE;
  45.     boolean test = false;
  46.     public static final Color compoundColor = Constants.compoundColor;
  47.     GPoint collisionPoint1 = new GPoint();
  48.     GPoint collisionPoint2 = new GPoint();
  49.     public double collision_x = 0;
  50.     public double collision_y = 0;
  51.     public double one_compound_tile = Constants.TILE_DIAMETER - Constants.COMPOUND_OFFSET;
  52.  
  53.     @Override
  54.     public void run() {
  55.  
  56.         setSize(800, 800);
  57.         setUpBoard();
  58.         setupStart();
  59.         // initGame();
  60.  
  61.         while (gamestate != Gamestate.EXITING) {
  62.             // System.out.println(selectedTile);
  63.             checkSelected();
  64.             // debugTesting();
  65.             moveTile();
  66.             pause(50);
  67.         }
  68.  
  69.         System.exit(0);
  70.     }
  71.  
  72.     /**
  73.      * Just a method to test some stuff. TODO: Remove upon release.
  74.      */
  75.     public void debugTesting() {
  76.         TileController controller = new TileController(selected);
  77.         controller.moveDown();
  78.     }
  79.  
  80.     /**
  81.      * Method to initialize a few things upon starting the game.
  82.      */
  83.     public void init() {
  84.         super.init();
  85.         addKeyListeners();
  86.         addMouseListeners();
  87.     }
  88.  
  89.     // TODO: Fix, this is just for testing purpose
  90.     public void initGame() {
  91.         button = new JButton("START");
  92.         button.setLocation(40, 40);
  93.         button.setSize(50, 50);
  94.         add(button, SOUTH);
  95.     }
  96.  
  97.     @Override
  98.     public void keyPressed(KeyEvent e) {
  99.         super.keyPressed(e);
  100.         switch (e.getKeyCode()) {
  101.         case KeyEvent.VK_Q:
  102.             gamestate = Gamestate.EXITING;
  103.             break;
  104.         case KeyEvent.VK_SPACE:
  105.             selectNextTile();
  106.             pause(20);
  107.             break;
  108.         case KeyEvent.VK_UP:
  109.             moveDirection = MoveDirections.UP;
  110.             break;
  111.         case KeyEvent.VK_DOWN:
  112.             moveDirection = MoveDirections.DOWN;
  113.             break;
  114.         case KeyEvent.VK_LEFT:
  115.             moveDirection = MoveDirections.LEFT;
  116.             break;
  117.         case KeyEvent.VK_RIGHT:
  118.             moveDirection = MoveDirections.RIGHT;
  119.             break;
  120.         // nur zum testen
  121.         case KeyEvent.VK_1:
  122.             collision();
  123.             break;
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * TO get our mouseclicks
  129.      */
  130.     @Override
  131.     public void mouseClicked(MouseEvent e) {
  132.         super.mouseClicked(e);
  133.     }
  134.  
  135.     /**
  136.      * Selects the next Tile after pressing tab.
  137.      */
  138.     public void selectNextTile() {
  139.         if (selected == null) {
  140.             selected = topLeft;
  141.         } else if (selected.equals(topLeft)) {
  142.             selected = topRight;
  143.         } else if (selected.equals(topRight)) {
  144.             selected = center;
  145.         } else if (selected.equals(center)) {
  146.             selected = botLeft;
  147.         } else if (selected.equals(botLeft)) {
  148.             selected = botRight;
  149.         } else if (selected.equals(botRight)) {
  150.             selected = null;
  151.         }
  152.  
  153.     }
  154.  
  155.     /**
  156.      * Gives a color to the currently selected tile and sets all other tiles to
  157.      * their default color.
  158.      */
  159.     public void checkSelected() {
  160.         if (selected == null) {
  161.             clearColor();
  162.             return;
  163.         }
  164.         if (selected.equals(topLeft)) {
  165.             clearColor();
  166.             topLeft.setColor(Color.RED);
  167.         } else if (selected.equals(topRight)) {
  168.             clearColor();
  169.             topRight.setColor(Color.RED);
  170.         } else if (selected.equals(center)) {
  171.             clearColor();
  172.             center.setColor(Color.RED);
  173.         } else if (selected.equals(botLeft)) {
  174.             clearColor();
  175.             botLeft.setColor(Color.RED);
  176.         } else if (selected.equals(botRight)) {
  177.             clearColor();
  178.             botRight.setColor(Color.RED);
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Moves the selected Tile to a given Direction.
  184.      */
  185.     public void moveTile() {
  186.         TileController controller = new TileController(selected);
  187.         switch (moveDirection) {
  188.         case UP:
  189.             controller.moveUp();
  190.             moveDirection = MoveDirections.IDLE;
  191.             break;
  192.         case DOWN:
  193.             if (collision()) {
  194.                 controller.moveDown();
  195.                 moveDirection = MoveDirections.IDLE;
  196.                 break;
  197.             } else {
  198.                 System.out.println("HALT");
  199.                 break;
  200.             }
  201.         case LEFT:
  202.             if (collision()) {
  203.                 controller.moveLeft();
  204.                 moveDirection = MoveDirections.IDLE;
  205.                 break;
  206.             } else {
  207.                 System.out.println("HALT");
  208.                 break;
  209.             }
  210.         case RIGHT:
  211.             if (collision()) {
  212.                 controller.moveRight();
  213.                 moveDirection = MoveDirections.IDLE;
  214.                 break;
  215.             } else {
  216.                 System.out.println("HALT");
  217.                 break;
  218.             }
  219.         case IDLE:
  220.             break;
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Clears the color of all tiles on the board, to select the next Tile.
  226.      */
  227.     public void clearColor() {
  228.         topLeft.setColor(compoundColor);
  229.         topRight.setColor(compoundColor);
  230.         center.setColor(compoundColor);
  231.         botLeft.setColor(compoundColor);
  232.         botRight.setColor(compoundColor);
  233.     }
  234.  
  235.     public void setupStart() {
  236.         // TODO: Add some crazy ass shit. Don´t know if we still need this tbh.
  237.     }
  238.  
  239.     /**
  240.      * Sets up the Boards default state.
  241.      */
  242.     public void setUpBoard() {
  243.         playField = new PlayField(0, 0);
  244.         topLeft = new TileTopLeft(0, 0);
  245.         topRight = new TileTopRight(200, 0);
  246.         botLeft = new TileBottomLeft(0, 200);
  247.         botRight = new TileBottomRight(200, 200);
  248.         center = new TileCenter(100, 100);
  249.         add(playField);
  250.         add(topLeft);
  251.         add(topRight);
  252.         add(botLeft);
  253.         add(botRight);
  254.         add(center);
  255.     }
  256.  
  257.     public boolean collision() {
  258.         if (selected == null) {
  259.         }
  260.         collision_x = Constants.BOARD_OFFSET_X + selected.getLocation().getX() + Constants.COMPOUND_OFFSET;
  261.         collision_y = Constants.BOARD_OFFSET_Y + selected.getLocation().getY() + Constants.COMPOUND_OFFSET;
  262.         if (selected.equals(topLeft)) {
  263.             switch (moveDirection) {
  264.             case UP:
  265.                 break;
  266.             case DOWN:
  267.                 collisionPoint1.setLocation(collision_x, collision_y + 2 * Constants.TILE_DIAMETER + one_compound_tile);
  268.                 collisionPoint2.setLocation(collision_x + 2 * Constants.TILE_DIAMETER + one_compound_tile, collision_y);
  269.  
  270.                 if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
  271.                     System.out.println("The targeted tile is out of bounds");
  272.                     return false;
  273.                 } else {
  274.                     if (getElementAt(collisionPoint1).equals(playField)
  275.                             && getElementAt(collisionPoint2).equals(playField)) {
  276.                         return true;
  277.                     } else {
  278.                         return false;
  279.                     }
  280.                 }
  281.             case LEFT:
  282.                 collisionPoint1.setLocation(collision_x - Constants.TILE_DIAMETER, collision_y);
  283.                 collisionPoint2.setLocation(collision_x - Constants.TILE_DIAMETER,
  284.                         collision_y + Constants.TILE_DIAMETER + one_compound_tile);
  285.                 if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
  286.                     System.out.println("The targeted tile is out of bounds");
  287.                     return false;
  288.                 } else {
  289.                     if (getElementAt(collisionPoint1).equals(playField)
  290.                             && getElementAt(collisionPoint2).equals(playField)) {
  291.                         return true;
  292.                     } else {
  293.                         return false;
  294.                     }
  295.                 }
  296.             case RIGHT:
  297.                 collisionPoint1.setLocation(collision_x, collision_y);
  298.                 collisionPoint2.setLocation(collision_x, collision_y + Constants.TILE_DIAMETER);
  299.                 if (getElementAt(collisionPoint1) == null || getElementAt(collisionPoint2) == null) {
  300.                     System.out.println("The targeted tile is out of bounds");
  301.                     return false;
  302.                 } else {
  303.                     if (getElementAt(collisionPoint1).equals(playField)
  304.                             && getElementAt(collisionPoint2).equals(playField)) {
  305.                         return true;
  306.                     } else {
  307.                         return false;
  308.                     }
  309.                 }
  310.             case IDLE:
  311.                 break;
  312.             }
  313.  
  314.         } else if (selected.equals(topRight)) {
  315.         } else if (selected.equals(center)) {
  316.         } else if (selected.equals(botLeft)) {
  317.         } else if (selected.equals(botRight)) {
  318.         }
  319.         return false;
  320.  
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement