From 5729344a510f662a01acc3def1f91c146af3b787 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 2 Jun 2012 17:24:32 -0400 Subject: Added movable player TileGameState has been renamed to MapViewGameState. Currently, there is a an @ symbol that can be moved around by pressing the arrow keys on the keyboard. The symbol is not perfectly positioned in its tile, which is a problem. Also, it looks pretty fuzzy and bad. The tiles of the game state also now change randomly every few turns. --- src/com/fourisland/frigidearth/GameState.java | 2 +- src/com/fourisland/frigidearth/Inputable.java | 16 ++++ src/com/fourisland/frigidearth/Main.java | 80 +++++++++++++--- .../fourisland/frigidearth/MapViewGameState.java | 103 +++++++++++++++++++++ src/com/fourisland/frigidearth/TileGameState.java | 54 ----------- .../fourisland/frigidearth/resources/RMG2000.ttf | Bin 0 -> 70260 bytes 6 files changed, 188 insertions(+), 67 deletions(-) create mode 100644 src/com/fourisland/frigidearth/Inputable.java create mode 100644 src/com/fourisland/frigidearth/MapViewGameState.java delete mode 100644 src/com/fourisland/frigidearth/TileGameState.java create mode 100644 src/com/fourisland/frigidearth/resources/RMG2000.ttf 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; * * @author hatkirby */ -public interface GameState extends Renderable +public interface GameState extends Renderable, Inputable { public void tick(); } 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 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +import java.awt.event.KeyEvent; + +/** + * + * @author hatkirby + */ +public interface Inputable +{ + public void processInput(KeyEvent e); +} 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; import java.awt.GraphicsEnvironment; import java.awt.Toolkit; import java.awt.Transparency; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.util.List; +import java.util.Stack; import java.util.concurrent.CopyOnWriteArrayList; import javax.swing.JFrame; @@ -25,8 +28,9 @@ import javax.swing.JFrame; */ public class Main { - static final int GAME_WIDTH = 320; - static final int GAME_HEIGHT = 240; + public static final int GAME_WIDTH = 320; + public static final int GAME_HEIGHT = 240; + public static final int FPS = (1000 / 60); // 60 fps private static JFrame mainWindow; private static Color[][] grid; @@ -34,6 +38,7 @@ public class Main private static int drawOffsetY = 0; private static Canvas gameCanvas; private static List renderables = new CopyOnWriteArrayList(); + private static Stack inputables = new Stack(); private static GameState gameState; public static void main(String[] args) @@ -45,31 +50,82 @@ public class Main mainWindow.setSize(GAME_WIDTH*2, GAME_HEIGHT*2); mainWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-GAME_WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-GAME_HEIGHT); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainWindow.addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent ke) + { + // No me importa + } + + @Override + public void keyPressed(KeyEvent ke) + { + inputables.peek().processInput(ke); + gameState.tick(); + } + + @Override + public void keyReleased(KeyEvent ke) + { + // No me importa + } + }); mainWindow.add(gameCanvas); mainWindow.setVisible(true); gameCanvas.createBufferStrategy(2); - gameState = new TileGameState(); - renderables.add(gameState); + setGameState(new MapViewGameState()); gameState.tick(); + long waitTime = System.nanoTime() + (1000000*FPS); for (;;) { - render(gameCanvas); - - try - { - Thread.sleep(10); - } catch (InterruptedException ex) + if (System.nanoTime() > waitTime) { - //Nothing + render(gameCanvas); + waitTime = System.nanoTime() + (1000000*FPS); } } } - public static void render(Canvas gameCanvas) + public static void setGameState(GameState m_gameState) + { + renderables.clear(); + inputables.clear(); + + gameState = m_gameState; + renderables.add(gameState); + inputables.push(gameState); + } + + public static void addRenderable(Renderable renderable) + { + renderables.add(renderable); + } + + public static void removeRenderable(Renderable renderable) + { + renderables.remove(renderable); + } + + public static void addInputable(Inputable inputable) + { + inputables.push(inputable); + } + + public static void removeInputable(Inputable inputable) + { + if (inputables.peek() == inputable) + { + inputables.pop(); + } else { + throw new IllegalStateException("Inputable to be removed must be at the top of the stack"); + } + } + + private static void render(Canvas gameCanvas) { BufferStrategy buffer = gameCanvas.getBufferStrategy(); 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 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.event.KeyEvent; +import java.util.Random; + +/** + * + * @author hatkirby + */ +public class MapViewGameState implements GameState +{ + private final int TILE_WIDTH = 16; + private final int TILE_HEIGHT = 16; + private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; + private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; + private Color[][] grid; + private int playerx = 4; + private int playery = 4; + + public MapViewGameState() + { + Random r = new Random(); + grid = new Color[GAME_WIDTH][GAME_HEIGHT]; + + for (int x=0; x 0) + { + playerx--; + } + } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) + { + if (playerx < GAME_WIDTH - 1) + { + playerx++; + } + } else if (e.getKeyCode() == KeyEvent.VK_UP) + { + if (playery > 0) + { + playery--; + } + } else if (e.getKeyCode() == KeyEvent.VK_DOWN) + { + if (playery < GAME_HEIGHT - 1) + { + playery++; + } + } + } +} 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 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package com.fourisland.frigidearth; - -import java.awt.Color; -import java.awt.Graphics2D; -import java.util.Random; - -/** - * - * @author hatkirby - */ -public class TileGameState implements GameState -{ - private final int TILE_WIDTH = 16; - private final int TILE_HEIGHT = 16; - private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; - private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; - private Color[][] grid; - - public TileGameState() - { - Random r = new Random(); - grid = new Color[GAME_WIDTH][GAME_HEIGHT]; - - for (int x=0; x