Keep Calm, Roll on.

In my last post I left off with asking myself a whole lot of design questions after I decided to centralize most if not all of my 'Game/Business Rules' into 'Roles' under the logical namespaces. In the end I can just live with what I came up with even if there is some duplication of code I don't think I am in anti-pattern land yet.

kkrac.jpg

Well now I think I will get back on track from where I left on with this post and that is filtering out 'Player Classes' once I select a 'Race'. I had the basics done but now there are a few more little things to add in and we will start with a cool one racial adjustment.

Some races get an adjustment to their ability scores and these adjustments are used when selecting a 'Class' So an 'Elf' with '8' dexterity roll get a +1 so they will be able to become a thief.

There are a few sticky little problems with this adjustment. Thought the set is small, only 4 of the races have an adjustment, I could easily just add that into my 'Creator' class as a 'adjust_for_race' sub that adjusts the scores. However I really really want this to be modular so I will not have to add in a new code in my creator if I can avoid it.

I could add it in as a 'Role' like I did for the 'AbilityMins' but the problem is I will have to make dummy roles for those 'Races' that have no adjustments so I am coding needlessly. I also have the problem that I have to change the 'Ability' values in the 'Creator' class not the 'Race' class hmm.

Well to follow my pattern from before I create a new 'Role' like this


package RPG::ADD::Race::Dwarf::AbilityAdjustment;
use 5.010;
use Moose::Role;

sub ability_adjustment {
my $self = shift;
return {constitution=>1,charisma=>-1};
}


and then add that to my 'Dwarf' class

package RPG::ADD::Creator::Race::Dwarf;
use Moose;
extends 'RPG::ADD::Creator::Race';
with (qw( RPG::ADD::Race::Dwarf::MaxLevel
RPG::ADD::Race::Dwarf::AbilityMins
RPG::ADD::Race::Dwarf::AbilityAdjustment
));

I could then use it in my 'Creator' before I test for the available 'Player Class', however some of my 'Races' may not have that role I can test for that with Moose using 'dose' like this

my $check_race = $race->new();
if ($check_race->does('AbilityAdjustment')){
...

However this somewhat unworkable as I have to use the full namespace like this

my $check_race = $race->new();
if ($check_race->does('RPG::ADD::Race::Dwarf::AbilityAdjustment')){
...

So back to the drawing board for that. I guest I could have have only one 'AbilityAdjustment' role like this

package RPG::ADD::Race::AbilityAdjustment;
use 5.010;
use Moose::Role;

sub ability_adjustment {
my $self = shift;
...


However, that is going to break my modularity and if I am going to do that why not just do it with a sub in 'Creator'.

Well when all else fails fall back to good old perl I can just use 'can' so I now tried this


if ($check_race->can('ability_adjustment')) {
foreach my $ability (keys($check_race->ability_adjustment())){
my $new_ability = $self->$ability();
$new_ability+=$check_race->ability_adjustment()->{$ability};
$self->$ability($new_ability);
}
}

$self->race($check_race);

and it works

my $str = RPG::ADD::Creator->new({race=>'Dwarf',strength=>9,
dexterity=>17,
constitution=>12,
charisma=>9,
intelligence=>8,
wisdom=>9,
});

print "constitution roll=".$str->Constitution->roll()."\n";
print "constitution=".$str->constitution()."\n";

constitution roll=12
constitution=13


Of course you are remembering from this post here that I want to keep my original rolls for later use.

I still haven't gotten to my state goal of limiting 'Player Class' by 'Race' and 'Ability Rolls' Well maybe next post.

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