Skip to content
Permalink
af42b10cc5
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
193 lines (170 sloc) 4.22 KB
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;
public class MockGui extends JPanel{
boolean gameOver;
private int x, y, gridsize, numnodes;
private int[][] nodes;
private MapTowerDefense map;//
public MockGui(int x, int y, int gridSize, int numNodes, int[][] nodes) {
this.x = x;
this.y = y;
gameOver = false;
this.gridsize = gridSize;
this.numnodes = numNodes;
this.nodes = nodes;
TowerPlacer TP = new TowerPlacer();
this.addMouseListener(TP);
//this.addMouseMotionListener(TP);
repaint();
}
public void setMap(MapTowerDefense map) {
this.map = map;//
}
public void step() {
int i = 0;
while (!gameOver) {
try {
Thread.sleep(10);
repaint();
i++;
//
map.getMF().getMinions()[0].minionTakeDamage();
map.getMF().assignAllDamage();
if (i > 63) {
map.createMinion(MinionTypes.BASIC);
i = 0;
}
//
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
public void paint(Graphics g) {
setBackground(new Color(99, 209, 62));
drawGrid(x, y, gridsize, g);
//drawMinion(96,96,0,g);
colorPath(g);
drawAllMinions(map.getMF(), g);
drawAllTowers(map.getTF(), g);
drawPlayerHealth(g);
}
public void drawGrid(int x, int y, int gridSize, Graphics g) {
g.setColor(Color.BLACK);
int i = 0;
while (i < x-gridSize) {
i+= gridSize;
g.drawLine(i, gridSize, i, y-gridSize);
}
i = 0;
while (i < y-gridSize) {
i+= gridSize;
g.drawLine(gridSize, i, x-gridSize, i);
}
}
public int[] hash (int x, int y) {
// Convert x y on grid to pixel
int[] result = new int[2];
result[0] = x*gridsize;
result[1] = y*gridsize;
return result;
}
public int[] unhash(int x, int y) {
int[] result = new int[2];
result[0] = x/gridsize;
result[1] = y/gridsize;
return result;
}
public void colorBlock(int x, int y, Graphics g) {
int[] coords = hash(x,y);
g.fillRect(coords[0], coords[1], gridsize, gridsize);
}
public void colorBlocks(int x1, int y1, int x2, int y2, Graphics g) {
if (x1 == x2) {
// Vertically
if (y1 < y2) {
while (y1 <= y2) {
colorBlock(x1, y1, g);
y1+=1;
}
} else {
while (y2 <= y1) {
colorBlock(x1, y2, g);
y2+=1;
}
}
} else {
// Horizontally
if (x1 < x2) {
while (x1 <= x2) {
colorBlock(x1, y1, g);
x1+=1;
}
} else {
while (x2 <= x1) {
colorBlock(x2, y1, g);
x2+=1;
}
}
}
}
public void colorPath(Graphics g) {
int i = 1;
g.setColor(new Color(213,196,161));
while (i < numnodes) {
colorBlocks(nodes[i-1][0], nodes[i-1][1], nodes[i][0], nodes[i][1], g);
i++;
}
}
public void drawAllMinions(MinionFactory MF, Graphics g) {
int i = 0;
while (i < MF.getNum()) {
if (MF.getMinions()[i].isAlive()) {
drawMinion(MF.getMinions()[i].getX(), MF.getMinions()[i].getY(), MF.getMinions()[i].getHealth(), g);
if (MF.getMinions()[i].go()) //TODO view code controlling model behavior. Bad! Need to refactor
{
gameOver = true;
}
}
i++;
}
}
public void drawPlayerHealth(Graphics g) {
int health = map.getTF().getTowerArray()[0].getHealth();
g.setColor(new Color(0,255,0));
colorBlocks(19, 0, 19, health, g);
g.setColor(new Color(255,0,0));
colorBlocks(19, 20, 19, health, g);
}
public void drawMinion(int x, int y, int health, Graphics g) {
g.setColor(new Color(255-health, 100, 100));
g.fillOval(x, y, 32, 32);
}
public void drawAllTowers(TowerFactory TF, Graphics g) {
int i = 0;
while (i < TF.getNum()) {
int[] local = hash(TF.getTowerArray()[i].getTowerXlocation(), TF.getTowerArray()[i].getTowerYlocation());
drawBasicTower(local[0], local[1], g);
i++;
}
}
public void drawBasicTower(int x, int y, Graphics g){
g.setColor(new Color(24,24,24));
g.fillRect(x, y, 32, 32);
}
private class TowerPlacer extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("x; " + x + " y: " + y);
int[] local = unhash(x,y);
map.getTF().createBasicTower(local[0], local[1]);
}
}
}