[开发者心得] “数据结构课作业笔记”#006-数组-从地刺到冲击层-队列的预兆

[复制链接]
546 |1
脑的研发记录 发表于 2023-11-10 18:07:58 | 显示全部楼层 |阅读模式
本帖最后由 脑的研发记录 于 2023-11-10 18:07 编辑

“数据结构课作业笔记”#006-数组-从地刺到冲击层-队列的预兆



所用编辑器版本0.25.0.4

目录总览:
零.开发背景

壹.第一个优化数组
贰.所需语法
叁.应用说明
肆.相关参考链接

伍.优化说明




零.开发背景
昨天写完地刺,还在想怎么过渡到下一篇,给地刺改了改参数,想起来新的数据类型与之前的数据比较像,应该能过渡,就着原来的基础直接改代码。在上午的概率统计课上灵感乍现,地刺方块先进后出,地刺方块可以先进先出,这样地刺生长消失,就成冲击波生长消失了。于是下午3:50下课回到宿舍开发,复制粘贴代码,调整参数,加一点新的函数,最终16:40改出来代码。


16:40-17:53写帖子
这次写的快是因为难度都给了上次开发
所以这次帖子短,实际上是因为相关内容被前一篇帖子分担了
所以实际上学习一学就学的一整套,问题一解决,连后续遇到的问题也解决了。
这就是学习新新新新的东西的意外故事。

壹.第一个优化数组
不多说,上代码,复制粘贴直接跑
最终成果

class ooh {
    n: Gameplay.GameObject;
    id: string;
}

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

    protected build: Array<ooh> = new Array(50);
    // 100改小成50,控制距离
    protected i: number = -1;
    protected flag_grow: number = 0;

    /** 当脚本被实例后,会在第一帧更新前调用此函数 */
    protected onStart(): void {
        for (var j = 0; j < this.build.length; j++) {
            this.build[j] = new ooh();
        }

        // AssetUtil.assetLoaded("197386");

        AssetUtil.asyncDownloadAsset("197386");

        setTimeout(() => {
            for (var j = 0; j < this.build.length; j++) {

                let obj = Gameplay.GameObject.spawn({
                    guid: "197386",
                    replicates: true,
                    transform: new Transform(new Vector(0, j*50, 25 ),
                    // 方块y方向坐标为j*50,以此实现每个方块间隔50,方块高度25
                        new Rotation(0, 0, 0),
                        new Vector((1+j*j)/90, 1, (1+j*j)/90))
                        // 指数级别增长,方块变宽大
                });

                // this.build[++this.i].n = obj;
                this.in(obj);

                // this.build[j].n.setCollision(Type.PropertyStatus.On, true);
                // this.build[j].n.setVisibility(Type.PropertyStatus.On, true);
                // this.build[j].id = "ooh" + this.i;

            }
        }, 3000);


        setTimeout(() => {
            var timer = setInterval(() => {
                console.log(this.i);
                // console.log(this.flag_grow);
                console.log(this.build.length);

                if (this.flag_grow == 0) {
                    var have: ooh = this.out();
                    have.n.setCollision(Type.PropertyStatus.Off, true);
                    have.n.setVisibility(Type.PropertyStatus.Off, true);
                    // if (this.i == -1) {
                    //     this.flag_grow = 1;
                    // }
                    if (this.i == this.build.length) {
                        this.flag_grow = 1;
                        this.i=-1;
                    }


                } else if (this.flag_grow == 1) {
                    // if (this.i == this.build.length) {
                    //     this.flag_grow = 0;
                    // }
                    if (this.i == this.build.length) {
                        this.flag_grow = 0;
                        this.i=-1;
                    }
                    var have: ooh = this.show();
                    have.n.setCollision(Type.PropertyStatus.On, true);
                    have.n.setVisibility(Type.PropertyStatus.On, true);
                }
            }, 20);
        }, 5000);
    }




    public in(obj: Gameplay.GameObject) {
        this.i++;
        this.build[this.i].n = obj;
    }

    public show(): ooh {
        this.i++;
        var have = this.build[this.i];
        return have;
    }

    // public out(): ooh {
    //     var have = this.build[this.i];
    //     this.i--;
    //     return have;
    // }

    public out(): ooh {
        var have = this.build[this.i];
        this.i++;
        return have;
    }

}





实现了一个从起点开始生长,然后从起点开始消失的方块堆。


代码成分说明:
定义结构初始化,绿色代码不用管

class ooh {
    n: Gameplay.GameObject;
    id: string;
}

// @Core.Class
// export default class adds extends Core.Script {

    protected build: Array<ooh> = new Array(50);
    // 100改小成50,控制距离
    protected i: number = -1;
    protected flag_grow: number = 0;

    /** 当脚本被实例后,会在第一帧更新前调用此函数 */
    // protected onStart(): void {
        for (var j = 0; j < this.build.length; j++) {
            this.build[j] = new ooh();
        }






资源加载且产出,数字变化长度延
 AssetUtil.asyncDownloadAsset("197386");

        setTimeout(() => {
            for (var j = 0; j < this.build.length; j++) {

                let obj = Gameplay.GameObject.spawn({
                    guid: "197386",
                    replicates: true,
                    transform: new Transform(new Vector(0, j*50, 25 ),
                    // 方块y方向坐标为j*50,以此实现每个方块间隔50,方块高度25
                        new Rotation(0, 0, 0),
                        new Vector((1+j*j)/90, 1, (1+j*j)/90))
                        // 指数级别增长,方块变宽大
                });

                // this.build[++this.i].n = obj;
                this.in(obj);

                // this.build[j].n.setCollision(Type.PropertyStatus.On, true);
                // this.build[j].n.setVisibility(Type.PropertyStatus.On, true);
                // this.build[j].id = "ooh" + this.i;

            }
        }, 3000);






