/
character.py
181 lines (134 loc) · 4.62 KB
/
character.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
import libtcodpy as T
from gameobject import *
from config import *
def random_walk():
dirs = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
return dirs[T.random_get_int(0, 0, 7)]
def direct_path(ox, oy, dx, dy, map):
path = T.path_new_using_map(map)
T.path_compute(path, ox, oy, dx, dy)
(nx, ny) = T.path_walk(path, False)
return (nx - ox, ny - oy)
class Actor(object):
def __init__(self, speed):
self.speed = speed
self.mp = 0
def tick(self):
self.mp += self.speed
def acted(self, turn_cost):
self.mp -= turn_cost
def can_act(self, turn_cost):
return self.mp >= turn_cost
class Character(GameObject, Actor):
def __init__(self, x, y, dlevel, char, descr, speed):
GameObject.__init__(self, x, y, dlevel, char, descr)
Actor.__init__(self, speed)
self.inv = []
self.last_attacker = None
self.encountered = False
def can_move(self, dx, dy, map):
if map.blocked_at(self.x + dx, self.y + dy):
return False
return True
def update(self, map, npcs):
self.move(0, 0)
def next_to(self, x, y):
return (abs(self.x - x) <= 1 and abs(self.y - y) <= 1)
class Monster(Character):
def __init__(self, x, y, dlevel, char, descr, speed, exp, fear, health):
Character.__init__(self, x, y, dlevel, char, descr, speed)
self.exp = exp
self.health = health
self.fear = fear
self.aggro = False
class Zombie(Monster):
def __init__(self, x, y, dlevel):
Monster.__init__(self, x, y, dlevel, 'Z', 'a zombie', 9, 10, 10, 20)
def update(self, map, screen, player, npcs):
blocked = False
# if next to player and has aggro, attack
if self.next_to(player.x, player.y) and self.aggro:
damage = roll(6)
player.health -= damage
player.last_attacker = self
self.acted(TURN_COST)
screen.add_message("Zombie hits you for %s damage" % damage)
# move randomly unless aggro'd then move directly to player
else:
if self.aggro:
(dx, dy) = direct_path(self.x, self.y, player.x, player.y, map.fov_map)
else:
(dx, dy) = random_walk()
# don't walk into other NPCs
for n in npcs:
if n.x == self.x + dx and n.y == self.y + dy:
blocked = True
# don't walk into player
if player.x == self.x + dx and player.y == self.y + dy:
blocked = True
# move if not blocked by map
if not blocked and not map.blocked_at(self.x + dx, self.y + dy):
self.acted(TURN_COST)
self.move(dx, dy)
class Player(Character):
def __init__(self, x, y, level):
Character.__init__(self, x, y, level, '@', 'the player', 12)
self.health = self.max_health = 100
self.weapon = None
self.turns = 0
self.last_target = None
self.exp = 0
self.fear = 0
self.panicked = False
def acted(self, turn_cost):
self.mp -= turn_cost
self.turns += 1
def get_weapons(self):
return filter(lambda i: isinstance(i, Weapon), self.inv)
def can_pick_up(self, item):
return self.x == item.x and self.y == item.y
def get_ammo(self):
for i in self.inv:
if isinstance(i, Ammo) and i.weapon_class == self.weapon.__class__:
return i.count
return 0
def update(self):
# fade fear
if self.turns % 2 == 0 and self.fear > 0:
self.fear -= 1
self.panicked = True if self.fear > 100 else False
def encounter(self, monster):
if not monster.encountered:
monster.encountered = True
self.fear += monster.fear
def use_ammo(self, weapon_class, count):
for i in self.inv:
if isinstance(i, Ammo) and i.weapon_class == weapon_class:
i.count -= (count if count <= i.count else i.count)
return
def pick_up(self, item):
item.x = item.y = -1
if isinstance(item, Stackable):
stacked = False
for i in self.inv:
if isinstance(i, item.__class__):
i.count += item.count
stacked = True
if not stacked:
self.inv.append(item)
else:
self.inv.append(item)
def wield(self, item):
try:
i = self.inv.index(item)
except ValueError:
return False
self.weapon = item
def in_range(self, npcs):
if not isinstance(self.weapon, RangedWeapon):
return []
results = []
for n in npcs:
if distance(self, n) <= self.weapon.range:
results.append(n)
return results