From 3f71b9f4a6173be5b39fb9bc9808ac464de2b394 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 3 Jun 2012 16:26:26 -0400 Subject: Added ability to kill mice When you walk into a mob, they lose one HP. Mice have only one HP, so when you walk into one (note: they are hard to catch as they move quite erratically), they immediately die. Mobs cannot yet hurt you but then again mice can't hurt you anyway. --- .../fourisland/frigidearth/MapViewGameState.java | 45 ++++++++++++++++++---- src/com/fourisland/frigidearth/Mob.java | 16 ++------ src/com/fourisland/frigidearth/mobs/Mouse.java | 8 +++- 3 files changed, 49 insertions(+), 20 deletions(-) (limited to 'src/com') diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index c1caab0..86fe437 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java @@ -519,9 +519,9 @@ public class MapViewGameState implements GameState // Render mobs for (Mob mob : mobs) { - if ((gridLighting[mob.getX()][mob.getY()]) && (mob.getX() > viewportx) && (mob.getX() < viewportx+VIEWPORT_WIDTH) && (mob.getY() > viewporty) && (mob.getY() < viewporty+VIEWPORT_HEIGHT)) + if ((gridLighting[mob.x][mob.y]) && (isInViewport(mob.x, mob.y))) { - g.drawImage(SystemFont.getCharacter(mob.getDisplayCharacter()), (mob.getX()-viewportx)*TILE_WIDTH, (mob.getY()-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); + g.drawImage(SystemFont.getCharacter(mob.getDisplayCharacter()), (mob.x-viewportx)*TILE_WIDTH, (mob.y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); } } @@ -557,8 +557,31 @@ public class MapViewGameState implements GameState if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked())) { - playerx = to.x; - playery = to.y; + // Check for mobs + boolean foundMob = false; + for (Mob mob : mobs) + { + if (mob.getPosition().equals(to)) + { + printMessage("You hit the " + mob.getName().toLowerCase()); + mob.health--; + + if (mob.health <= 0) + { + printMessage("You killed the " + mob.getName().toLowerCase() + "!"); + mobs.remove(mob); + } + + foundMob = true; + break; + } + } + + if (!foundMob) + { + playerx = to.x; + playery = to.y; + } } else { printMessage("Blocked: " + dir.name()); @@ -568,8 +591,6 @@ public class MapViewGameState implements GameState break; default: - printMessage("The sky is the limit! You can also place items in the same manner, but we'll get to that later."); - return; } @@ -582,7 +603,7 @@ public class MapViewGameState implements GameState { toDir = Direction.getRandomDirection(); Point to = toDir.to(mob.getPosition()); - if ((isValidPosition(to.x,to.y)) &&(!grid[to.x][to.y].isBlocked())) + if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked()) && (!to.equals(new Point(playerx, playery)))) { mob.moveInDirection(toDir); break; @@ -631,6 +652,16 @@ public class MapViewGameState implements GameState return true; } + private boolean isInViewport(int x, int y) + { + if (x < viewportx) return false; + if (x > viewportx+VIEWPORT_WIDTH) return false; + if (y < viewporty) return false; + if (y > viewporty+VIEWPORT_HEIGHT) return false; + + return true; + } + private void printMessage(String message) { String temp = message; diff --git a/src/com/fourisland/frigidearth/Mob.java b/src/com/fourisland/frigidearth/Mob.java index c07e039..fdc7a8a 100644 --- a/src/com/fourisland/frigidearth/Mob.java +++ b/src/com/fourisland/frigidearth/Mob.java @@ -12,8 +12,9 @@ import java.awt.Point; */ public abstract class Mob { - private int x; - private int y; + public int x; + public int y; + public int health; public Mob(int x, int y) { @@ -21,16 +22,6 @@ public abstract class Mob this.y = y; } - public int getX() - { - return x; - } - - public int getY() - { - return y; - } - public Point getPosition() { return new Point(x,y); @@ -44,4 +35,5 @@ public abstract class Mob } public abstract char getDisplayCharacter(); + public abstract String getName(); } diff --git a/src/com/fourisland/frigidearth/mobs/Mouse.java b/src/com/fourisland/frigidearth/mobs/Mouse.java index ad5aa60..5fdf7cd 100644 --- a/src/com/fourisland/frigidearth/mobs/Mouse.java +++ b/src/com/fourisland/frigidearth/mobs/Mouse.java @@ -15,11 +15,17 @@ public class Mouse extends Mob public Mouse(int x, int y) { super(x, y); + + health = 1; } - public char getDisplayCharacter() { return 'm'; } + + public String getName() + { + return "Mouse"; + } } -- cgit 1.4.1