about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/com/fourisland/frigidearth/Game.java4
-rw-r--r--src/com/fourisland/frigidearth/Item.java97
-rw-r--r--src/com/fourisland/frigidearth/ItemInstance.java16
-rw-r--r--src/com/fourisland/frigidearth/ItemType.java27
-rw-r--r--src/com/fourisland/frigidearth/MapViewGameState.java39
5 files changed, 183 insertions, 0 deletions
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 @@
4 */ 4 */
5package com.fourisland.frigidearth; 5package com.fourisland.frigidearth;
6 6
7import java.util.ArrayList;
8import java.util.List;
9
7/** 10/**
8 * 11 *
9 * @author hatkirby 12 * @author hatkirby
@@ -15,4 +18,5 @@ public class Game
15 public int defense = 0; 18 public int defense = 0;
16 public int level = 1; 19 public int level = 1;
17 public int experience = 0; 20 public int experience = 0;
21 public List<Item> inventory = new ArrayList<Item>();
18} 22}
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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7import java.awt.Color;
8import java.util.ArrayList;
9import java.util.EnumMap;
10import java.util.List;
11import java.util.Map;
12import java.util.Random;
13
14/**
15 *
16 * @author hatkirby
17 */
18public enum Item
19{
20 ScrollOfHealing {
21 public String getItemName()
22 {
23 return "Scroll of Healing";
24 }
25
26 public char getDisplayCharacter()
27 {
28 return '~';
29 }
30
31 public Color getDisplayColor()
32 {
33 return Color.YELLOW;
34 }
35
36 public ItemType getItemType()
37 {
38 return ItemType.Scroll;
39 }
40
41 public void useItem()
42 {
43 Main.currentGame.health += 20;
44
45 if (Main.currentGame.health > Main.currentGame.maxHealth)
46 {
47 Main.currentGame.health = Main.currentGame.maxHealth;
48 }
49 }
50 };
51
52 public abstract String getItemName();
53 public abstract char getDisplayCharacter();
54 public abstract Color getDisplayColor();
55 public abstract ItemType getItemType();
56 public abstract void useItem();
57
58 public static Item getWeightedRandomItem()
59 {
60 Map<ItemType,Integer> typeCounts = new EnumMap<ItemType,Integer>(ItemType.class);
61 for (Item item : Item.values())
62 {
63 if (typeCounts.containsKey(item.getItemType()))
64 {
65 typeCounts.put(item.getItemType(), typeCounts.get(item.getItemType())+1);
66 } else {
67 typeCounts.put(item.getItemType(), 1);
68 }
69 }
70
71 List<Double> probabilities = new ArrayList<Double>();
72 for (Item item : Item.values())
73 {
74 if (probabilities.isEmpty())
75 {
76 probabilities.add(item.getItemType().getRarity() / typeCounts.get(item.getItemType()));
77 } else {
78 probabilities.add(item.getItemType().getRarity() / typeCounts.get(item.getItemType()) + probabilities.get(probabilities.size()-1));
79 }
80 }
81
82 Random r = new Random();
83 double num = r.nextDouble();
84 int i=0;
85 for (Item item : Item.values())
86 {
87 if (num < probabilities.get(i))
88 {
89 return item;
90 }
91
92 i++;
93 }
94
95 return null;
96 }
97}
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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7/**
8 *
9 * @author hatkirby
10 */
11public class ItemInstance
12{
13 public Item item;
14 public int x;
15 public int y;
16}
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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7/**
8 *
9 * @author hatkirby
10 */
11public enum ItemType
12{
13 Scroll {
14 public double getRarity()
15 {
16 return 1;
17 }
18 },
19 Special {
20 public double getRarity()
21 {
22 return 0;
23 }
24 };
25
26 public abstract double getRarity();
27}
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
41 private String[] messages = new String[MESSAGE_HEIGHT]; 41 private String[] messages = new String[MESSAGE_HEIGHT];
42 private List<Room> rooms = new ArrayList<Room>(); 42 private List<Room> rooms = new ArrayList<Room>();
43 private List<Mob> mobs = new ArrayList<Mob>(); 43 private List<Mob> mobs = new ArrayList<Mob>();
44 private List<ItemInstance> items = new ArrayList<ItemInstance>();
44 private int playerx = 4; 45 private int playerx = 4;
45 private int playery = 4; 46 private int playery = 4;
46 private int viewportx = 0; 47 private int viewportx = 0;
@@ -448,6 +449,24 @@ public class MapViewGameState implements GameState
448 } 449 }
449 } 450 }
450 451
452 // and some random items
453 perf = 30;
454 for (;;)
455 {
456 if (Functions.random(0, 100) < perf)
457 {
458 perf /= 2;
459
460 ItemInstance ii = new ItemInstance();
461 ii.item = Item.getWeightedRandomItem();
462 ii.x = Functions.random(room.getX()+1, room.getX()+room.getWidth()-2);
463 ii.y = Functions.random(room.getY()+1, room.getY()+room.getHeight()-2);
464 items.add(ii);
465 } else {
466 break;
467 }
468 }
469
451 return true; 470 return true;
452 } 471 }
453 472
@@ -697,6 +716,15 @@ public class MapViewGameState implements GameState
697 g.drawImage(SystemFont.getCharacter('k', Color.YELLOW), (keyx-viewportx)*TILE_WIDTH, (keyy-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); 716 g.drawImage(SystemFont.getCharacter('k', Color.YELLOW), (keyx-viewportx)*TILE_WIDTH, (keyy-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
698 } 717 }
699 718
719 // Render items
720 for (ItemInstance ii : items)
721 {
722 if (gridLighting[ii.x][ii.y])
723 {
724 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);
725 }
726 }
727
700 // Render player 728 // Render player
701 g.drawImage(SystemFont.getCharacter('@', Color.WHITE), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); 729 g.drawImage(SystemFont.getCharacter('@', Color.WHITE), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
702 730
@@ -841,6 +869,17 @@ public class MapViewGameState implements GameState
841 } 869 }
842 } 870 }
843 } 871 }
872 } else {
873 for (ItemInstance ii : items)
874 {
875 if ((ii.x == playerx) && (ii.y == playery))
876 {
877 printMessage("You get a " + ii.item.getItemName().toLowerCase());
878 Main.currentGame.inventory.add(ii.item);
879 items.remove(ii);
880 break;
881 }
882 }
844 } 883 }
845 884
846 break; 885 break;