class Monster(Character): @classmethod def get_monster_kind(cls): return cls.__name__.replace('Monster', '') def __init__(self, name='Monster', hp=50, power=5): super().__init__(name, hp, power) self.name = self.get_monster_kind() + name self.attack_kind = AttackKindFactory(self.get_monster_kind()) def attack(self, other, a_kind): if self.attack_kind.get_kind() == a_kind: other.get_damage(self.power, a_kind) self.attack_kind.attack() def get_damage(self, power, a_kind): if a_kind == self.attack_kind.get_kind(): self.hp += power else: self.hp -= power def get_attack_kind(self): return self.attack_kind @abstractmethod def generate_gold(self): pass
class Monster(Character): @classmethod def get_monster_kind(cls): return cls.__name__.replace('Monster', '') def __init__(self, name='Monster', hp=50, power=5): super().__init__(name, hp, power) self.name = self.get_monster_kind() + name self.attack_kind = AttackKindFactory(self.get_monster_kind()) # AttackKindFactory가 하는 일? 역할은 무엇인가? 중요성을 느끼는 게 중요! # 얻을 수 있는 이점은?? # other = 플레이어 def attack(self, other, a_kind): if a_kind == self.attack_kind.get_kind(): # 다른 객체의 상태 정보를 변경할 때 # message passing other.get_damage(self.power, a_kind) self.attack_kind.attack() # 데미지를 입는다 def get_damage(self, power, a_kind): if a_kind == self.attack_kind.get_kind(): self.hp += power else: self.hp -= power def get_attack_kind(self): return self.attack_kind.get_kind() @abstractmethod def generate_gold(self): pass
def attack(self, other, a_kind): if a_kind in self.skills: # 다른 객체의 상태 정보를 변경할 때 # message passing other.get_damage(self.power, a_kind) # 이러한 방식은 객체를 계속 생성하므로 # 리소스를 많이 잡아먹음 AttackKindFactory(a_kind).attack()
def __init__(self, name='Monster', hp=50, power=5): super().__init__(name, hp, power) self.name = self.get_monster_kind() + name self.attack_kind = AttackKindFactory(self.get_monster_kind())
def attack(self, other, a_kind): if a_kind == self.attack_kind: other.get_damage(self.power, a_kind) AttackKindFactory(a_kind).attack()
from attack_kind import AttackKindFactory from Character import Player from monsters import MonstersFactory if __name__ == "__main__": fm = MonstersFactory("Fire") im = MonstersFactory("Ice") sm = MonstersFactory("Stone") kfm = MonstersFactory("Kungfu") monsters = [] monsters.extend((fm, im, sm, kfm)) player = Player('john', 120, 20, AttackKindFactory("Fire"), AttackKindFactory("Ice")) print(player) for mon in monsters: player.attack(mon, 'Fire') for mon in monsters: print(mon) print() print("Monster Attack!") for mon in monsters: print(mon.get_attack_kind()) mon.attack(player, mon.get_attack_kind()) print()