最近搜索

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

浏览:682
管理员 2021-10-20 22:52

image.png


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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