[开发者心得] 坤坤出品|PrefabEvent二次开发教程——收集物协议⑨

[复制链接]
1025 |0
森林鹿 发表于 2023-3-6 14:59:24 | 显示全部楼层 |阅读模式
目录
收集物协议⑨


不懂这是什么的小伙伴建议先看初始化流程①哦~


收集物协议(PrefabEvtCollection)

这个协议用于收集物的收集监听,获取收集物,以及相关UI的管理。需要配合DBServerTools进行使用,做到对已经收集到的收集物的过滤以及存储收集到的收集物。主要应用于收集物的获取和通知,配合Notify协议可以达到通知的效果。

示例-收集物获取:
需要安装一个收集物系统和图鉴功能预制体,并且在拖出的预制体中去设置这个收集物的信息。
image.png

下面的示例使用一个触发器来监听玩家的进入后,使用了DBTools获取已经有的收集物进行过滤避免重复收集,然后再添加到存档里面,最后通过addCollection去通知图鉴的获取。



import { PrefabEvent } from "./PrefabEvent";

@Core.Class
export default class AtlasItem extends Core.Script {

    public collider: Gameplay.Trigger;

    private getGuid(): string {
        //父节点的guid
        return this.gameObject.parent.guid;
    }

    onStart() {

        const handle = setTimeout(async () => {
            
            if (this.gameObject == null) return;
            clearInterval(handle);

            if (Util.SystemUtil.isServer()) {

                //获取触发器
                this.collider = this.gameObject as Gameplay.Trigger;
                //监听触发器进入事件
                this.collider.onEnter.add(async (go: Core.GameObject) => {
                    //判断是否是角色进入了触发器
                    if (go instanceof Gameplay.Character) {
                        let char = go as Gameplay.Character;
                        //获取图鉴数据
                        let hasAtlasId = await PrefabEvent.DBServerTool.asyncGetValue<string[]>(
                            char.player.getUserSystemId(), PrefabEvent.PrefabEvtCollection.name);
                        //检查是否并不含有图鉴数据
                        if (!hasAtlasId) hasAtlasId = [];
                        //过滤防止重复获取图鉴
                        if (hasAtlasId.indexOf(this.getGuid()) != -1) {
                            console.error("图鉴已获得");
                            return;
                        }
                        //将自己对应的图鉴数据推入
                        hasAtlasId.push(this.getGuid());
                        //保存图鉴数据
                        await PrefabEvent.DBServerTool.asyncSetValue(char.player.getUserSystemId(), PrefabEvent.PrefabEvtCollection.name, hasAtlasId);
                        //发出添加收集物的事件
                        PrefabEvent.PrefabEvtCollection.addCollection(this.getGuid(), char.player.getPlayerID());

                    }

                })

            }

        }, 100);

    }
}



回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表