当前位置: 首页 > news >正文

游戏人物设计 简单思路 那任务简单说是显示的 完成任务是每个条件脚本自己判断 完成后发给任务脚本信息

对的!你的理解完全正确。这就是事件驱动架构的任务系统。让我给你一个超清晰的实现:

核心思想:分工明确
任务脚本:只负责显示任务和监听完成信号
条件脚本:各自判断完成条件,完成后发信号

  1. 极简任务管理器 ✨
    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;
    

    }
    }

  2. 各种条件判断脚本 🎯
    收集条件脚本
    TYPESCRIPT
    @ccclass(‘CollectCondition’)
    export class CollectCondition extends Component {

    @property
    targetItem: string = “”; // 要收集的物品
    @property
    requiredCount: number = 1; // 需要数量
    @property
    questId: string = “”; // 对应的任务ID

    private 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 = “”; // 对应的任务ID

    private 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 = “”; // 对应的任务ID

    onLoad() {
    // 监听位置变化
    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);}
    

    }
    }

  3. 在编辑器中的设置方式 🎮
    任务管理器节点:
    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”

  4. 游戏启动任务 📋
    TYPESCRIPT
    @ccclass(‘GameStarter’)
    export class GameStarter extends Component {

    @property
    startingQuest: QuestData = null; // 在编辑器里配置初始任务

    start() {
    // 游戏开始后显示第一个任务
    const questManager = this.node.getComponent(SimpleQuestManager);
    questManager.showQuest(this.startingQuest);
    }
    }

  5. 任务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)

http://www.dtcms.com/a/528435.html

相关文章:

  • 利用消息队列(MQ)设计:解耦与异步削峰的艺术
  • 自由学习记录(111)
  • ESP32使用笔记(基于ESP-IDF):小智AI的ESP32项目架构与启动流程全面解析
  • 网站建设 软文网站创作思路
  • 未来之窗昭和仙君(三十七)抽奖随机算法修仙体——东方仙盟筑基期
  • HCIP---作业
  • 海天建设集团公司网站vi应用设计
  • Mybatis10-xml文件与mapper文件的目录位置说明
  • 安全的网站网站开发要
  • 面向模块的综合技术之综合策略优化(六)
  • Mem0:构建具有可扩展长期记忆的生产级AI代理 - 论文学习总结1
  • 【三相异步电动机判断好坏】
  • 整体设计 全面梳理复盘 之6 整体设计表格体系与执行逻辑迭代
  • SpringBoot集成Elasticsearch | Spring官方场景启动器(Spring Data Elasticsearch)方式
  • 【计挑赛】程序设计类真题(C++)
  • HTML HTML5基础(1)
  • 2025年9月电子学会全国青少年软件编程等级考试(Python五级)真题及答案
  • (论文速读)Anyattack: 面向视觉语言模型的大规模自监督对抗性攻击
  • 多线程六脉神剑第六剑:事件同步 (AutoResetEvent/ManualResetEvent)
  • Vue3 Composition API 实战指南
  • asp网站幻灯片不显示wordpress的站点是什么
  • 异步编程 await 和 async
  • Flask 学习路线图
  • 大数据统计网站南宁7天优化网络科技公司
  • ajax网站开发技术网店设计素材
  • GitHub 热榜项目 - 日榜(2025-10-25)
  • 【bug解决】[string “tolua.lua“]:1: ‘=‘ expected
  • Windows 10/11用户报告开始菜单和搜索栏故障
  • 仓颉语言核心技术解析:如何开发高性能服务端应用
  • Redis分布式锁演进全解析