all repos — Legends-RPG @ ec5f95f22de795323e125c8f65989af74c1f312a

A fantasy mini-RPG built with Python and Pygame.

data/observer.py (view raw)

  1"""
  2Module for all game observers.
  3"""
  4from . import constants as c
  5from .components import attack
  6from . import setup
  7
  8class Battle(object):
  9    """
 10    Observes events of battle and passes info to components.
 11    """
 12    def __init__(self, level):
 13        self.level = level
 14        self.info_box = level.info_box
 15        self.select_box = level.info_box
 16        self.arrow = level.arrow
 17        self.player = level.player
 18        self.enemies = level.enemy_group
 19        self.event_dict = self.make_event_dict()
 20
 21    def make_event_dict(self):
 22        """
 23        Make a dictionary of events the Observer can
 24        receive.
 25        """
 26        event_dict = {c.END_BATTLE: self.end_battle,
 27                      c.SELECT_ACTION: self.select_action,
 28                      c.SELECT_ITEM: self.select_item,
 29                      c.SELECT_ENEMY: self.select_enemy,
 30                      c.SELECT_MAGIC: self.select_magic,
 31                      c.ENEMY_ATTACK: self.enemy_attack,
 32                      c.PLAYER_ATTACK: self.player_attack,
 33                      c.ATTACK_ANIMATION: self.attack_animation,
 34                      c.RUN_AWAY: self.run_away,
 35                      c.BATTLE_WON: self.battle_won,
 36                      c.PLAYER_FINISHED_ATTACK: self.player_finished_attack}
 37
 38        return event_dict
 39
 40    def on_notify(self, event):
 41        """
 42        Notify Observer of event.
 43        """
 44        if event in self.event_dict:
 45            self.event_dict[event]()
 46
 47    def end_battle(self):
 48        """
 49        End Battle and flip to previous state.
 50        """
 51        self.level.end_battle()
 52
 53    def select_action(self):
 54        """
 55        Set components to select action.
 56        """
 57        self.level.state = c.SELECT_ACTION
 58        self.arrow.index = 0
 59        self.arrow.state = c.SELECT_ACTION
 60        self.arrow.image = setup.GFX['smallarrow']
 61        self.info_box.state = c.SELECT_ACTION
 62
 63    def select_enemy(self):
 64        self.level.state = c.SELECT_ENEMY
 65        self.arrow.index = 0
 66        self.arrow.state = c.SELECT_ENEMY
 67
 68    def select_item(self):
 69        self.level.state = c.SELECT_ITEM
 70        self.info_box.state = c.SELECT_ITEM
 71        self.arrow.become_select_item_state()
 72
 73    def select_magic(self):
 74        self.level.state = c.SELECT_MAGIC
 75        self.info_box.state = c.SELECT_MAGIC
 76        self.arrow.become_select_magic_state()
 77
 78    def enemy_attack(self):
 79        pass
 80
 81    def player_attack(self):
 82        enemy_posx = self.arrow.rect.x + 60
 83        enemy_posy = self.arrow.rect.y - 20
 84        enemy_pos = (enemy_posx, enemy_posy)
 85        enemy_to_attack = None
 86
 87        for enemy in self.enemies:
 88            if enemy.rect.topleft == enemy_pos:
 89                enemy_to_attack = enemy
 90
 91        self.player.enter_attack_state(enemy_to_attack)
 92        self.arrow.become_invisible_surface()
 93
 94    def attack_animation(self):
 95        """
 96        Make an attack animation over attacked enemy.
 97        """
 98        self.arrow.remove_pos(self.player.attacked_enemy)
 99        self.arrow.state = c.SELECT_ACTION
100        self.level.attack_enemy()
101        self.info_box.state = c.ENEMY_HIT
102
103    def run_away(self):
104        self.level.end_battle()
105
106    def battle_won(self):
107        self.level.end_battle()
108
109    def player_finished_attack(self):
110        pass