美文网首页
cocos速度控制(update)

cocos速度控制(update)

作者: 我是胡歌 | 来源:发表于2023-11-14 22:49 被阅读0次

    update (dt) {this.ss.angle+=dt*2;},

    click1(){

        // 正常速度

        cc.kSpeed(1);

        // 获取当前速度

        // const v = cc.kGetSpeed();

        //若也想影响到物理系统需要开启同步刷新:

        //cc.director.getPhysicsManager().enabled = true;

        //cc.director.getPhysicsManager().enabledAccumulator = true;

    },

    click2(){

        // 慢动作

        cc.kSpeed(0.1);

    },

//===========================================js导入为插件

(function (global) {

    'use strict';

    var cc = global.cc;

    cc.log('k-cocos v0.1');

    // 游戏速率

    cc.director._kSpeed = 1;

    var _originCalculateDeltaTime = cc.Director.prototype.calculateDeltaTime;

    cc.director.calculateDeltaTime = function (now) {

        _originCalculateDeltaTime.call(this, now);

        this._deltaTime *= this._kSpeed;

    }

    cc.kSpeed = function (speed) {

        cc.director._kSpeed = speed;

    }

    cc.kGetSpeed = function() {

        return cc.director._kSpeed

    }

    // 触点数量控制

    cc.kMultTouch = function (count) {

        // 2.3.0 版本以上

        if (cc.internal && cc.internal.inputManager) {

            cc.internal.inputManager._maxTouches = count;

            return;

        }

        // 低版本

        if (_cc && _cc.inputManager) {

            _cc.inputManager._maxTouches = count;

        }

        // 低版本兼容 QQ_PLAY 的一段逻辑

        if (CC_QQPLAY && BK && BK.inputManager) {

            BK.inputManager._maxTouches = count;

        }

    }

    // 扩展移动方式脚本

    cc.kSimpleMove = cc.Class({

        name: 'cc_kSimpleMove',

        extends: cc.Component,

        properties: {

            speed_x: 0,

            speed_y: 0,

            accelerate_x: 0,

            accelerate_y: 0,

            hasAim: false,

            aimPos_x: 0,

            aimPos_y: 0

        },

        editor: {

            // 想让该组件最后执行 update

            executionOrder: 9999

        },

        // 获取速度

        getMoveSpeed: function() {

            return new cc.Vec2(this.speed_x, this.speed_y);

        },

        // 设置运动速度

        setMoveSpeed: function(x, y) {

            if (x && typeof x === 'object') {

                this.speed_x = x.x || 0;

                this.speed_y = x.y || 0;

            } else {

                this.speed_x = x || 0;

                this.speed_y = y || 0;

            }

        },

        // 获取加速度

        getAccelerate: function() {

            return new cc.Vec2(this.accelerate_x, this.accelerate_y);

        },

        // 设置加速度

        setAccelerate: function(x, y) {

            if (x && typeof x === 'object') {

                this.accelerate_x = x.x || 0;

                this.accelerate_y = x.y || 0;

            } else {

                this.accelerate_x = x || 0;

                this.accelerate_y = y || 0;

            }

        },

        // 设置目的地

        setDestination: function(aim, speed, accelerate) {

            this.aimPos_x = aim.x || 0;

            this.aimPos_y = aim.y || 0;

            speed = speed || 0;

            accelerate = accelerate || 0;

            var dx = this.aimPos_x - this.node.x;

            var dy = this.aimPos_y - this.node.y;

            var len = Math.sqrt(dx * dx + dy * dy);

            var r_x = dx / len;

            var r_y = dy / len;

            this.setMoveSpeed(speed * r_x, speed * r_y);

            this.setAccelerate(accelerate * r_x, accelerate * r_y);

            this.hasAim = true;

        },

        update: function(dt) {

            this.speed_x += this.accelerate_x;

            this.speed_y += this.accelerate_y;

            // 有目标时,判断是否到达位置,要对比移动前位置,移动后位置与当前位置

            if (this.hasAim) {

                var dir_x1 = this.aimPos_x > this.node.x ? 1 : -1;

                var dir_y1 = this.aimPos_y > this.node.y ? 1 : -1;

                // 进行位移

                this.node.x += this.speed_x * dt;

                this.node.y += this.speed_y * dt;

                // 位移后是否越过目的地

                var dir_x2 = this.aimPos_x > this.node.x ? 1 : -1;

                var dir_y2 = this.aimPos_y > this.node.y ? 1 : -1;

                if (((dir_x1 * dir_x2) < 0) || ((dir_y1 * dir_y2) < 0)) {

                    this.hasAim = false;

                    this.node.x = this.aimPos_x;

                    this.node.y = this.aimPos_y;

                    this.setAccelerate(0, 0);

                    this.setMoveSpeed(0, 0);

                }

            } else {

                this.node.x += this.speed_x * dt;

                this.node.y += this.speed_y * dt;

            }

        }

    });

    // 强化节点

    cc.kNode = function (node) {

        // 节点数字标志

        node.kTag = 0;

        // 记录节点信息

        node.kInfo = 'init';

        // 简易状态机

        node._kState = 'init';

        Object.defineProperties(node, {

            // 简易状态机

            kState: {

                get() {

                    return this._kState;

                },

                set(val) {

                    var old = this._kState;

                    this._kState = val;

                    // 如果存在回调则调用,传入新旧状态

                    this.kStateCb && this.kStateCb(val, old);

                }

            },

            // 当前节点上所有组件

            kComponents: {

                get() {

                    return this._components;

                },

                set(val) {

                    cc.error(`can not set kComponents, please use addComponent()`);

                }

            },

            // 第一个子节点

            kFirstChild: {

                get() {

                    return this.children[0];

                },

                set(val) {

                    cc.error(`can not set kFirstChild, please use addChild()`);

                }

            },

            // 第二个子节点

            kSecondChild: {

                get() {

                    return this.children[1];

                },

                set(val) {

                    cc.error(`can not set kSecondChild, please use addChild()`);

                }

            },

            // 第三个子节点

            kThirdChild: {

                get() {

                    return this.children[2];

                },

                set(val) {

                    cc.error(`can not set kThirdChild, please use addChild()`);

                }

            },

            // 最后一个子节点

            kLastChild: {

                get() {

                    return this.children[this.childrenCount-1];

                },

                set(val) {

                    cc.error(`can not set kFirstChild, please use addChild()`);

                }

            }

        });

        // 自定义 _hitTest 回调

        node.kHitTest = function (cb) {

            this._hitTest = cb;

        }

        return node;

    }

})(window)

