Spring774
2025-10-27 a621979f62588ed1a899d17c937dc3aee8670ec1
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
108
109
import Global from "./Global";
import SoundManager from "./SoundManager";
 
const { ccclass, property } = cc._decorator;
 
@ccclass
export default class Ball extends cc.Component {
 
    private rigidbody: cc.RigidBody;
 
    private isHitBottom: boolean = false;
 
    private timer: number = 0;
    private speedupTimes: number = 0;
    private currSpeed: number = Global.BALL_SPEED;
    private collider: cc.PhysicsCircleCollider;
    private sprite: cc.Sprite = null;
    private hitWallCounter = 0;
 
    private manager = null;
 
    onLoad() {
        this.rigidbody = this.getComponent(cc.RigidBody);
        this.collider = this.getComponent('cc.PhysicsCircleCollider');
        //this.ballAudio = this.getComponent(cc.AudioSource);
        this.sprite = this.getComponent(cc.Sprite);
    }
 
    reuse(manager) {
        this.manager = manager;
    }
 
    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) {
        //保持速度
        this.remainSpeed();
        if (otherCollider.tag == Global.TAG_WALL_BOTTOM) {//球到底了
            this.isHitBottom = true;
            this.rigidbody.linearVelocity = cc.v2();
 
            this.node.runAction(cc.callFunc(this.recycle, this));
        } else if (otherCollider.tag == Global.TAG_WALL || otherCollider.tag == Global.TAG_Obstacle) {
            this.countHitWall();
        } else {
            this.hitWallCounter = 0;
        }
 
        SoundManager.playBall();
    }
 
    countHitWall() {
        this.hitWallCounter += 1;//
        if (this.hitWallCounter > 10) {//一直在墙中反弹,添加向下的力,避免无限反弹
            let currVelocity = this.rigidbody.linearVelocity;
            currVelocity.addSelf(cc.v2(Math.random() * 10 - 5, Math.random() * 10 - 5));
            this.rigidbody.linearVelocity = currVelocity;
            //this._hitWallCounter = 0;
        }
    }
 
    updateSkin(spriteFrame: cc.SpriteFrame) {
        this.sprite.spriteFrame = spriteFrame;
    }
 
    remainSpeed() {
        this.rigidbody.linearVelocity = this.rigidbody.linearVelocity.normalize().mul(this.currSpeed);
    }
 
    disablePhysics() {
        this.rigidbody.linearVelocity = cc.v2();
        if (this.collider) {
            this.collider.sensor = true;
            this.collider.apply();
        }
    }
 
    enablePhysics() {
        if (this.collider && this.collider.sensor) {
            this.collider.sensor = false;
            this.collider.apply();
        }
    }
 
    recycle() {
        this.manager.recycleBall(this);
    }
 
    fire(direction: cc.Vec2) {
        this.node.stopAllActions();
        this.timer = 0;
        this.speedupTimes = 0;
        this.isHitBottom = false;
        this.currSpeed = Global.BALL_SPEED;
        this.enablePhysics();
        this.rigidbody.linearVelocity = direction.mul(this.currSpeed);
    }
 
    update(dt) {
        //cc.log("ball alive. pos = " + this.node.position);
        if (this.isHitBottom) {
            return;
        }
 
        //速度太快会穿透刚体
        if (this.node.position.x > 400 || this.node.position.x < -400 || this.node.position.y > 600 || this.node.position.y < -600) {
            this.recycle();
        }
    }
}