LasTres/lib/LasTres/Area.pm
2023-06-26 01:37:23 +02:00

154 lines
3.7 KiB
Perl

package LasTres::Area;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use Moo::Role;
use LasTres::Battle;
use LasTres::EnemyTeam;
use LasTres::Util;
requires qw/identifier locations name parent/;
has children => (
is => 'ro',
lazy => 1,
builder => \&_build_children,
);
## OVERRIDE
sub get_auto_discover ($self) {
return 0;
}
## OVERRIDE
sub frames_to_move ($self) {
return 5;
}
## OVERRIDE
sub frames_to_explore ($self) {
return 7;
}
## OVERRIDE
sub has_movement_auto_combat ($self) {
return 0;
}
## OVERRIDE
sub min_enemies_per_head ($self) {
return 1;
}
## OVERRIDE
sub max_enemies_per_head ($self) {
return 1;
}
## OVERRIDE
sub list_of_enemies ( $self, $pj ) {
return [];
}
## OVERRIDE
sub chance_combat ($self) {
return 50;
}
## OVERRIDE (Always use $self->SUPER::on_move_explore_frame.)
sub on_move_explore_frame ( $self, $team ) {
return if $self->_try_to_start_battle($team);
}
sub _try_to_start_battle ( $self, $team ) {
if ( !$self->has_movement_auto_combat ) {
## Combat not enabled.
return 0;
}
my $dice_result = LasTres::Util::rand_range_int( 0, 100 );
if ( $dice_result > $self->chance_combat ) {
## No combat in this frame by RNG choice.
return 0;
}
## Creating enemy team.
my $battle = LasTres::Battle->new( team1 => $team, last_frame => 0 );
my $team2 = LasTres::EnemyTeam->new( current_battle => $battle->uuid );
my @enemy_members;
$self->_generate_enemies_team($team2, \@enemy_members, $team);
if ( !scalar @enemy_members ) {
## No enemies generated.
return 0;
}
$team2->members( \@enemy_members );
$battle->team2($team2);
$battle->save_redis;
$team->current_battle( $battle->uuid );
my @enemy_log_list;
my $first = 1;
for my $member ($team2->combat_members->@*) {
if (!$first) {
push @enemy_log_list, { text => ', ' };
}
$first = 0;
push @enemy_log_list, {
color => 'red',
text => $member->nick,
};
push @enemy_log_list, {
text => " NIVEL @{[$member->level]} VIDA @{[$member->health]}/@{[$member->max_health]}"
};
}
for my $member ($team->members) {
$member->append_log_line([
{
text => 'Empieza un combate contra: '
},
@enemy_log_list
]);
}
$team->update;
return 1;
}
sub _generate_enemies_team ($self, $enemy_team, $enemy_members_array, $team) {
for my $pj ( $team->combat_members ) {
my $number_of_enemies =
LasTres::Util::rand_range_int( $self->min_enemies_per_head,
$self->max_enemies_per_head );
$self->_generate_n_enemies_pj( $enemy_team, $number_of_enemies, $enemy_members_array, $pj );
}
}
sub _generate_n_enemies_pj ( $self, $enemy_team, $number_of_enemies,
$enemy_members_array, $pj )
{
my @list_possible_enemies = @{ $self->list_of_enemies };
if ( !scalar @list_possible_enemies ) {
warn "$self does not have defined enemies.";
return;
}
for ( my $i = 0 ; $i < $number_of_enemies ; $i++ ) {
my $choice_enemy_index =
LasTres::Util::rand_range_int( 0, $#list_possible_enemies );
my $choice_enemy = $list_possible_enemies[$choice_enemy_index];
push @$enemy_members_array, $choice_enemy->generate($enemy_team);
}
}
## OVERRIDE
sub can_explore {
return 1;
}
sub _build_children ($self) {
my $locations = $self->locations;
my @locations = map { $locations->{$_} } keys %$locations;
@locations = sort { $a->name cmp $b->name } @locations;
return \@locations;
}
1;