Spring774
2025-10-30 a972605e1cf8122330de5b6ff53857be130972be
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
import Global from "./Global";
 
const { ccclass, property } = cc._decorator;
 
@ccclass
export default class Brick extends cc.Component {
    private static counterId = 1;
 
    private manager = null;
    private isObstacle = false;
    private id = 0;
    private collider: cc.PhysicsBoxCollider = null;
    onLoad() {
        this.collider = this.node.getComponent(cc.PhysicsBoxCollider);
    }
 
    public get ID() {
        return this.id;
    }
 
    reuse(manager) {
        this.manager = manager;
        this.id = Brick.counterId++;
    }
 
    init(isObstacle: boolean, color: cc.Color, width: number, physicWidth: number) {
        this.isObstacle = isObstacle;
        this.node.color = color;
        this.node.width = width;
        this.node.height = width;
 
        if (isObstacle) {
            this.collider.tag = Global.TAG_Obstacle;
        } else {
            this.collider.tag = 0;
        }
 
        this.collider.size = cc.size(physicWidth, physicWidth);
        this.collider.apply();
    }
 
    recycle() {
        this.manager.recycleBrick(this);
    }
 
    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) {
        if (this.isObstacle) {
            return;
        }
        this.node.runAction(cc.callFunc(this.recycle, this));
    }
}