FlySystem.ts 3.5 KB

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