-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMap.java
224 lines (193 loc) · 11.1 KB
/
GameMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.util.HashMap;
import java.awt.Point;
import java.util.Random;
/**
* Responsible for coordinating players/locations
* around a 'map' object.
*
* @author Jamie Dixon
* @version v0.03
*/
public class GameMap
{
// instance variables
private HashMap<Point, Location> coords;
/**
* Constructor for objects of class GameMap
*/
public GameMap()
{
// initialise instance variables
coords = new HashMap<Point, Location>();
setupLocations();
}
/**
* Create all the rooms and link their exits together.
*/
private void setupLocations()
{
// create new locations for the dungeons and beasts game
// east hall
createPointOnMap(1,-2,"in the great hall", LocationType.HALL);
createPointOnMap(1,-1,"in the great hall", LocationType.HALL);
createPointOnMap(1,0,"in the great hall", LocationType.HALL);
createPointOnMap(1,1,"in the great hall", LocationType.HALL);
createPointOnMap(1,2,"in the great hall", LocationType.HALL);
createPointOnMap(1,3,"in the great hall", LocationType.HALL);
createPointOnMap(1,4,"in the great hall", LocationType.HALL);
createPointOnMap(1,5,"in the great hall", LocationType.HALL);
// west hall
createPointOnMap(-1,5,"in the great hall", LocationType.HALL);
createPointOnMap(-1,4,"in the great hall", LocationType.HALL);
createPointOnMap(-1,3,"in the great hall", LocationType.HALL);
createPointOnMap(-1,2,"in the great hall", LocationType.HALL);
createPointOnMap(-1,1,"in the great hall", LocationType.HALL);
createPointOnMap(-1,0,"in the great hall", LocationType.HALL);
createPointOnMap(-1,-1,"in the great hall", LocationType.HALL);
createPointOnMap(-1,-2,"in the great hall", LocationType.HALL);
createPointOnMap(0,2, "rumaging through bed chambers", LocationType.BEDROOM);
createPointOnMap(2,1, "rumaging through bed chambers", LocationType.BEDROOM);
createPointOnMap(0,5, "rumaging through bed chambers", LocationType.BEDROOM);
createPointOnMap(-2,5, "rumaging through bed chambers", LocationType.BEDROOM);
createPointOnMap(-3,5, "walking into a bathroom", LocationType.ROOM);
createPointOnMap(-2,2, "walking into a bathroom", LocationType.ROOM);
createPointOnMap(2,3, "standing in the kitchen", LocationType.ROOM);
createPointOnMap(3,3, "browsing the pantry", LocationType.ROOM);
createPointOnMap(0,0,"in a store room", LocationType.STORE);
createPointOnMap(0,-2,"standing in the grand entrance", LocationType.ENTRANCE);
createPointOnMap(0,-3,"standing on the entrance bridge", LocationType.BRIDGE);
createPointOnMap(0,-4,"standing at bridge gates", LocationType.BRIDGEGATE);
createPointOnMap(1,-4,"rummaging through forest", LocationType.OUTSIDE);
createPointOnMap(2,-4,"rummaging through forest", LocationType.OUTSIDE);
createPointOnMap(-1,-4,"rummaging through forest", LocationType.OUTSIDE);
createPointOnMap(-2,-4,"rummaging through forest", LocationType.OUTSIDE);
// debug inventory!!!
//getLocationByPoint(new Point(2, -4)).addItem("ether", new Item("item", 0, 30));;
// west tunnel & guard room
createPointOnMap(-2,-1,"standing in a guard room", LocationType.GUARDED);
createPointOnMap(-3,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-4,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,0,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,2,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,3,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,4,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,5,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-5,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-4,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-6,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(-4,7,"in a dungeon - there is no way out", LocationType.DUNGEON);
createPointOnMap(-6,7,"in a dungeon - there is no way out", LocationType.DUNGEON);
// east tunnel & guard room
createPointOnMap(2,-1,"standing in a guard room", LocationType.GUARDED);
createPointOnMap(3,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(4,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,-1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,0,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,1,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,2,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,3,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,4,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,5,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(5,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(6,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(4,6,"creeping up a dark and smelly tunnel", LocationType.TUNNEL);
createPointOnMap(4,7,"in a dungeon - there is no way out", LocationType.DUNGEON);
createPointOnMap(6,7,"in a dungeon - there is no way out", LocationType.DUNGEON);
createPointOnMap(4,-2, "stuck in a trap", LocationType.TRAP);
createPointOnMap(-2,6, "stuck in a trap", LocationType.TRAP);
createPointOnMap(-6,-1, "stuck in a trap", LocationType.TRAP);
autoSetExits();
}
/**
* Automatically set room exits - this method will iteraste through the entire
* hashmap to check the position of a location on the map, retrieve its neighbouring
* room if exists and setting it as an exit in the correct direction.
*
* @return boolean - true if the method completes successfully.
*/
private boolean autoSetExits()
{
// Iterate through the coordinates HashMap to check/add exits
for (Point key : coords.keySet()) {
if(getLocationByPoint(key).getLocationType() != LocationType.TRAP){
// if the HashMap contains a key with a point with an x value of this points x value + 1
if(coords.containsKey(new Point((int)key.getX()+1,(int)key.getY()))){
// there is a location to the east (set an exit?)
Point neighbourPoint = new Point((int)key.getX()+1,(int)key.getY());
// set exits to the east
getLocationByPoint(key).setExit("east", getLocationByPoint(neighbourPoint));
}
if(coords.containsKey(new Point((int)key.getX()-1,(int)key.getY()))){
Point neighbourPoint = new Point((int)key.getX()-1,(int)key.getY());
// there is a location to the west (set an exit?)
getLocationByPoint(key).setExit("west", getLocationByPoint(neighbourPoint));
}
if(coords.containsKey(new Point((int)key.getX(),(int)key.getY()+1))){
Point neighbourPoint = new Point((int)key.getX(),(int)key.getY()+1);
// there is a location to the north (set an exit?)
getLocationByPoint(key).setExit("north", getLocationByPoint(neighbourPoint));
}
if(coords.containsKey(new Point((int)key.getX(),(int)key.getY()-1))){
Point neighbourPoint = new Point((int)key.getX(),(int)key.getY()-1);
// there is a location to the west (set an exit?)
getLocationByPoint(key).setExit("south", getLocationByPoint(neighbourPoint));
}
}
}
return true;
}
/**
* createPointOnMap is responsible for creating map tiles first
* it will check if a item exists in the chosen position and if
* it does not exist in the HashMap it will create a new Point object
* and a new Location object storing them in the map coordinates HashMap
* respectively.
*
* @params int xPos, int yPos, String description
* @returns boolean true if the new location was added to map, false otherwise.
*/
private boolean createPointOnMap(int xPos, int yPos, String description, LocationType type)
{
Point thisPoint = new Point(xPos, yPos);
if(coords.containsKey(thisPoint)){
return false;
}else{
Location newLocation = new Location(description, type, thisPoint);
Random rand = new Random();
// Dont add characters or items in certain locations
if(type != LocationType.OUTSIDE && type != LocationType.TRAP && type != LocationType.DUNGEON){
// Give a 50% chance of this location gaining an item
float chance = rand.nextFloat();
if (chance <= 0.10f) {
newLocation.addItem("ether", new Item("Health boost.", 10, 10));
} else if (chance <= 0.20){
newLocation.addItem("coin", new Item("A gold coin.", 0, 50));
} else if (chance <= 0.30f){
newLocation.addItem("key", new Item("A key.", 0, 50));
}
chance = rand.nextFloat();
if(chance <= 0.20f) {
newLocation.addNPC("Dragon", new Character(CharacterType.DRAGON));
}
if(chance <= 0.40f) {
newLocation.addNPC("Witch", new Character(CharacterType.WITCH));
}
}
coords.put(new Point(xPos,yPos), newLocation);
return true;
}
}
/**
* getLocationByPoint - this method will return a reference to an instance
* of the Location object if it exists in the HashMap with the key point.
*
* @params Point point - a point object with x,y coords relating the map position
* @return Location - a location object if found with key point. Null otherwise.
*/
public Location getLocationByPoint(Point point)
{
return coords.get(point);
}
}