flyReward.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { _decorator, Component, Node, Prefab, AnimationComponent, SpriteFrame, SpriteComponent, Vec3, tween, LabelComponent, UITransformComponent } from 'cc';
  2. import { poolManager } from '../../framework/poolManager';
  3. import { Diamond } from './diamond';
  4. //奖励飞入弹窗
  5. const { ccclass, property } = _decorator;
  6. @ccclass('FlyReward')
  7. export class FlyReward extends Component {
  8. @property(Prefab)
  9. public pbReward: Prefab = null!;
  10. @property(SpriteFrame)
  11. public sfDiamond: SpriteFrame = null!;
  12. @property(Node)
  13. public ndRewardParent: Node = null!;
  14. @property(Node)
  15. public ndDiamond: Node = null!;
  16. @property(LabelComponent)
  17. public lbDiamondNum: LabelComponent = null!;
  18. public static isRewardFlying: boolean = false;//奖励是否还在飞行
  19. private _callback: Function = ()=>{};
  20. private _maxRewardCount: number = 15;
  21. public createReward(targetNum: number, callback: Function) {
  22. this._callback = callback;
  23. let targetPos = this.ndDiamond.position;
  24. let arrPromise: any = [];
  25. let costTime = 0;
  26. let move2TargetTime = 0;
  27. for(let i = 0; i < this._maxRewardCount; i++) {
  28. let p = new Promise((resolve, reject)=>{
  29. let ndRewardItem = poolManager.instance.getNode(this.pbReward, this.ndRewardParent) as Node;
  30. let spCom = ndRewardItem.getComponent(SpriteComponent) as SpriteComponent;
  31. spCom.spriteFrame = this.sfDiamond;
  32. //配置每个动作
  33. let delayTime = Math.floor(Math.random()*10) / 10;
  34. //随机目标位置
  35. let randTargetPos = new Vec3(Math.floor(Math.random() * 300) - 150, Math.floor(Math.random() * 300 - 150), 0);
  36. let pos = new Vec3(0,0,0);
  37. Vec3.subtract(pos, randTargetPos, new Vec3(0,0,0)).length() / 400;
  38. costTime = pos.length() / 400;
  39. //随机角度
  40. let randRotation = 120 + Math.floor(Math.random() * 60);
  41. randRotation = Math.floor(Math.random() * 2) === 1 ? randRotation : -randRotation;
  42. let pos2 = new Vec3(0,0,0);
  43. Vec3.subtract(pos2, randTargetPos, targetPos).length() / 1000;
  44. move2TargetTime = pos2.length() / 1000;
  45. FlyReward.isRewardFlying = true;
  46. tween(ndRewardItem)
  47. .to(costTime, {position: randTargetPos})
  48. .to(costTime, {eulerAngles: new Vec3(0, 0, randRotation)})
  49. .to(costTime*2/3, {scale: new Vec3(1.5, 1.5, 1.5)})
  50. .to(costTime/3, {scale: new Vec3(1,1,1)})
  51. .union()
  52. .call(()=>{
  53. ndRewardItem.getComponentInChildren(AnimationComponent)?.play();
  54. tween(ndRewardItem)
  55. .to(move2TargetTime, {position: targetPos})
  56. .call(()=>{
  57. resolve(null);
  58. // AudioManager.instance.playSound(gameConstants.MUSIC_LIST.GETDIAMOND);
  59. poolManager.instance.putNode(ndRewardItem);
  60. }).start();
  61. }).start();
  62. })
  63. arrPromise.push(p);
  64. }
  65. let scriptDiamond = this.ndDiamond.getComponent(Diamond) as Diamond;
  66. scriptDiamond.startRaise(targetNum, 0.1, 0.25, ()=>{
  67. Promise.all(arrPromise).then(()=>{
  68. setTimeout(()=>{
  69. FlyReward.isRewardFlying = false;
  70. this._callback && this._callback();
  71. this.node.destroy();
  72. }, 1000)
  73. }).catch((e)=>{
  74. console.log("e", e);
  75. })
  76. });
  77. }
  78. }