Each enemy will do an attack animation in turn after the player attacks the enemy.
Justin Armstrong justinmeister@gmail.com
Fri, 25 Apr 2014 09:52:07 -0700
5 files changed,
43 insertions(+),
10 deletions(-)
M
data/battlegui.py
→
data/battlegui.py
@@ -33,7 +33,7 @@ state_dict = {c.SELECT_ACTION: 'Select an action.',
c.SELECT_MAGIC: 'Select a magic spell.', c.SELECT_ITEM: 'Select an item.', c.SELECT_ENEMY: 'Select an enemy.', - c.ENEMY_ATTACK: 'The enemy attacks player!', + c.ENEMY_ATTACK: 'Enemy attacks player!', c.PLAYER_ATTACK: 'Player attacks enemy.', c.RUN_AWAY: 'Run away', c.ENEMY_HIT: 'Enemy hit with 20 damage.',
M
data/components/person.py
→
data/components/person.py
@@ -34,6 +34,7 @@ self.dialogue = ['Location: ' + str(self.location)]
self.default_direction = direction self.item = None self.wander_box = self.make_wander_box() + self.observers = [] def create_spritesheet_dict(self, sheet_key): """Implemented by inheriting classes"""@@ -314,7 +315,7 @@ self.image = self.spritesheet_dict['facing left 1']
self.image = pg.transform.scale2x(self.image) elif self.rect.x <= self.origin_pos[0] - 110: self.x_vel = FAST_BACK - self.observer.on_notify('attack animation') + self.notify('attack animation') else: if self.rect.x >= self.origin_pos[0]: self.rect.x = self.origin_pos[0]@@ -322,7 +323,7 @@ self.x_vel = 0
self.state = 'battle resting' self.image = self.spritesheet_dict['facing left 2'] self.image = pg.transform.scale2x(self.image) - self.observer.on_notify(c.PLAYER_FINISHED_ATTACK) + self.notify(c.PLAYER_FINISHED_ATTACK) def enter_enemy_attack_state(self): """@@ -347,6 +348,7 @@ if self.move_counter == 3:
self.x_vel = 0 self.state = 'battle resting' self.rect.x = STARTX + self.notify(c.SWITCH_ENEMY) elif self.x_vel == FAST_LEFT: if self.rect.x <= (STARTX - 15):@@ -355,9 +357,6 @@ elif self.x_vel == FAST_RIGHT:
if self.rect.x >= (STARTX + 15): self.move_counter += 1 self.x_vel = FAST_LEFT - - - def auto_moving(self): """@@ -367,6 +366,13 @@ self.animation()
assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \ 'Not centered on tile' + + def notify(self, event): + """ + Notify all observers of events. + """ + for observer in self.observers: + observer.on_notify(event) class Player(Person):
M
data/constants.py
→
data/constants.py
@@ -36,6 +36,7 @@
SELECT_ACTION = 'select action' SELECT_ENEMY = 'select enemy' ENEMY_ATTACK = 'enemy attack' +SWITCH_ENEMY = 'switch enemy' PLAYER_ATTACK = 'player attack' SELECT_ITEM = 'select item' SELECT_MAGIC = 'select magic'
M
data/observer.py
→
data/observer.py
@@ -16,7 +16,12 @@ self.select_box = level.info_box
self.arrow = level.arrow self.player = level.player self.enemies = level.enemy_group + self.set_observer_for_enemies() self.event_dict = self.make_event_dict() + + def set_observer_for_enemies(self): + for enemy in self.enemies: + enemy.observers.append(self) def make_event_dict(self): """@@ -29,6 +34,7 @@ c.SELECT_ITEM: self.select_item,
c.SELECT_ENEMY: self.select_enemy, c.SELECT_MAGIC: self.select_magic, c.ENEMY_ATTACK: self.enemy_attack, + c.SWITCH_ENEMY: self.switch_enemy, c.PLAYER_ATTACK: self.player_attack, c.ATTACK_ANIMATION: self.attack_animation, c.RUN_AWAY: self.run_away,@@ -78,6 +84,15 @@
def enemy_attack(self): enemy = self.level.enemy_list[self.level.enemy_index] enemy.enter_enemy_attack_state() + self.info_box.state = c.ENEMY_ATTACK + + def switch_enemy(self): + """Switch which enemy is attacking player.""" + if self.level.enemy_index < len(self.level.enemy_list) - 1: + self.level.enemy_index += 1 + self.on_notify(c.ENEMY_ATTACK) + else: + self.on_notify(c.SELECT_ACTION) def player_attack(self): enemy_posx = self.arrow.rect.x + 60
M
data/states/battle.py
→
data/states/battle.py
@@ -37,8 +37,8 @@ self.state = c.SELECT_ACTION
self.select_action_state_dict = self.make_selection_state_dict() self.name = 'battle' self.next = game_data['last state'] - self.observer = observer.Battle(self) - self.player.observer = self.observer + self.observers = [observer.Battle(self)] + self.player.observers.extend(self.observers) def make_background(self): """Make the blue/black background"""@@ -126,7 +126,10 @@ self.state = c.ENEMY_DEAD
self.info_box.state = c.ENEMY_DEAD elif self.state == c.ENEMY_DEAD: - self.state = c.ENEMY_ATTACK + if len(self.enemy_list): + self.state = c.ENEMY_ATTACK + else: + self.state = c.BATTLE_WON self.notify(self.state) elif self.state == c.SELECT_ITEM or self.state == c.SELECT_MAGIC:@@ -151,7 +154,8 @@ def notify(self, event):
""" Notify observer of event. """ - self.observer.on_notify(event) + for observer in self.observers: + observer.on_notify(event) def end_battle(self): """@@ -162,15 +166,22 @@ self.done = True
def attack_enemy(self): enemy = self.player.attacked_enemy + self.set_enemy_indices() if enemy: enemy.kill() self.enemy_list.pop(enemy.index) + self.enemy_index = 0 posx = enemy.rect.x - 32 posy = enemy.rect.y - 64 fire_sprite = attack.Fire(posx, posy) self.attack_animations.add(fire_sprite) self.player.attacked_enemy = None + + def set_enemy_indices(self): + for i, enemy in enumerate(self.enemy_list): + enemy.index = i + def draw_battle(self, surface): """Draw all elements of battle state"""