-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmy.php
More file actions
86 lines (69 loc) · 2.93 KB
/
Copy pathArmy.php
File metadata and controls
86 lines (69 loc) · 2.93 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
<?php
/**
* Created by JetBrains PhpStorm.
* User: kogtev_k
* Date: 07.06.13
* Time: 12:59
* To change this template use File | Settings | File Templates.
*/
class Army {
private $Units = array();
// private $City=null; //ссылка на город которому принадлежит армия
function __construct(/*City $City*/){
// $this->City=$City;
}
/* function getCity() {
return $this->City;
}
function setCity(City $City){
$this->City=$City;
}*/
function getUnits(){
return $this->Units;
}
function addUnit(Unit $unit){
array_push($this->Units,$unit);
}
// @TODO проверить правильность склеивания
function glueArmy(Army $addArmy){ //соединяем две армии
$this->Units=array_merge($this->Units,$addArmy->getUnits());
}
function randArmy($maxUnitCount=5) {
for($i=1;$i<=rand(1,$maxUnitCount);$i++) //@TODO возможна ошибка генерации максимального числа повторов, надеюсь что фор не генерирует это значение при каждой итерации
{
$armors=array();
list($name,$health,$strength)=UnitWarehouse::getRandomItem();
for($i=1;$i<=rand(0,3);$i++) //генерируем рандомное кол-во брони
$armors[]=ArmorWarehouse::getRandomItem();
$this->addUnit(new warriorUnit($name,$health,$strength,WeaponWarehouse::getRandomItem(),$armors));
}
}
// @TODO одинаковые юниты выводить в виде массива, например - Лучники[5]
function printArmy(){
foreach($this->Units as $unit){
if (is_array($unit)) { //если юнет не один а в массиве то обрабатываем массив
foreach ($unit as $u)
echo $u;
}else {
echo $unit;
}
}
}
function prepareForBattle(){ //считаем и устанавливаем счетчики здоровья у юнитов перед боем
foreach ($this->Units as $unit) $unit->prepareForBattle();
}
function setDamage($damage){ //наносим повреждения всем юнитам армии
foreach ($this->Units as $unit) $unit->setDamage($damage);
}
function checkDead(){ //проверяем и удаляем мертвые юниты
foreach ($this->Units as $i => $unit)
if ($unit->getCurrentHealth()<=0) {
$this->killUnit($i);
};
return (count($this->Units)>0);
}
function killUnit($num){
array_splice($this->Units,$num,1);
// unset($unit); //@TODO проверить действительно ли объект удаляется из памяти
}
}