<?php
class game {
private $user1 = [
'name' => '张三', // 名字
'life' => 1000, // 生命
'attack' => 5, // 攻击力
'speed' => 2, // 速度
'armor' => 0, // 护甲
'dodge' => 10, // 闪避
'armour_piercing' => 0, // 穿甲
'crit' => 10, // 暴击概率 小于等于100
'critical_damage' => 1.2, // 暴击伤害
'poisoning' => 80, // 中毒概率 小于等于100
'poisoning_damage' => 2, // 中毒伤害
];
private $user2 = [
'name' => '李四', // 名字
'life' => 1000, // 生命
'attack' => 50, // 攻击力
'speed' => 11, // 速度
'armor' => 0, // 护甲
'dodge' => 10, // 闪避
'armour_piercing' => 1, // 穿甲
'crit' => 10, // 暴击概率 0-100
'critical_damage' => 1.2, // 暴击伤害
'poisoning' => 10, // 中毒概率 0-100
'poisoning_damage' => 2, // 中毒伤害
];
private $sb_color = '#d01cff'; // 闪避颜色
private $zd_color = 'darkgreen'; // 中毒颜色
private $bj_color = 'red'; // 暴击颜色
private $tip1 = '<pre>'; // 提示消息
private $tip2 = ''; // 提示消息
private $js = 0; // 计数
private $poisoning_count = 4; // 中毒扣血次数
private $poisoning_layer1 = []; // 中毒层数 层数可叠加
private $poisoning_layer2 = []; // 中毒层数 层数可叠加
private $pt1 = 0; // 普通伤害
private $bj1 = 0; // 暴击伤害
private $pt2 = 0; // 普通伤害
private $bj2 = 0; // 暴击伤害
private $count1 = [
'gj' => 0, // 攻击次数
'sb' => 0, // 闪避次数
'bj' => 0, // 暴击次数
'zd' => 0, // 中毒次数
];
private $count2 = [
'gj' => 0, // 攻击次数
'sb' => 0, // 闪避次数
'bj' => 0, // 暴击次数
'zd' => 0, // 中毒次数
];
// 载入配置
public function __construct($user1 = [], $user2 = [])
{
if ($user1) {
foreach ($user1 as $key => $value) {
if (isset($this->user1[$key])) {
$this->user1[$key1] = $value;
}
}
}
if ($user2) {
foreach ($user2 as $key => $value) {
if (isset($this->user2[$key])) {
$this->user2[$key] = $value;
}
}
}
}
// 设置各自伤害
private function set_hurt()
{
// 普通伤害
$hj = $this->user2['armor'] - $this->user1['armour_piercing']; // 护甲 - 穿甲
$hj = $hj > 0 ? $hj : 0;
$this->pt1 = $this->user1['attack'] - $hj; // 进攻 - 被进攻防御 不暴击
// 暴击伤害
if ($this->pt1 < 0) $this->pt1 = 0;
$this->bj1 = $this->user1['attack'] * $this->user1['critical_damage'] - $hj; // 暴击
if ($this->bj1 < 0) $this->bj1 = 0;
// 普通伤害
$hj = $this->user1['armor'] - $this->user2['armour_piercing']; // 护甲 - 穿甲
$hj = $hj > 0 ? $hj : 0;
$this->pt2 = $this->user2['attack'] - $hj; // 被进攻 - 进攻防御 不暴击
// 暴击伤害
if ($this->pt2 < 0) $this->pt2 = 0;
$this->bj2 = $this->user2['attack'] * $this->user2['critical_damage'] - $hj; // 暴击
if ($this->bj2 < 0) $this->bj2 = 0;
}
// 计算毒素伤害
private function calculate_poisoning()
{
// 每秒扣除毒素伤害
if ($this->js % 10 == 0) {
if ($this->poisoning_layer1) {
$tmp = array_shift($this->poisoning_layer1) * $this->user2['poisoning_damage'];
$this->user1['life'] -= $tmp;
$this->tip2 .= '<span style="color: ' . $this->zd_color . ';">' . $this->user1['name'] . '受到毒素伤害' . ',' . $this->user1['name'] . '生命-' . $tmp . ',' . $this->user1['name'] . '剩余生命' . $this->user1['life'] . '</span><br/>';
if ($this->user1['life'] < 1) $this->get_result();
}
if ($this->poisoning_layer2) {
$tmp = array_shift($this->poisoning_layer2) * $this->user1['poisoning_damage'];
$this->user2['life'] -= $tmp;
$this->tip2 .= '<span style="color: ' . $this->zd_color . ';">' . $this->user2['name'] . '受到毒素伤害' . ',' . $this->user2['name'] . '生命-' . $tmp . ',' . $this->user2['name'] . '剩余生命' . $this->user2['life'] . '</span><br/>';
if ($this->user2['life'] < 1) $this->get_result();
}
}
}
// 是否命中
private function is_hit($risk)
{
return $risk > 0 ? mt_rand(0, 100) <= $risk ? true : false : 1000;
}
// 计算攻击伤害
private function calculate_attack()
{
// 攻击者攻击
if ($this->js % $this->user1['speed'] == 0) {
$this->count1['gj']++;
$sb = $this->user2['dodge'] > 0 ? mt_rand(0, 100) : 1000;
// 闪避
if ($this->is_hit($this->user2['dodge'])) {
$this->count1['sb']++;
$this->tip2 .= '<span style="color:' . $this->sb_color . ';">' . $this->user1['name'] . '攻击' . $this->user2['name'] . ',' . $this->user2['name'] . '闪避成功</span><br/>';
} else {
// 中毒
if ($this->is_hit($this->user1['poisoning'])) {
$this->tip2 .= '<span style="color:' . $this->zd_color . ';">' . $this->user1['name'] . '施加中毒效果</span><br/>';
$this->count2['zd']++;
for ($i = 0; $i < $this->poisoning_count; $i++) {
$this->poisoning_layer2[$i] = isset($this->poisoning_layer2[$i]) ? $this->poisoning_layer2[$i] += 1 : 1;
}
}
// 暴击
if ($this->is_hit($this->user1['crit'])) {
$this->count1['bj']++;
$this->user2['life'] -= $this->bj1;
$this->tip2 .= '<span style="color:' . $this->bj_color . ';">' . $this->user1['name'] . '暴击' . $this->user2['name'] . ',' . $this->user2['name'] . '生命-' . $this->bj1 . ',' . $this->user2['name'] . '剩余生命' . $this->user2['life'] . '</span><br/>';
} else {
$this->user2['life'] -= $this->pt1;
$this->tip2 .= '<span style="color:black;">' . $this->user1['name'] . '攻击' . $this->user2['name'] . ','.$this->user2['name'] . '生命-' . $this->pt1 . ',' . $this->user2['name'] . '剩余生命' . $this->user2['life'] . '</span><br/>';
}
}
}
if ($this->user2['life'] < 1) $this->get_result();
// 被攻击者攻击
if ($this->js % $this->user2['speed'] == 0) {
$this->count2['gj']++;
$sb = $this->user1['dodge'] > 0 ? mt_rand(0, 100) : 1000;
// 闪避
if ($this->is_hit($this->user1['dodge'])) {
$this->count1['sb']++;
$this->tip2 .= '<span style="color:' . $this->zd_color . ';">' . $this->user2['name'] . '攻击' . $this->user1['name'] . ',' . $this->user1['name'] . '闪避成功</span><br/>';
} else {
//中毒
if ($this->is_hit($this->user1['poisoning'])) {
$this->tip2 .= '<span style="color: darkgreen;">' . $this->user2['name'] . '施加中毒效果</span><br/>';
$this->count1['zd']++;
for ($i = 0; $i < $this->poisoning_count; $i++) {
$this->poisoning_layer1[$i] = isset($this->poisoning_layer1[$i]) ? $this->poisoning_layer1[$i] += 1 : 1;
}
}
// 暴击
if ($this->is_hit($this->user2['crit'])) {
$this->count2['bj']++;
$this->user1['life'] -= $this->bj2;
$this->tip2 .= '<span style="color:' . $this->bj_color . ';">' . $this->user2['name'] . '暴击' . $this->user1['name'] . ',' . $this->user1['name'] . '生命-' . $this->bj2 . ',' . $this->user1['name'] . '剩余生命' . $this->user1['life'] . '</span><br/>';
} else {
$this->user1['life'] -= $this->pt2;
$this->tip2 .= '<span style="color:black;">' . $this->user2['name'] . '攻击' . $this->user1['name'] . ',' . $this->user1['name'] . '生命-' . $this->pt2 . ',' . $this->user1['name'] . '剩余生命' . $this->user1['life'] . '</span><br/>';
}
}
}
if ($this->user1['life'] < 1) $this->get_result();
$this->js++;
}
// 获取决斗者信息
private function get_duelist()
{
$this->tip1 .= '攻击方<br/>
名字:	' . $this->user1['name'] . '<br/>
生命:	' . $this->user1['life'] . '<br/>
攻击力:	' . $this->user1['attack'] . '<br/>
攻速:	'.($this->user1['speed']/10).'s/次'.'<br/>
护甲:	' . $this->user1['armor'] . '<br/>
闪避:	' . $this->user1['dodge'] . '%'.'<br/>
穿甲:	' . $this->user1['armour_piercing'] . '<br/>
暴击率:	' . $this->user1['crit'] . '%'.'<br/>
暴击伤害:	' . $this->user1['critical_damage'] . '<br/>
中毒率:	' . $this->user1['poisoning'] . '%'.'<br/>
中毒伤害:	' . $this->user1['poisoning_damage'] . '/s<br/>
<br/>被攻击方<br/>
名字:	' . $this->user2['name'] . '<br/>
生命:	' . $this->user2['life'] . '<br/>
攻击力:	' . $this->user2['attack'] . '<br/>
攻速:	'.($this->user2['speed']/10).'s/次'.'<br/>
护甲:	' . $this->user2['armor'] . '<br/>
闪避:	' . $this->user2['dodge'] . '%'.'<br/>
穿甲:	' . $this->user2['armour_piercing'] . '<br/>
暴击率:	' . $this->user2['crit'] . '%'.'<br/>
暴击伤害:	' . $this->user2['critical_damage'] . '<br/>
中毒率:	' . $this->user2['poisoning'] . '%'.'<br/>
中毒伤害:	' . $this->user2['poisoning_damage'] . '/s<hr/>';
}
// 获取战斗结果
private function get_result()
{
$this->tip1 .= '胜利者:	';
$this->tip1 .= $this->user1['life'] > 0 ? $this->user1['name'] : $this->user2['name'];
$this->tip1 .= '<br/><hr/>战斗统计<br/>
<br/>'.$this->user1['name'] . '<br/>
剩余生命:	' . $this->user1['life'] . '<br/>
攻击次数:	' . $this->count1['gj'] . '<br/>
闪避次数:	' . $this->count1['sb'] . '<br/>
暴击次数:	' . $this->count1['bj'] . '<br/>
中毒次数:	' . $this->count1['zd'] . '<br/>
<br/>'.$this->user2['name'] . '<br/>
剩余生命:	' . $this->user2['life'] . '<br/>
攻击次数:	' . $this->count2['gj'] . '<br/>
闪避次数:	' . $this->count2['sb'] . '<br/>
暴击次数:	' . $this->count2['bj'] . '<br/>
中毒次数:	' . $this->count2['zd'];
$this->tip1 .= '<br/><hr/>战斗过程<br/><br/>' . $this->tip2 . '</pre>';
die($this->tip1);
}
// 战斗画面
public function fight(){
// 决斗者信息
$this->get_duelist();
// 设置普通/暴击伤害
$this->set_hurt();
while (true) {
// 每秒扣除毒素伤害
$this->calculate_poisoning();
// 计算攻击伤害
$this->calculate_attack();
}
}
}