新瓶旧酒酌三行,负一替换
  setTimeout(() => {
            var timer = setInterval(() => {
                console.log(this.i);
                // console.log(this.flag_grow);
                console.log(this.build.length);

                if (this.flag_grow == 0) {
                    var have: ooh = this.out();
                    have.n.setCollision(Type.PropertyStatus.Off, true);
                    have.n.setVisibility(Type.PropertyStatus.Off, true);
                    // if (this.i == -1) {
                    //     this.flag_grow = 1;
                    // }
                    if (this.i == this.build.length) {
                        this.flag_grow = 1;
                        this.i=-1;
                    }


                } else if (this.flag_grow == 1) {
                    // if (this.i == this.build.length) {
                    //     this.flag_grow = 0;
                    // }
                    if (this.i == this.build.length) {
                        this.flag_grow = 0;
                        this.i=-1;
                    }
                    var have: ooh = this.show();
                    have.n.setCollision(Type.PropertyStatus.On, true);
                    have.n.setVisibility(Type.PropertyStatus.On, true);
                }
            }, 20);
        }, 5000);
    }

只要是序号this.i等于数组长度,this.i就改成-1,回到数组起点,
回到起点的时候,flag_grow改变,每次循环都进入另一个if,执行show函数。show函数对应冲击波方块依次产生,out函数对应冲击波方块依次消失。





递减递加顺序变,就地重写展新颜。


public in(obj: Gameplay.GameObject) {
        this.i++;
        this.build[this.i].n = obj;
    }

    public show(): ooh {
        this.i++;
        var have = this.build[this.i];
        return have;
    }

    // public out(): ooh {
    //     var have = this.build[this.i];
    //     this.i--;
    //     return have;
    // }

    public out(): ooh {
        var have = this.build[this.i];
        this.i++;
        return have;
    }



原来从最后一个倒回到第一个,i--,是从this.i等于数组长度时,从最大值减少。现在由于对应从第一个到最后一个,i要从第一个开始,i就回到起点,然后一直数到最后一个,就i++。


不是因为if改变了,所以i--变成i++,而是最后产生的先消失,对应从小到大,从大到小,转变成,先产生的先消失,最后产生的最后消失,对应从小到大,然后跳回开头,继续从小到大。这样从第一个投产数组的只在一端增加减少,变成第一个优化数组的在一端增加,在一端减少。出现了两头跳转——从尾跳到头的新情况。



贰.所需语法

关于预先加载概念

预先加载物体,这样就不用在场景里拖进一个方块了,相当于提前把数据读取到游戏里,但是不使用数据。
     AssetUtil.asyncDownloadAsset("197386");



关于数据类型,在第一个自由数组中,interface改成class,  这样类型变化之后,数组的初始化也从手写数据换成自动填充。

class ooh {
    n: Gameplay.GameObject;
    id: string;
}

// @Core.Class
// export default class adds extends Core.Script {

    protected build: Array<ooh> = new Array(50);
    // 100改小成50,控制距离
    protected i: number = -1;
    protected flag_grow: number = 0;

    /** 当脚本被实例后,会在第一帧更新前调用此函数 */
    // protected onStart(): void {
        for (var j = 0; j < this.build.length; j++) {
            this.build[j] = new ooh();
        }








这个其实也是预先加载,数组的预先加载,生成一个有50项的数组。
 protected build: Array<ooh> = new Array(50);



这个是数组内的每一项的预先加载,数据项内预先加载格式,如果不写的话,直接跑代码,会有黄色字的报错“ TypeError: Cannot read property 'n' of undefined
其实就是因为n属于ooh这个class类型中定义的变量,new 分配了内存,然后把内存的代号传进去数组里,这样数组通过代号自动找到内存,然后把数据存进去。
其实 定义number类型变量a 本身就是找块地方存数字。只不过class类型的数据,系统不能按老方式自动分配内存,就需要手动写代码命令系统去按格式分配内存。
new 就是命令分配内存,  而 ooh()就是指定的内存分割成的格式。这一大块内存的划分地盘,一小块存数字number,另一大块存物体数据。
 this.build[j] = new ooh();



一般这个阶段的程序跑起来看不见效果,首先检查是不是没有分配内存,因为这个检查项最好改代码。





叁.应用说明
游戏道具上
冲击波,加点特效,就能玩,加点冲量预制体再改改应该可以有击退效果。 也可以修改冲击波的形状,形成分叉冲击波,也能改成陷阱。从下往上冲击困住玩家

肆.相关参考链接


返回主站
普通大学生“数据结构”课作业笔记#000-目录-逻辑结构与存储结构 口袋方舟论坛|面向全年龄的UGC互动内容平台与交流社区 (ark.online)



伍.优化说明
代码优化上
调整代码结构,封装更完整,在其他代码文件中调用
游戏开发上
大冲击波,小冲击波,调整冲击波消失时间,平衡不同冲击波的效果,保证游戏公平性
玩法设计上
除了第一人称释放技能攻击,扣血,
也可以像我的世界放置方块,用冲击波造场景,用冲击波与场景交互,习得冲击波技能,天堑变通途。



回复

使用道具 举报

空伊伊 发表于 2023-11-10 19:02:55 | 显示全部楼层
加油,保持这种思考习惯,你就是未来的巨佬!
回复

使用道具 举报

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