-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersonnagesManager.php
111 lines (89 loc) · 2.12 KB
/
PersonnagesManager.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
<?php
class Personnage
{
private $_degats,
$_id,
$_nom;
const CEST_MOI = 1; // Constante renvoyée par la méthode `frapper` si on se frappe soi-même.
const PERSONNAGE_TUE = 2; // Constante renvoyée par la méthode `frapper` si on a tué le personnage en le frappant.
const PERSONNAGE_FRAPPE = 3; // Constante renvoyée par la méthode `frapper` si on a bien frappé le personnage.
//fout la merde
public function construire(array $donnees)
{
$this->hydrate($donnees);
}
public function parler(){
echo 'va te faire foutre';
}
public function frapper(Personnage $perso)
{
if ($perso->id() == $this->_id)
{
return self::CEST_MOI;
}
// On indique au personnage qu'il doit recevoir des dégâts.
// Puis on retourne la valeur renvoyée par la méthode : self::PERSONNAGE_TUE ou self::PERSONNAGE_FRAPPE
return $perso->recevoirDegats();
}
public function hydrate(array $donnees)
{
foreach ($donnees as $key => $value)
{
$method = 'set'.ucfirst($key);
if (method_exists($this, $method))
{
$this->$method($value);
}
}
}
public function recevoirDegats()
{
$this->_degats += 5;
// Si on a 100 de dégâts ou plus, on dit que le personnage a été tué.
if ($this->_degats >= 100)
{
return self::PERSONNAGE_TUE;
}
// Sinon, on se contente de dire que le personnage a bien été frappé.
return self::PERSONNAGE_FRAPPE;
}
// GETTERS //
public function degats()
{
return $this->_degats;
}
public function id()
{
return $this->_id;
}
public function nom()
{
return $this->_nom;
}
public function setDegats($degats)
{
$degats = (int) $degats;
if ($degats >= 0 && $degats <= 100)
{
$this->_degats = $degats;
}
}
public function setId($id)
{
$id = (int) $id;
if ($id > 0)
{
$this->_id = $id;
}
}
public function setNom($nom)
{
if (is_string($nom))
{
$this->_nom = $nom;
}
}
}
$perso = new Personnage();
$perso->parler();
?>