-
Notifications
You must be signed in to change notification settings - Fork 0
/
battlescreen.py
515 lines (438 loc) · 16.7 KB
/
battlescreen.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
from gamescreen import GameScreen, WHITE, BLACK, RED, BLUE, ORANGE, DARK_RED
from camera import WIN_WIDTH, WIN_HEIGHT
from battlecontrols import SELECT_ACTION, SELECT_TARGET, SELECT_SPELL, SELECT_ITEM, EXECUTE_ACTIONS
from itemfactory import generate_item
import pygame
from pygame import Surface, Color, font
import math
import random
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MONSTER_FLOOR_Y = 300
UI_X, UI_Y = 0, WIN_HEIGHT*2/3
PARTY_DATA_X, PARTY_DATA_Y = UI_X, UI_Y
PARTY_DATA_WIDTH, PARTY_DATA_HEIGHT = 300, WIN_HEIGHT - UI_Y
ACTION_OPTIONS_WIDTH, ACTION_OPTIONS_HEIGHT = 110, PARTY_DATA_HEIGHT
MISC_DATA_WIDTH, MISC_DATA_HEIGHT = WIN_WIDTH - PARTY_DATA_WIDTH - ACTION_OPTIONS_WIDTH, PARTY_DATA_HEIGHT
MENU_SPACING_WIDTH, MENU_SPACING_HEIGHT = 140, 40
class BattleScreen(GameScreen):
""" MainGameScreen( ControlManager, Player ) -> MainGameScreen
The main game screen is used when the game is not paused and the player is doing
stuff or a cutscene is playing.
Attributes:
player: The player affected by this screen.
"""
def __init__(self, control_manager, player, monsters, tile, battle_data = None):
GameScreen.__init__(self, control_manager)
self.width, self.height = WIN_WIDTH, WIN_HEIGHT
self.player = player
self.monsters = monsters
self.init_active_party()
self.assign_monster_letters()
self.generate_rewards()
self.bg = self.generate_bg(tile)
self.ui_font = font.Font("./fonts/FreeSansBold.ttf", 16)
self.mode = SELECT_ACTION
self.action_index = 0
self.target_index = 0
self.item_select_index = 0
self.spell_index_x, self.spell_index_y = 0, 0
self.pending_action = None
self.spell_queue = []
self.turn_manager = TurnManager(self)
self.misc_message = None
self.victory_flag = False
self.battle_data = battle_data
def init_active_party(self):
self.active_party = []
for p in self.player.party:
if p.hitpoints[0] > 0: self.active_party.append(p)
self.reset_party_turn_index()
def assign_monster_letters(self):
alphabet_index = 0
existing_names = []
for m in self.monsters.monsters:
if not m.battle_letter_flag:
m.battle_name = m.name
continue
name = m.name + " " + ALPHABET[alphabet_index]
while (name in existing_names):
alphabet_index += 1
name = m.name + " " + ALPHABET[alphabet_index]
existing_names.append(name)
m.battle_name = name
alphabet_index = 0
def generate_rewards(self):
self.generate_item_drops()
self.generate_exp_yield()
def generate_item_drops(self):
self.item_drops = []
for m in self.monsters.monsters:
item_data = m.item_drop_data
for d in item_data:
if random.random() < d[1]:
item = generate_item(d[0])
self.item_drops.append(item)
def generate_exp_yield(self):
self.exp_yield = 0
for m in self.monsters.monsters:
self.exp_yield += m.exp_yield
def update(self):
""" mgs.update( ) -> None
Call update methods related to the game.
"""
self.screen_image.blit(self.bg, (0, 0))
self.draw_monsters()
self.draw_ui()
if self.mode == EXECUTE_ACTIONS: self.execute_update()
def execute_update(self):
self.turn_manager.update()
def generate_bg(self, tile):
bg = Surface((WIN_WIDTH, WIN_HEIGHT))
bg.fill(tile.bg_color)
return bg
def draw_monsters(self):
x_offset = 20
for m in self.monsters.monsters:
self.screen_image.blit(m.image, (x_offset, MONSTER_FLOOR_Y - m.image.get_height()))
x_offset += 100
if self.mode == SELECT_TARGET: self.draw_target_pointer()
def draw_target_pointer(self):
target = self.monsters.monsters[self.target_index]
width, height = target.image.get_width(), target.image.get_height()
monster_offset = (20 + 100*self.target_index, height)
pointer_x, pointer_y = monster_offset[0] + width/2 - 8, MONSTER_FLOOR_Y - height - 20
pointer = Surface((16, 16))
pointer.set_colorkey(BLACK)
pygame.draw.polygon(pointer, RED, [(0, 0), (16, 0), (8, 16)])
self.screen_image.blit(pointer, (pointer_x, pointer_y))
def draw_ui(self):
ui_pane = Surface((WIN_WIDTH, WIN_HEIGHT - UI_Y))
self.draw_party_data(ui_pane)
self.draw_action_options(ui_pane)
self.draw_misc_data(ui_pane)
self.screen_image.blit(ui_pane, (UI_X, UI_Y))
def draw_party_data(self, pane): #TEMP. CHange this if party structure changes.
data_pane = Surface((PARTY_DATA_WIDTH, PARTY_DATA_HEIGHT))
y_offset = 8
for i in xrange(len(self.player.party)):
p = self.player.party[i]
name_color = WHITE
if not self.mode == EXECUTE_ACTIONS and i == self.party_turn_index: name_color = ORANGE
name = p.name
name_image = self.ui_font.render(name, True, name_color)
data_pane.blit(name_image, (8, y_offset))
if p.hitpoints[0] > 0:
hp_text = "HP: " + str(p.hitpoints[0]) + "/" + str(p.hitpoints[1])
hp_image = self.ui_font.render(hp_text, True, ORANGE)
else:
hp_text = "DEAD"
hp_image = self.ui_font.render(hp_text, True, DARK_RED)
data_pane.blit(hp_image, (110, y_offset))
mp_text = "MP: " + str(p.mana[0]) + "/" + str(p.mana[1])
mp_image = self.ui_font.render(mp_text, True, BLUE)
data_pane.blit(mp_image, (220, y_offset))
y_offset += 40
for i in xrange(len(self.player.summons)):
s = self.player.summons[i]
name_color = WHITE
if not self.mode == EXECUTE_ACTIONS and i + len(self.player.party) == self.party_turn_index: name_color = ORANGE
name = s.name
name_image = self.ui_font.render(name, True, name_color)
data_pane.blit(name_image, (8, y_offset))
if s.hitpoints[0] > 0:
hp_text = "HP: " + str(s.hitpoints[0]) + "/" + str(s.hitpoints[1])
hp_image = self.ui_font.render(hp_text, True, ORANGE)
else:
hp_text = "DEAD"
hp_image = self.ui_font.render(hp_text, True, DARK_RED)
data_pane.blit(hp_image, (110, y_offset))
mp_text = "MP: " + str(s.mana[0]) + "/" + str(s.mana[1])
mp_image = self.ui_font.render(mp_text, True, BLUE)
data_pane.blit(mp_image, (220, y_offset))
y_offset += 40
pygame.draw.lines(data_pane, WHITE, True, [(0, 0), (PARTY_DATA_WIDTH - 2, 0), (PARTY_DATA_WIDTH - 2, PARTY_DATA_HEIGHT - 2), (0, PARTY_DATA_HEIGHT - 2)], 2)
pane.blit(data_pane, (0, 0))
def draw_action_options(self, pane):
data_pane = Surface((ACTION_OPTIONS_WIDTH, ACTION_OPTIONS_HEIGHT))
for i in xrange(len(ACTION_OPTION_LIST)):
text = ACTION_OPTION_LIST[i].capitalize()
text_image = self.ui_font.render(text, True, WHITE)
data_pane.blit(text_image, (28, 8 + 40*i))
pygame.draw.lines(data_pane, WHITE, True, [(0, 0), (ACTION_OPTIONS_WIDTH - 2, 0), (ACTION_OPTIONS_WIDTH - 2, ACTION_OPTIONS_HEIGHT - 2), (0, ACTION_OPTIONS_HEIGHT - 2)], 2)
if not self.mode == EXECUTE_ACTIONS: self.draw_action_pointer(data_pane)
pane.blit(data_pane, (PARTY_DATA_WIDTH, 0))
def draw_misc_data(self, pane):
data_pane = Surface((MISC_DATA_WIDTH, MISC_DATA_HEIGHT))
if self.mode in MISC_DRAW_MAP:
draw_method = MISC_DRAW_MAP[self.mode]
draw_method(self, data_pane)
pygame.draw.lines(data_pane, WHITE, True, [(0, 0), (MISC_DATA_WIDTH - 2, 0), (MISC_DATA_WIDTH - 2, MISC_DATA_HEIGHT - 2), (0, MISC_DATA_HEIGHT - 2)], 2)
pane.blit(data_pane, (PARTY_DATA_WIDTH + ACTION_OPTIONS_WIDTH, 0))
def misc_draw_select_target(self, pane):
target = self.monsters.monsters[self.target_index]
text = target.battle_name
text_image = self.ui_font.render(text, True, WHITE)
pane.blit(text_image, (8, 8))
def misc_draw_select_spell(self, pane):
spells = self.active_party_member().spells
for i in xrange(len(spells)):
s = spells[i]
x, y = i%2, i/2
spell_name_image = self.ui_font.render(s.name(), True, WHITE)
spell_cost_image = self.ui_font.render(str(s.mp_cost()), True, BLUE)
pane.blit(spell_name_image, (28 + x*MENU_SPACING_WIDTH, 8 + y*MENU_SPACING_HEIGHT))
pane.blit(spell_cost_image, (32 + x*MENU_SPACING_WIDTH + spell_name_image.get_width(), 8 + y*MENU_SPACING_HEIGHT))
self.draw_spell_pointer(pane)
def misc_draw_message(self, pane):
text_image = self.ui_font.render(self.misc_message, True, WHITE)
pane.blit(text_image, (8, 8))
def misc_draw_select_item(self, pane):
items = self.player.inventory.items
for i in xrange(len(items)):
item = items[i]
text_image = self.ui_font.render(item.name, True, WHITE)
pane.blit(text_image, (28, 8 + 40*i))
self.draw_item_pointer(pane)
def draw_action_pointer(self, pane):
pointer = self.right_pointer(WHITE, self.mode == SELECT_ACTION)
pane.blit(pointer, (4, 8 + 40*self.action_index))
def draw_item_pointer(self, pane):
pointer = self.right_pointer()
pane.blit(pointer, (4, 8 + 40*self.item_select_index))
def draw_spell_pointer(self, pane):
pointer = self.right_pointer()
pane.blit(pointer, (4 + MENU_SPACING_WIDTH*self.spell_index_x, 8 + MENU_SPACING_HEIGHT*self.spell_index_y))
def right_pointer(self, color = WHITE, filled = True):
pointer = Surface((16, 16))
if filled: pygame.draw.polygon(pointer, color, [(0, 0), (16, 8), (0, 16)])
else: pygame.draw.polygon(pointer, WHITE, [(0, 0), (16, 8), (0, 16)], 2)
return pointer
def pause_update(self):
""" mgs.pause_update( ) -> None
Update the game in its paused state.
"""
pass
def clear(self):
""" mgs.level_update( ) -> None
Revert the screen's contents to a black rectangle.
"""
self.contents = Surface((self.width, self.height))
def select_current_action(self):
action_key = ACTION_OPTION_LIST[self.action_index]
self.pending_action = action_key
action = ACTION_OPTION_MAP[action_key]
action(self)
def confirm_current_action(self):
self.player.enqueue_action(self.party_turn_index, self.pending_action, self.monsters.monsters[self.target_index])
self.party_turn_index += 1
while self.party_turn_index < self.party_member_count() and self.active_party_member().hitpoints[0] <= 0:
self.party_turn_index += 1
if self.party_turn_index >= self.party_member_count():
self.begin_executing()
else:
self.mode = SELECT_ACTION
def select_current_spell(self):
index = self.spell_index_y*2 + self.spell_index_x
spell = self.active_party_member().spells[int(index)]
if spell.mp_cost() > self.active_party_member().mana[0]: return
self.active_party_member().pending_spell = spell
spell_type, targeting = spell.spell_type(), spell.targeting()
if spell.select_enemy(): #TODO: many other cases, such as healing party members
self.mode = SELECT_TARGET
else:
self.confirm_current_action()
def enqueue_spell_set(self, spell, targets):
queue = self.turn_manager.turn_queue
index = self.turn_manager.actor_counter + 1
for t in targets:
self.turn_manager.turn_queue.insert(index, (999999, spell))
index += 1
def begin_executing(self):
self.mode = EXECUTE_ACTIONS
self.turn_manager.set_up_queue(self.monsters.monsters, self.active_party, self.player.summons)
self.turn_manager.select_actor(0)
def reset_party_turn_index(self):
self.party_turn_index = 0
while self.party_turn_index < self.party_member_count() and self.active_party_member().hitpoints[0] <= 0:
self.party_turn_index += 1
def select_attack(self):
self.mode = SELECT_TARGET
def select_spells(self):
if self.active_party_member().has_spells():
self.spell_index_x, self.spell_index_y = 0, 0
self.mode = SELECT_SPELL
def select_item(self):
self.mode = SELECT_ITEM
def party_member_count(self):
return self.player.party_member_count() + self.player.summon_count()
def active_party_member(self): # TODO: allow summons
count = self.player.party_member_count()
if self.party_turn_index >= count: return self.player.summons[self.party_turn_index - count]
return self.player.party[self.party_turn_index]
def target_count(self):
return len(self.monsters.monsters)
def item_count(self):
return self.player.inventory.item_count()
def current_spell_cols(self):
spell_count = self.active_party_member().spell_count()
if self.spell_index_y == spell_count/2: return spell_count%2
return 2
def current_spell_rows(self):
spell_count = self.active_party_member().spell_count()
end_cols = spell_count%2
rows = math.ceil(spell_count/2.0)
if end_cols != 0 and end_cols <= self.spell_index_x: rows -= 1
return rows
def exit_battle(self):
self.player.remove_summons()
self.screen_manager.switch_to_world_screen(self.player)
TURN_TICKS = 150
class TurnManager:
def __init__(self, screen):
self.screen = screen
self.turn_queue = []
self.current_actor = None
self.actor_counter = 0
self.time_counter = 0
self.exp_yield_flag = True
self.level_up_message_flag = True
self.level_ups = []
self.level_up_index = 0
def set_up_queue(self, monsters, party, summons):
for m in monsters: self.turn_queue.append((m.speed, m))
for p in party: self.turn_queue.append((p.speed, p))
for s in summons: self.turn_queue.append((s.speed, s))
self.turn_queue = sorted(self.turn_queue)
self.turn_queue.reverse()
def update(self):
self.time_counter += 1
if self.time_counter > TURN_TICKS:
self.time_counter = 0
if self.death_check(): return
if self.victory_check(): return
if self.actor_counter + 1 >= len(self.turn_queue):
self.finish_executing()
return
self.actor_counter += 1
self.select_actor()
def death_check(self):
for m in self.screen.monsters.monsters:
if m.hitpoints[0] <= 0:
self.remove_monster(m)
self.screen.misc_message = m.battle_name + " was slain!" #TODO: add to exp and gold rewards here
return True
for p in self.screen.active_party:
if p.hitpoints[0] <= 0:
self.screen.misc_message = p.name + " fell in battle!"
self.set_party_member_dead(p)
return True
for s in self.screen.player.summons:
if s.hitpoints[0] <= 0:
self.screen.misc_message = s.name + " was destroyed!"
self.remove_summon(s)
return True
return False
def victory_check(self):
if len(self.screen.monsters.monsters) == 0:
if self.screen.victory_flag:
self.victory_update()
return True
self.screen.misc_message = "Victory!"
self.screen.victory_flag = True
self.item_drop_index = 0
return True
return False
def victory_update(self):
if self.exp_yield_check(): return
if self.level_up_check(): return
if self.item_drop_check(): return
if self.post_battle_event_check(): return
self.screen.player.reset_flags()
#TODO: update experience, item drops, etc here.
self.screen.exit_battle()
def exp_yield_check(self):
if self.exp_yield_flag:
self.screen.misc_message = "You gained " + str(self.screen.exp_yield) + " experience!"
party = self.screen.player.party
for p in party:
exp = self.screen.exp_yield/len(party)
p.experience[0] += exp
if p.level_up_check(): self.level_ups.append(p)
self.exp_yield_flag = False
return True
return False
def level_up_check(self):
if self.level_up_index < len(self.level_ups):
p = self.level_ups[self.level_up_index]
if self.level_up_message_flag:
self.screen.misc_message = p.name + " grew to level " + str(p.exp_level) + "!"
self.level_up_message_flag = False
return True
if p.new_spells_check():
if p.new_spells_flag: p.new_spells_update(self.screen)
else: p.new_spells_flag = True
else: self.level_up_index += 1
return True
return False
def item_drop_check(self):
drops = self.screen.item_drops
count = len(drops)
if self.item_drop_index >= count: return False
item = drops[self.item_drop_index]
self.screen.misc_message = "Got " + item.name + "!"
self.screen.player.obtain_item(item)
self.item_drop_index += 1
return True
def post_battle_event_check(self):
if not self.screen.battle_data: return False
if not self.screen.battle_data.post_update(self.screen): return False
return True
def select_actor(self, index = None):
if not index: index = self.actor_counter
self.current_actor = self.turn_queue[index][1]
self.time_counter = 0
self.current_actor.execute_action(self.screen)
def finish_executing(self):
self.turn_queue = []
self.current_actor = None
self.actor_counter = 0
self.time_counter = 0
self.screen.mode = SELECT_ACTION
self.screen.reset_party_turn_index()
def remove_monster(self, monster):
self.screen.monsters.monsters.remove(monster)
self.screen.target_index = 0
for t in self.turn_queue:
if t[1] == monster:
self.turn_queue.remove(t)
return
def set_party_member_dead(self, party_member):
self.screen.active_party.remove(party_member)
for p in self.turn_queue:
if p[1] == party_member:
self.turn_queue.remove(p)
return
def remove_summon(self, summon):
self.screen.player.summons.remove(summon)
for s in self.turn_queue:
if s[1] == summon:
self.turn_queue.remove(s)
return
# spell constants
SINGLE = "single"
ATTACK = "attack"
SPELLS = "spells"
ITEMS = "items"
RUN = "run"
ACTION_OPTION_LIST = [ATTACK, SPELLS, ITEMS, RUN]
ACTION_OPTION_MAP = {
ATTACK:BattleScreen.select_attack,
SPELLS:BattleScreen.select_spells,
ITEMS:BattleScreen.select_item
}
MISC_DRAW_MAP = {
SELECT_TARGET:BattleScreen.misc_draw_select_target,
SELECT_SPELL:BattleScreen.misc_draw_select_spell,
EXECUTE_ACTIONS:BattleScreen.misc_draw_message,
SELECT_ITEM:BattleScreen.misc_draw_select_item
}