58
58
name : string
59
59
blocks : bool
60
60
61
+ PlayState = enum
62
+ PLAYING
63
+
64
+ PlayerAction = enum
65
+ NONE ,
66
+ EXIT ,
67
+ DIDNT_TAKE_TURN
68
+
61
69
var
62
70
main_console: PConsole
63
71
key: TKey
69
77
rooms : seq [Rect ] = @ []
70
78
things : seq [Thing ] = @ []
71
79
random : PRandom
80
+ game_state : PlayState
81
+ player_action : PlayerAction
72
82
73
83
# ########################################################################
74
84
# Rect
@@ -179,7 +189,7 @@ proc is_blocked(x : int, y : int) : bool =
179
189
proc place_things (room : Rect ) =
180
190
var num_monsters = random_get_int (random, 0 , MAX_ROOM_MONSTERS )
181
191
182
- for i in 0 ..< num_monsters:
192
+ for i in 0 .. num_monsters:
183
193
# choose random spot for this monster
184
194
var x = random_get_int (random, room.x1, room.x2)
185
195
var y = random_get_int (random, room.y1, room.y2)
@@ -249,26 +259,31 @@ proc make_map =
249
259
rooms.add (new_room)
250
260
num_rooms += 1
251
261
252
- proc handle_input () : bool =
262
+ proc handle_input () : PlayerAction =
253
263
discard sys_wait_for_event (EVENT_KEY_PRESS or EVENT_MOUSE , addr (key), addr (mouse), true )
254
- result = true
264
+ result = NONE
255
265
case key.vk
256
266
of K_ESCAPE :
257
- result = false
258
- of K_UP :
259
- player.move (0 , - 1 )
260
- fov_recompute = true
261
- of K_DOWN :
262
- player.move (0 , 1 )
263
- fov_recompute = true
264
- of K_LEFT :
265
- player.move (- 1 , 0 )
266
- fov_recompute = true
267
- of K_RIGHT :
268
- player.move (1 , 0 )
269
- fov_recompute = true
267
+ result = EXIT
270
268
else :
271
- result = true
269
+ result = NONE
270
+
271
+ if game_state == PLAYING and result == NONE :
272
+ case key.vk
273
+ of K_UP :
274
+ player.move (0 , - 1 )
275
+ fov_recompute = true
276
+ of K_DOWN :
277
+ player.move (0 , 1 )
278
+ fov_recompute = true
279
+ of K_LEFT :
280
+ player.move (- 1 , 0 )
281
+ fov_recompute = true
282
+ of K_RIGHT :
283
+ player.move (1 , 0 )
284
+ fov_recompute = true
285
+ else :
286
+ result = DIDNT_TAKE_TURN
272
287
273
288
# ########################################################################
274
289
# Exported procs
@@ -294,6 +309,8 @@ proc init*(title : string) : void =
294
309
map_set_properties (fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)
295
310
296
311
fov_recompute = true
312
+ player_action = NONE
313
+ game_state = PLAYING
297
314
298
315
console_clear (main_console)
299
316
# discard console_print_rect_ex(main_console, SCREEN_WIDTH_2, 3, SCREEN_WIDTH, 0, BKGND_NONE, CENTER, message)
@@ -304,9 +321,19 @@ proc main_loop*() : void =
304
321
render_all ()
305
322
306
323
console_flush ()
307
- player.clear ()
308
324
309
- if not handle_input ():
325
+ for thing in things:
326
+ thing.clear ()
327
+
328
+ player_action = handle_input ()
329
+
330
+ if player_action == EXIT :
310
331
break ;
311
332
333
+ # let monsters take their turn
334
+ if game_state == PLAYING and player_action == DIDNT_TAKE_TURN :
335
+ for thing in things:
336
+ if thing != player:
337
+ echo (" The " , thing.name, " growls!" )
338
+
312
339
random_delete (random)
0 commit comments