// Import everything
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;
// The class
public class Game extends JFrame implements MouseListener, MouseMotionListener {
// GUI Varuables
Container container;
JButton leftButton, rightButton, stopButton, addUnitButton, sprintButton;
// Animator
Animator animatorHuman;
// Insets
Insets insets;
// Images
ImageIcon leftButtonIcon, rightButtonIcon, stopButtonIcon, sprintButtonIcon, addUnitButtonIcon;
BufferedImage backBuffer;
Images images = new Images();
// Scrolling Variables
int focus_x, focus_y;
// Unit Variables
ArrayList units;
// Block Variables
ArrayList blocks;
boolean blockMode = false;
// Holder Variables
Point selectionStart, selectionEnd;
Rectangle selectionRectangle;
static final int STOP = 0, LEFT = 1, RIGHT = 2;
boolean isMouseDragging = false;
boolean update = false;
public Game() {
// Setup the window
setTitle("Project Deterioration - Build #76");
setSize(800, 600);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// Setup GamePlay
setupGameplay();
// Setup GUI
setupGUI();
}
public void setupGUI() {
// Initiate the container
container = getContentPane();
container.setLayout(null);
setContentPane(container);
// Initiate the ImageIcons
leftButtonIcon = new ImageIcon("images/game/left.png");
rightButtonIcon = new ImageIcon("images/game/right.png");
stopButtonIcon = new ImageIcon("images/game/stop.png");
sprintButtonIcon = new ImageIcon("images/game/sprint.png");
addUnitButtonIcon = new ImageIcon("images/game/addUnit.png");
// The Buttons
ButtonListener listener = new ButtonListener();
leftButton = new JButton(leftButtonIcon);
rightButton = new JButton(rightButtonIcon);
stopButton = new JButton(stopButtonIcon);
addUnitButton = new JButton(addUnitButtonIcon);
sprintButton = new JButton(sprintButtonIcon);
leftButton.setBounds(600, 450, 50, 50);
rightButton.setBounds(650, 450, 50, 50);
stopButton.setBounds(700, 450, 50, 50);
sprintButton.setBounds(750, 450, 50, 50);
addUnitButton.setBounds(600, 500, 50, 50);
leftButton.addActionListener(listener);
rightButton.addActionListener(listener);
stopButton.addActionListener(listener);
sprintButton.addActionListener(listener);
addUnitButton.addActionListener(listener);
container.add(leftButton);
container.add(rightButton);
container.add(stopButton);
container.add(sprintButton);
container.add(addUnitButton);
container.repaint();
}
public void setupGameplay() {
// Add Mouse Listeners
addMouseListener(this);
addMouseMotionListener(this);
// Double Buffering
backBuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
// Setup ArrayList
units = new ArrayList();
blocks = new ArrayList();
// Setup Animation
animatorHuman = new Animator(new Images().humanRightImages);
// Setup Insets
insets = getInsets();
setSize(insets.left + 800 + insets.right,
insets.top + 600 + insets.bottom);
// Setup SandboxWorld
int i = 0;
while (i < 800) {
blocks.add(new Block(i + focus_x, 442 + focus_y));
i += 26;
}
// Setup Thread
Move m = new Move();
m.start();
}
// Mouse methods I'm not using...
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
// And mouse methods I am using...
public void mousePressed(MouseEvent e) {
if (!e.isMetaDown() && !e.isAltDown()) {
isMouseDragging = true;
selectionStart = new Point(e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
if (isMouseDragging == true) {
isMouseDragging = false;
selectionEnd = new Point(e.getX(), e.getY());
int width = selectionEnd.x - selectionStart.x;
int height = selectionEnd.y - selectionStart.y;
selectionRectangle = new Rectangle(selectionStart.x, selectionStart.y, width, height);
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
int x = temp.getXPos();
int y = temp.getYPos();
int tempX = x + focus_x;
int tempY = y + focus_y;
temp.setX(tempX);
temp.setY(tempY);
if (temp.intersects(selectionRectangle)) {
temp.selectUnit(true);
System.out.println("Unit selected");
} else if (!temp.intersects(selectionRectangle)) {
temp.selectUnit(false);
System.out.println("Unit unselected");
}
temp.setX(x);
temp.setY(y);
}
}
}
public void mouseClicked(MouseEvent e) {
if (e.isMetaDown()) {
if (blockMode) {
blockMode = false;
System.out.println("Block Mode Off!");
} else if(!blockMode) {
blockMode = true;
System.out.println("Block Mode On!");
}
}
if (blockMode) {
int x = e.getX() - focus_x;
int y = e.getY() - focus_y;
System.out.println(x + " " + y);
blocks.add(new Block(x, y));
}
}
public void mouseDragged(MouseEvent e) {
if (!e.isMetaDown() && !e.isAltDown()) {
selectionEnd = new Point(e.getX(), e.getY());
int width = selectionEnd.x - selectionStart.x;
int height = selectionEnd.y - selectionStart.y;
}
}
public void mouseMoved(MouseEvent e) {
if (e.getX() < 50) {
focus_x += 3;
} else if (e.getX() > 750) {
focus_x -= 3;
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = backBuffer.createGraphics();
// Units
if (units.size() > 0) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
ImageIcon imageHuman = animatorHuman.getImage();
imageHuman.paintIcon(this, g, temp.getXPos() + focus_x, temp.getYPos() + focus_y);
}
}
// Blocks
if (blocks.size() > 0) {
for (int i = 0; i < blocks.size(); i++) {
Block temp = (Block) blocks.get(i);
ImageIcon imageBlock = (ImageIcon) images.blockImages.get(0);
imageBlock.paintIcon(this, g, temp.getXPos() + focus_x, temp.getYPos() + focus_y);
}
}
// Selection Rectangle
g.setColor(Color.GREEN);
if (selectionEnd != null && isMouseDragging) {
int x1 = selectionStart.x;
int y1 = selectionStart.y;
int width = selectionEnd.x - selectionStart.x;
int height = selectionEnd.y - selectionStart.y;
g.drawRect(x1, y1, width, height);
}
}
public class Move extends Thread {
public void run() {
while (true) {
repaint();
if (update) {
animatorHuman.update();
update = false;
} else if (!update) {
update = true;
}
if (units.size() > 0) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
System.out.println("Player: " + temp.getXPos() + " " + temp.getYPos());
if (temp.getUnitDirection() == RIGHT) {
temp.setX(temp.getXPos() + temp.getSpeed());
} else if (temp.getUnitDirection() == LEFT) {
temp.setX(temp.getXPos() - temp.getSpeed());
} else if (temp.getUnitDirection() == STOP) {
// do nothing
}
temp.setY(temp.getYPos() + temp.getGravity());
int i2 = 0;
while (i2 < blocks.size()) {
Block temp2 = (Block) blocks.get(i2);
if (temp2.collisionCheck(temp)) {
temp.setGravity(0);
} else if (!temp2.collisionCheck(temp)) {
temp.setGravity(3);
}
i2++;
}
}
}
try {
Thread.sleep(75);
} catch (Exception ex) {
break;
}
}
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Movement Buttons
if (e.getSource() == leftButton) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
if (temp.getSelected()) {
temp.setUnitDirection(LEFT);
}
}
} else if (e.getSource() == rightButton) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
if (temp.getSelected()) {
temp.setUnitDirection(RIGHT);
}
}
} else if (e.getSource() == stopButton) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
if (temp.getSelected()) {
temp.setUnitDirection(STOP);
}
}
} else if (e.getSource() == sprintButton) {
for (int i = 0; i < units.size(); i++) {
Unit temp = (Unit) units.get(i);
if (temp.getSelected()) {
if (temp.getSprinting()) {
temp.sprintUnit(false);
} else if (!temp.getSprinting()) {
temp.sprintUnit(true);
}
}
}
// Unit Buttons
} else if (e.getSource() == addUnitButton) {
units.add(new Unit(400 - focus_x, 300 - focus_y, 25, 50));
}
}
}
public static void main(String[] args) {
// Execute the Game
new Game();
}
}