FlySystem.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @description 获取道具特效
  3. * @author kaka
  4. * */
  5. const { ccclass } = cc._decorator;
  6. @ccclass
  7. export default class FlySystem extends cc.Component {
  8. onLoad() {
  9. mk.fly = this;
  10. }
  11. private is_new_cb = true;
  12. /**
  13. * @param type 类型 道具id 10001,10002
  14. * @param num 特效数量
  15. * @param pos 起始位置
  16. * @param endpos 飞行目标的世界坐标 或目标对象路径
  17. * @param cb 动画结束回调
  18. */
  19. public async PlayCoinAnim(type: number = 0, num: number, pos: cc.Vec2, endpos: cc.Vec2 | string, item_cb: Function = null) {
  20. for (let i = 0; i < num; i++) {
  21. let copyCoin = await mk.pool.getPrefab('game/prefab/coin');
  22. let url = 'game/texture/coin/' + type;
  23. let sf = await mk.loader.load(url, cc.SpriteFrame);
  24. copyCoin.getComponent(cc.Sprite).spriteFrame = sf;
  25. copyCoin.setParent(this.node);
  26. copyCoin.scale = mk.math.random(0.56, 0.84, false);
  27. copyCoin.angle = mk.math.random(-45, 45, false);
  28. let dir = cc.v2(mk.math.random(-1, 1, false), mk.math.random(-1, 1, false));
  29. dir = dir.normalize();
  30. let deltaInit = cc.v2(mk.math.random(10, 160, false) * dir.x, mk.math.random(10, 160, false) * dir.y);
  31. let initPos = pos.add(deltaInit);
  32. copyCoin.setPosition(pos);
  33. this.FlyIn(type, copyCoin, initPos, endpos, item_cb);
  34. }
  35. this.is_new_cb = true;
  36. }
  37. private FlyIn(type: number, target: cc.Node, end: cc.Vec2, endpos: cc.Vec2 | string = null, item_cb: Function = null) {
  38. cc.tween(target)
  39. .to(mk.math.random(0.3, 0.6, false), { position: cc.v3(end) }, cc.easeSineInOut())
  40. .call(() => {
  41. this.FlyOut(type, target, endpos, item_cb);
  42. })
  43. .start();
  44. }
  45. private FlyOut(type: number, target: cc.Node, end: cc.Vec2 | string, item_cb: Function = null) {
  46. let dur = mk.math.random(0.3, 1, false);
  47. cc.tween(target)
  48. .to(dur, { scale: 0.8 })
  49. .start();
  50. let end_pos: cc.Vec2 = null;
  51. let end_node: cc.Node = null;
  52. if (typeof end === 'string') {
  53. end_node = cc.find(end as string);
  54. let world_pos = end_node.parent.convertToWorldSpaceAR(end_node.getPosition());
  55. end_pos = this.node.convertToNodeSpaceAR(world_pos);
  56. }
  57. else {
  58. end_pos = this.node.convertToNodeSpaceAR(end);
  59. }
  60. cc.tween(target)
  61. .to(dur, { position: cc.v3(end_pos) }, cc.easeSineOut())
  62. .call(() => {
  63. console.log('end_pos ', target.position)
  64. mk.pool.return('game/prefab/coin', target);
  65. if (end_node) {
  66. cc.tween(end_node).to(0.1, { scale: 1.2 }).to(0.05, { scale: 1 }).start();
  67. }
  68. if (item_cb != null) {
  69. this.is_new_cb && item_cb();
  70. this.is_new_cb = false;
  71. }
  72. this.iconScale(type);
  73. })
  74. .start();
  75. }
  76. /**
  77. * icon缩放动画
  78. * @param type icon类型
  79. * @returns
  80. */
  81. private iconScale(type: number) {
  82. let node;
  83. if (type === 1) {
  84. node = gData.gameData.gameStyle.icon_hb;
  85. } else if (type === 3) {
  86. node = gData.gameData.gameStyle.icon_zb;
  87. }
  88. if (!node) return;
  89. let scale1 = cc.tween().to(0.05, { scale: 0.9 });
  90. let scale2 = cc.tween().to(0.05, { scale: 1 });
  91. let seque = cc.tween().sequence(scale1, scale2);
  92. cc.tween(node).then(seque).repeat(2).start();
  93. }
  94. }