A Character with to much class.

Well in my last post I found the usefulness of builders when using roles and I quickly found another opportunity to use them about 10 minutes after I finished the post.

Seems I forgot that multi-classed characters are special when it comes to Hit Points (HP).

gandalf_multiclass.jpg


The rule for their HPs are;

'Roll the appropriate die for each class, add in any constitution bonus, divide by the number of classes rounding up.'

So I now need some sort of way to express this and there are a few things to consider. I could stick it in the '_build_hps' sub of my 'Creator' class and be done with it but then I would have to check somehow if the Character is mutil-class but then I would have to do the same sort of 'if else tree' in my character class if there was only some way it could be one or the other.

Well this is where Moose roles come in to play again. I have a 'Player Character' and it can be in only 'one' of three states of 'Player Class', Single, Dual and Multi-Class so why not make three roles that have the same methods but different implementations??

So after a little more rejigging this is what I came up with for the Multi-Class Character.


package RPG::ADD::Class::Multi;
use Moose::Role;
with (qw(RPG::ADD::Attribute::HitPoints ));

sub _build_hps {
use RPG::ADD::Dice;
my $self = shift;
return $self->current_hps()
if ($self->current_hps());
my $hps = 0;
foreach my $player_ class (keys($self->class()){
my $throw = RPG::ADD::Dice->new($player_class->hit_dice+"D".$player_class->die);
$hps+= $throw->roll();
}

$hps = int(($hps+$self->hp_bonus())/scalar(keys($self->class()))+.5);
$hps = 1
if ($hps<1);
$self->current_hit_points($hps);
return $self->current_hit_points();
}

Now I am going to have to do a good deal of other changes as now my 'player class' attribute is expecting an object of some kind and fortunately before I went off and created yet another role I noticed that the above will work with both multi and single 'Player Class' character.

Oh well at least I stopped before I went too far so I think I will just drop this role and but it back as sub in my 'Creator Class' as it works for both as the '_build_hps' in character will work fine as well in either case.

I even spotted a fault in my '_build_hps' sub the other post. It seems that I do not need to record the individual die rolls of 'Classes' that start with more than 1 die, just the sum of the two.

Oh well live and learn.

tumblr_mktgspSVSp1s2v2ceo1_500.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations