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.
AdLike2048/assets/script/ui/BattleGame/DiamondTreasure.ts

90 lines
2.9 KiB

3 weeks ago
import { _decorator, Component, Node, sp, tween, v3, Vec3 } from 'cc';
import { BattleSpinePlayback } from 'db://assets/script/battle/presentation/BattleSpinePlayback';
const { ccclass } = _decorator;
/** 钻石宝箱(MidBoss 击杀掉落 → 飞左下角 → 点击播放开箱动画后销毁) */
@ccclass('DiamondTreasure')
export class DiamondTreasure extends Component {
private Sk: sp.Skeleton | null = null;
private clickSpine: Node | null = null;
isCanClick = false;
protected onLoad(): void {
this.Sk = this.node.getChildByName('宝箱')?.getComponent(sp.Skeleton) ?? null;
this.clickSpine = this.node.getChildByName('dianji') ?? null;
if (this.clickSpine) {
this.clickSpine.active = false;
}
}
protected onEnable(): void {
this.node.on(Node.EventType.TOUCH_START, this.onClickTreasure, this);
}
protected onDisable(): void {
this.node.off(Node.EventType.TOUCH_START, this.onClickTreasure, this);
}
public playSpine(name: string, callback: (() => void) | null = null, timeScale = 1, loop = true): void {
if (!this.Sk) {
callback?.();
return;
}
BattleSpinePlayback.play(this.Sk, name, { loop, timeScale, onComplete: callback ?? undefined });
}
/** 从击杀点出现并飞向战斗左下角 */
public fallTreasure(fromWorld?: Vec3): void {
if (fromWorld) {
this.node.worldPosition = fromWorld.clone();
}
this.isCanClick = false;
if (this.clickSpine) {
this.clickSpine.active = false;
}
this.playSpine('chuxian', () => {
if (gg.game.CurentSelectChapterId === 1 && this.clickSpine) {
this.clickSpine.active = true;
}
this.playSpine('idle');
this.isCanClick = true;
const target = this._resolveCornerWorldPos();
tween(this.node).to(1, { worldPosition: target }).start();
}, 1, false);
}
/** 点击播放开箱动画并销毁节点 */
public openTreasure(): void {
if (!this.isCanClick) return;
this.isCanClick = false;
gg.sdk.reportDY(
'inLevel',
`${gg.game.getReportDYBattleLabel()}-点击钻石宝箱`,
);
if (this.clickSpine) this.clickSpine.active = false;
this.playSpine('play', () => {
if (this.node?.isValid) {
this.node.destroy();
}
}, 1.5, false);
}
onClickTreasure(): void {
this.openTreasure();
}
/** 左下角落点(与老项目 Map 偏移一致) */
private _resolveCornerWorldPos(): Vec3 {
const mapNode = gg.game.CurentBattle?.Map?.node;
if (mapNode?.isValid) {
const p = mapNode.worldPosition;
return v3(p.x - 275, p.y - 380, p.z);
}
const wp = this.node.worldPosition;
return v3(wp.x - 120, wp.y - 200, wp.z);
}
}