From a78f1b0c84c6bd56c7346aa61ffda69e911520e1 Mon Sep 17 00:00:00 2001 From: jrb10014 Date: Fri, 27 Mar 2015 10:54:36 -0400 Subject: [PATCH] First commit --- .DS_Store | Bin 0 -> 6148 bytes .classpath | 6 + .gitignore | 1 + .project | 17 ++ .settings/org.eclipse.jdt.core.prefs | 11 ++ src/.DS_Store | Bin 0 -> 6148 bytes src/Main.java | 49 ++++++ src/MapTowerDefense.java | 46 ++++++ src/Minion.png | Bin 0 -> 2493 bytes src/MinionFactory.java | 42 +++++ src/MinionMock.java | 98 ++++++++++++ src/MockGui.java | 225 +++++++++++++++++++++++++++ src/StatGui.java | 12 ++ src/TowerFactory.java | 53 +++++++ src/TowerMock.java | 125 +++++++++++++++ 15 files changed, 685 insertions(+) create mode 100644 .DS_Store create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/.DS_Store create mode 100644 src/Main.java create mode 100644 src/MapTowerDefense.java create mode 100644 src/Minion.png create mode 100644 src/MinionFactory.java create mode 100644 src/MinionMock.java create mode 100644 src/MockGui.java create mode 100644 src/StatGui.java create mode 100644 src/TowerFactory.java create mode 100644 src/TowerMock.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..905e3a260973bbcf7f4877593eeba97ac3168335 GIT binary patch literal 6148 zcmeHK%}T>S5T3PFW4%OF6ujgrcu zsh75slGi#^Eg6Kq@he_hfA@Fexbfk8{+Nb5Cwy3z{IFty@E;hs#SFB>vFx5ntJ*)!d4vADM26qkDoPIrBM72Va1UeA88 zthxD>&7Fg0@8a_6`sViT{_#oEFYvZfGHLJ~p3%uc0xdXYHh|b*8C(X;DX_)|cZ<9; zW`G&^B?ELnIH-iK!AzsNI?&MX1NG+#NzkXa1fgTlHJE9{9u%Qd5p}9CPYj{cG47ag zuE9*BP6we@#(B)j!n{y~S{>t#8V+I;bb8Bs4D5s8Z0Fa;zLG#j~hNu+Jd}(KVQ9#0U!i2q+rZFav+e Fz#C|uu0j9+ literal 0 HcmV?d00001 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fceb480 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..9b62d4c --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + TowerDefenseWork + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +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.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +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 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D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xbV4$4U-Tu zh}iM)vzZwS-<*!xBzUe+W=f#3BCf09MQzc|hN26T2y*4hRhTMSxrGO|QxK7xv=-92 zAi`}(-R5zQ^C)Z6hq4VHnVY1%J!AEUrNRhgOjup=jPdkQUJj$Ur1-U@@8IF1(Rc0+ zG-XVCz>FK}EMdL8#$`B9er~U-<|ZX2m6Vh~KdcsEkk`LT>$ZR4Y?0PFOKV+FjMur0 zakhTp>>TADL*YY>sUwBGGY?N%vgMgV#<#M~@-xj~TTSQ@|S*xn^rHO#72 zt8{gB{ryh`1e|3sQad|4e0>jj`yA}zrvrQYHBcY6U9Jurtrld2>RXyjy{>Ck6OgWb zlkM)~t!~oBZPJD<5MzxHTg#K|6Rs@19cZ!LfeKe{*yyxojeTxzc6oUTL`q5$n_U?a za_$$WEsd4Wz~#_WCy(wQ`}1|MsX34b9wy|_NLsnd)_SQ;K~{pe z`U&XdCc}$*sK4@es-d>L+&^{5H}$ERi}kWh^h$L}gH^SXfU^S@`h8~lZ-N4QnKd1T z+8M@rmJ0A@nrR7RDykv(mEL%CKj#R;{&hln&)Ag>V#)i&j)kR3`LwSI@qm=U_$vsx`Ce1FzXJKAyW4 zD0%{rK_OQf=?g6vi2I%UWW}MIofm(3 zfL1w+E{6XD`zs}-ImvSJX*l(mg{qeT;GPSK`4^&q~jIw3zjE}4F- zFegFMQX#H?CE~qot1oP+E2yhr)Uz0E{Ct=va}4JJ>+yd9ijGaP6s9uv00000NkvXX Hu0mjfNX4!H literal 0 HcmV?d00001 diff --git a/src/MinionFactory.java b/src/MinionFactory.java new file mode 100644 index 0000000..544f45d --- /dev/null +++ b/src/MinionFactory.java @@ -0,0 +1,42 @@ + +public class MinionFactory { + private int X, Y; + private MapTowerDefense map; + private MinionMock[] minions; + private int Max; + private int id; + + public MinionFactory(int x, int y, MapTowerDefense _map) { + X = x; + Y = y; + map = _map; + Max = 100; + id = 0; + minions = new MinionMock[Max]; + } + + public void createMinion(int type) { + MinionMock m = new MinionMock(type, map); + if (id < Max) { + minions[id] = m; + id++; + } + } + public MinionMock[] getMinions() { + return minions; + } + public int getNum() { + return id; + } + + public void assignAllDamage() { + int i = 0; + while (i < id) { + if (minions[i].isAlive()) { + minions[i].minionTakeDamage(); + } + i++; + } + } + +} diff --git a/src/MinionMock.java b/src/MinionMock.java new file mode 100644 index 0000000..b1dc1ef --- /dev/null +++ b/src/MinionMock.java @@ -0,0 +1,98 @@ + +public class MinionMock { + int type; + MapTowerDefense map; + int x; + int y; + int speed; + int visits; + boolean alive; + int health; + + public MinionMock(int _type, MapTowerDefense _map) { + type = _type; + map = _map; + int[] unhashed = map.getGui().hash(map.getNodes()[0][0], map.getNodes()[0][1]); + x = unhashed[0]; + y = unhashed[1]; + speed = 1; + visits = 1; + alive = true; + health = 255; + } + public int getX() { + return x; + } + public int getY() { + return y; + } + 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-=speed; + } + if (direction == 1) { + x+=speed; + } + if (direction == 2) { + y+=speed; + } + if (direction == 3) { + x-=speed; + } + 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; + } + + } +} diff --git a/src/MockGui.java b/src/MockGui.java new file mode 100644 index 0000000..01402b2 --- /dev/null +++ b/src/MockGui.java @@ -0,0 +1,225 @@ +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 gameEnd; + 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; + TowerPlacer TP = new TowerPlacer(); + this.addMouseListener(TP); + this.addMouseMotionListener(TP); + + repaint(); + } + + public void setMap(MapTowerDefense _map) { + map = _map; + } + + public void step() { + int i = 0; + while (!gameEnd) { + try { + Thread.sleep(10); + repaint(); + i++; + //map.getMF().getMinions()[0].minionTakeDamage(); + map.getMF().assignAllDamage(); + + if (i > 63) { + map.createMinion(0); + 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); + drawHealth(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()) + { + gameEnd = true; + + } + } + i++; + } + } + + public void drawHealth(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 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 + + } + + @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]); + } + + @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 + + } + + } + + +} diff --git a/src/StatGui.java b/src/StatGui.java new file mode 100644 index 0000000..4835b53 --- /dev/null +++ b/src/StatGui.java @@ -0,0 +1,12 @@ +import javax.swing.JPanel; + + +public class StatGui extends JPanel { + private int X, Y; + + public StatGui (int x, int y) { + X = x; + Y = y; + } + +} diff --git a/src/TowerFactory.java b/src/TowerFactory.java new file mode 100644 index 0000000..59212d2 --- /dev/null +++ b/src/TowerFactory.java @@ -0,0 +1,53 @@ + +public class TowerFactory { + + int xloc, yloc; + MapTowerDefense _map; + TowerMock[] towerarray; + int quantity; + int _max; + + public TowerFactory(MapTowerDefense amap, int max){ + _map = amap; + _max = max; + towerarray = new TowerMock[_max]; + createNexus(); + quantity = 1; + } + public int assignTotalDamage(int x, int y) { + int i = 1; + int total = 0; + while (i < quantity) { + total += towerarray[i].dealDamage(x, y); + i++; + } + return total; + } + + + public TowerMock[] getTowerArray() { + return towerarray; + } + public int getNum() { + return quantity; + } + + + + + + + public void createNexus(){ + int[] local = _map.getNodes()[_map.getNum()-1]; + int x = local[0]; + int y = local[1]; + towerarray[0] = new TowerMock(0, x, y, false); + } + + public void createBasicTower(int x, int y){ + if (quantity < _max) { + towerarray[quantity] = new TowerMock(quantity, x, y, true); + quantity++; + } + } +} diff --git a/src/TowerMock.java b/src/TowerMock.java new file mode 100644 index 0000000..1acdcf9 --- /dev/null +++ b/src/TowerMock.java @@ -0,0 +1,125 @@ + +public class TowerMock{ + public int _xlocation, _ylocation, _value, _cost, _upgradecost, _towerid; + public int _level = 1; + public int _range = 96; + public int _power = 1; + public double _firerate = 1.0; + + public int health; // (FOR NEXUS ONLY) + + public boolean _isTower; //false is Nexus + public String Description = "Basic Tower with generic stats, no special abilities or " + + "mods. Stats are initialized in the constructor. Targets 1 minion at a time."; + + public MinionMock currenttarget; + + //baseline stats: range - radius of 10. firerate - 1 shot/s. power - 1 damage + public TowerMock(int id, int xloc, int yloc, boolean isTower){ + _towerid = id; + _xlocation = xloc; + _ylocation = yloc; + _isTower = isTower; + if (!isTower) { + health = 20; + } + } + + public int dealDamage(int x, int y) { + int deltax = (32*_xlocation - x); + int deltay = (32*_ylocation - y); + int dist2 = deltax*deltax + deltay*deltay; + if (dist2 > _range*_range) { + return 0; + } else { + return 1; + } + + } + + public boolean takeDamage() { + health--; + System.out.println(health); + if (health <= 0) { + return true; + // LOSE GAME TERMINATE + } else { + return false; + } + } + public int getHealth() { + return health; + } + + + + + + public int getTowerXlocation() { + return _xlocation; + } + + public int getTowerYlocation() { + return _ylocation; + } + + public int getRange() { + return _range; + } + + public double getFireRate() { + return _firerate; + } + + public int getPower() { + return _power; + } + + //get overall resale value of towers, at half of purchase cost + public int getSellValue() { + return (int) (_range + (_power * 10)+ (_firerate * 10))/2; + } + + //get cost this tower was purchased for + public int getCost() { + _cost = (int) (_range + ( _power * 10) + (_firerate * 10)); + return _cost; + } + + //get upgrade cost to upgrade tower, double the price of last cost + public int getupgradeCost() { + _upgradecost = _cost * 2; + return _upgradecost; + } + + public void upgrade(){ + _cost = _upgradecost; + _level = _level + 1; + this._range = this._range + 1; + this._firerate= this._firerate + 0.3; + this._power = this._power + 1; + } + + public int getLevel() { + return _level; + } + + public boolean isActuallyTower() { + return _isTower; + } + + public String getDescription() { + return Description; + } + + public void setTarget(MinionMock aminion){ + currenttarget = aminion; + } + + public int getID() { + return _towerid; + } + + + +}