最近搜索

第4节,创建我们的 eventListener 和RoadPoint脚本。

浏览:445
管理员 2021-10-20 14:52

image.png



import { _decorator, Component, Node, Vec3, Enum } from 'cc';
const { ccclass, property } = _decorator;
 
 
enum ROAD_POINT_TYPE {
    NORMAL = 1,
    START,
    GREETING,
    GOODBYE,
    END,
    AI_START
}
Enum(ROAD_POINT_TYPE);
 
enum ROAD_MOVE_TYPE {
    LINE = 1,
    BEND
}
Enum(ROAD_MOVE_TYPE);//序列化枚举
 
@ccclass('RoadPoint')
export class RoadPoint extends Component {
 
    public static RoadPointType = ROAD_POINT_TYPE;
    public static RoadMoveType = ROAD_MOVE_TYPE;
 
    @property({
        type: ROAD_POINT_TYPE,
        displayOrder: 1,
        tooltip:"点的类型"
    })
    type = ROAD_POINT_TYPE.NORMAL;
 
    @property({
        type: Node,
        displayOrder:2 
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.END;
        // }
    })
    nextStation: Node | null = null;
 
    @property({
        type: ROAD_MOVE_TYPE,
        displayOrder: 3
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.END;
        // }
    })
    move_type = ROAD_MOVE_TYPE.LINE;
 
    @property({
        type: Vec3, displayOrder: 4
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.GREETING || this.type === ROAD_POINT_TYPE.GOODBYE;
        // }
    })
    direction = new Vec3(1, 0, 0);//定义哪边开车门 拉客/送客 左边1   右边-1
 
    @property({
        displayOrder: 5
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.AI_START;
        // }
    })
    interval = 3;
 
    @property({
        displayOrder: 6
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.AI_START;
        // }
    })
    delayTime = 0;
 
    @property({
        displayOrder: 7
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.AI_START;
        // }
    })
    speed = 0.05;
 
    @property({
        displayOrder: 8
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type === ROAD_POINT_TYPE.AI_START;
        // }
    })
    cars = "201";
 
    @property({
        displayOrder: 9
        // ,
        // visible: function (this: RoadPoint) {
        //     return this.type !== ROAD_POINT_TYPE.END && this.move_type === ROAD_MOVE_TYPE.CURVE;
        // }
    })
    clockwise = true;
}




import { error, _decorator } from "cc";
const { ccclass } = _decorator;

interface IEventData{
    func:Function;
    target:any;
}

interface IEvent{
    [eventName:string] : IEventData[];
}

@ccclass("EventListener")
export class EventListener {
  
    public static handle :IEvent = {};

    public static on(eventName :string,cb:Function,target?:any){
        if(!this.handle[eventName]){
            this.handle[eventName] = [];
        }
        const data:IEventData = {func:cb,target:target}
        this.handle[eventName].push(data);
    }



    public static off(eventName :string ,cb:Function,target?:any){
        const list = this.handle[eventName];
        if(!list||list.length<=0){
            return;
        }
        for(let i=0;i<list.length;i++){
            const event  =  list[i];
            if(event.func===cb&& (!target||target==event.target)){
                list.splice(i,1);
                break;
            }
        }
    }

    public static dispatchEvent(eventName:string,...args:any){
        const list = this.handle[eventName];
        if(!list||list.length<=0){
            return;
        }
        for(let i=0;i<list.length;i++){
            const event  =  list[i];
            event.func.apply(event.target,args);
        }

    }


};


联系站长

站长微信:xiaomao0055

站长QQ:14496453