hanli/src/Core/SpellDisplay.php
hant c065e19113 优化技能显示系统:增强战斗中的法术信息显示
新增功能:
- 创建 SpellDisplay 工具类,统一管理法术显示逻辑
- 支持 14 种计算方式的中文说明显示
- 显示品质特定的基础伤害/治疗倍数
- 增强 Battle.php 中所有法术施放方法的信息显示

改进内容:
- castDamageSingleSpell: 显示计算方式、消耗、伤害倍数
- castDamageAoeSpell: 显示计算方式、消耗
- castHealSingleSpell: 显示计算方式、消耗
- castHealAoeSpell: 显示计算方式、消耗
- castSupportSpell: 显示品质颜色和消耗

技术细节:
- ItemDisplay.php 添加 getQualityIndex() 辅助方法
- SpellDisplay.php 修复 match 表达式语法(改用 if-elseif)
- 使用质量索引访问品质相关数组
- 显示强化后的实际消耗

🧙 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 22:52:48 +08:00

381 lines
15 KiB
PHP

<?php
namespace Game\Core;
/**
* 法术显示工具类 - 统一管理法术信息的展示
*/
class SpellDisplay
{
// 法术类型名称
private static array $typeNames = [
'heal_single' => '单体治疗',
'damage_single' => '单体伤害',
'damage_aoe' => '群体伤害',
'heal_aoe' => '群体治疗',
'support' => '辅助',
];
// 计算方式说明
private static array $calcTypeDescriptions = [
'matk' => '基于魔攻',
'patk' => '基于物攻',
'hp_percent' => '基于最大生命值百分比',
'hybrid' => '基于(魔攻+物攻)混合',
'crit_heal' => '暴击率影响治疗效果',
'crit_damage' => '暴击伤害系数影响伤害',
'crit_aoe' => '暴击率影响范围伤害',
'defense' => '基于防御属性',
'low_def_bonus' => '对低防御敌人伤害加成',
'matk_scaled' => '随敌人数量加成',
'dispersed_damage' => '伤害分散到所有敌人',
'smart_heal' => '智能治疗(优先低血量)',
'hp_missing' => '基于缺失生命值',
'team_sync' => '基于队伍规模',
];
// 品质颜色
private static array $qualityColors = [
'common' => Colors::WHITE,
'rare' => Colors::BLUE,
'epic' => Colors::MAGENTA,
'legendary' => Colors::YELLOW,
];
// 品质名称
private static array $qualityNames = [
'common' => '普通',
'rare' => '稀有',
'epic' => '史诗',
'legendary' => '传奇',
];
// 颜色常量
private static string $reset = Colors::RESET;
private static string $yellow = Colors::YELLOW;
private static string $green = Colors::GREEN;
private static string $cyan = Colors::CYAN;
private static string $white = Colors::WHITE;
private static string $gray = Colors::GRAY;
private static string $magenta = Colors::MAGENTA;
private static string $blue = Colors::BLUE;
/**
* 获取法术品质颜色
*/
public static function getQualityColor(string $quality): string
{
return self::$qualityColors[$quality] ?? self::$white;
}
/**
* 获取法术品质名称
*/
public static function getQualityName(string $quality): string
{
return self::$qualityNames[$quality] ?? '普通';
}
/**
* 获取法术类型名称
*/
public static function getTypeName(string $type): string
{
return self::$typeNames[$type] ?? $type;
}
/**
* 获取计算方式描述
*/
public static function getCalcTypeDescription(string $calcType): string
{
return self::$calcTypeDescriptions[$calcType] ?? $calcType;
}
/**
* 格式化法术名称(带品质颜色和等级)
*/
public static function formatName(array $spell): string
{
$quality = $spell['quality'] ?? 'common';
$color = self::getQualityColor($quality);
$name = $spell['name'] ?? '未知法术';
$level = $spell['level'] ?? 1;
$enhanceLevel = $spell['enhanceLevel'] ?? 0;
$enhanceStr = $enhanceLevel > 0 ? self::$yellow . "+{$enhanceLevel}" . self::$reset : "";
return $color . $name . self::$reset . " Lv.{$level}" . $enhanceStr;
}
/**
* 格式化法术简要信息(用于列表)
*/
public static function renderListItem(array $spell): string
{
$parts = [];
// 名称(带品质颜色和强化)
$parts[] = self::formatName($spell);
// 法术类型
$spellType = $spell['spellType'] ?? $spell['type'] ?? 'unknown';
$typeName = self::getTypeName($spellType);
$parts[] = self::$gray . "[{$typeName}]" . self::$reset;
// 消耗和基本描述
$cost = $spell['cost'] ?? 0;
$parts[] = self::$cyan . "消耗:{$cost}" . self::$reset;
return implode(" ", $parts);
}
/**
* 格式化法术详细信息(用于详情面板)
* 返回多行数组
*/
public static function renderDetail(array $spell, string $linePrefix = ""): array
{
$lines = [];
$quality = $spell['quality'] ?? 'common';
$qualityColor = self::getQualityColor($quality);
$qualityName = self::getQualityName($quality);
$spellType = $spell['spellType'] ?? $spell['type'] ?? 'unknown';
$typeName = self::getTypeName($spellType);
$calcType = $spell['calc_type'] ?? 'unknown';
$calcTypeDesc = self::getCalcTypeDescription($calcType);
$enhanceLevel = $spell['enhanceLevel'] ?? 0;
$spellId = $spell['spellId'] ?? 'unknown';
// 名称行
$lines[] = $linePrefix . self::formatName($spell);
$lines[] = $linePrefix;
// 基本信息
$lines[] = $linePrefix . "品质: " . $qualityColor . $qualityName . self::$reset;
$lines[] = $linePrefix . "类型: " . self::$white . $typeName . self::$reset;
$lines[] = $linePrefix . "法术ID: " . self::$gray . $spellId . self::$reset;
$lines[] = $linePrefix;
// 强化信息
if ($enhanceLevel > 0) {
$damageBonus = $enhanceLevel * 5;
$costReduction = $enhanceLevel * 2;
$lines[] = $linePrefix . "强化: " . self::$yellow . "+{$enhanceLevel}" . self::$reset .
self::$gray . " (伤害+{$damageBonus}% 消耗-{$costReduction})" . self::$reset;
$lines[] = $linePrefix;
}
// 计算方式
$lines[] = $linePrefix . self::$cyan . "━━ 基础数值 ━━" . self::$reset;
$lines[] = $linePrefix . "计算方式: " . self::$green . $calcTypeDesc . self::$reset;
// 根据法术类型显示不同的基础数值
if ($spellType === 'heal_single' || $spellType === 'heal_aoe') {
$lines[] = self::formatHealValues($spell, $linePrefix, $enhanceLevel);
} elseif ($spellType === 'damage_single' || $spellType === 'damage_aoe') {
$lines[] = self::formatDamageValues($spell, $linePrefix, $enhanceLevel);
}
$lines[] = $linePrefix;
// 消耗信息
$baseCost = $spell['cost'] ?? 20;
$actualCost = max(1, $baseCost - ($enhanceLevel * 2));
if ($enhanceLevel > 0) {
$lines[] = $linePrefix . "魔法消耗: " . self::$white . $baseCost . self::$reset .
"" . self::$green . $actualCost . self::$reset;
} else {
$lines[] = $linePrefix . "魔法消耗: " . self::$green . $baseCost . self::$reset;
}
$lines[] = $linePrefix;
// 描述
$desc = $spell['desc'] ?? '';
if ($desc) {
$lines[] = $linePrefix . self::$gray . $desc . self::$reset;
}
return $lines;
}
/**
* 格式化治疗法术的基础数值
*/
private static function formatHealValues(array $spell, string $linePrefix, int $enhanceLevel): string
{
$calcType = $spell['calc_type'] ?? 'matk';
$healRatio = $spell['heal_ratio'] ?? [0.5, 0.8, 1.2, 1.8];
$healBase = $spell['heal_base'] ?? [20, 40, 70, 120];
$qualityIndex = self::getQualityIndex($spell['quality'] ?? 'common');
$ratio = $healRatio[$qualityIndex] ?? 0.5;
$base = $healBase[$qualityIndex] ?? 20;
$lines = "";
match($calcType) {
'matk' => $lines = self::formatMatkHeal($ratio, $base, $linePrefix, $enhanceLevel),
'hp_percent' => $lines = self::formatHpPercentHeal($ratio, $linePrefix),
'hybrid' => $lines = self::formatHybridHeal($ratio, $base, $linePrefix, $enhanceLevel),
'hp_missing' => $lines = self::formatHpMissingHeal($ratio, $linePrefix),
'crit_heal' => $lines = self::formatCritHeal($ratio, $base, $spell['crit_bonus'] ?? [0.5, 0.7, 1.0, 1.5], $linePrefix, $enhanceLevel),
'defense' => $lines = self::formatDefenseHeal($ratio, $base, $linePrefix, $enhanceLevel),
default => $lines = $linePrefix . "基础治疗: " . self::$green . "{$ratio}x魔攻 + {$base}" . self::$reset,
};
return $lines;
}
/**
* 格式化伤害法术的基础数值
*/
private static function formatDamageValues(array $spell, string $linePrefix, int $enhanceLevel): string
{
$calcType = $spell['calc_type'] ?? 'matk';
$damageRatio = $spell['damage_ratio'] ?? [1.2, 1.6, 2.0, 2.6];
$qualityIndex = self::getQualityIndex($spell['quality'] ?? 'common');
$ratio = $damageRatio[$qualityIndex] ?? 1.2;
if ($calcType === 'matk') {
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x魔攻" . self::$reset;
} elseif ($calcType === 'patk') {
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x物攻" . self::$reset;
} elseif ($calcType === 'hybrid') {
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x(魔攻+物攻)" . self::$reset;
} elseif ($calcType === 'crit_damage') {
$critBonus = $spell['crit_dmg_bonus'] ?? [0.3, 0.5, 0.8, 1.2];
$bonus = $critBonus[$qualityIndex] ?? 0.3;
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x物攻" . self::$reset . "\n" .
$linePrefix . "暴击加成: " . self::$yellow . "{$bonus}x暴伤系数" . self::$reset;
} elseif ($calcType === 'low_def_bonus') {
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x物攻(低防御加成)" . self::$reset;
} elseif ($calcType === 'matk_scaled') {
$enemyBonus = $spell['enemy_count_bonus'] ?? [0.1, 0.15, 0.2, 0.3];
$bonus = $enemyBonus[$qualityIndex] ?? 0.1;
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x魔攻" . self::$reset . "\n" .
$linePrefix . "敌人数加成: " . self::$yellow . "+" . ($bonus*100) . "%/敌人" . self::$reset;
} elseif ($calcType === 'dispersed_damage') {
$dispersion = $spell['dispersion'] ?? [0.8, 0.75, 0.7, 0.65];
$disp = $dispersion[$qualityIndex] ?? 0.8;
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x魔攻" . self::$reset . "\n" .
$linePrefix . "分散系数: " . self::$yellow . "{$disp}(敌人越多衰减越严重)" . self::$reset;
} elseif ($calcType === 'crit_aoe') {
$critBonus = $spell['crit_bonus'] ?? [0.4, 0.6, 0.9, 1.3];
$bonus = $critBonus[$qualityIndex] ?? 0.4;
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x魔攻" . self::$reset . "\n" .
$linePrefix . "暴击影响: " . self::$yellow . "{$bonus}x暴击率" . self::$reset;
}
return $linePrefix . "伤害倍数: " . self::$green . "{$ratio}x" . self::$reset;
}
/**
* 格式化魔攻治疗
*/
private static function formatMatkHeal(float $ratio, float $base, string $linePrefix, int $enhanceLevel): string
{
$healBonus = $enhanceLevel * 5;
if ($enhanceLevel > 0) {
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x魔攻 + {$base}" . self::$reset .
" " . self::$yellow . "(+{$healBonus}%)" . self::$reset;
}
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x魔攻 + {$base}" . self::$reset;
}
/**
* 格式化生命值百分比治疗
*/
private static function formatHpPercentHeal(float $ratio, string $linePrefix): string
{
return $linePrefix . "恢复量: " . self::$green . ($ratio * 100) . "% 最大生命值" . self::$reset;
}
/**
* 格式化混合治疗
*/
private static function formatHybridHeal(float $ratio, float $base, string $linePrefix, int $enhanceLevel): string
{
$healBonus = $enhanceLevel * 5;
if ($enhanceLevel > 0) {
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x(魔攻+物攻) + {$base}" . self::$reset .
" " . self::$yellow . "(+{$healBonus}%)" . self::$reset;
}
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x(魔攻+物攻) + {$base}" . self::$reset;
}
/**
* 格式化缺失生命值治疗
*/
private static function formatHpMissingHeal(float $ratio, string $linePrefix): string
{
return $linePrefix . "恢复量: " . self::$green . ($ratio * 100) . "% 缺失生命值" . self::$reset;
}
/**
* 格式化暴击治疗
*/
private static function formatCritHeal(float $ratio, float $base, array $critBonus, string $linePrefix, int $enhanceLevel): string
{
$qualityIndex = self::getQualityIndex(0);
$bonus = $critBonus[$qualityIndex] ?? 0.5;
$healBonus = $enhanceLevel * 5;
$result = $linePrefix . "恢复量: " . self::$green . "{$ratio}x魔攻 + {$base}" . self::$reset;
$result .= "\n" . $linePrefix . "暴击系数: " . self::$yellow . "{$bonus}x暴击率加成" . self::$reset;
if ($enhanceLevel > 0) {
$result .= "\n" . $linePrefix . "强化加成: " . self::$yellow . "+{$healBonus}%" . self::$reset;
}
return $result;
}
/**
* 格式化防御治疗
*/
private static function formatDefenseHeal(float $ratio, float $base, string $linePrefix, int $enhanceLevel): string
{
$healBonus = $enhanceLevel * 5;
if ($enhanceLevel > 0) {
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x(物防+魔防) + {$base}" . self::$reset .
" " . self::$yellow . "(+{$healBonus}%)" . self::$reset;
}
return $linePrefix . "恢复量: " . self::$green . "{$ratio}x(物防+魔防) + {$base}" . self::$reset;
}
/**
* 根据品质获取数组索引
*/
private static function getQualityIndex(string $quality): int
{
return match($quality) {
'common' => 0,
'rare' => 1,
'epic' => 2,
'legendary' => 3,
default => 0,
};
}
/**
* 渲染战斗中的法术简洁信息
*/
public static function renderBattleInfo(array $spell, string $prefix = " "): string
{
$quality = $spell['quality'] ?? 'common';
$color = self::getQualityColor($quality);
$name = $spell['name'] ?? '未知法术';
$cost = $spell['cost'] ?? 20;
$enhanceLevel = $spell['enhanceLevel'] ?? 0;
$calcType = $spell['calc_type'] ?? 'unknown';
$calcDesc = self::getCalcTypeDescription($calcType);
$enhanceStr = $enhanceLevel > 0 ? self::$yellow . "+{$enhanceLevel}" . self::$reset : "";
return $prefix . $color . $name . self::$reset . $enhanceStr .
" [消耗:{$cost} {$calcDesc}]";
}
}