A Class Without Class

Well in this entry I am going go to the next step and this time enter a 'Race' into the instantiation arguments of my Creator and it should then give me a list of 'Player Classes' that are available to the entered Ability rolls and Race selection.

Of course I will still have to the same checks for 'Grot' and 'Race' as I do not want someone to sneak something in by simply adding a 'Race' to the mix.

0227_LARP-592x323.jpg

So like my LARP party above lets start with a new code in my 'Creator' class's 'BEGIN';


  if (exists($attr->{race})){
     my $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));
     $self->race($check_race);	
   }
   else {        
     foreach my $race ($finder->modules()){
          my $check_race = $race->new();
          next 
               unless $check_race->can_be($attr);
           push($self->allowed_races(),$check_race);
    	
       }
   }
So I first check to see if I have 'race' in my attr HashRef. If it is there, I then use 'Module::PluginFinder' to look for the race and if not found I throw an exception. If I have that race I then create a new instance of it, recheck with 'can_be' to make sure this set of rolls can still be the passed in race and finally I just set my race attribute new instantiated race.

If there is no 'race' in my attr HashRef I simply use the same code form the other post o produce the available races.

Now the other part of the story. In each of my 'Race' classes I have added a dummy sub so I can find them with 'Module::PluginFinder' so my 'Dwarf' class looks like this now;


package RPG::ADD::Creator::Race::Dwarf;
 use Moose;
 extends 'RPG::ADD::Creator::Race';
 with 'RPG::ADD::Character::Race::Dwarf::MaxLevel';
 
has '+ability_mins' =>(
	default =>sub{{strength=>8,constitution=>12}},
);

sub Dwarf { return 1;}
Notice as well I added in a new role called 'MaxLevel' it is like this

package RPG::ADD::Character::Race::Dwarf::MaxLevel;
use Moose::Role;
requires 'is_pc';

sub allowed_classes {
	my $self = shift;
	my $classes  = {Fighter=>9,Thief=>'U',Assassin=>9 };
	return $classes
		if ($self->is_pc());
	
	$classes->{Cleric} = 8;
	
	return $classes
}

It has, for now, just one sub that returns a 'HashRef' of the maximum 'Player Class level' for the Dwarf race. I have also set it up so I can get both the 'PC' and the 'NPC' max levels as well so I can use this bit of code when generating 'NPC'. Now because I can use it in more than one place I have moved it under a different Namespace but thankfully moose can easily find it.

I also added into my 'Race' class an attribute for 'is_pc' so I can use this role as it is a required part of the role.

So now in my Character Class I can just do this $self->race()->allowed_classes and I will get my set of allowed classes.

Wait! Maybe I can finally try and use delegation to tidy that up. So I just change my 'race' Attribute a little like this


has 'race' =>(
	is		=>'rw',
	isa		=>'RPG::ADD::Creator::Race',
	init_arg => undef,
	handles => {
          allowed_classes => 'allowed_classes',
   },

and when I run it like this;


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

print "allowd Classes= ".Dumper($str->allowed_classes);

I get;


allowd Classes= $VAR1 = {
          'Assassin' => 9,
          'Thief' => 'U',
          'Fighter' => 9
        };

Great. Well not 100% seems I forgot to check if these classes are allowed with the ability scores. I know he or she can be an Thief or Fighter but not an Assassin as that requires both higher intelligence and dexterity.

So I guess I have my topic for my 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