|
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\CharacterAttackSkill; |
|
9
|
|
|
use HeroesofAbenez\Combat\SkillAttack as Skill; |
|
10
|
|
|
use HeroesofAbenez\Combat\NotImplementedException; |
|
11
|
|
|
use Nexendrie\Utils\Numbers; |
|
12
|
|
|
|
|
13
|
1 |
|
final class SkillAttack implements ICombatAction { |
|
14
|
1 |
|
use \Nette\SmartObject; |
|
15
|
|
|
|
|
16
|
|
|
public const ACTION_NAME = "skill_attack"; |
|
17
|
|
|
|
|
18
|
|
|
public function getName(): string { |
|
19
|
1 |
|
return static::ACTION_NAME; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getPriority(): int { |
|
23
|
1 |
|
return 1001; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function shouldUse(CombatBase $combat, Character $character): bool { |
|
27
|
1 |
|
$attackTarget = $combat->selectAttackTarget($character); |
|
28
|
1 |
|
if(is_null($attackTarget)) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
1 |
|
if(count($character->usableSkills) < 1) { |
|
32
|
1 |
|
return false; |
|
33
|
|
|
} |
|
34
|
1 |
|
return ($character->usableSkills[0] instanceof CharacterAttackSkill); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function doSingleAttack(Character $attacker, Character $defender, CharacterAttackSkill $skill, CombatBase $combat): void { |
|
|
|
|
|
|
38
|
1 |
|
$result = []; |
|
39
|
1 |
|
$result["result"] = $combat->successCalculator->hasHit($attacker, $defender, $skill); |
|
40
|
1 |
|
$result["amount"] = 0; |
|
41
|
1 |
|
if($result["result"]) { |
|
42
|
1 |
|
$amount = (int) (($attacker->damage - $defender->defense) / 100 * $skill->damage); |
|
43
|
1 |
|
$result["amount"] = Numbers::range($amount, 0, $defender->hitpoints); |
|
44
|
|
|
} |
|
45
|
1 |
|
if($result["amount"] > 0) { |
|
46
|
1 |
|
$defender->harm($result["amount"]); |
|
47
|
|
|
} |
|
48
|
1 |
|
$result["action"] = $this->getName(); |
|
49
|
1 |
|
$result["name"] = $skill->skill->name; |
|
50
|
1 |
|
$result["character1"] = $attacker; |
|
51
|
1 |
|
$result["character2"] = $defender; |
|
52
|
1 |
|
$combat->logDamage($attacker, $result["amount"]); |
|
53
|
1 |
|
$combat->log->log($result); |
|
54
|
1 |
|
$skill->resetCooldown(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @throws NotImplementedException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function do(CombatBase $combat, Character $character): void { |
|
61
|
|
|
/** @var CharacterAttackSkill $skill */ |
|
62
|
1 |
|
$skill = $character->usableSkills[0]; |
|
63
|
1 |
|
$targets = []; |
|
64
|
|
|
/** @var Character $primaryTarget */ |
|
65
|
1 |
|
$primaryTarget = $combat->selectAttackTarget($character); |
|
66
|
1 |
|
switch($skill->skill->target) { |
|
67
|
1 |
|
case Skill::TARGET_SINGLE: |
|
68
|
1 |
|
$targets[] = $primaryTarget; |
|
69
|
1 |
|
break; |
|
70
|
|
|
case Skill::TARGET_ROW: |
|
71
|
|
|
$targets = $combat->getTeam($primaryTarget)->getItems(["positionRow" => $primaryTarget->positionRow]); |
|
72
|
|
|
break; |
|
73
|
|
|
case Skill::TARGET_COLUMN: |
|
74
|
|
|
$targets = $combat->getTeam($primaryTarget)->getItems(["positionColumn" => $primaryTarget->positionColumn]); |
|
75
|
|
|
break; |
|
76
|
|
|
default: |
|
77
|
|
|
throw new NotImplementedException("Target {$skill->skill->target} for attack skills is not implemented."); |
|
78
|
|
|
} |
|
79
|
|
|
/** @var Character $target */ |
|
80
|
1 |
|
foreach($targets as $target) { |
|
81
|
1 |
|
for($i = 1; $i <= $skill->skill->strikes; $i++) { |
|
82
|
1 |
|
if($target->hitpoints > 0) { |
|
83
|
1 |
|
$this->doSingleAttack($character, $target, $skill, $combat); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
1 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
?> |