38 lines
950 B
PHP
38 lines
950 B
PHP
<?php
|
|
namespace Game\Database;
|
|
|
|
use Game\Model\NPC;
|
|
|
|
class NPCRepository implements RepositoryInterface {
|
|
private array $data;
|
|
|
|
public function __construct(JsonFileLoader $loader) {
|
|
// NPC 配置通常以 ID 为键存储在 JSON 文件的顶层,所以直接加载
|
|
$this->data = $loader->load('npcs.json');
|
|
}
|
|
|
|
public function find(int|string $id): ?array {
|
|
return $this->data[$id] ?? null;
|
|
}
|
|
|
|
public function findAll(): array {
|
|
return $this->data;
|
|
}
|
|
|
|
/**
|
|
* 辅助方法:创建 NPC 模型实例
|
|
*/
|
|
public function createNPC(string $id): ?NPC {
|
|
$data = $this->find($id);
|
|
if (!$data) {
|
|
return null;
|
|
}
|
|
|
|
// 假设 NPC 模型的构造函数是 public function __construct(string $id, string $name, array $dialogue)
|
|
return new NPC(
|
|
$id,
|
|
$data['name'],
|
|
$data['dialogue']
|
|
);
|
|
}
|
|
} |