//==========================================ts

declare namespace cc {

    /**

     * 控制游戏速率,影响 dt

     * @param speed 速度

     */

    export function kSpeed(speed: number): void;

    /**

     * 获取当前游戏速率

     */

    export function kGetSpeed(): number;

    /**

     * 将普通节点扩展成超级节点

     * @param node 节点

     */

    export function kNode(node: cc.Node): cc.Node;

    /**

     * 传入 0 时禁止所有点击,传入 1 为单点触控,一些平台无法超过 5 个触点

     * @param count 触点数量

     */

    export function kMultTouch(count: number): void;

    /**

     * 节点扩展属性与方法

     */

    export interface Node {

        /**

         * 扩展属性,数字标签

         */

        kTag: number;

        /**

         * 扩展属性,字符串信息

         */

        kInfo: string;

        /**

         * 扩展属性,简易状态机

         */

        kState: string;

        /**

         * 扩展属性,第一个子节点

         */

        kFirstChild: cc.Node;

        /**

         * 扩展属性,最后一个子节点

         */

        kLastChild: cc.Node;

        /**

         * 扩展属性,获取当前节点上所有组件

         */

        kComponents: any[];

        /**

         * 扩展方法,kInfo 改变后触发

         */

        kStateCb: (newVal: string, oldVal: string) => void;

    }

    export class kSimpleMove extends Component {

        /**

         * 设置当前移动速度

         */

        setMoveSpeed: (x: number | cc.Vec2 = 0, y: number = 0) => void;

        /**

         * 获取当前移动速度

         */

        getMoveSpeed: () => cc.Vec2;

        /**

         * 设置当前加速度

         */

        setAccelerate: (x: number | cc.Vec2 = 0, y: number = 0) => void;

        /**

         * 获取当前加速度

         */

        getAccelerate: () => cc.Vec2;

        /**

         * 设置目的地和运动时速度与可选的加速度,到达后速度与加速度归零

         */

        setDestination: (aim: cc.Vec2, speed: number, accelerate?: number) => void;

    }

}

相关文章

网友评论

      本文标题:cocos速度控制(update)

      本文链接:https://www.haomeiwen.com/subject/ayfswdtx.html