From b50ccd824487a38ced129c4826b1afb8adf569aa Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 3 Jun 2012 12:52:05 -0400 Subject: Started work on mobs Mice are randomly added to rooms and they move randomly. They move through the player, currently, however. --- src/com/fourisland/frigidearth/Direction.java | 45 +++++++++++- src/com/fourisland/frigidearth/Functions.java | 12 +++ .../fourisland/frigidearth/MapViewGameState.java | 85 ++++++++++++++++------ src/com/fourisland/frigidearth/Mob.java | 47 ++++++++++++ src/com/fourisland/frigidearth/mobs/Mouse.java | 25 +++++++ 5 files changed, 188 insertions(+), 26 deletions(-) create mode 100644 src/com/fourisland/frigidearth/Mob.java create mode 100644 src/com/fourisland/frigidearth/mobs/Mouse.java (limited to 'src/com') diff --git a/src/com/fourisland/frigidearth/Direction.java b/src/com/fourisland/frigidearth/Direction.java index 3c24a89..070be9d 100644 --- a/src/com/fourisland/frigidearth/Direction.java +++ b/src/com/fourisland/frigidearth/Direction.java @@ -4,16 +4,41 @@ */ package com.fourisland.frigidearth; +import java.awt.Point; +import java.awt.event.KeyEvent; + /** * * @author hatkirby */ public enum Direction { - North, - East, - South, - West; + North { + public Point to(Point pos) + { + return new Point(pos.x, pos.y-1); + } + }, + East { + public Point to(Point pos) + { + return new Point(pos.x+1, pos.y); + } + }, + South { + public Point to(Point pos) + { + return new Point(pos.x, pos.y+1); + } + }, + West { + public Point to(Point pos) + { + return new Point(pos.x-1, pos.y); + } + }; + + public abstract Point to(Point pos); public static Direction getRandomDirection() { @@ -28,4 +53,16 @@ public enum Direction default: return null; // This can't happen } } + + public static Direction fromKeyEvent(KeyEvent e) + { + switch (e.getKeyCode()) + { + case KeyEvent.VK_UP: return North; + case KeyEvent.VK_RIGHT: return East; + case KeyEvent.VK_DOWN: return South; + case KeyEvent.VK_LEFT: return West; + default: return null; + } + } } diff --git a/src/com/fourisland/frigidearth/Functions.java b/src/com/fourisland/frigidearth/Functions.java index cb57086..32943ce 100644 --- a/src/com/fourisland/frigidearth/Functions.java +++ b/src/com/fourisland/frigidearth/Functions.java @@ -18,4 +18,16 @@ public class Functions return r.nextInt(max - min + 1) + min; } + + public static int rollDice(int dice, int sides) + { + int result = 0; + + for (int i=0; i rooms = new ArrayList(); + private List mobs = new ArrayList(); private int playerx = 4; private int playery = 4; private int viewportx = 0; @@ -225,6 +228,13 @@ public class MapViewGameState implements GameState rooms.add(room); + // Place mice in random rooms because yolo + if (Functions.random(0, 100) < 25) + { + Mob mob = new Mouse(Functions.random(room.getX()+1, room.getX()+room.getWidth()-2), Functions.random(room.getY()+1, room.getY()+room.getHeight()-2)); + mobs.add(mob); + } + return true; } @@ -460,38 +470,59 @@ 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)) + { + g.drawImage(SystemFont.getCharacter(mob.getDisplayCharacter()), (mob.getX()-viewportx)*TILE_WIDTH, (mob.getY()-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); + } + } + // Render player g.drawImage(SystemFont.getCharacter('@'), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); } public void processInput(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_LEFT) - { - if ((playerx > 0) && (!grid[playerx-1][playery].isBlocked())) - { - playerx--; - } - } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) - { - if ((playerx < GAME_WIDTH - 1) && (!grid[playerx+1][playery].isBlocked())) - { - playerx++; - } - } else if (e.getKeyCode() == KeyEvent.VK_UP) + switch (e.getKeyCode()) { - if ((playery > 0) && (!grid[playerx][playery-1].isBlocked())) - { - playery--; - } - } else if (e.getKeyCode() == KeyEvent.VK_DOWN) + case KeyEvent.VK_LEFT: + case KeyEvent.VK_RIGHT: + case KeyEvent.VK_UP: + case KeyEvent.VK_DOWN: + Direction dir = Direction.fromKeyEvent(e); + Point to = dir.to(new Point(playerx, playery)); + + if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked())) + { + playerx = to.x; + playery = to.y; + } else { + return; + } + + break; + + default: + return; + } + + // Move mobs randomly + for (Mob mob : mobs) { - if ((playery < GAME_HEIGHT - 1) && (!grid[playerx][playery+1].isBlocked())) + Direction toDir = null; + + for (int i=0; i<10; i++) { - playery++; + toDir = Direction.getRandomDirection(); + Point to = toDir.to(mob.getPosition()); + if ((isValidPosition(to.x,to.y)) &&(!grid[to.x][to.y].isBlocked())) + { + mob.moveInDirection(toDir); + break; + } } - } else { - return; } adjustViewport(); @@ -524,4 +555,14 @@ public class MapViewGameState implements GameState viewporty = 0; } } + + private boolean isValidPosition(int x, int y) + { + if (x < 0) return false; + if (x > GAME_WIDTH) return false; + if (y < 0) return false; + if (y > GAME_HEIGHT) return false; + + return true; + } } diff --git a/src/com/fourisland/frigidearth/Mob.java b/src/com/fourisland/frigidearth/Mob.java new file mode 100644 index 0000000..c07e039 --- /dev/null +++ b/src/com/fourisland/frigidearth/Mob.java @@ -0,0 +1,47 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +import java.awt.Point; + +/** + * + * @author hatkirby + */ +public abstract class Mob +{ + private int x; + private int y; + + public Mob(int x, int y) + { + this.x = x; + this.y = y; + } + + public int getX() + { + return x; + } + + public int getY() + { + return y; + } + + public Point getPosition() + { + return new Point(x,y); + } + + public void moveInDirection(Direction dir) + { + Point to = dir.to(getPosition()); + x = to.x; + y = to.y; + } + + public abstract char getDisplayCharacter(); +} diff --git a/src/com/fourisland/frigidearth/mobs/Mouse.java b/src/com/fourisland/frigidearth/mobs/Mouse.java new file mode 100644 index 0000000..ad5aa60 --- /dev/null +++ b/src/com/fourisland/frigidearth/mobs/Mouse.java @@ -0,0 +1,25 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth.mobs; + +import com.fourisland.frigidearth.Mob; + +/** + * + * @author hatkirby + */ +public class Mouse extends Mob +{ + public Mouse(int x, int y) + { + super(x, y); + } + + + public char getDisplayCharacter() + { + return 'm'; + } +} -- cgit 1.4.1