diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2012-06-03 12:52:05 -0400 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2012-06-03 12:52:05 -0400 |
commit | b50ccd824487a38ced129c4826b1afb8adf569aa (patch) | |
tree | 5b41df65992fb4a46898187069c87a99d70234e2 /src | |
parent | c732582f681412d364e2f0509d2fdbf5319b9402 (diff) | |
download | frigidearth-b50ccd824487a38ced129c4826b1afb8adf569aa.tar.gz frigidearth-b50ccd824487a38ced129c4826b1afb8adf569aa.tar.bz2 frigidearth-b50ccd824487a38ced129c4826b1afb8adf569aa.zip |
Started work on mobs
Mice are randomly added to rooms and they move randomly. They move through the player, currently, however.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/fourisland/frigidearth/Direction.java | 45 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/Functions.java | 12 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/MapViewGameState.java | 85 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/Mob.java | 47 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/mobs/Mouse.java | 25 |
5 files changed, 188 insertions, 26 deletions
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 @@ | |||
4 | */ | 4 | */ |
5 | package com.fourisland.frigidearth; | 5 | package com.fourisland.frigidearth; |
6 | 6 | ||
7 | import java.awt.Point; | ||
8 | import java.awt.event.KeyEvent; | ||
9 | |||
7 | /** | 10 | /** |
8 | * | 11 | * |
9 | * @author hatkirby | 12 | * @author hatkirby |
10 | */ | 13 | */ |
11 | public enum Direction | 14 | public enum Direction |
12 | { | 15 | { |
13 | North, | 16 | North { |
14 | East, | 17 | public Point to(Point pos) |
15 | South, | 18 | { |
16 | West; | 19 | return new Point(pos.x, pos.y-1); |
20 | } | ||
21 | }, | ||
22 | East { | ||
23 | public Point to(Point pos) | ||
24 | { | ||
25 | return new Point(pos.x+1, pos.y); | ||
26 | } | ||
27 | }, | ||
28 | South { | ||
29 | public Point to(Point pos) | ||
30 | { | ||
31 | return new Point(pos.x, pos.y+1); | ||
32 | } | ||
33 | }, | ||
34 | West { | ||
35 | public Point to(Point pos) | ||
36 | { | ||
37 | return new Point(pos.x-1, pos.y); | ||
38 | } | ||
39 | }; | ||
40 | |||
41 | public abstract Point to(Point pos); | ||
17 | 42 | ||
18 | public static Direction getRandomDirection() | 43 | public static Direction getRandomDirection() |
19 | { | 44 | { |
@@ -28,4 +53,16 @@ public enum Direction | |||
28 | default: return null; // This can't happen | 53 | default: return null; // This can't happen |
29 | } | 54 | } |
30 | } | 55 | } |
56 | |||
57 | public static Direction fromKeyEvent(KeyEvent e) | ||
58 | { | ||
59 | switch (e.getKeyCode()) | ||
60 | { | ||
61 | case KeyEvent.VK_UP: return North; | ||
62 | case KeyEvent.VK_RIGHT: return East; | ||
63 | case KeyEvent.VK_DOWN: return South; | ||
64 | case KeyEvent.VK_LEFT: return West; | ||
65 | default: return null; | ||
66 | } | ||
67 | } | ||
31 | } | 68 | } |
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 | |||
18 | 18 | ||
19 | return r.nextInt(max - min + 1) + min; | 19 | return r.nextInt(max - min + 1) + min; |
20 | } | 20 | } |
21 | |||
22 | public static int rollDice(int dice, int sides) | ||
23 | { | ||
24 | int result = 0; | ||
25 | |||
26 | for (int i=0; i<dice; i++) | ||
27 | { | ||
28 | result += random(1, sides); | ||
29 | } | ||
30 | |||
31 | return result; | ||
32 | } | ||
21 | } | 33 | } |
diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index 43d61b7..c6f82d0 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java | |||
@@ -4,8 +4,10 @@ | |||
4 | */ | 4 | */ |
5 | package com.fourisland.frigidearth; | 5 | package com.fourisland.frigidearth; |
6 | 6 | ||
7 | import com.fourisland.frigidearth.mobs.Mouse; | ||
7 | import java.awt.Color; | 8 | import java.awt.Color; |
8 | import java.awt.Graphics2D; | 9 | import java.awt.Graphics2D; |
10 | import java.awt.Point; | ||
9 | import java.awt.event.KeyEvent; | 11 | import java.awt.event.KeyEvent; |
10 | import java.util.ArrayList; | 12 | import java.util.ArrayList; |
11 | import java.util.List; | 13 | import java.util.List; |
@@ -32,6 +34,7 @@ public class MapViewGameState implements GameState | |||
32 | private Tile[][] grid; | 34 | private Tile[][] grid; |
33 | private boolean[][] gridLighting; | 35 | private boolean[][] gridLighting; |
34 | private List<Room> rooms = new ArrayList<Room>(); | 36 | private List<Room> rooms = new ArrayList<Room>(); |
37 | private List<Mob> mobs = new ArrayList<Mob>(); | ||
35 | private int playerx = 4; | 38 | private int playerx = 4; |
36 | private int playery = 4; | 39 | private int playery = 4; |
37 | private int viewportx = 0; | 40 | private int viewportx = 0; |
@@ -225,6 +228,13 @@ public class MapViewGameState implements GameState | |||
225 | 228 | ||
226 | rooms.add(room); | 229 | rooms.add(room); |
227 | 230 | ||
231 | // Place mice in random rooms because yolo | ||
232 | if (Functions.random(0, 100) < 25) | ||
233 | { | ||
234 | Mob mob = new Mouse(Functions.random(room.getX()+1, room.getX()+room.getWidth()-2), Functions.random(room.getY()+1, room.getY()+room.getHeight()-2)); | ||
235 | mobs.add(mob); | ||
236 | } | ||
237 | |||
228 | return true; | 238 | return true; |
229 | } | 239 | } |
230 | 240 | ||
@@ -460,38 +470,59 @@ public class MapViewGameState implements GameState | |||
460 | } | 470 | } |
461 | } | 471 | } |
462 | 472 | ||
473 | // Render mobs | ||
474 | for (Mob mob : mobs) | ||
475 | { | ||
476 | if ((gridLighting[mob.getX()][mob.getY()]) && (mob.getX() > viewportx) && (mob.getX() < viewportx+VIEWPORT_WIDTH) && (mob.getY() > viewporty) && (mob.getY() < viewporty+VIEWPORT_HEIGHT)) | ||
477 | { | ||
478 | g.drawImage(SystemFont.getCharacter(mob.getDisplayCharacter()), (mob.getX()-viewportx)*TILE_WIDTH, (mob.getY()-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); | ||
479 | } | ||
480 | } | ||
481 | |||
463 | // Render player | 482 | // Render player |
464 | g.drawImage(SystemFont.getCharacter('@'), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); | 483 | g.drawImage(SystemFont.getCharacter('@'), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); |
465 | } | 484 | } |
466 | 485 | ||
467 | public void processInput(KeyEvent e) | 486 | public void processInput(KeyEvent e) |
468 | { | 487 | { |
469 | if (e.getKeyCode() == KeyEvent.VK_LEFT) | 488 | switch (e.getKeyCode()) |
470 | { | ||
471 | if ((playerx > 0) && (!grid[playerx-1][playery].isBlocked())) | ||
472 | { | ||
473 | playerx--; | ||
474 | } | ||
475 | } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) | ||
476 | { | ||
477 | if ((playerx < GAME_WIDTH - 1) && (!grid[playerx+1][playery].isBlocked())) | ||
478 | { | ||
479 | playerx++; | ||
480 | } | ||
481 | } else if (e.getKeyCode() == KeyEvent.VK_UP) | ||
482 | { | 489 | { |
483 | if ((playery > 0) && (!grid[playerx][playery-1].isBlocked())) | 490 | case KeyEvent.VK_LEFT: |
484 | { | 491 | case KeyEvent.VK_RIGHT: |
485 | playery--; | 492 | case KeyEvent.VK_UP: |
486 | } | 493 | case KeyEvent.VK_DOWN: |
487 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) | 494 | Direction dir = Direction.fromKeyEvent(e); |
495 | Point to = dir.to(new Point(playerx, playery)); | ||
496 | |||
497 | if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked())) | ||
498 | { | ||
499 | playerx = to.x; | ||
500 | playery = to.y; | ||
501 | } else { | ||
502 | return; | ||
503 | } | ||
504 | |||
505 | break; | ||
506 | |||
507 | default: | ||
508 | return; | ||
509 | } | ||
510 | |||
511 | // Move mobs randomly | ||
512 | for (Mob mob : mobs) | ||
488 | { | 513 | { |
489 | if ((playery < GAME_HEIGHT - 1) && (!grid[playerx][playery+1].isBlocked())) | 514 | Direction toDir = null; |
515 | |||
516 | for (int i=0; i<10; i++) | ||
490 | { | 517 | { |
491 | playery++; | 518 | toDir = Direction.getRandomDirection(); |
519 | Point to = toDir.to(mob.getPosition()); | ||
520 | if ((isValidPosition(to.x,to.y)) &&(!grid[to.x][to.y].isBlocked())) | ||
521 | { | ||
522 | mob.moveInDirection(toDir); | ||
523 | break; | ||
524 | } | ||
492 | } | 525 | } |
493 | } else { | ||
494 | return; | ||
495 | } | 526 | } |
496 | 527 | ||
497 | adjustViewport(); | 528 | adjustViewport(); |
@@ -524,4 +555,14 @@ public class MapViewGameState implements GameState | |||
524 | viewporty = 0; | 555 | viewporty = 0; |
525 | } | 556 | } |
526 | } | 557 | } |
558 | |||
559 | private boolean isValidPosition(int x, int y) | ||
560 | { | ||
561 | if (x < 0) return false; | ||
562 | if (x > GAME_WIDTH) return false; | ||
563 | if (y < 0) return false; | ||
564 | if (y > GAME_HEIGHT) return false; | ||
565 | |||
566 | return true; | ||
567 | } | ||
527 | } | 568 | } |
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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | package com.fourisland.frigidearth; | ||
6 | |||
7 | import java.awt.Point; | ||
8 | |||
9 | /** | ||
10 | * | ||
11 | * @author hatkirby | ||
12 | */ | ||
13 | public abstract class Mob | ||
14 | { | ||
15 | private int x; | ||
16 | private int y; | ||
17 | |||
18 | public Mob(int x, int y) | ||
19 | { | ||
20 | this.x = x; | ||
21 | this.y = y; | ||
22 | } | ||
23 | |||
24 | public int getX() | ||
25 | { | ||
26 | return x; | ||
27 | } | ||
28 | |||
29 | public int getY() | ||
30 | { | ||
31 | return y; | ||
32 | } | ||
33 | |||
34 | public Point getPosition() | ||
35 | { | ||
36 | return new Point(x,y); | ||
37 | } | ||
38 | |||
39 | public void moveInDirection(Direction dir) | ||
40 | { | ||
41 | Point to = dir.to(getPosition()); | ||
42 | x = to.x; | ||
43 | y = to.y; | ||
44 | } | ||
45 | |||
46 | public abstract char getDisplayCharacter(); | ||
47 | } | ||
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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | package com.fourisland.frigidearth.mobs; | ||
6 | |||
7 | import com.fourisland.frigidearth.Mob; | ||
8 | |||
9 | /** | ||
10 | * | ||
11 | * @author hatkirby | ||
12 | */ | ||
13 | public class Mouse extends Mob | ||
14 | { | ||
15 | public Mouse(int x, int y) | ||
16 | { | ||
17 | super(x, y); | ||
18 | } | ||
19 | |||
20 | |||
21 | public char getDisplayCharacter() | ||
22 | { | ||
23 | return 'm'; | ||
24 | } | ||
25 | } | ||