pinball/assets/_Game/Scripts/Environments/FloatingText.ts

62 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-06-10 21:35:13 -07:00
import { _decorator, CCFloat, Component, geometry, Label, Sprite, SpriteFrame, tween, Vec3, Widget } from 'cc';
2024-03-21 01:59:08 -07:00
import ObjectPool from '../Pool/ObjectPool';
2024-06-10 21:35:13 -07:00
import Utils from '../Utilities';
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;
@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-06-10 21:35:13 -07:00
public async 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;
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' },
)
.call(() => {
this._sprite.spriteFrame = null;
ObjectPool.release(this.node);
})
2024-03-08 03:07:41 -08:00
.start();
2024-06-10 21:35:13 -07:00
await Utils.delay(0.05);
this._sprite.getComponent(Widget).updateAlignment();
2024-03-08 03:07:41 -08:00
}
}