This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NPC.py
429 lines (342 loc) · 12.8 KB
/
NPC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!usr/bin/python
# -*- coding: utf-8 -*-
import pygame as pg
import Spells
from ResourceHelpers import SpritesHelper
from Enums import BattleEnum as Battle
from Events import BattleEvent
import random as rand
from Items import *
def action(func): # Decorator for NPC actions, posts NextTurn event so NPC can't take several actions at once
def wrapped(*args, **kwargs):
status = func(*args, **kwargs)
event = pg.event.Event(BattleEvent, {'sub': Battle.NextTurn, 'status': status})
pg.event.post(event)
return wrapped
class MapNPC:
"""
Defines NPC for map state
"""
def __init__(self, x, y, party, bg, id):
"""
:param party: list of npc party members (turned to objects by eval function)
:param bg: name of battle background file
:param id: unique party id (for quests etc.)
"""
self.party = party
self.bg = bg
self.id = id
self.x = x
self.y = y
self.create_map_image()
def create_map_image(self):
member = eval(self.party[0])
self.image = member.load_map_sprite(member)
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def on_load(self):
self.create_map_image()
class MapTrader:
"""
Defines trader for map state
"""
def __init__(self,x, y):
self.x = x
self.y = y
self.load_sprite()
def load_sprite(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('trader', 'map')), (30, 38))
self.image.set_colorkey(pg.Color("#7bd5fe"))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def on_load(self):
self.load_sprite()
class MapWizard:
"""
Defines wizard for map state
"""
def __init__(self, x, y):
self.x = x
self.y = y
self.load_sprite()
def load_sprite(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('wizard', 'map')), (30, 38))
self.image.set_colorkey(pg.Color("#7bd5fe"))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def on_load(self):
self.load_sprite()
class BaseNPC(pg.sprite.Sprite):
"""
Defines basic NPC class for battle state
"""
def __init__(self, res_name, hp, mp, dmg, exp, gold, loot, spells):
super().__init__()
self.spells = spells # List of spell objects which NPC can cast
self._res_name = res_name
self.HP = hp
self.MAX_HP = hp
self.MP = mp
self.MAX_MP = mp
self.DMG = dmg
self._loot = loot # list of Item,Drop Rate dicts
self.load_sprites()
self.EXP = exp # Experience points which every player character gets for defeating this NPC
self.gold = gold # Gold for defeating this NPC
def decide(self, player_party, npc_party):
"""
Method which is called when npc must decide what to do on it's turn
:param player_party: player party object
:param npc_party: list of npc party members
"""
pass
def load_sprites(self):
"""
Loads all sprite images
"""
pass
def load_map_sprite(self):
"""
Load map sprite and return it
:return: pygame image
"""
pass
def get_attributes(self):
attrs = {'hp': self.HP, 'max_hp': self.MAX_HP, 'mp': self.MP, 'max_mp': self.MAX_MP}
return attrs
def apply_damage(self, dmg):
if dmg >= self.HP:
self.HP = 0
args_dict = {'sub': Battle.CharacterKO, 'pc': self}
event = pg.event.Event(BattleEvent, args_dict)
pg.event.post(event)
else:
self.HP -= dmg
@action
def attack(self, player):
damaged = player.apply_damage(self.DMG)
if damaged is False:
status = "{} dodged {}'s damage".format(player.name, self.name)
else:
status = '{} dealt {} damage to {}'.format(self.name, self.DMG, player.name)
return status
@action
def cast_spell(self, spell, target):
"""
cast selected spell on target
:param spell: spell object
:param target: BaseMember or BaseNPC object
:return: status string - information about spell cast
"""
spell.apply(target)
self.MP -= spell.mp
status = '{} casted {} on {}'.format(self.name, spell, target.name)
return status
def get_loot(self):
"""
Generate loot for this NPC
:return: list of Item objects
"""
loot = []
chance = rand.random()
for i in self._loot:
if chance >= i['rate']:
loot.append(i['item']())
return loot
def post_status(self, status):
"""
Called after making decision to set status explaining npc's action
:param status: string - action description
"""
args_dict = {'status': status, 'sub': Battle.StatusUpdate}
event = pg.event.Event(BattleEvent, args_dict)
pg.event.post(event)
class Test(BaseNPC):
"""
Test NPC
"""
Counter = 0
def __init__(self):
loot = [{'item': ManaPotion, 'rate': 0.25}]
super().__init__('test', 100, 0, 5, 15, 30, loot, [])
self.name = 'Test NPC {}'.format(Test.Counter + 1)
Test.Counter += 1
def __del__(self):
Test.Counter -= 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('test', 'battle_idle')), (30, 38))
self.image.set_colorkey(pg.Color("#7bd5fe"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('test', 'map')), (30, 38))
image.set_colorkey(pg.Color("#7bd5fe"))
return image
def decide(self, player_party, npc_party):
alive = player_party.get_alive()
if len(alive) > 0:
self.attack(alive[0])
class FireElemental(BaseNPC):
"""
Fire elemental NPC.Targets player with smallest amount of health first
"""
Counter = 0
def __init__(self):
loot = [{'item': FireBlade, 'rate': 0.1}]
spells = [Spells.FireBreath()]
super(FireElemental, self).__init__('fire_elem', 50, 20, 5, 10, 30, loot, spells)
self.name = 'Fire elemental {}'.format(FireElemental.Counter + 1 if FireElemental.Counter > 1 else "")
FireElemental.Counter += 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('fire_elem', 'battle_idle')), (24, 36))
self.image.set_colorkey(pg.Color("#fec5c5"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('fire_elem', 'map')), (24, 36))
image.set_colorkey(pg.Color("#fec5c5"))
return image
def decide(self, player_party, npc_party):
"""
Choose player with smallest amount of health.Cast fire breath if enough MP
"""
if len(player_party.get_alive()) > 0:
min_member = player_party.get_alive()[0]
for i in player_party.get_alive():
if i.HP < min_member.HP:
min_member = i
if self.MP >= self.spells[0].mp:
self.cast_spell(self.spells[0], min_member)
else:
self.attack(min_member)
class WaterElemental(BaseNPC):
"""
Fire elemental NPC.Targets random player
"""
Counter = 0
def __init__(self):
super(WaterElemental, self).__init__('water_elem', 40, 0, 20, 10, 10, [], [])
self.name = 'Water elemental {}'.format(WaterElemental.Counter + 1 if WaterElemental.Counter > 1 else "")
WaterElemental.Counter += 1
def __del__(self):
WaterElemental.Counter -= 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('water_elem', 'battle_idle')), (24, 36))
self.image.set_colorkey(pg.Color("#fec5c5"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('water_elem', 'map')), (24, 36))
image.set_colorkey(pg.Color("#fec5c5"))
return image
def decide(self, player_party, npc_party):
"""
Choose random player
"""
if len(player_party.get_alive()) > 0:
self.attack(rand.choice(player_party.get_alive()))
class EarthElemental(BaseNPC):
"""
Earth elemental NPC.Targets strongest player
"""
Counter = 0
def __init__(self):
super(EarthElemental, self).__init__('earth_elem', 35, 0, 25, 15, 25, [], [])
self.name = 'Earth elemental {}'.format(EarthElemental.Counter + 1 if EarthElemental.Counter > 1 else "")
EarthElemental.Counter += 1
def __del__(self):
EarthElemental.Counter -= 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('earth_elem', 'battle_idle')), (24, 36))
self.image.set_colorkey(pg.Color("#fec5c5"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('earth_elem', 'map')), (24, 36))
image.set_colorkey(pg.Color("#fec5c5"))
return image
def decide(self, player_party, npc_party):
"""
Choose player with largest amount of health
"""
if len(player_party.get_alive()) > 0:
max_member = player_party.get_alive()[0]
for i in player_party.get_alive():
if i.HP > max_member.HP:
max_member = i
self.attack(max_member)
class LightElemental(BaseNPC):
"""
Light elemental NPC.Targets player with smallest amount of health first
"""
Counter = 0
def __init__(self):
loot = []
spells = []
super(LightElemental, self).__init__('light_elem', 250, 0, 25, 30, 400, loot, spells)
self.name = 'Light elemental {}'.format(LightElemental.Counter + 1 if LightElemental.Counter > 1 else "")
LightElemental.Counter += 1
def __del__(self):
LightElemental.Counter -= 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('light_elem', 'battle_idle')), (24, 36))
self.image.set_colorkey(pg.Color("#fec5c5"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('light_elem', 'map')), (24, 36))
image.set_colorkey(pg.Color("#fec5c5"))
return image
def decide(self, player_party, npc_party):
"""
Choose player with smallest amount of health.Cast fire breath if enough MP
"""
if len(player_party.get_alive()) > 0:
min_member = player_party.get_alive()[0]
for i in player_party.get_alive():
if i.HP < min_member.HP:
min_member = i
self.attack(min_member)
class DarkElemental(BaseNPC):
"""
Darkness elemental NPC.Very hard to beat
"""
Counter = 0
def __init__(self):
loot = [{'item': StoneArmor, 'rate': 0.3}]
spells = []
super(DarkElemental, self).__init__('dark_elem', 500, 0, 25, 60, 1000, loot, spells)
self.name = 'Dark elemental {}'.format(DarkElemental.Counter + 1 if DarkElemental.Counter > 1 else "")
DarkElemental.Counter += 1
def __del__(self):
DarkElemental.Counter -= 1
def load_sprites(self):
helper = SpritesHelper()
self.image = pg.transform.scale(pg.image.load(helper.get_sprite('dark_elem', 'battle_idle')), (24, 36))
self.image.set_colorkey(pg.Color("#fec5c5"))
self.rect = self.image.get_rect()
def load_map_sprite(self):
helper = SpritesHelper()
image = pg.transform.scale(pg.image.load(helper.get_sprite('dark_elem', 'map')), (24, 36))
image.set_colorkey(pg.Color("#fec5c5"))
return image
def decide(self, player_party, npc_party):
"""
Choose player with smallest amount of health
"""
if len(player_party.get_alive()) > 0:
min_member = player_party.get_alive()[0]
for i in player_party.get_alive():
if i.HP < min_member.HP:
min_member = i
self.attack(min_member)