From bce2feb38a033ae8856be56b5dbf234fd7ac0a6e Mon Sep 17 00:00:00 2001 From: dwm10005 Date: Mon, 13 Apr 2015 13:09:31 -0400 Subject: [PATCH] Define/read sprite images in enum for readability, maintainability and sane file I/O --- src/MinionMock.java | 18 +++++++++++++++--- src/MinionTypes.java | 27 ++++++++++++++++++++++----- src/MockGui.java | 15 +++++++-------- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/MinionMock.java b/src/MinionMock.java index d065834..3826ae4 100644 --- a/src/MinionMock.java +++ b/src/MinionMock.java @@ -1,10 +1,15 @@ +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]; @@ -12,6 +17,7 @@ public MinionMock(MinionTypes type, MapTowerDefense map) { health = type.getHealth(); attackDamage = type.getAttackDamage(); speed = type.getMovementSpeed(); + sprite = type.getSprite(); visits = 1; alive = true; } @@ -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 diff --git a/src/MinionTypes.java b/src/MinionTypes.java index 6f76bb8..e8245d0 100644 --- a/src/MinionTypes.java +++ b/src/MinionTypes.java @@ -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() { @@ -25,4 +37,9 @@ public int getAttackDamage() { public int getMovementSpeed() { return movementSpeed; } + + public BufferedImage getSprite() + { + return sprite; + } } diff --git a/src/MockGui.java b/src/MockGui.java index ae722f0..a36ba13 100644 --- a/src/MockGui.java +++ b/src/MockGui.java @@ -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; @@ -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()) {