Back on the Moose Track

Well with my last post I managed to get a little closer to my original goal of selecting a 'Character Class' after I have selected a 'Race' as the image below explains

add.jpg

I sometimes have a Hobbit (well Halfling in A D&D) of doing that.

Well in this post I centralized all of the 'Race' rules under one namespace and so I though I might as well do that for 'Character Class' rules as well.

So for each of my 'Character Classes' I could just add a new role like this


package RPG::ADD::Class::Fighter::MinAbilities;
use Moose::Role;
sub ability_mins {
	my $self = shift;
	return {strength=>9,constitution=>11};
}

and then just like before I added in a 'Class' class and some 'Character Classes' like this



package RPG::ADD::Creator::Class;
use Moose;

sub can_be{
my $self = shift;
my ($attr) = @_;

foreach my $ability (keys($self->ability_mins())){
return 0
if ($attr->{$ability} < $self->ability_mins()->{$ability});

}
return 1;
}



package RPG::ADD::Creator::Class::Fighter;

use lib "D:/blogs/lib";
extends 'RPG::ADD::Creator::Class';
with 'RPG::ADD::Class::Fighter::MinAbilities';

sub Fighter { return 1;}

and then use much the same code I used for 'Race' so in the end I have the following in my 'Creator' BUILD sub like this


    my $race_finder = Module::PluginFinder->new(
                 search_path => 'RPG::ADD::Creator::Race',

filter => sub {
my ( $module, $searchkey ) = @_;
$module->can( $searchkey );
},
);

my $class_finder = Module::PluginFinder->new(
search_path => 'RPG::ADD::Creator::Class',

filter => sub {
my ( $module, $searchkey ) = @_;
$module->can( $searchkey );
},
);

if (exists($attr->{race})){

my $race = $race_finder->find_module( $attr->{race} );
throw_exception( "NeitherRoleNorRoleNameIsGiven", {message=>"There is no such thing as a ".$attr->{race}." Player Charater"})
unless($race);

my $check_race = $race->new();

throw_exception( "NeitherRoleNorRoleNameIsGiven", {message=>"This set of ability rolls cannot be a ".$attr->{race} })
unless($check_race->can_be($attr));

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};
$attr->{$ability} = $new_ability;
$self->$ability($new_ability);
}
}
$self->race($check_race);
foreach my $allowed_class (keys($self->allowed_classes())){
my $class = $class_finder->find_module($allowed_class );
my $check_class = $class->new();
next
unless $check_class->can_be($attr);
push($self->available_classes(),$allowed_class);
}
}
else {
...

and a new attribute for 'available_classes'



has 'available_classes' =>(
is =>'ro',
isa =>'ArrayRef',
default =>sub{return []},
);

and in the end it works


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

print "allowed Classes= ".Dumper($str->allowed_classes);
print "available Classes=".Dumper($str->available_classes());

allowed Classes= $VAR1 = {
'Assassin' => 9,
'Thief' => 'U',
'Fighter' => 9
};
available Classes=$VAR1 = [
'Thief',
'Fighter'
];

Well I think I have some refactoring to do.

6a0120a85dcdae970b012877707a45970c.png

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