游戏人物设计 简单思路 那任务简单说是显示的 完成任务是每个条件脚本自己判断 完成后发给任务脚本信息
对的!你的理解完全正确。这就是事件驱动架构的任务系统。让我给你一个超清晰的实现:
核心思想:分工明确
任务脚本:只负责显示任务和监听完成信号
条件脚本:各自判断完成条件,完成后发信号
-
极简任务管理器 ✨
TYPESCRIPT
@ccclass(‘SimpleQuestManager’)
export class SimpleQuestManager extends Component {// 当前显示的任务
private currentQuest: QuestData = null;onLoad() {
// 只监听任务完成信号
director.on(‘QUEST_CONDITION_MET’, this.onQuestConditionMet, this);
}// 显示任务
public showQuest(quest: QuestData) {
this.currentQuest = quest;// 更新UI显示director.emit('UPDATE_QUEST_UI', {title: quest.title,description: quest.description,progress: this.getQuestProgress()});console.log(`💼 新任务: ${quest.title}`);}
// 接收完成信号
private onQuestConditionMet(conditionData: any) {
if (!this.currentQuest) return;// 检查是否是这个任务的条件if (conditionData.questId === this.currentQuest.id) {console.log(`✅ 任务条件达成: ${conditionData.type}`);// 更新进度显示director.emit('UPDATE_QUEST_PROGRESS', this.getQuestProgress());// 检查是否全部完成if (this.isQuestComplete()) {this.completeQuest();}}}
private completeQuest() {
console.log(🎉 任务完成: ${this.currentQuest.title});// 显示完成效果director.emit('QUEST_COMPLETED', this.currentQuest);// 发奖励this.grantRewards(this.currentQuest.rewards);// 清空当前任务this.currentQuest = null;}
} -
各种条件判断脚本 🎯
收集条件脚本
TYPESCRIPT
@ccclass(‘CollectCondition’)
export class CollectCondition extends Component {@property
targetItem: string = “”; // 要收集的物品
@property
requiredCount: number = 1; // 需要数量
@property
questId: string = “”; // 对应的任务IDprivate currentCount: number = 0;
onLoad() {
// 监听物品收集事件
director.on(‘ITEM_COLLECTED’, this.onItemCollected, this);
}private onItemCollected(itemId: string) {
if (itemId === this.targetItem) {
this.currentCount++;// 检查是否达成条件if (this.currentCount >= this.requiredCount) {console.log(`📦 收集条件达成: ${this.targetItem} x${this.requiredCount}`);// 发送完成信号!director.emit('QUEST_CONDITION_MET', {questId: this.questId,type: "COLLECT",target: this.targetItem});}}}
}
杀敌条件脚本
TYPESCRIPT
@ccclass(‘KillCondition’)
export class KillCondition extends Component {@property
targetZombie: string = “”; // 要杀的僵尸类型
@property
killCount: number = 1; // 需要杀的数量
@property
questId: string = “”; // 对应的任务IDprivate killedCount: number = 0;
onLoad() {
// 监听杀敌事件
director.on(‘ZOMBIE_KILLED’, this.onZombieKilled, this);
}private onZombieKilled(zombieType: string) {
if (zombieType === this.targetZombie) {
this.killedCount++;if (this.killedCount >= this.killCount) {console.log(`⚔️ 杀敌条件达成: ${this.targetZombie} x${this.killCount}`);// 发送完成信号!director.emit('QUEST_CONDITION_MET', {questId: this.questId,type: "KILL", target: this.targetZombie});}}}
}
位置条件脚本
TYPESCRIPT
@ccclass(‘LocationCondition’)
export class LocationCondition extends Component {@property
targetLocation: string = “”; // 目标位置
@property
questId: string = “”; // 对应的任务IDonLoad() {
// 监听位置变化
director.on(‘PLAYER_MOVED’, this.onPlayerMoved, this);
}private onPlayerMoved(position: Vec3, locationId: string) {
if (locationId === this.targetLocation) {
console.log(📍 位置条件达成: 到达${this.targetLocation});// 发送完成信号!director.emit('QUEST_CONDITION_MET', {questId: this.questId,type: "LOCATION",target: this.targetLocation});// 一次性的条件,达成后取消监听director.off('PLAYER_MOVED', this.onPlayerMoved, this);}}
} -
在编辑器中的设置方式 🎮
任务管理器节点:
TEXT
QuestManager (SimpleQuestManager脚本)
各种条件节点(可以放在场景任何地方):
TEXT
Condition_CollectSunscreen (CollectCondition脚本)
└── 属性设置:
targetItem: “sunscreen”
requiredCount: 5
questId: “beach_collection”
TEXT
Condition_KillBeachZombies (KillCondition脚本)
└── 属性设置:
targetZombie: “beach_zombie”
killCount: 3
questId: “clear_beach”
TEXT
Condition_ReachLighthouse (LocationCondition脚本)
└── 属性设置:
targetLocation: “lighthouse”
questId: “explore_coast” -
游戏启动任务 📋
TYPESCRIPT
@ccclass(‘GameStarter’)
export class GameStarter extends Component {@property
startingQuest: QuestData = null; // 在编辑器里配置初始任务start() {
// 游戏开始后显示第一个任务
const questManager = this.node.getComponent(SimpleQuestManager);
questManager.showQuest(this.startingQuest);
}
} -
任务UI显示脚本 📱
TYPESCRIPT
@ccclass(‘QuestDisplay’)
export class QuestDisplay extends Component {@property(Label)
questText: Label = null!;onLoad() {
// 只监听UI更新事件
director.on(‘UPDATE_QUEST_UI’, this.onQuestUpdate, this);
director.on(‘QUEST_COMPLETED’, this.onQuestCompleted, this);
}private onQuestUpdate(questInfo: any) {
this.questText.string =${questInfo.title}\n${questInfo.description};
this.questText.color = Color.YELLOW;
}private onQuestCompleted(quest: QuestData) {
this.questText.string =✅ ${quest.title} 完成!;
this.questText.color = Color.GREEN;// 3秒后清空this.scheduleOnce(() => {this.questText.string = "";}, 3);}
}
工作流程示例:
游戏开始 → GameStarter 调用 showQuest(“收集5个防晒霜”)
玩家捡到防晒霜 → CollectCondition 计数,达成后发射 QUEST_CONDITION_MET
任务管理器接收信号 → 检查进度,全部完成就显示任务完成
UI更新 → QuestDisplay 显示完成状态
这样做的巨大优势:
✅ 超低耦合:条件脚本完全不知道任务管理器的存在
✅ 容易调试:每个条件独立工作,容易测试
✅ 灵活扩展:新增条件类型只需新建脚本
✅ 可视化配置:在编辑器里拖拖拽拽就配置好了
这就是现代游戏任务系统的标准做法!你的理解完全正确!🎯
tokens used: 74016, model: DeepSeek API (deepseek-chat)
