I forgot to commit the created actions.

This commit is contained in:
Sergiotarxz 2023-06-29 09:06:58 +02:00
parent 80ae5ddb39
commit 48874c57c1
4 changed files with 285 additions and 0 deletions

57
lib/LasTres/PJAction.pm Normal file
View File

@ -0,0 +1,57 @@
package LasTres::PJAction;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use Moo::Role;
requires( 'identifier', 'callback', 'name' );
## OVERRIDE
# This should be a square icon, ideally of 400x400px
# If not set the frontend should attempt to show the
# action with as much dignity as possible.
#
# Should return nothing or a string that is a absolute url
# to the resource.
sub icon ( $self, $pj ) {
return;
}
## OVERRIDE
# This function is called when a user is supposed
# to see that something is possible to be done, but they cannot because
# some prerequisite is missing.
# If you do not want the possible action to be showed do not list it,
# do not use this function in this case.
#
# Should return a true or falsy value that is not undef.
sub is_disabled ( $self, $pj ) {
return 0;
}
## OVERRIDE
# This function should return a text explanation about why
# the pj cannot use this action but they still see it as
# something possible to be done.
#
# Should return nothing if the pj can do the action and a string
# with the reason otherwise.
sub disabled_reason ( $self, $pj ) {
return;
}
## DO NOT EXTEND NOT SUPPORTED.
sub hash ( $self, $pj ) {
return {
identifier => $self->identifier,
icon => $self->icon,
name => $self->name,
is_disabled => $self->is_disabled,
disabled_reason => $self->disabled_reason,
};
}
1;

View File

@ -0,0 +1,84 @@
package LasTres::PJAction::DefaultHeal;
use v5.36.0;
use strict;
use warnings;
use Moo;
with 'LasTres::PJAction';
## OVERRIDE
sub name {
return 'Dormir aquí.';
}
## OVERRIDE
sub identifier {
return 'dormir';
}
## OVERRIDE (Always use $self->SUPER::is_disabled.)
sub is_disabled($self, $pj) {
my $team = $pj->team;
if (!$team->leader->uuid eq $pj->uuid) {
return 1;
}
if (!$self->_check_if_someone_needs_to_restore($pj)) {
return 1;
}
return 0;
}
## OVERRIDE (Always use $self->SUPER::disabled_reason($self, $pj).)
sub disabled_reason($self, $pj) {
my $team = $pj->team;
if (!$team->leader->uuid eq $pj->uuid) {
return 'No eres el líder del equipo.';
}
if (!$self->_check_if_someone_needs_to_restore($pj)) {
return 'Todo tu equipo esta lleno de vitalidad y listo para la aventura.';
}
return;
}
sub _check_if_someone_needs_to_restore($self, $pj) {
my $team = $pj->team;
for my $member ($team->combat_members->@*) {
if ($member->health < $member->max_health) {
return 1;
}
if ($member->mana < $member->max_mana) {
return 1;
}
}
return 0;
}
## OVERRIDE (Always use $self->SUPER::callback($self, $pj).)
# Possibly for all the healing actions you would want to
# extend this Action, if you want something special in the callback
# remember to call super.
sub callback($self, $pj) {
my $team = $pj->team;
for my $member ($team->combat_members->@*) {
$member->health($member->max_health);
$member->mana($member->max_mana);
$member->update;
$member->update_team_sprites;
}
if (Scalar::Util::blessed($self) eq __PACKAGE__) {
$self->_callback_unique_sleep($pj);
}
}
sub _callback_unique_sleep($self, $pj) {
my $team = $pj->team;
$team->append_log_line([
{
text => 'Tras dormir os sentís mucho mejor, a por más aventuras.'
}
]);
}
1;

View File

@ -0,0 +1,102 @@
package LasTres::Action::GolpearArbolCentralTribuDeLaLima;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use Moo;
with 'LasTres::PJAction';
## DO NOT EXTEND NOT SUPPORTED.
sub name {
return 'Golpear el árbol.';
}
sub identifier {
return 'golpear_arbol_central_tribu_de_la_lima';
}
sub is_disabled ( $self, $pj ) {
if ($pj->health < 1) {
return 1;
}
return 0;
}
sub disabled_reason ( $self, $pj ) {
if ($pj->health < 1) {
return "Estás debilitado.";
}
return;
}
sub _chance_enemy ( $self, $pj ) {
if ( $pj->level > 20 ) {
return 0;
}
if ( $pj->level > 10 ) {
return 25;
}
return 50;
}
sub callback ( $self, $pj ) {
$pj = $pj->get_from_storage;
my $damage_for_hitting_the_tree = int( $pj->health / 10 );
my $current_health = int( $pj->health - $damage_for_hitting_the_tree );
# We do not let the player die. That would be problematic out of combat.
$pj->health($current_health);
$pj->update;
my $team = $pj->team;
$team->append_log_line(
[
{
text => $pj->nick,
color => 'green',
},
{
text => ' golpeó el árbol y se hizo '
. $damage_for_hitting_the_tree
. ' de daño. '
. $pj->health . '/'
. $pj->max_health . '.'
}
]
);
if ($team->is_defeated) {
return;
}
if ( LasTres::Util::rand_range_int( 0, 100 ) <= $self->_chance_enemy($pj) )
{
$self->_start_battle($pj);
return;
}
}
sub _start_battle ( $self, $pj ) {
require LasTres::EnemyArea;
require LasTres::Battle;
require LasTres::Util;
my $team = $pj->team;
$team->append_log_line([
{
text => 'Una criatura que vive en el árbol se ha molestado, preparate para luchar...'
}
]);
my $battle = LasTres::Battle->start_battle_machine($team,
[
LasTres::EnemyArea->new(
race => 'conejo',
nick => 'Conejo territorial.',
level => LasTres::Util::rand_range_int(1,3),
)
]
);
}
1;

42
lib/LasTres/PJActions.pm Normal file
View File

@ -0,0 +1,42 @@
package LasTres::PJActions;
use v5.36.0;
use strict;
use warnings;
use Moo;
use Module::Pluggable search_path => ['LasTres::PJAction'],
instantiate => 'new',
on_require_error => sub ($plugin, $error) {
die $error;
};
has hash => (
is => 'rw',
lazy => 1,
builder => \&_build_hash,
);
sub _build_hash($self) {
my @actions = $self->plugins();
my %hash;
for my $action (@actions) {
my $set_success = 0;
if (exists $hash{$action->identifier}) {
warn "$action could not be added because a action with the "
. "identifier @{[$action->identifier]} already exists.";
}
eval {
if ($action->does('LasTres::PJAction')) {
$hash{$action->identifier} = $action;
$set_success = 1;
}
};
if (!$set_success) {
warn "$action does not implement the role LasTres::PJAction.";
}
}
return \%hash;
}
1;