Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TowerTypes class added to place Basic and AOE towers and their sprites
  • Loading branch information
ssl10003 committed Apr 18, 2015
1 parent 5bf190c commit 931edba
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Main.java
Expand Up @@ -7,7 +7,7 @@ public class Main extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("Tower Defense Refactor Mock GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640,640+23);
frame.setSize(650,720);
int num = 6;
int[][] nodes = new int[num][2];
nodes[0][0] = 1;
Expand Down
37 changes: 23 additions & 14 deletions src/MockGui.java
Expand Up @@ -150,6 +150,15 @@ public class MockGui extends JPanel{
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()) {
Expand All @@ -164,15 +173,6 @@ public class MockGui extends JPanel{
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.drawImage(image, x,y, null);
}
Expand All @@ -193,14 +193,18 @@ public class MockGui extends JPanel{
int i = 0;
while (i < TF.getNum()) {
int[] local = hash(TF.getTowerArray()[i].getTowerXlocation(), TF.getTowerArray()[i].getTowerYlocation());
drawBasicTower(local[0], local[1], g);
drawBasicTower(TF.getTowerArray()[i], 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);
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);

//tm.getTowerXlocation()
//tm.getTowerYlocation()
}


Expand All @@ -213,7 +217,12 @@ public class MockGui extends JPanel{
System.out.println("x; " + x + " y: " + y);
int[] local = unhash(x,y);

map.getTF().createBasicTower(local[0], local[1]);
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);
}
}

}
Expand Down
11 changes: 8 additions & 3 deletions src/StatGui.java
Expand Up @@ -18,6 +18,7 @@ public class StatGui extends JPanel implements ActionListener{
private int playermoney = 1000;
JLabel hpLabel, moneyLabel, towersnumLabel;
JButton AOETowerbutton, BasicTowerbutton;
public int towerPlacerVar = 1; //1: Basic, 2:AOE

public StatGui (int x, int y) {
X = x;
Expand Down Expand Up @@ -88,16 +89,20 @@ public class StatGui extends JPanel implements ActionListener{
public void actionPerformed(ActionEvent e){
if("select aoe tower".equals(e.getActionCommand())){
AOETowerbutton.setEnabled(true);
//set tower placer variable to place aoe tower
towerPlacerVar = 2;
System.out.println("AOE tower selected!");
}
else if("select basic tower".equals(e.getActionCommand())){
BasicTowerbutton.setEnabled(true);
//set tower placer variable to place basic tower
System.out.println("Basic b**ch tower selected!");
towerPlacerVar = 1;
System.out.println("Basic tower selected!");
}
}

public int gettowerPlacerVar(){
return towerPlacerVar;
}

public void updateHealth(){
hpLabel.setText("[Health]: " + map.getTF().getTowerArray()[0].getHealth() + "/" + maxHealth);
}
Expand Down
9 changes: 3 additions & 6 deletions src/TowerFactory.java
Expand Up @@ -36,17 +36,14 @@ public class TowerFactory {






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);
towerarray[0] = new TowerMock(0, x, y, false, TowerTypes.NEXUS);
}

public void createBasicTower(int x, int y){
public void createBasicTower(int x, int y, TowerTypes type){
//Checks if there's already a tower here
//TODO make this better
if (!_map.getStatGui().spendMoney(cost)) {
Expand All @@ -61,7 +58,7 @@ public class TowerFactory {
}
i++;
}
towerarray[quantity] = new TowerMock(quantity, x, y, true);
towerarray[quantity] = new TowerMock(quantity, x, y, true, type);
quantity++;
}
}
Expand Down
44 changes: 31 additions & 13 deletions src/TowerMock.java
@@ -1,20 +1,21 @@
import java.awt.image.BufferedImage;

public class TowerMock{
public int _xlocation, _ylocation, _value, _cost, _upgradecost, _towerid;
public int _xlocation, _ylocation, _sellCost, _buyCost, _upgradecost, _towerid;
public int _level = 1;
public int _range = 96;
public int _power = 1;
public double _firerate = 1.0;

private BufferedImage _sprite;
public TowerTypes type;
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 String Description = "Tower lol.";

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;
Expand All @@ -24,6 +25,27 @@ public class TowerMock{
health = 20;
}
}
*/

public TowerMock(int id, int xloc, int yloc, boolean isTower, TowerTypes type){
_towerid = id;
_xlocation = xloc;
_ylocation = yloc;
_isTower = isTower;
if (!isTower) {
health = 20;
}

this.type=type;
this._buyCost=type.getBuycost();
this._sellCost= type.getSellcost();
this._sprite=type.getSprite();

}

public BufferedImage getSprite(){
return _sprite;
}

public int dealDamage(int x, int y) {
int deltax = (32*_xlocation - x);
Expand Down Expand Up @@ -56,10 +78,6 @@ public class TowerMock{
public int getHealth() {
return health;
}





public int getTowerXlocation() {
return _xlocation;
Expand Down Expand Up @@ -88,18 +106,18 @@ public class TowerMock{

//get cost this tower was purchased for
public int getCost() {
_cost = (int) (_range + ( _power * 10) + (_firerate * 10));
return _cost;
_buyCost = (int) (_range + ( _power * 10) + (_firerate * 10));
return _buyCost;
}

//get upgrade cost to upgrade tower, double the price of last cost
public int getupgradeCost() {
_upgradecost = _cost * 2;
_upgradecost = _buyCost * 2;
return _upgradecost;
}

public void upgrade(){
_cost = _upgradecost;
_buyCost = _upgradecost;
_level = _level + 1;
this._range = this._range + 1;
this._firerate= this._firerate + 0.3;
Expand Down
44 changes: 44 additions & 0 deletions src/TowerTypes.java
@@ -0,0 +1,44 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public enum TowerTypes {
//NAME (Cost to buy, Cost to sell, "sprite dir")
NEXUS (1, 1, "Nexus Tower.png"),
BASIC (50, 25, "Basic Tower.png"),
AOE (100, 50, "AOE Tower.png");

private int _buycost, _sellcost;
private BufferedImage sprite;
private static final String SPRITE_FILE_DIR = "src/resources/images/";

TowerTypes(int buycost, int sellcost, String spriteFileName) {
this._buycost = buycost;
this._sellcost = sellcost;

try {
this.sprite = ImageIO.read(new File(SPRITE_FILE_DIR.concat(spriteFileName)));
} catch (IOException ex) {
System.out.println("image not found");
}
}

public int getBuycost() {
return _buycost;
}

public int getSellcost() {
return _sellcost;
}

public String getName() {
return this.toString();
}

public BufferedImage getSprite()
{
return sprite;
}
}

0 comments on commit 931edba

Please sign in to comment.