最近搜索

第16节 AI小车的运动实现

浏览:443
管理员 2021-11-03 14:07

修改 RoadPoint代码

image.png

    private _arrayCars :string[] = [];
    private _cd  :Function |null= null;

    public start(){
        this._arrayCars = this.cars.split(",");
    }
    
    public startSchedule(cd:Function){
        if(this.type!=ROAD_POINT_TYPE.AI_START){
            return;
        }
        this._cd = cd;
        this.scheduleOnce(this._startDelay,this.delayTime);//一定延迟时间 后开始发车。
    }
    public stopSchedule(){
        this.unschedule(this._startDelay);
        this.unschedule(this._scheduleCD);
    }
    private _startDelay(){
        this._scheduleCD();
        this.schedule(this._scheduleCD,this.interval,macro.REPEAT_FOREVER);//根据时间,间隔 每隔多少时间。生产一个小车
    }
    private _scheduleCD(){
        const index = Math.floor(Math.random()*this._arrayCars.length);
        if(this._cd){
            this._cd(this,this._arrayCars[index]);
        }
    }


修改carManager代码  入口 就在这里

image.png

import { _decorator, Component, Node, loader, Prefab } from 'cc';
import { Car } from './Car';
import { RoadPoint } from './RoadPoint';
import { PoolManager } from './data/PoolManager';
const { ccclass, property } = _decorator;

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

    @property({
        type: Car
    })
    mainCar: Car | null = null;
    //mainCai :Car  = null;

    private _currentPath: Node[] = [];//当前路径
    private _aiCars: Car[] = [];//当前的ai小车

    public reset(points: Node[]) {
        if (points.length <= 0) {
            console.log("没有point在map");
            return;
        }
        this._currentPath = points;
        this._createMainCar(points[0]);

        //从设置小车开始执行。
        this._startSchedule();
    }

    public controMoving(isRunnding = true) {
        if (isRunnding) {
            //这个mainCar就是脚本。
            this.mainCar?.startRunning();
        } else {
            this.mainCar?.stopRunning();
        }
    }
    private _createMainCar(point: Node) {
        this.mainCar?.setEntry(point, true);
    }
    
    private _startSchedule() {
        for(let i=1; i<this._currentPath.length; i++){
            const point = this._currentPath[i];
            //点上面有RoadPoint
            const roadPoint = point.getComponent(RoadPoint);
            roadPoint?.startSchedule(this._createEnemy.bind(this))
        }
    }

    private _stopSchedule() {
        
    }

    private _createEnemy(road : RoadPoint , carID:string){
        const path = `car/car${carID}`;
        const self = this;
        console.log("self=carmanager");
        loader.loadRes(path, Prefab, (err: any, prefab: Prefab) => {
            if (err) {
                console.warn(err);
                return;
            }
            const car  = PoolManager.getNode(prefab,self.node);
            const carComp = car?.getComponent(Car);//car的脚本 = carComp
            if(carComp!=null){
                this._aiCars.push(carComp);
                carComp?.setEntry(road.node);
                carComp.maxSpeed = road.speed;
                carComp.startRunning();
                carComp.moveAfterFinished(this._recycleAICar.bind(this));
            }
        }); 
    }

    private _recycleAICar(car :Car){
        //在列表中找到这个car
        console.log("carManager._recycleAICar=回收小车");
        const index  = this._aiCars.indexOf(car);
        if(index>=0){
            //-1是没有找到
            //pool 回收
            PoolManager.setNode(car.node);
            //从数组里面删除
            this._aiCars.splice(index,1);
        }
    }


}



修改car代码 

_arrivalStation 后面添加代码

image.png

image.png




联系站长

站长微信:xiaomao0055

站长QQ:14496453