FlySystem.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, cb: Function = null) {
  19. for (let i = 0; i < num; i++) {
  20. let copyCoin = await mk.pool.getPrefab('game/prefab/coin');
  21. let url = 'module/icon/reward' + 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.8, 1.2, 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(copyCoin, initPos, endpos, cb);
  33. }
  34. }
  35. private FlyIn(target: cc.Node, end: cc.Vec2, endpos: cc.Vec2 | string = null, 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(target, endpos, cb);
  40. })
  41. .start();
  42. }
  43. private FlyOut(target: cc.Node, end: cc.Vec2 | string, 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 (cb != null) {
  67. cb();
  68. }
  69. })
  70. .start();
  71. }
  72. }