80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
||
require __DIR__ . '/../vendor/autoload.php';
|
||
|
||
use Game\Core\ServiceContainer;
|
||
use Game\Event\Event;
|
||
use Symfony\Component\Console\Input\ArrayInput;
|
||
use Symfony\Component\Console\Output\BufferedOutput;
|
||
use Symfony\Component\Console\Helper\HelperSet;
|
||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||
|
||
$input = new ArrayInput([]);
|
||
$output = new BufferedOutput();
|
||
$helperSet = new HelperSet(['question' => new QuestionHelper()]);
|
||
|
||
$container = new ServiceContainer($input, $output, $helperSet);
|
||
$dispatcher = $container->registerServices();
|
||
$stateManager = $container->getStateManager();
|
||
|
||
// 创建测试玩家
|
||
$player = new \Game\Model\Player("ShopTest", 100, 10, 5);
|
||
$player->gainGold(200); // 给玩家一些金币用于测试
|
||
$stateManager->setPlayer($player);
|
||
$stateManager->setCurrentTileId('TOWN_01');
|
||
|
||
echo "=== NPC商店功能测试 ===\n\n";
|
||
|
||
// 获取NPC仓库
|
||
$npcRepo = $container->getNpcRepository();
|
||
$npc = $npcRepo->createNPC('BLACKSMITH');
|
||
|
||
if (!$npc) {
|
||
die("❌ 无法创建铁匠NPC\n");
|
||
}
|
||
|
||
echo "1. 创建NPC: {$npc->getName()}\n";
|
||
echo " 是否有商店: " . ($npc->hasShop ? '是' : '否') . "\n";
|
||
echo " 商店库存数量: " . count($npc->shopInventory) . "\n\n";
|
||
|
||
echo "2. 玩家初始状态:\n";
|
||
echo " - 金币: {$player->getGold()}\n";
|
||
echo " - 背包物品数: " . count($player->getInventory()) . "\n\n";
|
||
|
||
echo "3. 模拟与NPC交互并打开商店...\n";
|
||
// 模拟InteractionSystem的行为
|
||
$dispatcher->dispatch(new Event('SystemMessage', [
|
||
'message' => "👤 你走近了 <fg=cyan;options=bold>{$npc->getName()}</>。"
|
||
]));
|
||
|
||
// 模拟NPC对话
|
||
$defaultMsg = is_array($npc->dialogue) ? ($npc->dialogue['greeting'] ?? '...') : '...';
|
||
$dispatcher->dispatch(new Event('SystemMessage', [
|
||
'message' => "<fg=cyan>{$npc->getName()}</>:<fg=white>{$defaultMsg}</>"
|
||
]));
|
||
|
||
// 检查商店
|
||
if ($npc->hasShop) {
|
||
$dispatcher->dispatch(new Event('SystemMessage', [
|
||
'message' => "\n🛍️ <fg=yellow>{$npc->getName()}</> 还经营着一家商店。"
|
||
]));
|
||
$dispatcher->dispatch(new Event('SystemMessage', [
|
||
'message' => "<fg=gray>输入 [S] 打开商店,[X] 离开</>"
|
||
]));
|
||
|
||
// 设置待处理的商店NPC
|
||
$stateManager->setPendingShopNpc($npc);
|
||
|
||
// 模拟输入S打开商店
|
||
echo "\n模拟输入 S 打开商店...\n";
|
||
$dispatcher->dispatch(new Event('OpenShopEvent', [
|
||
'npc' => $npc
|
||
]));
|
||
}
|
||
|
||
echo "\n" . $output->fetch() . "\n";
|
||
|
||
echo "\n4. 测试完成后的状态:\n";
|
||
echo " - 金币: {$player->getGold()}\n";
|
||
echo " - 背包物品数: " . count($player->getInventory()) . "\n";
|
||
|
||
echo "\n=== 测试完成 ===\n"; |