-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.php
More file actions
117 lines (91 loc) · 3.13 KB
/
Unit.php
File metadata and controls
117 lines (91 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Created by JetBrains PhpStorm.
* User: kogtev_k
* Date: 07.06.13
* Time: 12:59
* To change this template use File | Settings | File Templates.
*/
abstract class Unit {
private $name; //имя юнита
private $health; //здоровье юнита
private $health_with_armor; //здоровье юнита для боя, изменяется при защите
private $weight; //перевозимый груз
private $weapon;
private $armor=array();
function __construct($name, $health, $weight, weapon $weapon=null, array $armors=null) {
$this->name = $name;
$this->health = $health;
$this->weight = $weight;
if (isset($weapon))
$this->weapon= $weapon;
else
$this->weapon= new weapon();
if (isset($armors))
$this->giveArmors($armors);
}
function __toString() {
return "Hi, I'm a $this->name. My health - $this->health($this->health_with_armor), i can transit - $this->weight. Weapon - ".$this->weapon->action().", armor - ".$this->allArmorStrength()." \r\n";
}
function getName(){
return $this->name;
}
function giveWeapon(weapon $weapon){
if ($this->weapon!=$weapon){
unset($this->weapon);
$this->weapon=$weapon;
}
}
private function existArmor(armor $armor){ //проверяем есть ли уже у данного юнита устанавливаемое оружие
foreach ($this->armor as $exist_armor)
if ($exist_armor->getName()==$armor->getName())
return true;
return false;
}
function giveArmor(armor $armor){
if (!$this->existArmor($armor)){
$this->armor[]=$armor;
return true;
}
return false;
}
function giveArmors(array $armors){
foreach ($armors as $armor)
if (!($armor instanceof armor)) throw new BadMethodCallException;
$this->armor = $armors;
return true;
}
function allArmorStrength() {
$result=0;
foreach ($this->armor as $armor)
$result+=$armor->action();
return $result;
}
protected function getWeapon(){
return $this->weapon;
}
protected function getArmor() {
return $this->armor;
}
function getAttackStrength() {
return $this->weapon->action();
}
function getHealth(){
return $this->health;
}
function getCurrentHealth(){ //текущее здоровье во время боя
return $this->health_with_armor;
}
function prepareForBattle() { //востанавливаем счетчики здоровья для битвы
$this->health_with_armor=$this->health+$this->allArmorStrength(); //изначальное здоровье юниту суммируем с его броней
}
function setDamage($damage) {
$this->health_with_armor-=$damage;
}
}
class supplierUnit extends Unit { //юнит снабженец
}
class warriorUnit extends Unit { //юнит воин
}
class Hero extends warriorUnit {
}