From 70282b22d8452f72d7daa14ec62267b37a01f482 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Mon, 4 Jun 2012 17:42:39 -0400 Subject: Started to add items Items now exist and can be spawned randomly (though it's weighted so that items of a rarer type such as rings which spawn less frequently than swords let's say). There's currently only one item defined: a scroll of healing. Items can be seen and picked up but there is no inventory interface and no way to use items yet. --- src/com/fourisland/frigidearth/Game.java | 4 + src/com/fourisland/frigidearth/Item.java | 97 ++++++++++++++++++++++ src/com/fourisland/frigidearth/ItemInstance.java | 16 ++++ src/com/fourisland/frigidearth/ItemType.java | 27 ++++++ .../fourisland/frigidearth/MapViewGameState.java | 39 +++++++++ 5 files changed, 183 insertions(+) create mode 100644 src/com/fourisland/frigidearth/Item.java create mode 100644 src/com/fourisland/frigidearth/ItemInstance.java create mode 100644 src/com/fourisland/frigidearth/ItemType.java diff --git a/src/com/fourisland/frigidearth/Game.java b/src/com/fourisland/frigidearth/Game.java index 5d669de..3487729 100644 --- a/src/com/fourisland/frigidearth/Game.java +++ b/src/com/fourisland/frigidearth/Game.java @@ -4,6 +4,9 @@ */ package com.fourisland.frigidearth; +import java.util.ArrayList; +import java.util.List; + /** * * @author hatkirby @@ -15,4 +18,5 @@ public class Game public int defense = 0; public int level = 1; public int experience = 0; + public List inventory = new ArrayList(); } diff --git a/src/com/fourisland/frigidearth/Item.java b/src/com/fourisland/frigidearth/Item.java new file mode 100644 index 0000000..689d991 --- /dev/null +++ b/src/com/fourisland/frigidearth/Item.java @@ -0,0 +1,97 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +/** + * + * @author hatkirby + */ +public enum Item +{ + ScrollOfHealing { + public String getItemName() + { + return "Scroll of Healing"; + } + + public char getDisplayCharacter() + { + return '~'; + } + + public Color getDisplayColor() + { + return Color.YELLOW; + } + + public ItemType getItemType() + { + return ItemType.Scroll; + } + + public void useItem() + { + Main.currentGame.health += 20; + + if (Main.currentGame.health > Main.currentGame.maxHealth) + { + Main.currentGame.health = Main.currentGame.maxHealth; + } + } + }; + + public abstract String getItemName(); + public abstract char getDisplayCharacter(); + public abstract Color getDisplayColor(); + public abstract ItemType getItemType(); + public abstract void useItem(); + + public static Item getWeightedRandomItem() + { + Map typeCounts = new EnumMap(ItemType.class); + for (Item item : Item.values()) + { + if (typeCounts.containsKey(item.getItemType())) + { + typeCounts.put(item.getItemType(), typeCounts.get(item.getItemType())+1); + } else { + typeCounts.put(item.getItemType(), 1); + } + } + + List probabilities = new ArrayList(); + for (Item item : Item.values()) + { + if (probabilities.isEmpty()) + { + probabilities.add(item.getItemType().getRarity() / typeCounts.get(item.getItemType())); + } else { + probabilities.add(item.getItemType().getRarity() / typeCounts.get(item.getItemType()) + probabilities.get(probabilities.size()-1)); + } + } + + Random r = new Random(); + double num = r.nextDouble(); + int i=0; + for (Item item : Item.values()) + { + if (num < probabilities.get(i)) + { + return item; + } + + i++; + } + + return null; + } +} diff --git a/src/com/fourisland/frigidearth/ItemInstance.java b/src/com/fourisland/frigidearth/ItemInstance.java new file mode 100644 index 0000000..e60da0d --- /dev/null +++ b/src/com/fourisland/frigidearth/ItemInstance.java @@ -0,0 +1,16 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +/** + * + * @author hatkirby + */ +public class ItemInstance +{ + public Item item; + public int x; + public int y; +} diff --git a/src/com/fourisland/frigidearth/ItemType.java b/src/com/fourisland/frigidearth/ItemType.java new file mode 100644 index 0000000..d9bf9c5 --- /dev/null +++ b/src/com/fourisland/frigidearth/ItemType.java @@ -0,0 +1,27 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +/** + * + * @author hatkirby + */ +public enum ItemType +{ + Scroll { + public double getRarity() + { + return 1; + } + }, + Special { + public double getRarity() + { + return 0; + } + }; + + public abstract double getRarity(); +} diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index 8419244..cfb7690 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java @@ -41,6 +41,7 @@ public class MapViewGameState implements GameState private String[] messages = new String[MESSAGE_HEIGHT]; private List rooms = new ArrayList(); private List mobs = new ArrayList(); + private List items = new ArrayList(); private int playerx = 4; private int playery = 4; private int viewportx = 0; @@ -448,6 +449,24 @@ public class MapViewGameState implements GameState } } + // and some random items + perf = 30; + for (;;) + { + if (Functions.random(0, 100) < perf) + { + perf /= 2; + + ItemInstance ii = new ItemInstance(); + ii.item = Item.getWeightedRandomItem(); + ii.x = Functions.random(room.getX()+1, room.getX()+room.getWidth()-2); + ii.y = Functions.random(room.getY()+1, room.getY()+room.getHeight()-2); + items.add(ii); + } else { + break; + } + } + return true; } @@ -697,6 +716,15 @@ public class MapViewGameState implements GameState g.drawImage(SystemFont.getCharacter('k', Color.YELLOW), (keyx-viewportx)*TILE_WIDTH, (keyy-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); } + // Render items + for (ItemInstance ii : items) + { + if (gridLighting[ii.x][ii.y]) + { + g.drawImage(SystemFont.getCharacter(ii.item.getDisplayCharacter(), ii.item.getDisplayColor()), (ii.x-viewportx)*TILE_WIDTH, (ii.y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); + } + } + // Render player g.drawImage(SystemFont.getCharacter('@', Color.WHITE), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); @@ -841,6 +869,17 @@ public class MapViewGameState implements GameState } } } + } else { + for (ItemInstance ii : items) + { + if ((ii.x == playerx) && (ii.y == playery)) + { + printMessage("You get a " + ii.item.getItemName().toLowerCase()); + Main.currentGame.inventory.add(ii.item); + items.remove(ii); + break; + } + } } break; -- cgit 1.4.1