Completed
Push — master ( 29e2cf...033bf5 )
by Jakub
02:40
created

Attack   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 20
dl 0
loc 37
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldUse() 0 2 1
A getName() 0 2 1
A getPriority() 0 2 1
A do() 0 19 3
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\CombatLogEntry;
9
use Nexendrie\Utils\Numbers;
10
11 1
final class Attack implements ICombatAction {
12
  public function getName(): string {
13 1
    return CombatLogEntry::ACTION_ATTACK;
14
  }
15
16
  public function getPriority(): int {
17 1
    return 0;
18
  }
19
20
  public function shouldUse(CombatBase $combat, Character $character): bool {
21 1
    return true;
22
  }
23
24
  /**
25
   * Do an attack
26
   * Hit chance = Attacker's hit - Defender's dodge, but at least 15%
27
   * Damage = Attacker's damage - defender's defense
28
   */
29
  public function do(CombatBase $combat, Character $character): void {
30 1
    $result = [];
31
    /** @var Character $defender */
32 1
    $defender = $combat->selectAttackTarget($character);
33 1
    $result["result"] = $combat->successCalculator->hasHit($character, $defender);
34 1
    $result["amount"] = 0;
35 1
    if($result["result"]) {
36 1
      $amount = $character->damage - $defender->defense;
37 1
      $result["amount"] = Numbers::range($amount, 0, $defender->hitpoints);
38
    }
39 1
    if($result["amount"] > 0) {
40 1
      $defender->harm($result["amount"]);
41
    }
42 1
    $result["action"] = CombatLogEntry::ACTION_ATTACK;
43 1
    $result["name"] = "";
44 1
    $result["character1"] = $character;
45 1
    $result["character2"] = $defender;
46 1
    $combat->logDamage($character, $result["amount"]);
47 1
    $combat->log->log($result);
48 1
  }
49
}
50
?>