90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Services\Game;
|
||
|
|
|
||
|
|
use App\Models\Character;
|
||
|
|
|
||
|
|
class EnergyService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Consume energy for an action
|
||
|
|
*/
|
||
|
|
public function consume(Character $character, int $amount, string $action): bool
|
||
|
|
{
|
||
|
|
if (!$character->canAct()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$character->hasEnergy($amount)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $character->consumeEnergy($amount);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Regenerate energy for a character
|
||
|
|
*/
|
||
|
|
public function regenerate(Character $character): void
|
||
|
|
{
|
||
|
|
$regenRate = config('game.energy.regen_rate', 5);
|
||
|
|
$maxEnergy = config('game.energy.max', 100);
|
||
|
|
|
||
|
|
// Ensure character has max_energy set
|
||
|
|
if ($character->max_energy <= 0) {
|
||
|
|
$character->max_energy = $maxEnergy;
|
||
|
|
}
|
||
|
|
|
||
|
|
$character->addEnergy($regenRate);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the energy regen rate
|
||
|
|
*/
|
||
|
|
public function getRegenRate(): int
|
||
|
|
{
|
||
|
|
return config('game.energy.regen_rate', 5);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the energy regen interval in seconds
|
||
|
|
*/
|
||
|
|
public function getRegenInterval(): int
|
||
|
|
{
|
||
|
|
return config('game.energy.regen_interval', 300);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if character has enough energy for an action type
|
||
|
|
*/
|
||
|
|
public function hasEnoughFor(Character $character, string $actionType): bool
|
||
|
|
{
|
||
|
|
$cost = config("game.energy.action_costs.{$actionType}", config('game.energy.action_costs.default', 10));
|
||
|
|
|
||
|
|
return $character->hasEnergy($cost);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the energy cost for an action type
|
||
|
|
*/
|
||
|
|
public function getCostFor(string $actionType): int
|
||
|
|
{
|
||
|
|
return config("game.energy.action_costs.{$actionType}", config('game.energy.action_costs.default', 10));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Restore all energy for a character
|
||
|
|
*/
|
||
|
|
public function restoreAll(Character $character): void
|
||
|
|
{
|
||
|
|
$maxEnergy = config('game.energy.max', 100);
|
||
|
|
|
||
|
|
if ($character->max_energy <= 0) {
|
||
|
|
$character->max_energy = $maxEnergy;
|
||
|
|
}
|
||
|
|
|
||
|
|
$character->energy = $character->max_energy;
|
||
|
|
$character->save();
|
||
|
|
}
|
||
|
|
}
|