-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokemon.php
167 lines (149 loc) · 4.88 KB
/
Pokemon.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
//Creating the namespace and calling it in my Pickachu and Charmeleon classes
namespace Pokemon;
// If a class is called if it has not already been called, the function does so automatically.
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
class Pokemon
{
//Creating the properties
static $livingPokemons;
private $name;
private $energyType;
private $hitPoints;
public $attacks;
private $weakness;
private $resistance;
/**
* Constructor used to create a Pokemon object
* @param string $name
* @param mixed $energyType
* @param int $hitPoints
* @param mixed $attacks
* @param mixed $weakness
* @param mixed $resistance
*/
// Declarte construct method which accepts six parameters
protected function __construct($name, $energyType, $hitPoints, $attacks, $weakness, $resistance)
{
$this->name = $name;
$this->energyType = $energyType;
$this->hitPoints = $hitPoints;
$this->health = $hitPoints;
$this->attacks = $attacks;
$this->weakness = $weakness;
$this->resistance = $resistance;
//Increases the livingPokemons by 1, each time the constructor is executed.
self::$livingPokemons++;
}
public function battleTurn($target, $attacks)
{
$energyType = $target->getEnergyType()->getName();
$weaknessEnergyType = $target->getWeakness()->getWeaknessType();
$multiplierEnergyType = $target->getWeakness()->getWeaknessValue();
$resistanceEnergyType = $target->getResistance()->getResistanceType();
$resistance = $target->getResistance()->getResistanceValue();
// If the weakness matches the energy type of the attacking Pokemon, the multiplier is used.
if ($weaknessEnergyType == $energyType) {
$damage = $attacks->getAttackDamage() * $multiplierEnergyType;
echo "<p>" . $this->getPokemonByName() . " valt aan met " . $attacks->getAttackName() . "! Dit doet niet heel veel damage </p>";
echo " <p>" . $damage . " damage </p>";
} // If the resistance matches the energy type, the resistance is subtracted from the attack damage.
elseif ($resistanceEnergyType == $energyType) {
$damage = $attacks->getAttackDamage() - $resistance;
echo "<p>" . $this->getPokemonByName() . " valt aan met " . $attacks->getAttackName() . "! Dit doet niet heel veel damage </p>";
echo " <p>" . $damage . " damage </p>";
} // Shows the attack with the damage that has been done.
else {
$damage = $attacks->getAttackdamage();
echo $this->getPokemonByName() . " valt aan met " . $attacks->getAttackName() . "<p> " . $damage . " damage</p>";
}
$this->damageDone($damage, $target);
}
/**
* Laat zien of de Pokemon dood is of hij laat zien hoeveel HP er nog over is.
* @param int $damage
* @param mixed $target
*/
public function damageDone($damage, $target)
{
// If the HP is 0 then a Pokemon is taken from it
$target->health -= $damage;
if ($target->getHealth() <= 0) {
echo $target->getPokemonByName() . " Is verslagen!<br>";
self::$livingPokemons--;
} elseif ($target->getHealth() <= 15) {
echo $target->getPokemonByName() . ' heeft nu nog maar ' . $target->getHealth() . " hp over!<br>";
} // Shows how much HP there is left
else {
echo $target->getPokemonByName() . " heeft nu nog " . $target->getHealth() . " hp over!<br>";
}
}
//Creating all the functions
/**
* @return int $livingPokemons
*/
//Using self because it is a non object context and a static function
public static function getPopulation()
{
return self::$livingPokemons;
}
/**
* @return string $name
*/
public function getPokemonByName()
{
return $this->name;
}
/**
* @return string $energyType
*/
public function getEnergyType()
{
return $this->energyType;
}
/**
* @return int $hitPoints
*/
public function getHitpoints()
{
return $this->hitPoints;
}
/**
* @return mixed $attacks
*/
public function getAttack()
{
return $this->attacks;
}
public function getAttackByName($name)
{
foreach ($this->attacks as $attack) {
if ($attack->name == $name) {
return $attack;
}
}
}
/**
* @return mixed $weakness
*/
public function getWeakness()
{
return $this->weakness;
}
/**
* @return mixed $resistance
*/
public function getResistance()
{
return $this->resistance;
}
/**
* @return int $health
*/
public function getHealth()
{
return $this->health;
}
}