Skip to content

Commit 56e2a73

Browse files
committed
Game state and player actions
1 parent 9b36f2b commit 56e2a73

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/nimrpg
33
/view
44
/d20
5+
rpg

rpg.nim

+46-19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ type
5858
name : string
5959
blocks : bool
6060

61+
PlayState = enum
62+
PLAYING
63+
64+
PlayerAction = enum
65+
NONE,
66+
EXIT,
67+
DIDNT_TAKE_TURN
68+
6169
var
6270
main_console: PConsole
6371
key: TKey
@@ -69,6 +77,8 @@ var
6977
rooms : seq[Rect] = @[]
7078
things : seq[Thing] = @[]
7179
random : PRandom
80+
game_state : PlayState
81+
player_action : PlayerAction
7282

7383
#########################################################################
7484
# Rect
@@ -179,7 +189,7 @@ proc is_blocked(x : int, y : int) : bool =
179189
proc place_things(room : Rect) =
180190
var num_monsters = random_get_int(random, 0, MAX_ROOM_MONSTERS)
181191

182-
for i in 0..<num_monsters:
192+
for i in 0..num_monsters:
183193
# choose random spot for this monster
184194
var x = random_get_int(random, room.x1, room.x2)
185195
var y = random_get_int(random, room.y1, room.y2)
@@ -249,26 +259,31 @@ proc make_map =
249259
rooms.add(new_room)
250260
num_rooms += 1
251261

252-
proc handle_input() : bool =
262+
proc handle_input() : PlayerAction =
253263
discard sys_wait_for_event(EVENT_KEY_PRESS or EVENT_MOUSE, addr(key), addr(mouse), true)
254-
result = true
264+
result = NONE
255265
case key.vk
256266
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
270268
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
272287

273288
#########################################################################
274289
# Exported procs
@@ -294,6 +309,8 @@ proc init*(title : string) : void =
294309
map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)
295310

296311
fov_recompute = true
312+
player_action = NONE
313+
game_state = PLAYING
297314

298315
console_clear(main_console)
299316
#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 =
304321
render_all()
305322

306323
console_flush()
307-
player.clear()
308324

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:
310331
break;
311332

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+
312339
random_delete(random)

0 commit comments

Comments
 (0)