From 6a2d1d894db801c7212d745f83bfb29557836659 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Mon, 4 Jun 2012 19:23:01 -0400 Subject: Added inventory overlay Now items can be used! Such fun. --- .../fourisland/frigidearth/InventoryOverlay.java | 117 +++++++++++++++++++++ src/com/fourisland/frigidearth/Item.java | 2 + src/com/fourisland/frigidearth/Main.java | 5 + .../fourisland/frigidearth/MapViewGameState.java | 19 +++- 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/com/fourisland/frigidearth/InventoryOverlay.java (limited to 'src') diff --git a/src/com/fourisland/frigidearth/InventoryOverlay.java b/src/com/fourisland/frigidearth/InventoryOverlay.java new file mode 100644 index 0000000..21f2194 --- /dev/null +++ b/src/com/fourisland/frigidearth/InventoryOverlay.java @@ -0,0 +1,117 @@ +/* + * 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.awt.event.KeyEvent; + +/** + * + * @author hatkirby + */ +public class InventoryOverlay implements Renderable, Inputable +{ + private final int TILE_WIDTH = 12; + private final int TILE_HEIGHT = 12; + private final int LIST_HEIGHT = 13; + + private int id = 0; + private int scroll = 0; + private int width = 0; + + public InventoryOverlay() + { + for (Item item : Main.currentGame.inventory) + { + if (item.getItemName().length() > width) + { + width = item.getItemName().length(); + } + } + + width += 2; + } + + public void render(Graphics2D g) + { + g.setColor(Color.BLACK); + g.fillRect(TILE_WIDTH, TILE_HEIGHT, width*TILE_WIDTH, Math.min(LIST_HEIGHT+2,Main.currentGame.inventory.size()+1)*TILE_HEIGHT); + + if (scroll > 0) + { + g.drawImage(SystemFont.getCharacter((char) 24, Color.WHITE), (1+width/2)*TILE_WIDTH, TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); + } + + for (int i=scroll; i (scroll+LIST_HEIGHT-1)) + { + scroll++; + } + } + + break; + + case KeyEvent.VK_ENTER: + if (Main.currentGame.inventory.get(id).useItem()) + { + Main.currentGame.inventory.remove(id); + ((MapViewGameState) Main.getGameState()).tick(); + Main.removeRenderable(this); + Main.removeInputable(this); + + return; + } + } + } + +} diff --git a/src/com/fourisland/frigidearth/Item.java b/src/com/fourisland/frigidearth/Item.java index 717d09c..1fe4027 100644 --- a/src/com/fourisland/frigidearth/Item.java +++ b/src/com/fourisland/frigidearth/Item.java @@ -73,6 +73,8 @@ public enum Item public boolean useItem() { + ((MapViewGameState) Main.getGameState()).printMessage("There's nothing to use the key on!"); + return false; } }; diff --git a/src/com/fourisland/frigidearth/Main.java b/src/com/fourisland/frigidearth/Main.java index 09aa273..e7a5d44 100644 --- a/src/com/fourisland/frigidearth/Main.java +++ b/src/com/fourisland/frigidearth/Main.java @@ -91,6 +91,11 @@ public class Main extends Canvas render(gameCanvas); } + public static GameState getGameState() + { + return gameState; + } + public static void addRenderable(Renderable renderable) { renderables.add(renderable); diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index 793fae9..bbfb9ac 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java @@ -899,10 +899,27 @@ public class MapViewGameState implements GameState break; } + case KeyEvent.VK_I: + if (Main.currentGame.inventory.isEmpty()) + { + printMessage("You have no items"); + } else { + InventoryOverlay io = new InventoryOverlay(); + Main.addRenderable(io); + Main.addInputable(io); + + return; + } + default: return; } + tick(); + } + + public void tick() + { // Move mobs for (Mob mob : mobs) { @@ -1122,7 +1139,7 @@ public class MapViewGameState implements GameState return true; } - private void printMessage(String message) + public void printMessage(String message) { String temp = message; -- cgit 1.4.1