Bei einem Versuch, ein komplexeres Kampfsystem zu implementieren kam ich ganz schnell bei dem Wunsch an, Multimethoden zu haben, denn wenn an einem Angriff der Angreifer, der Gegner, eine Waffe, vielleicht noch eine Rüstung, Talente und aktive Zauber beteiligt sind, dann ist eigentlich keines dieser Objekte jetzt ein geeigneter Platz, um da den Kampf-Algorithmus zu implementieren.
Andererseits muss man's auch nicht komplizierter machen als nötig.
Ich wollte mal ein Programm schreiben, dass mir folgenden Dialog erlaubt:
Code: Alles auswählen
Scene: At the border of the Qua'atl jungle [4]
Description:
The dense forest opens to a small clearing.
In the north, a mountain range is visible, but the path that you've
been followed for days outs over a narrow suspension bridge,
their moorings looking all but trustworthy.
Exits:
1. Back to the jungle [3]
2. Over the suspension bridge [5]
Random encounters: Jungle. Triggered.
Your startle a Jaguar on search for prey.
He seems to be hungry and attacks.
Opponents:
1. Jaguar (Health 4/4)
Party (in order of initiative):
1. Thomu (Health 5/5, Bow, behind)
2. Greog (Health 3/3, Sword, front)
3. Adali (Health 2/4, Quarterstaff, behind)
[ask player for actions, then sumarize]
Actions:
- Adali casts "Protection" on Greog
and casts "Strength" on Greog
- Thomu attacks the jaguar with his bow
- Greog does a wild attack on the jaguar with his sword
Combat:
Thomu shoots and misses the jaguar.
The jaguar attacks Greog and inflicts 1 wound
and it attacks Thomu and inflicts 2 wounds.
Greog swings and hits the jaguar, inflicting 2 wounds
and he swings again and misses the jaguar.
Adali casts "Protection" on Greog
and she casts "Strength" on Greog.
Summary:
Opponents:
1. Jaguar (Health 2/4)
Party (in order of initiative):
1. Thomu (Health 3/5, Bow, behind)
2. Greog (Health 2/3, Sword, front)
3. Adali (Health 2/4, Quarterstaff, behind)
Actions:
- Thomu draws his dagger
and he attacks the jaguar
- Greog attacks the jaguar
- Adali casts "Healing" on Thomu
Combat:
Thomu stabs and misses the jaguar.
The jaguar attacks Andali and inflicts 3 wounds.
Adali is deadly wounded and does down.
Greog interrupts his action (automatic action)
and uses a healing potion on Andali (+1 health)
and so on
Als ich das auf deutsch versucht habe, bin ich an den verschiedenen Fällen und Pronomen gestorben. Auf englisch ist es einfacher. Dennoch, abhängig von den hier nur implizit erwähnten Regeln ist es relativ kompliziert, eine Kampfrunde durchzuführen. Ich hatte dafür eine Klasse `Battle` vorgesehen, die sich um alles kümmert:
Code: Alles auswählen
battle = Battle(location=jungle_border, party=[thomu, greog, adali], opponents=[jaguar])
battle.perform()
class Battle:
def perform(self):
self.determine_initiatives()
self.sort_combatants_by_initiative()
for combatant in self.combatants:
self.perform_actions(combatant)
self.print_combat()
self.print_summary()
def perform_actions(self, combatant):
for action in combatant.actions:
action.perform(self)
class AttackAction:
def __init__(self, actor, target, weapon):
self.actor, self.target, self.weapon = actor, target, weapon
def perform(self, battle):
battle.attack(self.actor, self.target, self.weapon)
class CastAction:
def __init__(self, actor, target, spell):
self.actor, self.target, self.spell = actor, target, spell
def perform(self, battle):
battle.cast(self.actor, self.target, self.spell)
class Battle...
def attack(self, actor, target, weapon):
attack_bonus = actor.attack_bonus_against(target) + weapon.attack_bonus_against(target)
defense_bonus = target.defense_bonus_against(weapon)
damage_bonus = weapon.damage_bonus_against(target)
if roll(actor.attack_skill + attack_bonus) > roll(target.defense_skill + defense_bonus):
wounds = roll(weapon.damage + damage_bonus)
self.report.report_attack_hit(actor, target, weapon, wounds)
self.apply_wounds(target, wounds)
else:
self.report.report_attack_miss(actor, target, weapon)
def apply_wounds(target, wounds):
target.health -= wounds
if target.health < 0:
self.report.report_deadly_wound(target)
self.change_action_to_help(target)
and so on
Stefan