Skip to content

Commit

Permalink
Define/read sprite images in enum for readability, maintainability and
Browse files Browse the repository at this point in the history
sane file I/O
  • Loading branch information
dwm10005 committed Apr 13, 2015
1 parent b82d02c commit bce2feb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/MinionMock.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import java.awt.image.BufferedImage;


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

public MinionMock(MinionTypes type, MapTowerDefense map) {
this.type = type;
this.map = map;
int[] unhashed = map.getGui().hash(map.getNodes()[0][0], map.getNodes()[0][1]);
x = unhashed[0];
y = unhashed[1];
health = type.getHealth();
attackDamage = type.getAttackDamage();
speed = type.getMovementSpeed();
sprite = type.getSprite();
visits = 1;
alive = true;
}
Expand All @@ -21,9 +27,15 @@ public int getX() {
public int getY() {
return y;
}
public int getType() {
return 0; //TODO
public MinionTypes getType() {
return type;
}

public BufferedImage getSprite()
{
return sprite;
}

public int findDirection() {
if (visits == map.getNum()) {
// CALL TO MAKE NEXUS LOSE HEALTH
Expand Down
27 changes: 22 additions & 5 deletions src/MinionTypes.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public enum MinionTypes {
//NAME (Health, Attack, Speed)
WEAKLING(100, 1, 1),
BASIC (255, 1, 1),
SPEEDO (100, 1, 4),
SUPERSPEEDO (255, 1, 4);
WEAKLING(100, 1, 1, "Minion.png"),
BASIC (255, 1, 1, "Minion.png"),
SPEEDO (100, 1, 4, "Minion.png"),
SUPERSPEEDO (255, 1, 4, "Minion.png");

private int health, attackDamage, movementSpeed;
private BufferedImage sprite;
private static final String SPRITE_FILE_DIR = "src/";

MinionTypes(int health, int attackDamage, int movementSpeed) {
MinionTypes(int health, int attackDamage, int movementSpeed, String spriteFileName) {
this.health = health;
this.attackDamage = attackDamage;
this.movementSpeed = movementSpeed;
try {
this.sprite = ImageIO.read(new File(SPRITE_FILE_DIR.concat(spriteFileName)));
} catch (IOException ex) {
System.out.println("image not found");
}
}

public int getHealth() {
Expand All @@ -25,4 +37,9 @@ public int getAttackDamage() {
public int getMovementSpeed() {
return movementSpeed;
}

public BufferedImage getSprite()
{
return sprite;
}
}
15 changes: 7 additions & 8 deletions src/MockGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ 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);
drawMinion(MF.getMinions()[i], g);
if (MF.getMinions()[i].go()) //TODO view code controlling model behavior. Bad! Need to refactor
{
gameOver = true;
Expand All @@ -173,16 +173,15 @@ public void drawPlayerHealth(Graphics 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);
try {
image = ImageIO.read(new File("src/Minion.png"));
} catch (IOException ex) {
System.out.println("image not found");
}

g.drawImage(image, x,y, null);
}

public void drawMinion(MinionMock m, Graphics g)
{
g.drawImage(m.getSprite(), m.getX(), m.getY(), null);
}

public void drawAllTowers(TowerFactory TF, Graphics g) {
int i = 0;
while (i < TF.getNum()) {
Expand Down

0 comments on commit bce2feb

Please sign in to comment.