49
49
block_sight : bool
50
50
explored : bool
51
51
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
+
52
59
# Generic object represented by a character on the screen
53
60
# A Thing can be: player, monster, item, stairs, ...
54
61
Thing = ref object of RootObj
57
64
symbol : char
58
65
name : string
59
66
blocks : bool
67
+ fighter : Fighter
68
+ ai : AI
69
+
70
+ BasicMonster = ref object of AI
60
71
61
72
PlayState = enum
62
73
PLAYING
@@ -124,6 +135,21 @@ proc create_v_tunnel(y1 : int, y2 : int, x : int) =
124
135
# Thing
125
136
# ########################################################################
126
137
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
+
127
153
method move (self : Thing , dx : int , dy : int ) =
128
154
# move by the given amount, if the destination is not blocked
129
155
if not map[self.x + dx][self.y + dy].blocked:
@@ -140,6 +166,13 @@ method draw(self : Thing) =
140
166
method clear (self : Thing ) =
141
167
console_put_char (main_console, self.x, self.y, ' ' , BKGND_NONE )
142
168
169
+ # ########################################################################
170
+ # AI
171
+ # ########################################################################
172
+ method take_turn (self : BasicMonster ) =
173
+ echo (" The " , Thing (self.owner).name, " growls!" )
174
+
175
+
143
176
# ########################################################################
144
177
# Internal procs
145
178
# ########################################################################
@@ -202,10 +235,14 @@ proc place_things(room : Rect) =
202
235
203
236
if random_get_int (random, 0 , 100 ) < 80 :
204
237
# 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)
206
241
else :
207
242
# 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)
209
246
210
247
things.add (monster)
211
248
@@ -315,7 +352,9 @@ proc init*(title : string) : void =
315
352
316
353
random = random_new ()
317
354
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)
319
358
320
359
things.add (player)
321
360
0 commit comments