I'm playing around with a game concept. Here is my basic enemy.

package Enemy; use Moos; has wounds => (); has att_type => (); has def_type => (); sub BUILDARGS { my ($self, $type) = @_; my %monster = ( 'Rat' => { wounds => 1, att_type => 'light', def_type => 'dodge', }, ); die "Unknown monster type" unless exists $monster{$type}; $monster{$type}{'type'} = $type; return $monster{$type}; } 1; } my $monster = Enemy->new('Rat');

I'm having some trouble with the design of the attack / defense traits of an enemy. I would like to have multiple attacks with some statistics of their own. Something like this:

my %attack = ( basic_light => { type => 'light', damage => '1', defmod => { block + => '0', dodge => '0' } }, basic_heavy => { type => 'heavy', damage => '2', defmod => { block + => '0', dodge => '10' } }, );

During the fight, I will call something like:

$a = $attacker->choose_attack(); # with the intention of # $a == { type => 'light', damage => '1', defmod => { block => '0', do +dge => '0' } }

Still, I don't want to pass anything too complex to the constructor. I think that I have to keep the attack definitions within the Enemy class, and to have something to translate the attack type to a pattern.

For example: att_type => 'medium' would translate to an array of ( $attack{basic_light}, $attack{basic_heavy}), and the choose_attack() method would pick an attack from this array at random. Which would correspond to an enemy that hits heavy about 50% of the time.

How can I add it to the code in an elegant way? Should I drop the att_type and just initialize the monster with an attack pattern? I humbly seek your words of wisdom. Any advice will be welcome.

- Luke


In reply to Game related OO design question by blindluke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.