Skip to content

Commit 900c8f9

Browse files
committed
Crappy composition
1 parent 069b368 commit 900c8f9

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

nimrpg.nim

-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ import rpg
1616
init("NimRPG")
1717

1818
main_loop()
19-

rpg.nim

+42-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ type
4949
block_sight : bool
5050
explored : bool
5151

52+
Fighter = ref object of RootObj
53+
max_hp, hp, defense, power : int
54+
owner : ref RootObj
55+
56+
AI = ref object of RootObj
57+
owner : ref RootObj
58+
5259
# Generic object represented by a character on the screen
5360
# A Thing can be: player, monster, item, stairs, ...
5461
Thing = ref object of RootObj
@@ -57,6 +64,10 @@ type
5764
symbol : char
5865
name : string
5966
blocks : bool
67+
fighter : Fighter
68+
ai : AI
69+
70+
BasicMonster = ref object of AI
6071

6172
PlayState = enum
6273
PLAYING
@@ -124,6 +135,21 @@ proc create_v_tunnel(y1 : int, y2 : int, x : int) =
124135
# Thing
125136
#########################################################################
126137

138+
proc newThing(x : int, y : int, symbol : char, name : string, color : TColor, blocks : bool, fighter : Fighter = nil, ai : AI = nil) : Thing =
139+
result = new Thing
140+
result.x = x
141+
result.y = y
142+
result.symbol = symbol
143+
result.name = name
144+
result.color = color
145+
result.blocks = blocks
146+
result.fighter = fighter
147+
if fighter != nil:
148+
fighter.owner = result
149+
result.ai = ai
150+
if ai != nil:
151+
ai.owner = result
152+
127153
method move(self : Thing, dx : int, dy : int) =
128154
#move by the given amount, if the destination is not blocked
129155
if not map[self.x + dx][self.y + dy].blocked:
@@ -140,6 +166,13 @@ method draw(self : Thing) =
140166
method clear(self : Thing) =
141167
console_put_char(main_console, self.x, self.y, ' ', BKGND_NONE)
142168

169+
#########################################################################
170+
# AI
171+
#########################################################################
172+
method take_turn(self : BasicMonster) =
173+
echo("The ", Thing(self.owner).name, " growls!")
174+
175+
143176
#########################################################################
144177
# Internal procs
145178
#########################################################################
@@ -202,10 +235,14 @@ proc place_things(room : Rect) =
202235

203236
if random_get_int(random, 0, 100) < 80:
204237
# 80 % chance of getting an orc
205-
monster = Thing(x : x, y : y, symbol : 'o', color : DESATURATED_GREEN, name : "Orc", blocks : true)
238+
var fighter_component = Fighter(hp : 10, defense : 0, power : 3)
239+
var ai_component = BasicMonster()
240+
monster = Thing(x : x, y : y, symbol : 'o', color : DESATURATED_GREEN, name : "Orc", blocks : true, fighter : fighter_component, ai : ai_component)
206241
else:
207242
# create a troll
208-
monster = Thing(x : x, y : y, symbol : 'T', color : DARKER_GREEN, name : "Troll", blocks : true)
243+
var fighter_component = Fighter(hp : 16, defense : 1, power : 4)
244+
var ai_component = BasicMonster()
245+
monster = Thing(x : x, y : y, symbol : 'T', color : DARKER_GREEN, name : "Troll", blocks : true, fighter : fighter_component, ai : ai_component)
209246

210247
things.add(monster)
211248

@@ -315,7 +352,9 @@ proc init*(title : string) : void =
315352

316353
random = random_new()
317354

318-
player = Thing(x : 0, y : 0, color : RED, symbol : '@', name : "Hero", blocks : true)
355+
var fighter_component = Fighter(hp : 30, defense : 2, power : 5)
356+
var ai_component = BasicMonster()
357+
player = Thing(x : 0, y : 0, color : RED, symbol : '@', name : "Hero", blocks : true, fighter : fighter_component, ai : ai_component)
319358

320359
things.add(player)
321360

0 commit comments

Comments
 (0)