MatchMaster/assets/shop/script/ShopJungleShine.ts
2026-08-07 10:59:22 +08:00

78 lines
2.7 KiB
TypeScript

const { ccclass, requireComponent } = cc._decorator;
/** 商城 Jungle 背景专用的横向扫光。 */
@ccclass
@requireComponent(cc.Sprite)
export default class ShopJungleShine extends cc.Component {
private sprite: cc.Sprite = null;
private material: cc.Material = null;
private moveDuration: number = 0.72;
private repeatDelay: number = 1.25;
private delayRemaining: number = 0.15;
private elapsed: number = 0;
private startPosition: number = -0.35;
private endPosition: number = 1.35;
protected onLoad(): void {
this.sprite = this.getComponent(cc.Sprite);
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("shop");
if (!bundle || !this.sprite) {
cc.warn("[ShopJungleShine] 商城分包未加载");
return;
}
bundle.load("effect/jungle_shine", cc.EffectAsset, (error: Error, effect: cc.EffectAsset) => {
if (error || !effect || !cc.isValid(this.node) || !this.sprite) {
if (error) {
cc.warn("[ShopJungleShine] 扫光 Shader 加载失败", error);
}
return;
}
let material = new cc.Material();
// @ts-ignore Cocos Creator 2.4 运行时通过 effectAsset 创建材质。
material.effectAsset = effect;
material.setProperty("shinePosition", this.startPosition);
material.setProperty("shineWidth", 0.075);
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);
});
}
}