On Fri, 10 Feb 2012 18:07:58 +0200
ynon perek <[email protected]> wrote:

> Is it also possible to remove a role from an instance ?

While it would seemingly make sense to (ab)use the roles/traits system
for something like this, it probably is a better idea to have an
attribute that contains a list of abilities that the instance could use
to determine its actions.

has abilities => (
  is => 'ro',
  traits => [qw/Hash/],
  isa => 'HashRef',
  default => sub { +{} },
  handles => {
    has_ability => 'exists',
    get_ability => 'get',
    set_ability => 'set',
  }
);

...

sub deal_damage {
  my ($self) = @_;
  return $self->base_damage + ($self->has_ability('Strong') ?
    $self->get_ability : 0);
}

# attack
$badguy->set_ability('Strong' => 5);
 
$target->receive_damage($badguy->deal_damage);


Doing it this way also makes it easier to serialize (like making save
file).


-- 

Nicholas Perez
XMPP/Email: [email protected]
http://search.cpan.org/~nperez/
http://github.com/nperez

Reply via email to