-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.d
302 lines (221 loc) · 6.63 KB
/
game.d
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// D imports
import std.stdio : writeln;
import std.random : uniform;
import std.conv;
// allegro imports
import allegro5.allegro;
import allegro5.allegro_ttf;
import allegro5.allegro_font;
// app imports
import globals;
import main;
import world;
import render;
import input;
import dungeon;
import entity : Entity;
import message : addMessage, MessageColor;
/*
* present the main menu
*
*/
void mainMenu()
{
//init the keyboard AA
//will need to use in menu so declare here
bool[string] keyList;
keyList["esc"] = false;
keyList["up"] = false;
keyList["down"] = false;
keyList["left"] = false;
keyList["right"] = false;
keyList["action"] = false;
debug writeln("... main menu");
// stay in menu until new game, load, quit
// for now just play new
newGame(keyList);
}
/**
* initialise and play a new game
*
**/
void newGame(ref bool[string] keyList)
{
bool flagPlaying = true;
// init game
if(!loadResources())
{
writeln("Failed to load resources");
return;
}
// init the game timer
import std.datetime : StopWatch;
StopWatch gameTimer;
// init the mouse
Mouse mouse;
// init map
generateMap();
//TileRegion region = TileRegion([null,null],null,SplitType.vertical, Rect(0,100,0,100));
//worldMap = generateCADungeonLevel(region, copy2DArray(worldMap));
//worldMap = generateBSPDungeonLevel(region, copy2DArray(worldMap));
//writeln(tileArrayToString(worldMap));
// init player
Entity player;
player = Entity(Creatures.human, gameTimer);
// init mobs
foreach(i; 0..numMobs)
{
mobs[i] = Entity(Creatures.butterfly, gameTimer);
}
addMessage(MessageColor.green, "Renascent...");
addMessage(MessageColor.white, "Arrows: Move");
addMessage(MessageColor.white, "NumPad Arrows: Face");
addMessage(MessageColor.white, "NumPad 0: Action");
addMessage(MessageColor.white, "When you have sufficient soul points you can transform yourself into any previous form at will");
addMessage(MessageColor.green, "If you could find a compass, you could work out your position... handy for quests");
addMessage(MessageColor.red, "LOOK OUT out for the giant red butterfly!");
addMessage(MessageColor.white, "This is a very long message, designed solely for testing purposes so lets
hope it works properly, otherwise I'll have to re-write the function. Dang!");
// MAIN LOOP
while(flagPlaying)
{
// update the assoc array with key states
player = updateInput(keyList, mouse, player);
if(keyList["esc"]) flagPlaying = false;
if(keyList["action"])
{
processAction(player);
keyList["action"] = false;
}
if(keyList["up"])
{
if(canGoNorth(player))
{
player.locY = coordNorth(player.locY);
}
keyList["up"] = false;
}
if(keyList["down"])
{
if(canGoSouth(player))
{
player.locY = coordSouth(player.locY);
}
keyList["down"] = false;
}
if(keyList["left"])
{
if(canGoWest(player))
{
player.locX = coordWest(player.locX);
}
keyList["left"] = false;
}
if(keyList["right"])
{
if(canGoEast(player))
{
player.locX = coordEast(player.locX);
}
keyList["right"] = false;
}
// clear screen and render all the bits
al_clear_to_color(al_map_rgb(0, 0, 0));
renderMap(player);
renderPlayer(player);
foreach(m; mobs)
{
renderMobs(m, player);
}
renderHUD(mouse, player);
renderMessages();
// show
al_flip_display();
}
}
// these functions return the co-ordinate to the NES or W, with wrap if necessary
int coordWest(int cX)
{
return (cX == 0) ? MapSize - 1 : cX - 1;
}
int coordEast(int cX)
{
return (cX == MapSize - 1) ? 0 : cX + 1;
}
int coordNorth(int cY)
{
return (cY == 0) ? MapSize - 1 : cY - 1;
}
int coordSouth(int cY)
{
return (cY == MapSize - 1) ? 0 : cY + 1;
}
// these function test if cell in given direction is passable
bool canGoNorth(Entity e)
{
int testY = coordNorth(e.locY);
return (worldMap[testY][e.locX].canPass) ? true : false;
}
bool canGoSouth(Entity e)
{
int testY = coordSouth(e.locY);
return (worldMap[testY][e.locX].canPass) ? true : false;
}
bool canGoWest(Entity e)
{
int testX = coordWest(e.locX);
return (worldMap[e.locY][testX].canPass) ? true : false;
}
bool canGoEast(Entity e)
{
int testX = coordEast(e.locX);
return (worldMap[e.locY][testX].canPass) ? true : false;
}
bool loadResources()
{
debug writeln("... loading resources");
imgTileSet = al_load_bitmap("./resources/tileset.png");
if(imgTileSet == null) return false;
imgPlayer = al_load_bitmap("./resources/player.png");
if(imgPlayer == null) return false;
imgMob = al_load_bitmap("./resources/mob.png");
if(imgMob == null) return false;
messageFont = al_load_bitmap_font("./resources/a4_font.tga");
if(messageFont == null) return false;
return true;
}
void processAction(Entity e)
{
// get cell on which action will be performed
int targetX, targetY;
switch(e.facing)
{
case Direction.north :
targetX = e.locX;
targetY = coordNorth(e.locY);
break;
case Direction.south :
targetX = e.locX;
targetY = coordSouth(e.locY);
break;
case Direction.west :
targetY = e.locY;
targetX = coordWest(e.locX);
break;
case Direction.east :
targetY = e.locY;
targetX = coordEast(e.locX);
break;
default:
}
// process the action, depends upon type of tile in target cell
switch(worldMap[targetY][targetX].tileType)
{
case TileType.water :
addMessage(MessageColor.green, "Gulp, slurp... you feel refreshed");
break;
default:
addMessage(MessageColor.red, "wtf?");
break;
}
}