Skip to content

Commit

Permalink
Merge pull request #1 from djas/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
dwm10005 committed Apr 6, 2015
2 parents f72681f + ffbe474 commit 56778fa
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 92 deletions.
12 changes: 6 additions & 6 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 3 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.source=1.7
4 changes: 2 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Main extends JFrame{


public static void main(String[] args) {
JFrame frame = new JFrame("Tower Defense Mock GUI");
JFrame frame = new JFrame("Tower Defense Refactor Mock GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640,640+23);
int num = 6;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) {


//map.getTF().createBasicTower(14, 8);
map.createMinion(0);
map.createMinion(MinionTypes.BASIC);
mg.step();

}
Expand Down
2 changes: 1 addition & 1 deletion src/MapTowerDefense.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public int[][] getNodes() {
public MockGui getGui() {
return MG;
}
public void createMinion(int type) {
public void createMinion(MinionTypes type) {
MF.createMinion(type);
}

Expand Down
2 changes: 1 addition & 1 deletion src/MinionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MinionFactory(int x, int y, MapTowerDefense _map) {
minions = new MinionMock[Max];
}

public void createMinion(int type) {
public void createMinion(MinionTypes type) {
MinionMock m = new MinionMock(type, map);
if (id < Max) {
minions[id] = m;
Expand Down
23 changes: 9 additions & 14 deletions src/MinionMock.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@

public class MinionMock {
int type;
MapTowerDefense map;
int x;
int y;
int speed;
int visits;
boolean alive;
int health;
private int type, x, y, speed, visits, health, attackDamage;
private boolean alive;
private MapTowerDefense map;

public MinionMock(int _type, MapTowerDefense _map) {
type = _type;
map = _map;
public MinionMock(MinionTypes type, MapTowerDefense map) {
this.map = map;
int[] unhashed = map.getGui().hash(map.getNodes()[0][0], map.getNodes()[0][1]);
x = unhashed[0];
y = unhashed[1];
speed = 1;
health = type.getHealth();
attackDamage = type.getAttackDamage();
speed = type.getMovementSpeed();
visits = 1;
alive = true;
health = 255;
}
public int getX() {
return x;
Expand All @@ -27,7 +22,7 @@ public int getY() {
return y;
}
public int getType() {
return type;
return 0; //TODO
}
public int findDirection() {
if (visits == map.getNum()) {
Expand Down
103 changes: 103 additions & 0 deletions src/MinionTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

public enum MinionTypes {

BASIC (255, 1, 1);

private int health, attackDamage, movementSpeed;

MinionTypes(int health, int attackDamage, int movementSpeed) {
this.health = health;
this.attackDamage = attackDamage;
this.movementSpeed = movementSpeed;
}

public int getHealth() {
return health;
}

public int getAttackDamage() {
return attackDamage;
}

public int getMovementSpeed() {
return movementSpeed;
}

/*
public int getType() {
return type;
}
*/

/*
public int findDirection() {
if (visits == map.getNum()) {
// CALL TO MAKE NEXUS LOSE HEALTH
return -1;
} else {
int xGoal = map.getNodes()[visits][0];
int yGoal = map.getNodes()[visits][1];
int[] Goals = map.getGui().hash(xGoal, yGoal);
xGoal = Goals[0];
yGoal = Goals[1];
if (x == xGoal) {
if (y == yGoal) {
visits++;
return findDirection();
}
if (yGoal > y) {
// MOVE DOWN
return 2;
} else {
return 0;
}
} else {
if (x > xGoal) {
return 3;
} else {
return 1;
}
}
}
}
public boolean go() {
int direction = findDirection();
if (direction == -1) {
alive = false;
return map.getTF().getTowerArray()[0].takeDamage();
}
if (direction == 0) {
y-=movespeed;
}
if (direction == 1) {
x+=movespeed;
}
if (direction == 2) {
y+=movespeed;
}
if (direction == 3) {
x-=movespeed;
}
return false;
}
public boolean isAlive() {
return alive;
}
public int getHealth() {
return health;
}
public void minionTakeDamage() {
int dam = map.getTF().assignTotalDamage(x, y);
health-=dam;
if (health <= 0) {
alive = false;
}
}
*/
}
98 changes: 33 additions & 65 deletions src/MockGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,47 @@
import java.awt.geom.*;

public class MockGui extends JPanel{
boolean gameEnd;
private int X, Y, GRIDSIZE, NUMNODES;
private int[][] NODES;
private MapTowerDefense map;
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) {
X = x;
Y = y;
gameEnd = false;
GRIDSIZE = gridSize;
NUMNODES = numNodes;
NODES = 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);
//this.addMouseMotionListener(TP);

repaint();
}

public void setMap(MapTowerDefense _map) {
map = _map;
public void setMap(MapTowerDefense map) {
this.map = map;//
}

public void step() {
int i = 0;
while (!gameEnd) {
while (!gameOver) {
try {
Thread.sleep(10);
repaint();
i++;
//map.getMF().getMinions()[0].minionTakeDamage();
//
map.getMF().getMinions()[0].minionTakeDamage();

map.getMF().assignAllDamage();

if (i > 63) {
map.createMinion(0);
map.createMinion(MinionTypes.BASIC);
i = 0;
}
//
} catch (InterruptedException e) {
//e.printStackTrace();
}
Expand All @@ -54,12 +57,12 @@ public void step() {

public void paint(Graphics g) {
setBackground(new Color(99, 209, 62));
drawGrid(X, Y, GRIDSIZE, g);
drawGrid(x, y, gridsize, g);
//drawMinion(96,96,0,g);
colorPath(g);
drawAllMinions(map.getMF(), g);
drawAllTowers(map.getTF(), g);
drawHealth(g);
drawPlayerHealth(g);
}
public void drawGrid(int x, int y, int gridSize, Graphics g) {
g.setColor(Color.BLACK);
Expand All @@ -77,20 +80,20 @@ public void drawGrid(int x, int y, int gridSize, Graphics g) {
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;
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;
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);
g.fillRect(coords[0], coords[1], gridsize, gridsize);
}
public void colorBlocks(int x1, int y1, int x2, int y2, Graphics g) {
if (x1 == x2) {
Expand Down Expand Up @@ -124,8 +127,8 @@ public void colorBlocks(int x1, int y1, int x2, int y2, Graphics g) {
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);
while (i < numnodes) {
colorBlocks(nodes[i-1][0], nodes[i-1][1], nodes[i][0], nodes[i][1], g);
i++;
}
}
Expand All @@ -134,17 +137,17 @@ public void drawAllMinions(MinionFactory MF, Graphics g) {
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())
if (MF.getMinions()[i].go()) //TODO view code controlling model behavior. Bad! Need to refactor
{
gameEnd = true;
gameOver = true;

}
}
i++;
}
}

public void drawHealth(Graphics g) {
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);
Expand All @@ -171,25 +174,8 @@ public void drawBasicTower(int x, int y, Graphics g){
g.fillRect(x, y, 32, 32);
}

private class TowerPlacer implements MouseListener, MouseMotionListener{

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

private class TowerPlacer extends MouseAdapter{

@Override
public void mousePressed(MouseEvent e) {
Expand All @@ -200,24 +186,6 @@ public void mousePressed(MouseEvent e) {

map.getTF().createBasicTower(local[0], local[1]);
}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

}

Expand Down
Loading

0 comments on commit 56778fa

Please sign in to comment.