no message
This commit is contained in:
125
Question14-AdvancedInheritance/common/Character.php
Normal file
125
Question14-AdvancedInheritance/common/Character.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
abstract class Character
|
||||
{
|
||||
/** 名前 */
|
||||
private $name = 'モンスター';
|
||||
|
||||
/** 体力 */
|
||||
private $hp;
|
||||
|
||||
/** 力 */
|
||||
private $power;
|
||||
|
||||
/** 防御力 */
|
||||
private $endurance;
|
||||
|
||||
/**
|
||||
* 体力、攻撃力、防御力の初期値を設定しつつ、Characterインスタンスを生成します
|
||||
* @param hp
|
||||
* @param attack
|
||||
* @param defence
|
||||
*/
|
||||
function __construct($hp, $power, $endurance)
|
||||
{
|
||||
$this->name = $this->getCharacterName();
|
||||
$this->hp = $hp;
|
||||
$this->power = $power;
|
||||
$this->endurance = $endurance;
|
||||
}
|
||||
/**
|
||||
* 名前を取得します
|
||||
* @return string 名前
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 力を取得します
|
||||
* @return int 力の値
|
||||
*/
|
||||
public function getPower()
|
||||
{
|
||||
return $this->power;
|
||||
}
|
||||
|
||||
/**
|
||||
* 耐久を取得します
|
||||
* @return int 耐久の値
|
||||
*/
|
||||
public function getEndurance()
|
||||
{
|
||||
return $this->endurance;
|
||||
}
|
||||
/**
|
||||
* キャラクター情報を表示します
|
||||
* @return string
|
||||
*/
|
||||
public function showParameter()
|
||||
{
|
||||
$str = nl2br('名 前' . $this->name
|
||||
. "\r\n" . '体 力' . $this->hp
|
||||
. "\r\n" . ' 力 ' . $this->power
|
||||
. "\r\n" . '耐 久' . $this->endurance);
|
||||
return $str;
|
||||
}
|
||||
/**
|
||||
* 戦闘を行います。負の数が返ってきた場合、逃げるを選択したことになります
|
||||
* @param int $defence 攻撃を受ける側の耐久
|
||||
* @return int 最終的に計算されたダメージ量
|
||||
*/
|
||||
public function battle($defence)
|
||||
{
|
||||
$random = rand(0, 100);
|
||||
|
||||
$rate = $this->getEscapeRate();
|
||||
|
||||
// 設定された確率で逃げる
|
||||
if ($random < $rate) {
|
||||
return -1;
|
||||
} else {
|
||||
|
||||
// ダメージの算出(0ダメージにはならないようにする)
|
||||
$dam = $this->damage($defence);
|
||||
if ($dam <= 0) {
|
||||
$dam = 1;
|
||||
}
|
||||
return $dam;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 受けたダメージ分、HPを減らす。
|
||||
* HPが0以下になった場合は、倒されたと判断し、falseを返す
|
||||
* @param int $damage 受けるダメージ
|
||||
* @return bool true -> 生き残っている場合、false -> 倒された場合
|
||||
*/
|
||||
public function dealsDamage($damage)
|
||||
{
|
||||
$this->hp -= $damage;
|
||||
if ($this->hp <= 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* このモンスターの名前を取得します
|
||||
* @return string 設定する名前
|
||||
*/
|
||||
protected abstract function getCharacterName();
|
||||
|
||||
/**
|
||||
* このモンスターが逃げる確率を設定します。
|
||||
* 例えば、10%で逃げる場合は、10を設定します
|
||||
* @return int 逃げる確率
|
||||
*/
|
||||
protected abstract function getEscapeRate();
|
||||
|
||||
/**
|
||||
* モンスター毎に攻撃判定を行います。
|
||||
* 例えば、通常攻撃・スキル・魔法などモンスターごとに行動割合を決定し、最終的に計算されたダメージ量を判定します
|
||||
* @param int $defence 攻撃を受ける側の耐久
|
||||
* @return int 最終的に計算されたダメージ量
|
||||
*/
|
||||
protected abstract function damage($defence);
|
||||
}
|
||||
Reference in New Issue
Block a user