Skip to content

Commit 9b36f2b

Browse files
committed
Character is now Thing. Map bounds fixes.
1 parent a941d38 commit 9b36f2b

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

rpg.nim

+41-31
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ type
5050
explored : bool
5151

5252
# Generic object represented by a character on the screen
53-
# A Character can be: player, monster, item, stairs, ...
54-
Character = ref object of RootObj
53+
# A Thing can be: player, monster, item, stairs, ...
54+
Thing = ref object of RootObj
5555
x, y : int
5656
color : TColor
5757
symbol : char
@@ -62,12 +62,12 @@ var
6262
main_console: PConsole
6363
key: TKey
6464
mouse: TMouse
65-
player : Character
65+
player : Thing
6666
map : array[0..MAP_WIDTH, array[0..MAP_HEIGHT, Tile]]
6767
fov_map : PMap
6868
fov_recompute : bool
6969
rooms : seq[Rect] = @[]
70-
objects : seq[Character] = @[]
70+
things : seq[Thing] = @[]
7171
random : PRandom
7272

7373
#########################################################################
@@ -94,11 +94,11 @@ method intersect(self : Rect, other : Rect) : bool =
9494

9595
proc create_room(room : Rect) =
9696
# go through the tiles in the rectangle and make them passable
97-
for i in room.x1 + 1..room.x2:
98-
for j in room.y1 + 1..room.y2:
99-
map[i][j].blocked = false
100-
map[i][j].block_sight = false
101-
map[i][j].explored = false
97+
for x in room.x1 + 1..room.x2:
98+
for y in room.y1 + 1..room.y2:
99+
map[x][y].blocked = false
100+
map[x][y].block_sight = false
101+
map[x][y].explored = false
102102

103103
proc create_h_tunnel(x1 : int, x2 : int, y : int) =
104104
#horizontal tunnel. min() and max() are used in case x1>x2
@@ -115,23 +115,23 @@ proc create_v_tunnel(y1 : int, y2 : int, x : int) =
115115
map[x][y].explored = false
116116

117117
#########################################################################
118-
# Character
118+
# Thing
119119
#########################################################################
120120

121-
method move(self : Character, dx : int, dy : int) =
121+
method move(self : Thing, dx : int, dy : int) =
122122
#move by the given amount, if the destination is not blocked
123123
if not map[self.x + dx][self.y + dy].blocked:
124124
self.x += dx
125125
self.y += dy
126126

127-
method draw(self : Character) =
127+
method draw(self : Thing) =
128128
# draw the character that represents this object at its position
129129
if map_is_in_fov(fov_map, self.x, self.y):
130130
# only draw if it's visible to the player
131131
console_set_default_foreground(main_console, self.color)
132132
console_put_char(main_console, self.x, self.y, self.symbol, BKGND_NONE)
133133

134-
method clear(self : Character) =
134+
method clear(self : Thing) =
135135
console_put_char(main_console, self.x, self.y, ' ', BKGND_NONE)
136136

137137
#########################################################################
@@ -144,8 +144,8 @@ proc render_all() =
144144
fov_recompute = false
145145
map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
146146
# go through all tiles and set their background color
147-
for i in 0..<MAP_WIDTH:
148-
for j in 0..<MAP_HEIGHT:
147+
for i in 0..MAP_WIDTH:
148+
for j in 0..MAP_HEIGHT:
149149
var visible = map_is_in_fov(fov_map, i, j)
150150
var wall = map[i][j].block_sight
151151
if not visible:
@@ -161,43 +161,53 @@ proc render_all() =
161161
console_set_char_background(main_console, i, j, COLOR_LIGHT_GROUND, BKGND_SET)
162162
map[i][j].explored = true
163163

164-
for thing in objects:
164+
for thing in things:
165165
thing.draw()
166166

167167
console_blit(main_console, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, nil, 0, 0, 1.0, 1.0)
168168

169-
proc place_objects(room : Rect) =
169+
proc is_blocked(x : int, y : int) : bool =
170+
result = false
171+
# first test the map tile
172+
if map[x][y].blocked:
173+
result = true
174+
# now check for any blocking things
175+
for thing in things:
176+
if thing.blocks and thing.x == x and thing.y == y:
177+
result = true
178+
179+
proc place_things(room : Rect) =
170180
var num_monsters = random_get_int(random, 0, MAX_ROOM_MONSTERS)
171181

172182
for i in 0..<num_monsters:
173183
# choose random spot for this monster
174184
var x = random_get_int(random, room.x1, room.x2)
175185
var y = random_get_int(random, room.y1, room.y2)
176186

177-
var monster : Character
187+
var monster : Thing
178188

179189
if random_get_int(random, 0, 100) < 80:
180190
# 80 % chance of getting an orc
181-
monster = Character(x : x, y : y, symbol : 'o', color : DESATURATED_GREEN, name : "Orc", blocks : true)
191+
monster = Thing(x : x, y : y, symbol : 'o', color : DESATURATED_GREEN, name : "Orc", blocks : true)
182192
else:
183193
# create a troll
184-
monster = Character(x : x, y : y, symbol : 'T', color : DARKER_GREEN, name : "Troll", blocks : true)
194+
monster = Thing(x : x, y : y, symbol : 'T', color : DARKER_GREEN, name : "Troll", blocks : true)
185195

186-
objects.add(monster)
196+
things.add(monster)
187197

188198
proc make_map =
189199
# fill map with "blocked" tiles
190-
for i in 0..<MAP_WIDTH:
191-
for j in 0..<MAP_HEIGHT:
200+
for i in 0..MAP_WIDTH:
201+
for j in 0..MAP_HEIGHT:
192202
map[i][j] = Tile(blocked : true, block_sight: true)
193203

194204
var num_rooms : int = 0
195205

196206
for r in 0..<MAX_ROOMS:
197207
var w = random_get_int(random, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
198208
var h = random_get_int(random, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
199-
var x = random_get_int(random, 0, MAP_WIDTH - w - 2)
200-
var y = random_get_int(random, 0, MAP_HEIGHT - h - 2)
209+
var x = random_get_int(random, 0, MAP_WIDTH - w - 1)
210+
var y = random_get_int(random, 0, MAP_HEIGHT - h - 1)
201211

202212
var new_room = newRect(x, y, w, h)
203213

@@ -233,8 +243,8 @@ proc make_map =
233243
create_v_tunnel(prev_center.y, center_coords.y, center_coords.x)
234244
create_h_tunnel(prev_center.x, center_coords.x, prev_center.y)
235245

236-
# add some objects to the room
237-
place_objects(new_room)
246+
# add some things to the room
247+
place_things(new_room)
238248
# finally, append the new room to the list
239249
rooms.add(new_room)
240250
num_rooms += 1
@@ -271,16 +281,16 @@ proc init*(title : string) : void =
271281

272282
random = random_new()
273283

274-
player = Character(x : 0, y : 0, color : RED, symbol : '@', name : "Hero", blocks : true)
284+
player = Thing(x : 0, y : 0, color : RED, symbol : '@', name : "Hero", blocks : true)
275285

276-
objects.add(player)
286+
things.add(player)
277287

278288
make_map()
279289

280290
# create FOV map
281291
fov_map = map_new(MAP_WIDTH, MAP_HEIGHT)
282-
for y in 0..<MAP_HEIGHT:
283-
for x in 0..<MAP_WIDTH:
292+
for y in 0..MAP_HEIGHT:
293+
for x in 0..MAP_WIDTH:
284294
map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)
285295

286296
fov_recompute = true

0 commit comments

Comments
 (0)