LasTres/lib/LasTres/TalkingNPC.pm

188 lines
3.9 KiB
Perl

package LasTres::TalkingNPC;
use v5.36.0;
use strict;
use warnings;
use utf8;
use feature 'signatures';
use Moo::Role;
requires qw/identifier name/;
## IMPLEMENTORS MUST IMPLEMENT
# sub identifier;
#
# Identifier must be unique across
# npcs, failure to do so may
# result in a runtime error and
# is not supported.
#
# sub name($self, $pj);
#
## IMPLEMENTORS MUST EXTEND
# sub talk($self,$pj,$word = undef);
## DO NOT EXTEND NOT SUPPORTED.
sub hash ($self) {
return {
identifier => $self->identifier,
name => $self->name,
(
( defined $self->icon )
? ( icon => $self->icon )
: ()
),
};
}
## OVERRIDE
sub icon {
return;
}
## OVERRIDE
sub color ( $self, $pj ) {
return 'blue';
}
## OVERRIDE
# Refer to show_wordlessly_talk_started for
# detail about when it is convenient to
# override.
sub show_told_word ( $self, $pj, $word ) {
my $team = $pj->team;
$team->append_log_line(
[
{
text => $pj->nick,
color => 'green',
},
{
text => ' dijo "',
},
{
text => $word->name,
color => 'purple',
},
{
text => '" a ',
},
{
text => $self->name($pj),
color => $self->color($pj),
},
{
text => '.',
},
]
);
}
## OVERRIDE
# Override this if you want a special text
# on the dialog start.
# This is useful when the pj is talking
# with a not talk capable life form.
# For example a for a dog this function
# could be.
# sub show_wordlessly_talk_started($self, $pj) {
# my $team = $pj->team;
# $team->append_log_line(
# [
# {
# text => $self->name($pj),
# color => $self->color($pj),
# },
# {
# text => ' ladra a ',
#
# },
# {
# text => $pj,
# color => 'green',
# },
# {
# text => '.',
# },
# ]
# );
# }
sub show_wordlessly_talk_started ( $self, $pj ) {
my $team = $pj->team;
$team->append_log_line(
[
{
text => $pj,
color => 'green',
},
{
text => ' habló con ',
},
{
text => $self->name($pj),
color => $self->color($pj),
},
{
text => '.',
},
]
);
}
## OVERRIDE (Always use $self->SUPER::talk.)
# You should override with whatever is going to be said
# by the npc using send_response_dialog($self,$pj,$array_text)
# and.- if you want.- with what happens on talk with
# the npc that is not a dialog with the common
# append_log_line, is polite to send all
# the team members the contents of
# the conversation so they get context about
# what their partners need to do.
sub talk ( $self, $pj, $word = undef ) {
$pj = $pj->get_from_storage;
my $team = $pj->team;
if ( defined $word ) {
$self->show_told_word( $pj, $word );
return;
}
}
## OVERRIDE
sub verb ( $self, $pj ) {
return 'dice';
}
## DO NOT EXTEND NOT SUPPORTED.
sub send_response_dialog ( $self, $pj, $array_text ) {
$pj = $pj->get_from_storage;
my $team = $pj->team;
if ( !ref $array_text eq 'ARRAY' ) {
die '$array_text should be an array.';
}
$team->append_log_line(
[
{ text => $self->name($pj), color => $self->color($pj) },
{ text => " @{[$self->verb($pj)]}.- " },
@$array_text
]
);
}
## DO NOT EXTEND NOT SUPPORTED.
{
my %hash;
sub instance ($class) {
if ( !exists $hash{$class} ) {
$hash{$class} = $class->new;
}
return $hash{$class};
}
}
1;