59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
const { ccclass, property, requireComponent } = cc._decorator;
|
|
|
|
/** 挂到带 Sprite 的节点上,并在 Inspector 中指定 jungle_shine.effect。 */
|
|
@ccclass
|
|
@requireComponent(cc.Sprite)
|
|
export default class JungleShine extends cc.Component {
|
|
@property(cc.EffectAsset)
|
|
effectAsset: cc.EffectAsset = null;
|
|
|
|
private material: cc.Material = null;
|
|
private delayRemaining: number = 0.15;
|
|
private elapsed: number = 0;
|
|
|
|
protected onLoad(): void {
|
|
let sprite = this.getComponent(cc.Sprite);
|
|
if (!sprite || !this.effectAsset) {
|
|
cc.warn("[JungleShine] 请在 Inspector 中绑定 jungle_shine.effect");
|
|
return;
|
|
}
|
|
|
|
let material = new cc.Material();
|
|
// @ts-ignore Cocos Creator 2.4 通过 effectAsset 创建运行时材质。
|
|
material.effectAsset = this.effectAsset;
|
|
material.setProperty("shinePosition", -0.35);
|
|
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;
|
|
sprite.setMaterial(0, material);
|
|
}
|
|
|
|
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 / 0.72);
|
|
this.material.setProperty("shinePosition", -0.35 + 1.7 * progress);
|
|
if (progress >= 1) {
|
|
this.elapsed = 0;
|
|
this.delayRemaining = 1.25;
|
|
this.material.setProperty("shinePosition", -0.35);
|
|
}
|
|
}
|
|
|
|
protected onDestroy(): void {
|
|
if (this.material && cc.isValid(this.material)) {
|
|
this.material.destroy();
|
|
}
|
|
this.material = null;
|
|
}
|
|
}
|