Yeah, I concur with McA. Create a new class for each type of enemy.

package Enemy { use Moo::Role; has wounds => (is => 'ro'); ...; } # Look how tiny these classes are. # Creating lots of classes doesn't have to be painful. package Rat { use Moo; with 'Enemy' } package Cat { use Moo; with 'Enemy' }

Also, please don't use Moos for anything serious. I'm an officially listed as a co-author of it, so I'm entitled to say that. ;-)

Use Moo, or Moose, or Moops. Here's my above example using Moops, showing how it eliminates a lot of boiler-plate code.

use Moops; role Enemy { has wounds => (is => 'ro', isa => Int); } class Rat with Enemy; class Cat with Enemy;

Let's extend that a little with some attack types:

use Moops; class Attack { has name => (is => 'ro', isa => Str); has damage => (is => 'ro', isa => Int); method light ($class: ) { state $me = $class->new( name => 'light attack', damage => 1 ); return $me; } method heavy ($class: ) { state $me = $class->new( name => 'heavy attack', damage => 3 ); return $me; } } role Enemy { requires "get_attack"; has wounds => (is => 'ro'); method is_weak () { $self->wounds > 5 } } class Rat with Enemy { method get_attack (Object $target) { return Attack->light; } } class Cat with Enemy { has lives => (is => 'rw', isa => Int, default => 9); method get_attack (Object $target) { return Attack->heavy; } around is_weak () { return false if $self->lives > 2; return $self->$next(@_); } } class Monkey with Enemy { method get_attack (Object $target) { $target->is_weak || $self->is_weak ? Attack->light : Attack->heavy +; } } class Ninja with Enemy { method get_attack (Object $target) { return Attack->new(name => 'ninja attack', damage => int rand(5)); } } class Priest with Enemy { method get_attack (Object $target) { $target->is_sinful ? Attack->heavy : Attack->light; } }

That kind of thing.


In reply to Re: Game related OO design question by tobyink
in thread 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.