You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.6 KiB
86 lines
2.6 KiB
import { _decorator, CCInteger, Color, Component, Node, PolygonCollider2D, RigidBody2D, Sprite } from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/** 木板脱离屏幕后销毁的 Y 阈值(本地坐标) */
|
|
const BOARD_FALL_DESTROY_Y = -1000;
|
|
/** 螺丝全部拔完后木板掉落时的整体色调 */
|
|
const BOARD_FALL_GRAY = '#646464';
|
|
|
|
/**木板子元素*/
|
|
@ccclass('Aelement')
|
|
export class Aelement extends Component {
|
|
|
|
@property(CCInteger)
|
|
aelementHoleNum: number = 2;
|
|
|
|
/** 木板上尚未脱落的螺丝数量 */
|
|
private _remainingScrews = 0;
|
|
/** 螺丝全部脱落后,开始检测木板掉落 */
|
|
private _fallCheckEnabled = false;
|
|
|
|
/** 木板是否已进入掉落态(置灰),不再参与遮挡其它螺丝 */
|
|
get isFalling(): boolean {
|
|
return this._fallCheckEnabled;
|
|
}
|
|
|
|
/** 获取木板上所有孔洞节点(孔洞1、孔洞2…) */
|
|
getHoleNodes(): Node[] {
|
|
return this.node.children
|
|
.filter((c) => c.name.startsWith('孔洞'))
|
|
.sort((a, b) => a.name.localeCompare(b.name, 'zh'));
|
|
}
|
|
|
|
/** 生成螺丝后登记数量 */
|
|
initScrewCount(count: number) {
|
|
this._remainingScrews = count;
|
|
this._fallCheckEnabled = false;
|
|
}
|
|
|
|
/** 螺丝从木板上脱落(弹出或消除)时由 LuosiDing 调用 */
|
|
onScrewLeft() {
|
|
if (this._remainingScrews <= 0) return;
|
|
this._remainingScrews--;
|
|
if (this._remainingScrews <= 0) {
|
|
this._fallCheckEnabled = true;
|
|
this._applyFallGrayColor();
|
|
}
|
|
}
|
|
|
|
/** 螺丝拔完开始掉落时,将木板及子节点 Sprite 整体置灰 */
|
|
private _applyFallGrayColor() {
|
|
const gray = new Color().fromHEX(BOARD_FALL_GRAY);
|
|
const visit = (n: Node) => {
|
|
const sp = n.getComponent(Sprite);
|
|
if (sp) sp.color = gray;
|
|
for (const child of n.children) visit(child);
|
|
};
|
|
visit(this.node);
|
|
}
|
|
|
|
/** 开启木板刚体与多边形碰撞体(prefab 默认关闭,生成后再启用) */
|
|
enablePhysics() {
|
|
const rb = this.node.getComponent(RigidBody2D);
|
|
if (rb){
|
|
rb.enabled = true;
|
|
//rb.gravityScale = 0.5
|
|
rb.angularDamping = 0.8;
|
|
}
|
|
|
|
|
|
const poly = this.node.getComponent(PolygonCollider2D);
|
|
if (poly) {
|
|
poly.enabled = false;
|
|
poly.enabled = true;
|
|
}
|
|
}
|
|
|
|
update() {
|
|
if (!this._fallCheckEnabled || !this.node?.isValid) return;
|
|
if (this.node.position.y < BOARD_FALL_DESTROY_Y) {
|
|
console.log('木板销毁完成');
|
|
this.node.destroy();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|