105 lines
3.4 KiB
PHP
105 lines
3.4 KiB
PHP
<?php
|
||
namespace Game\Model;
|
||
|
||
/**
|
||
* Quest: 任务模型 - 包含配置数据和运行时进度
|
||
*/
|
||
class Quest {
|
||
// --- 配置数据 (来自 JSON) ---
|
||
public string $id;
|
||
public string $name;
|
||
public string $description;
|
||
public string $type;
|
||
public array $target; // e.g., ['entityId' => 'GOBLIN', 'count' => 1]
|
||
public array $rewards;
|
||
public bool $isRepeatable = false;
|
||
|
||
// ⭐ 新增:运行时状态,默认为 0
|
||
protected int $currentCount = 0;
|
||
|
||
// ⭐ 新增:触发器和对话数据
|
||
public ?string $triggerType; // 'NPC' or 'SYSTEM'
|
||
public ?string $triggerValue; // NPC_ID or EVENT_NAME
|
||
public array $dialogue; // Dialogue Tree
|
||
|
||
public function __construct(string $id, string $name, string $description, string $type, array $target, array $rewards, ?string $triggerType = null, ?string $triggerValue = null, array $dialogue = []) {
|
||
$this->id = $id;
|
||
$this->name = $name;
|
||
$this->description = $description;
|
||
$this->type = $type;
|
||
$this->target = $target;
|
||
$this->rewards = $rewards;
|
||
$this->triggerType = $triggerType;
|
||
$this->triggerValue = $triggerValue;
|
||
$this->dialogue = $dialogue;
|
||
}
|
||
|
||
// --- 进度管理方法 (用于业务逻辑) ---
|
||
|
||
/**
|
||
* 增加任务当前进度
|
||
*/
|
||
public function incrementCurrentCount(int $amount = 1): void {
|
||
// 目标数量存储在 $this->target['count'] 中
|
||
$targetCount = $this->target['count'] ?? 1;
|
||
$this->currentCount = min($targetCount, $this->currentCount + $amount);
|
||
}
|
||
|
||
/**
|
||
* ⭐ 简便方法:增加进度(兼容旧API)
|
||
*/
|
||
public function incrementProgress(int $amount = 1): void {
|
||
$this->incrementCurrentCount($amount);
|
||
}
|
||
|
||
/**
|
||
* 检查任务是否完成
|
||
*/
|
||
public function isCompleted(): bool {
|
||
$targetCount = $this->target['count'] ?? 1;
|
||
return $this->currentCount >= $targetCount;
|
||
}
|
||
|
||
// --- Getter/Setter (用于 UI 和 SaveLoadService) ---
|
||
|
||
public function getName(): string { return $this->name; }
|
||
public function getDescription(): string { return $this->description; }
|
||
public function getId(): string { return $this->id; }
|
||
public function getType(): string { return $this->type; }
|
||
public function getTarget(): array { return $this->target; }
|
||
public function getRewards(): array { return $this->rewards; }
|
||
|
||
/**
|
||
* ⭐ 获取任务标题(兼容旧API)
|
||
*/
|
||
public function getTitle(): string { return $this->name; }
|
||
|
||
/**
|
||
* 获取当前进度 (用于存档)
|
||
*/
|
||
public function getCurrentCount(): int { return $this->currentCount; }
|
||
|
||
/**
|
||
* ⭐ 核心修正:设置当前进度 (用于存档加载)
|
||
*/
|
||
public function setCurrentCount(int $count): void {
|
||
$this->currentCount = $count;
|
||
}
|
||
|
||
public function toArray()
|
||
{
|
||
return [
|
||
'id' => $this->id,
|
||
'name' => $this->name,
|
||
'description' => $this->description,
|
||
'type' => $this->type,
|
||
'target' => $this->target,
|
||
'rewards' => $this->rewards,
|
||
'isRepeatable' => $this->isRepeatable,
|
||
'currentCount' => $this->currentCount,
|
||
'triggerType' => $this->triggerType,
|
||
'triggerValue' => $this->triggerValue,
|
||
'dialogue' => $this->dialogue,
|
||
];
|
||
}
|
||
} |