105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
const { ccclass, property, requireComponent } = cc._decorator;
|
|
|
|
@ccclass
|
|
@requireComponent(cc.Sprite)
|
|
export default class CardFlipEffect extends cc.Component {
|
|
@property({ tooltip: "翻转持续时间" })
|
|
duration: number = 1.0;
|
|
|
|
@property({ tooltip: "是否启用弯曲效果" })
|
|
enableBend: boolean = true;
|
|
|
|
@property({ tooltip: "弯曲强度" })
|
|
bendStrength: number = 0.2;
|
|
|
|
@property({ tooltip: "是否启用阴影效果" })
|
|
enableShadow: boolean = true;
|
|
|
|
private sprite: cc.Sprite = null;
|
|
private material: cc.Material = null;
|
|
public flipProgress: number = 0;
|
|
private isFlipping: boolean = false;
|
|
|
|
onLoad() {
|
|
this.sprite = this.getComponent(cc.Sprite);
|
|
if (this.sprite) {
|
|
// 获取材质实例,确保不影响其他精灵
|
|
this.material = this.sprite.getMaterial(0);
|
|
if (this.material) {
|
|
// 克隆材质以避免影响其他节点
|
|
this.material = this.sprite.material;
|
|
}
|
|
}
|
|
}
|
|
|
|
start() {
|
|
// 初始化材质参数
|
|
if (this.material) {
|
|
this.material.setProperty('flipProgress', this.flipProgress);
|
|
this.material.setProperty('bendStrength', this.bendStrength);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 开始翻转动画
|
|
*/
|
|
startFlip() {
|
|
if (this.isFlipping) return;
|
|
|
|
this.isFlipping = true;
|
|
this.flipProgress = 0;
|
|
|
|
cc.tween(this)
|
|
.to(this.duration, { flipProgress: 1 }, {
|
|
onUpdate: (target, ratio) => {
|
|
this.updateFlipProgress(ratio);
|
|
},
|
|
onComplete: () => {
|
|
this.isFlipping = false;
|
|
}
|
|
})
|
|
.start();
|
|
}
|
|
|
|
/**
|
|
* 重置翻转状态
|
|
*/
|
|
resetFlip() {
|
|
this.flipProgress = 0;
|
|
this.updateFlipProgress(0);
|
|
this.isFlipping = false;
|
|
}
|
|
|
|
/**
|
|
* 更新翻转进度
|
|
* @param progress 翻转进度 (0-1)
|
|
*/
|
|
updateFlipProgress(progress: number) {
|
|
this.flipProgress = progress;
|
|
if (this.material) {
|
|
this.material.setProperty('flipProgress', progress);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置弯曲强度
|
|
* @param strength 弯曲强度
|
|
*/
|
|
setBendStrength(strength: number) {
|
|
this.bendStrength = strength;
|
|
if (this.material) {
|
|
this.material.setProperty('bendStrength', strength);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 应用自定义材质
|
|
* @param material 材质
|
|
*/
|
|
applyMaterial(material: cc.Material) {
|
|
if (this.sprite && material) {
|
|
this.sprite.setMaterial(0, material);
|
|
this.material = material;
|
|
}
|
|
}
|
|
} |