Attack   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
eloc 23
c 3
b 0
f 1
dl 0
loc 46
rs 10
ccs 20
cts 21
cp 0.9524

4 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldUse() 0 3 1
A getName() 0 3 1
A getPriority() 0 3 1
A do() 0 22 4
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat\CombatActions;
5
6
use HeroesofAbenez\Combat\Character;
7
use HeroesofAbenez\Combat\CombatBase;
8
use HeroesofAbenez\Combat\ICombatAction;
9
use Nexendrie\Utils\Numbers;
10
11 1
final class Attack implements ICombatAction
12
{
13
    public const ACTION_NAME = "attack";
14
15
    public function getName(): string
16
    {
17 1
        return self::ACTION_NAME;
18
    }
19
20
    public function getPriority(): int
21
    {
22 1
        return 0;
23
    }
24
25
    public function shouldUse(CombatBase $combat, Character $character): bool
26
    {
27 1
        return true;
28
    }
29
30
    /**
31
     * Do an attack
32
     * Hit chance = Attacker's hit - Defender's dodge, but at least 15%
33
     * Damage = Attacker's damage - defender's defense
34
     */
35
    public function do(CombatBase $combat, Character $character): void
36
    {
37 1
        $result = [];
38 1
        $defender = $combat->selectAttackTarget($character);
39 1
        if ($defender === null) {
40
            return;
41
        }
42 1
        $result["result"] = $combat->successCalculator->hasHit($character, $defender);
43 1
        $result["amount"] = 0;
44 1
        if ($result["result"]) {
45 1
            $amount = $character->damage - $defender->defense;
46 1
            $result["amount"] = Numbers::range($amount, 0, $defender->hitpoints);
47
        }
48 1
        if ($result["amount"] > 0) {
49 1
            $defender->harm($result["amount"]);
50
        }
51 1
        $result["action"] = $this->getName();
52 1
        $result["name"] = "";
53 1
        $result["character1"] = $character;
54 1
        $result["character2"] = $defender;
55 1
        $combat->logDamage($character, $result["amount"]);
56 1
        $combat->log->log($result);
57 1
    }
58
}
59