FlySystem.ts 4.3 KB

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