2024-04-22 04:12:57 -07:00
|
|
|
import { _decorator, CCFloat, Component, geometry, Label, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
|
2024-03-21 01:59:08 -07:00
|
|
|
import ObjectPool from '../Pool/ObjectPool';
|
2024-03-08 03:07:41 -08:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass('FloatingText')
|
|
|
|
export class FloatingText extends Component {
|
|
|
|
@property({ type: Label, visible: true })
|
2024-03-10 03:12:55 -07:00
|
|
|
private _label: Label;
|
2024-04-22 04:12:57 -07:00
|
|
|
@property({ type: Sprite, visible: true })
|
|
|
|
private _sprite: Sprite;
|
2024-03-08 03:07:41 -08:00
|
|
|
@property({ type: CCFloat, visible: true })
|
|
|
|
private _moveSpeed = 1;
|
|
|
|
@property({ type: geometry.AnimationCurve, visible: true })
|
|
|
|
private _animationCurve: geometry.AnimationCurve = new geometry.AnimationCurve();
|
|
|
|
private readonly _scaleUpDuration = 0.2;
|
|
|
|
private readonly _scaleDownDuration = 0.2;
|
|
|
|
private _moveDuration = 0;
|
|
|
|
|
2024-04-22 04:12:57 -07:00
|
|
|
public show(text: string, position: Vec3, scale = 1, duration = 1, image: SpriteFrame = null) {
|
2024-03-08 03:07:41 -08:00
|
|
|
if (duration < this._scaleUpDuration + this._scaleDownDuration) {
|
|
|
|
console.warn(
|
|
|
|
`show duration time of floating text must be greater than ${this._scaleUpDuration + this._scaleDownDuration}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
duration = this._scaleUpDuration + this._scaleDownDuration + 0.5;
|
|
|
|
}
|
|
|
|
this._moveDuration = duration - (this._scaleUpDuration + this._scaleDownDuration);
|
|
|
|
this.node.setWorldPosition(position);
|
|
|
|
this.node.setScale(Vec3.ZERO);
|
2024-03-10 03:12:55 -07:00
|
|
|
this._label.string = text;
|
2024-04-22 04:12:57 -07:00
|
|
|
this._sprite.spriteFrame = image;
|
2024-03-08 03:07:41 -08:00
|
|
|
tween(this.node)
|
|
|
|
.to(this._scaleUpDuration, { scale: new Vec3(scale, scale, 1) }, { easing: 'backOut' })
|
|
|
|
.by(
|
|
|
|
this._moveDuration,
|
|
|
|
{
|
|
|
|
worldPosition: new Vec3(0, 1 * this._moveSpeed, 10),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
easing: (k) => {
|
|
|
|
return this._animationCurve.evaluate(k);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.to(
|
|
|
|
this._scaleDownDuration,
|
|
|
|
{
|
|
|
|
scale: Vec3.ZERO,
|
|
|
|
},
|
|
|
|
{ easing: 'backIn' },
|
|
|
|
)
|
2024-04-22 04:12:57 -07:00
|
|
|
.call(() => {
|
|
|
|
this._sprite.spriteFrame = null;
|
|
|
|
ObjectPool.release(this.node);
|
|
|
|
})
|
2024-03-08 03:07:41 -08:00
|
|
|
.start();
|
|
|
|
}
|
|
|
|
}
|