-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
207 lines (180 loc) · 5.86 KB
/
events.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
import random
MOORE = [(1,0), (-1,0), (0,1), (0,-1), (1,1), (-1,1), (-1,-1), (1,-1)]
class EventQueue(object):
def __init__(self):
self.game_events = []
def apply(self):
self.apply_game_events()
def apply_game_events(self):
for event in list(self.game_events):
event.apply(self.owner)
self.game_events.remove(event)
if self.game_events != []:
self.apply_game_events()
class TurnQueue(object):
def __init__(self):
self.order = []
self.global_time = 0
def __iter__(self):
return iter(self.order)
def sort(self):
self.order = list(self.owner.session.world[0].entities.nodes)
random.shuffle(self.order)
self.order.sort(key=lambda entity: entity.time, reverse = True)
if self.order[0].time < 0:
self.return_time(-1 * self.order[0].time)
self.sort()
def return_time(self, time):
self.global_time += time
for entity in self.order:
entity.time += time
def apply(self):
if self.order == []: self.sort()
if self.order[0] != self.owner.stack.focus:
while self.order[0] != self.owner.stack.focus:
EntityTurn(self.order[0])
self.owner.event_queue.apply()
elif self.owner.stack[0].commands != []:
while self.order[0] == self.owner.stack[0].focus and self.owner.stack[0].commands != []:
self.owner.stack[0].apply_command()
self.owner.event_queue.apply()
event_queue = EventQueue()
turn_queue = TurnQueue()
class EventHandler(object):
def move_entity(self, entity, rel):
MoveEntity(entity, rel)
def wait_entity(self, entity):
WaitEntity(entity)
class Event(object):
def __init__(self, *args):
self.owner = event_queue
self.owner.game_events.append(self)
self.args = args
class SpendTime(Event):
def apply(self, game):
entity, time = self.args[0], self.args[1]
if time < 1: time = 1
entity.time += -time
game.turn_queue.sort()
class EntityTurn(Event):
def apply(self, game):
entity, rel = self.args[0], random.choice(MOORE)
entity.brain.observe()
if entity.brain.path != []:
step = entity.brain.path[0]
new_rel = step[0] - entity.x, step[1] - entity.y
entity.brain.path.pop(0)
MoveEntity(entity, new_rel)
else:
WaitEntity(entity)
class WaitEntity(Event):
def apply(self, game):
entity = self.args[0]
entity.update()
SpendTime(entity, 5)
class MoveEntity(Event):
def apply(self, game):
entity, rel = self.args[0], self.args[1]
pos = entity.x + rel[0], entity.y + rel[1]
if pos not in game.session.world[0].terrain:
return
if game.session.world[0].terrain[pos]['chasm']:
return
if pos in game.session.world[0].features:
return
if pos in game.session.world[0].entities:
BumpEntity(entity, game.session.world[0].entities[pos])
return
if pos in game.stack.focus.fov and entity not in game.stack[0].entities:
game.stack[0].add_entity_sprite(entity)
game.session.world[0].entities[pos] = entity
if entity in game.stack[0].entities:
game.stack[0].entities[entity].add_to_path(pos)
if pos not in game.stack.focus.fov:
game.stack[0].entities[entity].add_to_path(False)
if entity == game.stack.focus:
entity.brain.observe()
SpendTime(entity, 5)
class BumpEntity(Event):
def apply(self, game):
bumper, target = self.args[0], self.args[1]
MeleeAttack(bumper, target)
class MeleeAttack(Event):
def apply(self, game):
attacker, target = self.args[0], self.args[1]
attack_roll = roll(attacker.melee_to_hit)
if attack_roll == True:
MeleeDamage(attacker, target)
elif attack_roll == False:
FloatText('MISS', target, (250,250,250))
elif attack_roll >= target.dodge:
MeleeDamage(attacker, target)
else:
FloatText('MISS', target, (250,250,250))
SpendTime(attacker, 5)
if attacker in game.stack[0].entities:
step = target.x - attacker.x, target.y - attacker.y
half_step = attacker.x + (step[0] / 3.0), attacker.y + (step[1] / 3.0)
game.stack[0].entities[attacker].add_to_path(half_step)
game.stack[0].entities[attacker].add_to_path(attacker.pos)
class MeleeDamage(Event):
def apply(self, game):
attacker, target = self.args[0], self.args[1]
damage_roll = roll(attacker.melee_damage)
if damage_roll == True:
target.damage += 20
FloatText('CRITICAL!', target, (250,0,0))
elif damage_roll == False:
FloatText(0, target, (250,250,250))
elif damage_roll > target.soak:
damage = damage_roll - target.soak
target.damage += damage
FloatText(-damage, target, (250,0,0))
else:
FloatText(0, target, (250,250,250))
if target != game.stack.focus:
if attacker == game.stack.focus:
if target.damage >= target.soak:
FloatText(str(target.experience) + ' xp', target, (250,250,0))
attacker.experience += target.experience
CheckForLevelUp(attacker)
CheckForDeath(target)
class CheckForLevelUp(Event):
def apply(self, game):
entity = self.args[0]
goal_post = entity.level * 300
if entity.experience >= goal_post:
FlashText('Level Up!', entity, (0,0,250))
entity.level += 1
class CheckForDeath(Event):
def apply(self, game):
entity = self.args[0]
if entity.damage >= entity.soak:
EntityDeath(entity)
class EntityDeath(Event):
def apply(self, game):
entity = self.args[0]
if entity in game.stack[0].entities:
game.stack[0].entities[entity].add_to_path(False)
entity.die()
game.turn_queue.sort()
class FloatText(Event):
def apply(self, game):
text, entity, color = str(self.args[0]), self.args[1], self.args[2]
if entity in game.stack[0].entities:
game.stack[0].entities[entity].float_text(text, color)
class FlashText(Event):
def apply(self, game):
text, entity, color = str(self.args[0]), self.args[1], self.args[2]
if entity in game.stack[0].entities:
game.stack[0].entities[entity].flash_text(text, color)
def roll(modifier = 0):
result = random.randrange(1, 21)
if result == 20: return True
if result == 1: return False
return result + modifier
def init(game):
event_queue.owner = game
game.event_queue = event_queue
turn_queue.owner = game
game.turn_queue = turn_queue