最近搜索

第8节 让小车,按点移动,到最后会停。

浏览:560
管理员 2021-10-20 16:45




参考代码Car的脚本。


import { _decorator, Component, Node, Vec3 } from 'cc';
import { RoadPoint } from './RoadPoint';
const { ccclass, property } = _decorator;

@ccclass('Car')
export class Car extends Component {

    public _currentRoadPoint: RoadPoint | null = null;

    private _pointA = new Vec3();//第1条线, 的起点。
    private _pointB = new Vec3();//第1条线, 的下个点。
    private _currentSpeed = 1.2;
    private _isMove = false;

    private _offset = new Vec3();

    private _tempVec = new Vec3();//用于计算小车离开下个点的距离


    public update(dt: number) {
        if (this._isMove) {
            console.log("car--update-move");
            //这是快捷世界坐标。不能让我们改。所以先存起来。
            this._offset.set(this.node.worldPosition);
            switch (this._currentRoadPoint?.move_type) {
                case RoadPoint.RoadMoveType.BEND:
                    break;
                default:    
                //A就是我起点(当前的点 小车自身的点)。B就是下一个点。
                //从点a  到 点b  z的坐标是减少    b.z=50   a.z=0  大于0
                    const z = this._pointA.z - this._pointB.z;
                    console.log("点a到点b的z差距:"+z);
                    if (z !== 0) {
                        //代表我们动z轴就能跑到 目的,然后判断 加 还是 减
                        if (z > 0) {
                            console.log("减法:");
                            //offset就是小车当前位置。随着动而动
                            this._offset.z -= this._currentSpeed;
                            if(this._offset.z < this._pointB.z){
                                this._offset.z = this._pointB.z;
                            }
                        } else {
                            this._offset.z += this._currentSpeed;
                            if(this._offset.z>this._pointB.z){
                                this._offset.z  = this._pointB.z;
                            }
                        }
                    }else{
                        const x = this._pointA.x - this._pointB.x;
                        if (x > 0) {
                            this._offset.x += this._currentSpeed;

                            if(this._offset.x>this._pointB.x){
                                this._offset.x  = this._pointB.x;
                            }
                        } else {
                            this._offset.x -= this._currentSpeed;
                            if(this._offset.x < this._pointB.x){
                                this._offset.x  = this._pointB.x;
                            }
                        }
                    }
                    break;
            }
            //重新设置小车的坐标
            this.node.setWorldPosition(this._offset);

            //计算  pointB和offset之间的长度。
            Vec3.subtract(this._tempVec,this._pointB,this._offset);
            if(this._tempVec.length()<=0.01){
                this._arrivalStation();
            }
        }
    }

    /**
     * 
     * @param entry 是第1条路径 的起始点。上面有脚本RoadPoint
     */
    public setEntry(entry: Node) {
        //世界坐标,绝对位置,他在别的模型的位置,可能有偏移量,不准。所以使用世界坐标系
        this.node.setWorldPosition(entry.worldPosition);

        //这是传来的  1条直接的2个点。创建路径的点。上面有RoadPoint脚本
        this._currentRoadPoint = entry.getComponent(RoadPoint) as RoadPoint;
        if (!this._currentRoadPoint) {
            console.log("这个点没有roadPoint" + entry.name);
        }
        //A就是我起点(当前的点 小车自身的点)。B就是下一个点。
        this._pointA.set(entry.worldPosition);
        this._pointB.set(this._currentRoadPoint.nextStation.worldPosition);
        
        //计算车身,是否需要,转动方向
        //下一点的z  减去 小车的z  如果是0    如果不是0   
        // 原理我是知道,但是为什么会有这种效果。我不知道。
        const z = this._pointB.z - this._pointA.z;// 50 - 80 
        console.log("z:"+z);
        if (z !== 0) {
            if (z < 0) {
                console.log("z是负数,没有偏移不用转向")
                //没有偏移不用转向  这个函数,经过测试 会自动修正方向
                this.node.eulerAngles = new Vec3();
            } else {
                console.log("z是正数")
                //旋转用的是y转。
                this.node.eulerAngles = new Vec3(0, 180, 0);
            }
        } else {
            const x = this._pointB.x - this._pointA.x;
            console.log("x:"+x);
            if (x > 0) {
                console.log("x是正数")
                this.node.eulerAngles = new Vec3(0, 270, 0);
            } else {
                console.log("x是负数")
                this.node.eulerAngles = new Vec3(0, 90, 0);
            }
        }
    }

    public startRunning() {
        if (this._currentRoadPoint) {
            this._isMove = true;
        }
    }

    public stopRunning() {
        this._isMove = false;
    }

    private _arrivalStation(){
        console.log("arrival....");
        this._pointA.set(this._pointB);

        //这个就是B上面的  RoadPoint   
        this._currentRoadPoint= this._currentRoadPoint?.nextStation?.getComponent(RoadPoint) as RoadPoint;
        if(this._currentRoadPoint.nextStation){
            this._pointB.set(this._currentRoadPoint.nextStation.worldPosition);
        }else{
            this._isMove= false;
            this._currentRoadPoint = null;
        }
 
    }


}


联系站长

站长微信:xiaomao0055

站长QQ:14496453