data/states/battle.py (view raw)
1"""This is the state that handles battles against
2monsters"""
3
4import random
5import pygame as pg
6from .. import tools, battlegui, observer
7from .. components import person, attack, attackitems
8from .. import constants as c
9
10
11class Battle(tools._State):
12 def __init__(self):
13 super(Battle, self).__init__()
14 self.game_data = {}
15 self.current_time = 0.0
16 self.timer = 0.0
17 self.allow_input = False
18 self.allow_info_box_change = False
19 self.name = 'battle'
20 self.state = None
21
22 self.player = None
23 self.attack_animations = None
24 self.sword = None
25 self.enemy_index = 0
26 self.attacked_enemy = None
27 self.attacking_enemy = None
28 self.enemy_group = None
29 self.enemy_pos_list = []
30 self.enemy_list = []
31
32 self.background = None
33 self.info_box = None
34 self.arrow = None
35 self.select_box = None
36 self.player_health_box = None
37 self.select_action_state_dict = {}
38 self.next = None
39 self.observers = []
40
41 def startup(self, current_time, game_data):
42 """Initialize state attributes"""
43 self.current_time = current_time
44 self.timer = current_time
45 self.game_data = game_data
46 self.state = c.SELECT_ACTION
47 self.next = game_data['last state']
48
49 self.player = self.make_player()
50 self.attack_animations = pg.sprite.Group()
51 self.sword = attackitems.Sword(self.player)
52 self.enemy_group, self.enemy_pos_list, self.enemy_list = self.make_enemies()
53 self.background = self.make_background()
54 self.info_box = battlegui.InfoBox(game_data)
55 self.arrow = battlegui.SelectArrow(self.enemy_pos_list,
56 self.info_box)
57 self.select_box = battlegui.SelectBox()
58 self.player_health_box = battlegui.PlayerHealth(self.select_box.rect,
59 self.game_data)
60
61 self.select_action_state_dict = self.make_selection_state_dict()
62 self.observers = [observer.Battle(self)]
63 self.player.observers.extend(self.observers)
64
65 @staticmethod
66 def make_background():
67 """Make the blue/black background"""
68 background = pg.sprite.Sprite()
69 surface = pg.Surface(c.SCREEN_SIZE).convert()
70 surface.fill(c.BLACK_BLUE)
71 background.image = surface
72 background.rect = background.image.get_rect()
73 background_group = pg.sprite.Group(background)
74
75 return background_group
76
77 @staticmethod
78 def make_enemies():
79 """Make the enemies for the battle. Return sprite group"""
80 pos_list = []
81
82 for column in range(3):
83 for row in range(3):
84 x = (column * 100) + 100
85 y = (row * 100) + 100
86 pos_list.append([x, y])
87
88 enemy_group = pg.sprite.Group()
89
90 for enemy in range(random.randint(1, 6)):
91 enemy_group.add(person.Person('devil', 0, 0,
92 'down', 'battle resting'))
93
94 for i, enemy in enumerate(enemy_group):
95 enemy.rect.topleft = pos_list[i]
96 enemy.image = pg.transform.scale2x(enemy.image)
97 enemy.index = i
98
99 enemy_list = [enemy for enemy in enemy_group]
100
101 return enemy_group, pos_list[0:len(enemy_group)], enemy_list
102
103 @staticmethod
104 def make_player():
105 """Make the sprite for the player's character"""
106 player = person.Player('left', 630, 220, 'battle resting', 1)
107 player.image = pg.transform.scale2x(player.image)
108
109 return player
110
111 def make_selection_state_dict(self):
112 """
113 Make a dictionary of states with arrow coordinates as keys.
114 """
115 pos_list = self.arrow.make_select_action_pos_list()
116 state_list = [c.SELECT_ENEMY, c.SELECT_ITEM, c.SELECT_MAGIC, c.RUN_AWAY]
117 return dict(zip(pos_list, state_list))
118
119 def update(self, surface, keys, current_time):
120 """Update the battle state"""
121 self.current_time = current_time
122 self.check_input(keys)
123 self.check_timed_events()
124 self.check_if_battle_won()
125 self.enemy_group.update(current_time)
126 self.player.update(keys, current_time)
127 self.attack_animations.update()
128 self.info_box.update()
129 self.arrow.update(keys)
130
131 self.draw_battle(surface)
132
133 def check_input(self, keys):
134 """
135 Check user input to navigate GUI.
136 """
137 if self.allow_input:
138 if keys[pg.K_RETURN]:
139 self.notify(c.END_BATTLE)
140
141 elif keys[pg.K_SPACE]:
142 if self.state == c.SELECT_ACTION:
143 self.state = self.select_action_state_dict[
144 self.arrow.rect.topleft]
145 self.notify(self.state)
146
147 elif self.state == c.SELECT_ENEMY:
148 self.state = c.PLAYER_ATTACK
149 self.notify(self.state)
150
151 elif self.state == c.SELECT_ITEM or self.state == c.SELECT_MAGIC:
152 if self.arrow.index == (len(self.arrow.pos_list) - 1):
153 self.state = c.SELECT_ACTION
154 self.notify(self.state)
155
156 self.allow_input = False
157
158 if keys[pg.K_RETURN] == False and keys[pg.K_SPACE] == False:
159 self.allow_input = True
160
161 def check_timed_events(self):
162 """
163 Check if amount of time has passed for timed events.
164 """
165 timed_states = [c.DISPLAY_ENEMY_ATTACK_DAMAGE,
166 c.ENEMY_HIT,
167 c.ENEMY_DEAD]
168
169 if self.state in timed_states:
170 if (self.current_time - self.timer) > 1200:
171 if self.state == c.DISPLAY_ENEMY_ATTACK_DAMAGE:
172 if self.enemy_index == (len(self.enemy_list) - 1):
173 self.state = c.SELECT_ACTION
174 else:
175 self.state = c.SWITCH_ENEMY
176 elif self.state == c.ENEMY_HIT:
177 self.state = c.ENEMY_DEAD
178 elif self.state == c.ENEMY_DEAD:
179 if len(self.enemy_list):
180 self.state = c.ENEMY_ATTACK
181 else:
182 self.state = c.BATTLE_WON
183
184 self.notify(self.state)
185
186 def check_if_battle_won(self):
187 """
188 Check if state is SELECT_ACTION and there are no enemies left.
189 """
190 if self.state == c.SELECT_ACTION:
191 if len(self.enemy_group) == 0:
192 self.notify(c.BATTLE_WON)
193
194 def notify(self, event):
195 """
196 Notify observer of event.
197 """
198 for new_observer in self.observers:
199 new_observer.on_notify(event)
200
201 def end_battle(self):
202 """
203 End battle and flip back to previous state.
204 """
205 self.game_data['last state'] = self.name
206 self.done = True
207
208 def attack_enemy(self):
209 enemy = self.player.attacked_enemy
210 self.set_enemy_indices()
211
212 if enemy:
213 enemy.kill()
214 self.enemy_list.pop(enemy.index)
215 self.enemy_index = 0
216 posx = enemy.rect.x - 32
217 posy = enemy.rect.y - 64
218 fire_sprite = attack.Fire(posx, posy)
219 self.attack_animations.add(fire_sprite)
220 self.player.attacked_enemy = None
221
222 def set_enemy_indices(self):
223 for i, enemy in enumerate(self.enemy_list):
224 enemy.index = i
225
226 def draw_battle(self, surface):
227 """Draw all elements of battle state"""
228 self.background.draw(surface)
229 self.enemy_group.draw(surface)
230 self.attack_animations.draw(surface)
231 surface.blit(self.player.image, self.player.rect)
232 surface.blit(self.info_box.image, self.info_box.rect)
233 surface.blit(self.select_box.image, self.select_box.rect)
234 surface.blit(self.arrow.image, self.arrow.rect)
235 self.player_health_box.draw(surface)
236 self.sword.draw(surface)
237
238 def player_damaged(self, damage):
239 self.game_data['player stats']['Health']['current'] -= damage
240
241 def set_timer_to_current_time(self):
242 """Set the timer to the current time."""
243 self.timer = self.current_time