pkRedPrizeItem.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { _decorator, Component, Node, Sprite, Animation, Prefab, instantiate, Widget, Vec3, UITransform, tween, Tween, SpriteFrame, EventHandler, Label } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. import { ConfigData } from '../Data/ConfigData';
  4. import { g } from '../Data/g';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('PkRedPrizeItem')
  7. export class PkRedPrizeItem extends Component {
  8. @property({ tooltip: "开始光圈特效", type: Node }) public turnLightAnim: Node;
  9. @property({ tooltip: "出场特效", type: Node }) public comeOutAnim: Node;
  10. @property({ tooltip: "奖品", type: Sprite }) public prize: Sprite;
  11. @property({ tooltip: "粒子光效box", type: Node }) public particleBox: Node;
  12. @property({ tooltip: "神石文本", type: Node }) public dimaondTxt: Node;
  13. @property({ tooltip: "金额文本", type: Label }) public moneyTxt: Label;
  14. @property({ tooltip: "金额文本框", type: Node }) public moneyTxtBox: Node;
  15. public animBackFun: EventHandler;
  16. start() {
  17. }
  18. public comeOut() {
  19. this.comeOutAnim.getComponent(Animation).play();
  20. }
  21. /**
  22. * 设置奖品图片
  23. * @param type 0:神将,1:神石
  24. * @param icon 奖品编号
  25. * @param backFun 窗口关闭回调
  26. */
  27. public async setData(type: number, icon: number, backFun: EventHandler = null, prizeId: number) {
  28. if (type == 0) {
  29. let imgRoles = await ResourcesUtils.load<SpriteFrame>("Roles/" + icon + "/spriteFrame", SpriteFrame, this.node);
  30. this.prize.spriteFrame = imgRoles;
  31. g.gameData.addMyLocalGenral(-1, icon);
  32. } else {
  33. let imgRed = await ResourcesUtils.load<SpriteFrame>("Images/red/redOpening/spriteFrame", SpriteFrame, this.node);
  34. this.prize.spriteFrame = imgRed;
  35. this.moneyTxtBox.active = true;
  36. this.dimaondTxt.active = true;
  37. this.dimaondTxt.getComponent(Label).string = "(" + icon + "神石)";
  38. this.moneyTxt.string = (icon / ConfigData.configMap.get("systemConfig").moneyRate) + "";
  39. g.userData.diamond += icon;
  40. }
  41. if (backFun) {
  42. this.animBackFun = backFun;
  43. }
  44. let _anim = this.turnLightAnim.getComponent(Animation);
  45. this.scheduleOnce(function () {
  46. _anim.play();
  47. }, prizeId * 1.2);
  48. }
  49. //添加粒子特效
  50. public async addParticle(num: number, starY: number = 100, endYMix: number = 500) {
  51. for (let i = 0; i < num; i++) {
  52. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/粒子光效", null, this.node);
  53. let particle = instantiate(prefab);
  54. particle.parent = this.particleBox;
  55. let _w = this.particleBox.getComponent(UITransform).width;
  56. let tempx = Math.random() * (_w * 0.8);
  57. let _x = tempx > ((_w * 0.8) / 2) ? ((_w * 0.8) / 2) - tempx : tempx;
  58. particle.setPosition(new Vec3(_x, starY + Math.random() * 200));
  59. let _scale = Math.random() * 0.5 + 0.5;
  60. particle.scale = new Vec3(_scale, _scale, 1);
  61. let endY = Math.random() * 300 + endYMix;
  62. let endX = Math.random() * (_w + 100);
  63. endX = endX < ((_w + 100) / 2) ? endX : (_w + 100) / 2 - endX;
  64. let t = tween(particle);
  65. t.to(1.5, { position: new Vec3(endX, endY, 0) }).start();
  66. }
  67. }
  68. public animEndFun() {
  69. this.animBackFun && this.animBackFun.emit([]);
  70. }
  71. }