Permalink
Cannot retrieve contributors at this time
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?
TowerDefense/src/MockGui.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
276 lines (240 sloc)
7.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
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.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.imageio.ImageIO; | |
import java.util.Random; | |
public class MockGui extends JPanel{ | |
boolean gameOver; | |
private int x, y, gridsize, numnodes; | |
private int[][] nodes; | |
private MapTowerDefense map;// | |
private BufferedImage image; | |
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.getTF().basicTotalDamage(); | |
map.getMF().getMinions()[0].minionTakeDamage(); | |
map.getStatGui().updateHealth(); | |
map.getStatGui().updateMoney(); | |
map.getStatGui().updateNumTowers(); | |
map.getMF().assignAllDamage(); | |
if (i > 63) { | |
map.getMF().createRandominion(); | |
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); | |
drawAllTowers(map.getTF(), g); | |
drawAllMinions(map.getMF(), g); | |
drawPlayerHealth(g); | |
drawAllBasicAttacks(map.getTF(),g); | |
drawAllAOEAttacks(map.getTF(),g); | |
} | |
public void drawGrid(int x, int y, int gridSize, Graphics g) { | |
g.setColor(new Color(99, 209, 62)); | |
g.fillRect(0, 0, 32*20, 32*20); | |
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 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 drawAllMinions(MinionFactory MF, Graphics g) { | |
int i = 0; | |
while (i < MF.getNum()) { | |
if (MF.getMinions()[i].isAlive()) { | |
drawMinion(MF.getMinions()[i], g); | |
if (MF.getMinions()[i].go()) //TODO view code controlling model behavior. Bad! Need to refactor | |
{ | |
gameOver = true; | |
} | |
} | |
i++; | |
} | |
} | |
public void drawMinion(int x, int y, int health, Graphics g) { | |
g.drawImage(image, x,y, null); | |
} | |
// IS THIS METHOD STILL CALLED? | |
public void drawMinion(MinionMock m, Graphics g) | |
{ | |
g.drawImage(m.getSprite(), m.getX(), m.getY(), null); | |
String h = String.format("[%s/%s]", m.getHealth(), m.getMaxHealth()); | |
g.setColor(Color.RED); | |
g.drawString(h, m.getX()-15, m.getY()-10); | |
g.drawString(m.getName(), m.getX()-15, m.getY()+45); | |
//g.drawString(("V: " + m.getValue() + "\nD: " + m.getAttackDamage()), m.getX(), m.getY()+46); | |
} | |
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(TF.getTowerArray()[i], local[0], local[1], g); | |
i++; | |
} | |
} | |
public void drawBasicTower(TowerMock tm, int x, int y, Graphics g){ | |
//g.setColor(new Color(24,24,24)); | |
//g.fillRect(x, y, 32, 32); | |
g.drawImage(tm.getSprite(),x , y, null); | |
} | |
public void drawBasicAttack(int x, int y, Graphics g){ | |
g.setColor(Color.yellow); | |
g.fillRoundRect(x, y, 7, 7, 7, 7); | |
} | |
//if Basic tower is attacking, draw dots to where minion is | |
public void drawAllBasicAttacks(TowerFactory TF, Graphics g){ | |
for(int i = 0; i < TF.getNum(); i++){ | |
if(TF.getTowerArray()[i].basicDealDamage() && TF.getTowerArray()[i].type == TowerTypes.BASIC){ | |
int towerDotX = (TF.getTowerArray()[i]._xlocation * 32) + 15; | |
int towerDotY = (TF.getTowerArray()[i]._ylocation *32) + 15; | |
int halfDotX = (((TF.getTowerArray()[i]._xlocation * 32) + 15)+ (TF.getTowerArray()[i].currentTarget.getX() + 15))/2; | |
int halfDotY = (((TF.getTowerArray()[i]._ylocation * 32) + 15)+ (TF.getTowerArray()[i].currentTarget.getY() + 15))/2; | |
int minionDotX = TF.getTowerArray()[i].currentTarget.getX() + 15; | |
int minionDotY = TF.getTowerArray()[i].currentTarget.getY()+15; | |
drawBasicAttack(towerDotX,towerDotY, g); | |
drawBasicAttack((towerDotX + halfDotX)/2, (towerDotY + halfDotY)/2, g); | |
drawBasicAttack(halfDotX, halfDotY, g); | |
drawBasicAttack((halfDotX + minionDotX)/2, (halfDotY + minionDotY)/2, g); | |
drawBasicAttack(minionDotX, minionDotY, g); | |
} | |
} | |
} | |
public void drawAOEAttack(int x, int y, Graphics g){ | |
g.setColor(Color.MAGENTA); | |
g.drawOval(x-70, y-70, 140, 140); | |
g.drawOval(x-65, y-65, 130, 130); | |
g.drawOval(x-60, y-60, 120, 120); | |
} | |
public void drawAllAOEAttacks(TowerFactory TF, Graphics g){ | |
for(int i = 0; i < TF.getNum(); i++){ | |
if( TF.getTowerArray()[i].basicDealDamage() && TF.getTowerArray()[i].type == TowerTypes.AOE){ | |
drawAOEAttack((TF.getTowerArray()[i]._xlocation * 32) + 15, (TF.getTowerArray()[i]._ylocation *32) + 15, g); | |
} | |
} | |
} | |
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); | |
if(map.getStatGui().gettowerPlacerVar() == 2){ | |
map.getTF().createBasicTower(local[0], local[1], TowerTypes.AOE); | |
} | |
else if(map.getStatGui().gettowerPlacerVar() == 1){ | |
map.getTF().createBasicTower(local[0], local[1], TowerTypes.BASIC); | |
} | |
} | |
} | |
} |