Lines 100.00% 19 / 19
Functions and Methods 100.00% 9 / 9
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Vitals 100.00% 19 / 19 100.00% 9 / 9 10 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 alive 100.00% 1 / 1 100.00% 1 / 1 1
 hurt 100.00% 2 / 2 100.00% 1 / 1 1
 heal 100.00% 3 / 3 100.00% 1 / 1 1
 spend 100.00% 2 / 2 100.00% 1 / 1 1
 regain 100.00% 3 / 3 100.00% 1 / 1 1
 die 100.00% 1 / 1 100.00% 1 / 1 1
 revive 100.00% 4 / 4 100.00% 1 / 1 2
 recount 100.00% 2 / 2 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Core\Game\Attribute;
6
7use Lorekeeper\Core\Support\Gauge;
8
9final class Vitals
10{
11    /**
12     * @param Gauge<Health> $health
13     * @param Gauge<Mana> $mana
14     */
15    public function __construct(
16        public private(set) Gauge $health,
17        public private(set) Gauge $mana,
18    ) {}
19
20    public function alive(): bool
21    {
22        return !$this->health->empty();
23    }
24
25    public function hurt(Health $damage): void
26    {
27        $left = max(0, $this->health->current->value - $damage->value);
28        $this->health = new Gauge(new Health($left), $this->health->maximum);
29    }
30
31    public function heal(Health $amount): void
32    {
33        $maximum = $this->health->maximum;
34        $healed = min($maximum->value, $this->health->current->value + $amount->value);
35        $this->health = new Gauge(new Health($healed), $maximum);
36    }
37
38    public function spend(Mana $cost): void
39    {
40        $left = max(0, $this->mana->current->value - $cost->value);
41        $this->mana = new Gauge(new Mana($left), $this->mana->maximum);
42    }
43
44    public function regain(Mana $amount): void
45    {
46        $maximum = $this->mana->maximum;
47        $regained = min($maximum->value, $this->mana->current->value + $amount->value);
48        $this->mana = new Gauge(new Mana($regained), $maximum);
49    }
50
51    public function die(): void
52    {
53        $this->health = new Gauge(new Health(0), $this->health->maximum);
54    }
55
56    public function revive(): void
57    {
58        if ($this->alive()) {
59            return;
60        }
61
62        $this->health = new Gauge($this->health->maximum, $this->health->maximum);
63        $this->mana = new Gauge($this->mana->maximum, $this->mana->maximum);
64    }
65
66    /**
67     * @param Gauge<Health> $health
68     * @param Gauge<Mana> $mana
69     */
70    public function recount(Gauge $health, Gauge $mana): void
71    {
72        $this->health = $health;
73        $this->mana = $mana;
74    }
75}