Files
2026-10-06 12:10:09 +09:00

98 lines
1.9 KiB
PHP

<?php
namespace inheritance\Common;
class Character
{
/** 名前 */
private $name = 'キャラクター';
/** 体力 */
private $hp;
/** 力 */
private $power;
/** 防御力 */
private $endurance;
/**
* 体力、攻撃力、防御力の初期値を設定しつつ、Characterインスタンスを生成します
*
* @param int $hp
* @param int $power
* @param int $endurance
*/
public function __construct($hp, $power, $endurance)
{
$this->hp = $hp;
$this->power = $power;
$this->endurance = $endurance;
}
/**
* 名前を設定します
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* 名前を取得します
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* 攻撃を行います
*
* @return int 攻撃力を返す
*/
public function attack()
{
return $this->power;
}
/**
* ダメージ判定を行います
*
* @param int $attack
* @return bool 体力が0になったら true、まだ残っていれば false
*/
public function damage($attack)
{
$calc = $attack - $this->endurance;
if ($calc <= 0) {
$calc = 1;
}
$this->hp -= $calc;
if ($this->hp <= 0) {
$this->hp = 0;
return true;
}
return false;
}
/**
* キャラクター情報を表示します
*
* @return string
*/
public function showParameter()
{
$str = nl2br('名 前:' . $this->name
. "\r\n" . '体 力:' . $this->hp
. "\r\n" . ' 力 :' . $this->power
. "\r\n" . '耐 久:' . $this->endurance);
return $str;
}
}