<?php
class Personnage
{
private $_degats,
$_id,
$_nom;
const CEST_MOI = 1; // Constante renvoyée par la méthode `frapper` si on se frappe soit-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
public function __construct(array $donnees)
{
$this->hydrate($donnees);
}
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 nomValide()
{
return !empty($this->_nom);
}
public function recevoirDegats()
{
$this->_degats += -5;
if ($this->_degats <= 1)
{
$perso->_degats += +10;
}
// Sinon, on se contente de dire que le personnage a bien été frappé
return self::PERSONNAGE_FRAPPE;
}
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;
}
}
}
?>
mon probleme vient surtout de :
<?php
public function recevoirDegats()
{
$this->_degats += -5;
if ($this->_degats <= 1)
{
$perso->_degats += +10;
}
?>
je sais pas quoi mettre a la place de $perso pour que sa soit notre degat qui augmentepar contre j'ai pas encore remplacer degats mais a la place c'est plutot la vie