diff options
-rw-r--r-- | src/com/fourisland/frigidearth/GameState.java | 2 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/Inputable.java | 16 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/Main.java | 80 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/MapViewGameState.java | 103 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/TileGameState.java | 54 | ||||
-rw-r--r-- | src/com/fourisland/frigidearth/resources/RMG2000.ttf | bin | 0 -> 70260 bytes |
6 files changed, 188 insertions, 67 deletions
diff --git a/src/com/fourisland/frigidearth/GameState.java b/src/com/fourisland/frigidearth/GameState.java index 335760a..08aa173 100644 --- a/src/com/fourisland/frigidearth/GameState.java +++ b/src/com/fourisland/frigidearth/GameState.java | |||
@@ -8,7 +8,7 @@ package com.fourisland.frigidearth; | |||
8 | * | 8 | * |
9 | * @author hatkirby | 9 | * @author hatkirby |
10 | */ | 10 | */ |
11 | public interface GameState extends Renderable | 11 | public interface GameState extends Renderable, Inputable |
12 | { | 12 | { |
13 | public void tick(); | 13 | public void tick(); |
14 | } | 14 | } |
diff --git a/src/com/fourisland/frigidearth/Inputable.java b/src/com/fourisland/frigidearth/Inputable.java new file mode 100644 index 0000000..f30251a --- /dev/null +++ b/src/com/fourisland/frigidearth/Inputable.java | |||
@@ -0,0 +1,16 @@ | |||
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.event.KeyEvent; | ||
8 | |||
9 | /** | ||
10 | * | ||
11 | * @author hatkirby | ||
12 | */ | ||
13 | public interface Inputable | ||
14 | { | ||
15 | public void processInput(KeyEvent e); | ||
16 | } | ||
diff --git a/src/com/fourisland/frigidearth/Main.java b/src/com/fourisland/frigidearth/Main.java index ab4b8b6..fe936b0 100644 --- a/src/com/fourisland/frigidearth/Main.java +++ b/src/com/fourisland/frigidearth/Main.java | |||
@@ -13,9 +13,12 @@ import java.awt.GraphicsDevice; | |||
13 | import java.awt.GraphicsEnvironment; | 13 | import java.awt.GraphicsEnvironment; |
14 | import java.awt.Toolkit; | 14 | import java.awt.Toolkit; |
15 | import java.awt.Transparency; | 15 | import java.awt.Transparency; |
16 | import java.awt.event.KeyEvent; | ||
17 | import java.awt.event.KeyListener; | ||
16 | import java.awt.image.BufferStrategy; | 18 | import java.awt.image.BufferStrategy; |
17 | import java.awt.image.BufferedImage; | 19 | import java.awt.image.BufferedImage; |
18 | import java.util.List; | 20 | import java.util.List; |
21 | import java.util.Stack; | ||
19 | import java.util.concurrent.CopyOnWriteArrayList; | 22 | import java.util.concurrent.CopyOnWriteArrayList; |
20 | import javax.swing.JFrame; | 23 | import javax.swing.JFrame; |
21 | 24 | ||
@@ -25,8 +28,9 @@ import javax.swing.JFrame; | |||
25 | */ | 28 | */ |
26 | public class Main | 29 | public class Main |
27 | { | 30 | { |
28 | static final int GAME_WIDTH = 320; | 31 | public static final int GAME_WIDTH = 320; |
29 | static final int GAME_HEIGHT = 240; | 32 | public static final int GAME_HEIGHT = 240; |
33 | public static final int FPS = (1000 / 60); // 60 fps | ||
30 | 34 | ||
31 | private static JFrame mainWindow; | 35 | private static JFrame mainWindow; |
32 | private static Color[][] grid; | 36 | private static Color[][] grid; |
@@ -34,6 +38,7 @@ public class Main | |||
34 | private static int drawOffsetY = 0; | 38 | private static int drawOffsetY = 0; |
35 | private static Canvas gameCanvas; | 39 | private static Canvas gameCanvas; |
36 | private static List<Renderable> renderables = new CopyOnWriteArrayList<Renderable>(); | 40 | private static List<Renderable> renderables = new CopyOnWriteArrayList<Renderable>(); |
41 | private static Stack<Inputable> inputables = new Stack<Inputable>(); | ||
37 | private static GameState gameState; | 42 | private static GameState gameState; |
38 | 43 | ||
39 | public static void main(String[] args) | 44 | public static void main(String[] args) |
@@ -45,31 +50,82 @@ public class Main | |||
45 | mainWindow.setSize(GAME_WIDTH*2, GAME_HEIGHT*2); | 50 | mainWindow.setSize(GAME_WIDTH*2, GAME_HEIGHT*2); |
46 | mainWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-GAME_WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-GAME_HEIGHT); | 51 | mainWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-GAME_WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-GAME_HEIGHT); |
47 | mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 52 | mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
53 | mainWindow.addKeyListener(new KeyListener() { | ||
54 | @Override | ||
55 | public void keyTyped(KeyEvent ke) | ||
56 | { | ||
57 | // No me importa | ||
58 | } | ||
59 | |||
60 | @Override | ||
61 | public void keyPressed(KeyEvent ke) | ||
62 | { | ||
63 | inputables.peek().processInput(ke); | ||
64 | gameState.tick(); | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | public void keyReleased(KeyEvent ke) | ||
69 | { | ||
70 | // No me importa | ||
71 | } | ||
72 | }); | ||
48 | mainWindow.add(gameCanvas); | 73 | mainWindow.add(gameCanvas); |
49 | mainWindow.setVisible(true); | 74 | mainWindow.setVisible(true); |
50 | 75 | ||
51 | gameCanvas.createBufferStrategy(2); | 76 | gameCanvas.createBufferStrategy(2); |
52 | 77 | ||
53 | gameState = new TileGameState(); | 78 | setGameState(new MapViewGameState()); |
54 | renderables.add(gameState); | ||
55 | 79 | ||
56 | gameState.tick(); | 80 | gameState.tick(); |
57 | 81 | ||
82 | long waitTime = System.nanoTime() + (1000000*FPS); | ||
58 | for (;;) | 83 | for (;;) |
59 | { | 84 | { |
60 | render(gameCanvas); | 85 | if (System.nanoTime() > waitTime) |
61 | |||
62 | try | ||
63 | { | ||
64 | Thread.sleep(10); | ||
65 | } catch (InterruptedException ex) | ||
66 | { | 86 | { |
67 | //Nothing | 87 | render(gameCanvas); |
88 | waitTime = System.nanoTime() + (1000000*FPS); | ||
68 | } | 89 | } |
69 | } | 90 | } |
70 | } | 91 | } |
71 | 92 | ||
72 | public static void render(Canvas gameCanvas) | 93 | public static void setGameState(GameState m_gameState) |
94 | { | ||
95 | renderables.clear(); | ||
96 | inputables.clear(); | ||
97 | |||
98 | gameState = m_gameState; | ||
99 | renderables.add(gameState); | ||
100 | inputables.push(gameState); | ||
101 | } | ||
102 | |||
103 | public static void addRenderable(Renderable renderable) | ||
104 | { | ||
105 | renderables.add(renderable); | ||
106 | } | ||
107 | |||
108 | public static void removeRenderable(Renderable renderable) | ||
109 | { | ||
110 | renderables.remove(renderable); | ||
111 | } | ||
112 | |||
113 | public static void addInputable(Inputable inputable) | ||
114 | { | ||
115 | inputables.push(inputable); | ||
116 | } | ||
117 | |||
118 | public static void removeInputable(Inputable inputable) | ||
119 | { | ||
120 | if (inputables.peek() == inputable) | ||
121 | { | ||
122 | inputables.pop(); | ||
123 | } else { | ||
124 | throw new IllegalStateException("Inputable to be removed must be at the top of the stack"); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | private static void render(Canvas gameCanvas) | ||
73 | { | 129 | { |
74 | BufferStrategy buffer = gameCanvas.getBufferStrategy(); | 130 | BufferStrategy buffer = gameCanvas.getBufferStrategy(); |
75 | GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); | 131 | GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java new file mode 100644 index 0000000..543ce0b --- /dev/null +++ b/src/com/fourisland/frigidearth/MapViewGameState.java | |||
@@ -0,0 +1,103 @@ | |||
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.Color; | ||
8 | import java.awt.Font; | ||
9 | import java.awt.Graphics2D; | ||
10 | import java.awt.event.KeyEvent; | ||
11 | import java.util.Random; | ||
12 | |||
13 | /** | ||
14 | * | ||
15 | * @author hatkirby | ||
16 | */ | ||
17 | public class MapViewGameState implements GameState | ||
18 | { | ||
19 | private final int TILE_WIDTH = 16; | ||
20 | private final int TILE_HEIGHT = 16; | ||
21 | private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; | ||
22 | private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; | ||
23 | private Color[][] grid; | ||
24 | private int playerx = 4; | ||
25 | private int playery = 4; | ||
26 | |||
27 | public MapViewGameState() | ||
28 | { | ||
29 | Random r = new Random(); | ||
30 | grid = new Color[GAME_WIDTH][GAME_HEIGHT]; | ||
31 | |||
32 | for (int x=0; x<GAME_WIDTH; x++) | ||
33 | { | ||
34 | for (int y=0; y<GAME_HEIGHT; y++) | ||
35 | { | ||
36 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | public void tick() | ||
42 | { | ||
43 | Random r = new Random(); | ||
44 | |||
45 | if (r.nextBoolean()) | ||
46 | { | ||
47 | for (int x=0; x<GAME_WIDTH; x++) | ||
48 | { | ||
49 | for (int y=0; y<GAME_HEIGHT; y++) | ||
50 | { | ||
51 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | |||
57 | public void render(Graphics2D g) | ||
58 | { | ||
59 | // Render tiles | ||
60 | for (int x=0; x<GAME_WIDTH; x++) | ||
61 | { | ||
62 | for (int y=0; y<GAME_HEIGHT; y++) | ||
63 | { | ||
64 | g.setColor(grid[x][y]); | ||
65 | g.fillRect(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | // Render player | ||
70 | g.setColor(Color.white); | ||
71 | g.setFont(new Font("Lucida Sans", Font.BOLD, 16)); | ||
72 | g.drawString("@", playerx*TILE_WIDTH+3, (playery+1)*TILE_HEIGHT); | ||
73 | } | ||
74 | |||
75 | public void processInput(KeyEvent e) | ||
76 | { | ||
77 | if (e.getKeyCode() == KeyEvent.VK_LEFT) | ||
78 | { | ||
79 | if (playerx > 0) | ||
80 | { | ||
81 | playerx--; | ||
82 | } | ||
83 | } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) | ||
84 | { | ||
85 | if (playerx < GAME_WIDTH - 1) | ||
86 | { | ||
87 | playerx++; | ||
88 | } | ||
89 | } else if (e.getKeyCode() == KeyEvent.VK_UP) | ||
90 | { | ||
91 | if (playery > 0) | ||
92 | { | ||
93 | playery--; | ||
94 | } | ||
95 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) | ||
96 | { | ||
97 | if (playery < GAME_HEIGHT - 1) | ||
98 | { | ||
99 | playery++; | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | } | ||
diff --git a/src/com/fourisland/frigidearth/TileGameState.java b/src/com/fourisland/frigidearth/TileGameState.java deleted file mode 100644 index b64e551..0000000 --- a/src/com/fourisland/frigidearth/TileGameState.java +++ /dev/null | |||
@@ -1,54 +0,0 @@ | |||
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.Color; | ||
8 | import java.awt.Graphics2D; | ||
9 | import java.util.Random; | ||
10 | |||
11 | /** | ||
12 | * | ||
13 | * @author hatkirby | ||
14 | */ | ||
15 | public class TileGameState implements GameState | ||
16 | { | ||
17 | private final int TILE_WIDTH = 16; | ||
18 | private final int TILE_HEIGHT = 16; | ||
19 | private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; | ||
20 | private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; | ||
21 | private Color[][] grid; | ||
22 | |||
23 | public TileGameState() | ||
24 | { | ||
25 | Random r = new Random(); | ||
26 | grid = new Color[GAME_WIDTH][GAME_HEIGHT]; | ||
27 | |||
28 | for (int x=0; x<GAME_WIDTH; x++) | ||
29 | { | ||
30 | for (int y=0; y<GAME_HEIGHT; y++) | ||
31 | { | ||
32 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | |||
37 | public void tick() | ||
38 | { | ||
39 | |||
40 | } | ||
41 | |||
42 | public void render(Graphics2D g) | ||
43 | { | ||
44 | for (int x=0; x<GAME_WIDTH; x++) | ||
45 | { | ||
46 | for (int y=0; y<GAME_HEIGHT; y++) | ||
47 | { | ||
48 | g.setColor(grid[x][y]); | ||
49 | g.fillRect(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | |||
54 | } | ||
diff --git a/src/com/fourisland/frigidearth/resources/RMG2000.ttf b/src/com/fourisland/frigidearth/resources/RMG2000.ttf new file mode 100644 index 0000000..5012ff1 --- /dev/null +++ b/src/com/fourisland/frigidearth/resources/RMG2000.ttf | |||
Binary files differ | |||