MatchMaster/assets/jungle_treasure/script/JungleTitleShine.ts
COMPUTER\EDY da57305717 更新
2026-08-07 16:06:02 +08:00

82 lines
2.9 KiB
TypeScript

const { ccclass, requireComponent } = cc._decorator;
/** Jungle title 专用的左至右高亮扫光。 */
@ccclass
@requireComponent(cc.Sprite)
export default class JungleTitleShine extends cc.Component {
private sprite: cc.Sprite = null;
private material: cc.Material = null;
private moveDuration: number = 0.85;
// 扫光总周期约 6 秒,与每 2 秒一次的首礼包动画错开。
private repeatDelay: number = 5.15;
private startDelay: number = 3;
private elapsed: number = 0;
private delayRemaining: number = 0;
private startPosition: number = -0.35;
private endPosition: number = 1.35;
protected onLoad(): void {
this.sprite = this.getComponent(cc.Sprite);
this.delayRemaining = this.startDelay;
this.loadMaterial();
}
protected update(dt: number): void {
if (!this.material) {
return;
}
if (this.delayRemaining > 0) {
this.delayRemaining = Math.max(0, this.delayRemaining - dt);
return;
}
this.elapsed += dt;
let progress = Math.min(1, this.elapsed / this.moveDuration);
let position = this.startPosition
+ (this.endPosition - this.startPosition) * progress;
this.material.setProperty("shinePosition", position);
if (progress >= 1) {
this.elapsed = 0;
this.delayRemaining = this.repeatDelay;
this.material.setProperty("shinePosition", this.startPosition);
}
}
protected onDestroy(): void {
if (this.material && cc.isValid(this.material)) {
this.material.destroy();
}
this.material = null;
this.sprite = null;
}
private loadMaterial(): void {
let bundle = cc.assetManager.getBundle("jungle_treasure");
if (!bundle || !this.sprite) {
cc.warn("[JungleTreasure] title 扫光材质所在分包未加载");
return;
}
bundle.load("effect/title_shine", cc.EffectAsset, (error: Error, effect: cc.EffectAsset) => {
if (error || !effect || !cc.isValid(this.node) || !this.sprite) {
if (error) {
cc.warn("[JungleTreasure] title 扫光 Shader 加载失败", error);
}
return;
}
let material = new cc.Material();
// Cocos Creator 2.4 使用 effectAsset 为运行时材质指定 Shader。
// @ts-ignore
material.effectAsset = effect;
material.setProperty("shinePosition", this.startPosition);
material.setProperty("shineWidth", 0.13);
material.setProperty("shineStrength", 1.8);
material.setProperty("shineAngle", 0.2);
material.setProperty("shineColor", [1.0, 0.95, 0.68, 1.0]);
this.material = material;
this.sprite.setMaterial(0, material);
});
}
}