1. // Import everything
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. import java.awt.image.BufferedImage;
  6.  
  7. import java.util.*;
  8.  
  9. import javax.swing.*;
  10.  
  11. // The class
  12. public class Game extends JFrame implements MouseListener, MouseMotionListener {
  13.    
  14.     // GUI Varuables
  15.     Container container;
  16.     JButton leftButton, rightButton, stopButton, addUnitButton, sprintButton;
  17.    
  18.     // Animator
  19.     Animator animatorHuman;
  20.    
  21.     // Insets
  22.     Insets insets;
  23.    
  24.     // Images
  25.     ImageIcon leftButtonIcon, rightButtonIcon, stopButtonIcon, sprintButtonIcon, addUnitButtonIcon;
  26.     BufferedImage backBuffer;
  27.     Images images = new Images();
  28.    
  29.     // Scrolling Variables
  30.     int focus_x, focus_y;
  31.    
  32.     // Unit Variables
  33.     ArrayList units;
  34.    
  35.     // Block Variables
  36.     ArrayList blocks;
  37.     boolean blockMode = false;
  38.    
  39.     // Holder Variables
  40.     Point selectionStart, selectionEnd;
  41.     Rectangle selectionRectangle;
  42.     static final int STOP = 0, LEFT = 1, RIGHT = 2;
  43.     boolean isMouseDragging = false;
  44.     boolean update = false;
  45.    
  46.     public Game() {
  47.         // Setup the window
  48.         setTitle("Project Deterioration - Build #76");
  49.         setSize(800, 600);
  50.         setResizable(false);
  51.         setLocationRelativeTo(null);
  52.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53.         setVisible(true);
  54.        
  55.         // Setup GamePlay
  56.         setupGameplay();
  57.        
  58.         // Setup GUI
  59.         setupGUI();
  60.     }
  61.    
  62.     public void setupGUI() {
  63.         // Initiate the container
  64.         container = getContentPane();
  65.         container.setLayout(null);
  66.         setContentPane(container);
  67.        
  68.         // Initiate the ImageIcons
  69.         leftButtonIcon = new ImageIcon("images/game/left.png");
  70.         rightButtonIcon = new ImageIcon("images/game/right.png");
  71.         stopButtonIcon = new ImageIcon("images/game/stop.png");
  72.         sprintButtonIcon = new ImageIcon("images/game/sprint.png");
  73.         addUnitButtonIcon = new ImageIcon("images/game/addUnit.png");
  74.        
  75.         // The Buttons
  76.         ButtonListener listener = new ButtonListener();
  77.        
  78.         leftButton = new JButton(leftButtonIcon);
  79.         rightButton = new JButton(rightButtonIcon);
  80.         stopButton = new JButton(stopButtonIcon);
  81.         addUnitButton = new JButton(addUnitButtonIcon);
  82.         sprintButton = new JButton(sprintButtonIcon);
  83.        
  84.         leftButton.setBounds(600, 450, 50, 50);
  85.         rightButton.setBounds(650, 450, 50, 50);
  86.         stopButton.setBounds(700, 450, 50, 50);
  87.         sprintButton.setBounds(750, 450, 50, 50);
  88.         addUnitButton.setBounds(600, 500, 50, 50);
  89.        
  90.         leftButton.addActionListener(listener);
  91.         rightButton.addActionListener(listener);
  92.         stopButton.addActionListener(listener);
  93.         sprintButton.addActionListener(listener);
  94.         addUnitButton.addActionListener(listener);
  95.        
  96.         container.add(leftButton);
  97.         container.add(rightButton);
  98.         container.add(stopButton);
  99.         container.add(sprintButton);
  100.         container.add(addUnitButton);
  101.         container.repaint();
  102.     }
  103.    
  104.     public void setupGameplay() {
  105.         // Add Mouse Listeners
  106.         addMouseListener(this);
  107.         addMouseMotionListener(this);
  108.        
  109.         // Double Buffering
  110.         backBuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
  111.        
  112.         // Setup ArrayList
  113.         units = new ArrayList();
  114.         blocks = new ArrayList();
  115.        
  116.         // Setup Animation
  117.         animatorHuman = new Animator(new Images().humanRightImages);
  118.        
  119.         // Setup Insets
  120.         insets = getInsets();
  121.         setSize(insets.left + 800 + insets.right,
  122.                 insets.top + 600 + insets.bottom);
  123.                
  124.         // Setup SandboxWorld
  125.         int i = 0;
  126.         while (i < 800) {
  127.             blocks.add(new Block(i + focus_x, 442 + focus_y));
  128.             i += 26;
  129.         }
  130.        
  131.         // Setup Thread
  132.         Move m = new Move();
  133.         m.start();
  134.     }
  135.    
  136.     // Mouse methods I'm not using...
  137.     public void mouseEntered(MouseEvent e) {}
  138.     public void mouseExited(MouseEvent e) {}
  139.    
  140.     // And mouse methods I am using...
  141.     public void mousePressed(MouseEvent e) {
  142.         if (!e.isMetaDown() && !e.isAltDown()) {
  143.             isMouseDragging = true;
  144.             selectionStart = new Point(e.getX(), e.getY());
  145.         }
  146.     }
  147.    
  148.     public void mouseReleased(MouseEvent e) {
  149.         if (isMouseDragging == true) {
  150.             isMouseDragging = false;
  151.             selectionEnd = new Point(e.getX(), e.getY());
  152.             int width = selectionEnd.x - selectionStart.x;
  153.             int height = selectionEnd.y - selectionStart.y;
  154.             selectionRectangle = new Rectangle(selectionStart.x, selectionStart.y, width, height);
  155.            
  156.             for (int i = 0; i < units.size(); i++) {
  157.                 Unit temp = (Unit) units.get(i);
  158.                
  159.                 int x = temp.getXPos();
  160.                 int y = temp.getYPos();
  161.                 int tempX = x + focus_x;
  162.                 int tempY = y + focus_y;
  163.                
  164.                 temp.setX(tempX);
  165.                 temp.setY(tempY);
  166.                
  167.                 if (temp.intersects(selectionRectangle)) {
  168.                     temp.selectUnit(true);
  169.                     System.out.println("Unit selected");
  170.                 } else if (!temp.intersects(selectionRectangle)) {
  171.                     temp.selectUnit(false);
  172.                     System.out.println("Unit unselected");
  173.                 }
  174.                
  175.                 temp.setX(x);
  176.                 temp.setY(y);
  177.             }
  178.         }
  179.     }
  180.    
  181.     public void mouseClicked(MouseEvent e) {
  182.         if (e.isMetaDown()) {
  183.             if (blockMode) {
  184.                 blockMode = false;
  185.                 System.out.println("Block Mode Off!");
  186.             } else if(!blockMode) {
  187.                 blockMode = true;
  188.                 System.out.println("Block Mode On!");
  189.             }
  190.         }
  191.         if (blockMode) {
  192.             int x = e.getX() - focus_x;
  193.             int y = e.getY() - focus_y;
  194.             System.out.println(x + " " + y);
  195.             blocks.add(new Block(x, y));
  196.         }
  197.     }
  198.    
  199.     public void mouseDragged(MouseEvent e) {
  200.         if (!e.isMetaDown() && !e.isAltDown()) {
  201.             selectionEnd = new Point(e.getX(), e.getY());
  202.             int width = selectionEnd.x - selectionStart.x;
  203.             int height = selectionEnd.y - selectionStart.y;
  204.         }
  205.     }
  206.    
  207.     public void mouseMoved(MouseEvent e) {
  208.         if (e.getX() < 50) {
  209.             focus_x += 3;
  210.         } else if (e.getX() > 750) {
  211.             focus_x -= 3;
  212.         }
  213.     }
  214.    
  215.     public void paint(Graphics g) {
  216.         super.paint(g);
  217.        
  218.         Graphics2D g2d = backBuffer.createGraphics();
  219.                
  220.         // Units
  221.         if (units.size() > 0) {
  222.             for (int i = 0; i < units.size(); i++) {
  223.                 Unit temp = (Unit) units.get(i);
  224.                 ImageIcon imageHuman = animatorHuman.getImage();
  225.                 imageHuman.paintIcon(this, g, temp.getXPos() + focus_x, temp.getYPos() + focus_y);
  226.                
  227.             }
  228.         }
  229.        
  230.         // Blocks
  231.         if (blocks.size() > 0) {
  232.             for (int i = 0; i < blocks.size(); i++) {
  233.                 Block temp = (Block) blocks.get(i);
  234.                 ImageIcon imageBlock = (ImageIcon) images.blockImages.get(0);
  235.                 imageBlock.paintIcon(this, g, temp.getXPos() + focus_x, temp.getYPos() + focus_y);
  236.             }
  237.         }
  238.        
  239.         // Selection Rectangle
  240.         g.setColor(Color.GREEN);
  241.        
  242.         if (selectionEnd != null && isMouseDragging) {
  243.             int x1 = selectionStart.x;
  244.             int y1 = selectionStart.y;
  245.             int width = selectionEnd.x - selectionStart.x;
  246.             int height = selectionEnd.y - selectionStart.y;
  247.             g.drawRect(x1, y1, width, height);
  248.         }
  249.     }
  250.    
  251.     public class Move extends Thread {
  252.        
  253.         public void run() {
  254.            
  255.             while (true) {
  256.                 repaint();
  257.                 if (update) {
  258.                     animatorHuman.update();
  259.                     update = false;
  260.                 } else if (!update) {
  261.                     update = true;
  262.                 }
  263.                
  264.                 if (units.size() > 0) {
  265.                     for (int i = 0; i < units.size(); i++) {
  266.                         Unit temp = (Unit) units.get(i);
  267.                         System.out.println("Player: " + temp.getXPos() + " " + temp.getYPos());
  268.                         if (temp.getUnitDirection() == RIGHT) {
  269.                             temp.setX(temp.getXPos() + temp.getSpeed());
  270.                         } else if (temp.getUnitDirection() == LEFT) {
  271.                             temp.setX(temp.getXPos() - temp.getSpeed());
  272.                         } else if (temp.getUnitDirection() == STOP) {
  273.                             // do nothing
  274.                         }
  275.                        
  276.                         temp.setY(temp.getYPos() + temp.getGravity());
  277.                        
  278.                         int i2 = 0;
  279.                         while (i2 < blocks.size()) {
  280.                            
  281.                             Block temp2 = (Block) blocks.get(i2);
  282.                             if (temp2.collisionCheck(temp)) {
  283.                                 temp.setGravity(0);
  284.                             } else if (!temp2.collisionCheck(temp)) {
  285.                                 temp.setGravity(3);
  286.                             }
  287.                            
  288.                             i2++;
  289.                         }
  290.                     }
  291.                 }
  292.                
  293.                 try {
  294.                     Thread.sleep(75);
  295.                 } catch (Exception ex) {
  296.                     break;
  297.                 }
  298.             }
  299.            
  300.         }
  301.        
  302.     }
  303.    
  304.     private class ButtonListener implements ActionListener {
  305.        
  306.         public void actionPerformed(ActionEvent e) {
  307.             // Movement Buttons
  308.             if (e.getSource() == leftButton) {
  309.                 for (int i = 0; i < units.size(); i++) {
  310.                     Unit temp = (Unit) units.get(i);
  311.                     if (temp.getSelected()) {
  312.                         temp.setUnitDirection(LEFT);
  313.                     }
  314.                 }
  315.             } else if (e.getSource() == rightButton) {
  316.                 for (int i = 0; i < units.size(); i++) {
  317.                     Unit temp = (Unit) units.get(i);
  318.                     if (temp.getSelected()) {
  319.                         temp.setUnitDirection(RIGHT);
  320.                     }
  321.                 }
  322.             } else if (e.getSource() == stopButton) {
  323.                 for (int i = 0; i < units.size(); i++) {
  324.                     Unit temp = (Unit) units.get(i);
  325.                     if (temp.getSelected()) {
  326.                         temp.setUnitDirection(STOP);
  327.                     }
  328.                 }
  329.             } else if (e.getSource() == sprintButton) {
  330.                 for (int i = 0; i < units.size(); i++) {
  331.                     Unit temp = (Unit) units.get(i);
  332.                     if (temp.getSelected()) {
  333.                         if (temp.getSprinting()) {
  334.                             temp.sprintUnit(false);
  335.                         } else if (!temp.getSprinting()) {
  336.                             temp.sprintUnit(true);
  337.                         }
  338.                     }
  339.                 }
  340.             // Unit Buttons
  341.             } else if (e.getSource() == addUnitButton) {
  342.                 units.add(new Unit(400 - focus_x, 300 - focus_y, 25, 50));
  343.             }
  344.         }
  345.        
  346.     }
  347.    
  348.     public static void main(String[] args) {
  349.         // Execute the Game
  350.         new Game();
  351.     }
  352.    
  353. }