AddParliclePkBase.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { _decorator, Component, Node, Prefab, instantiate, UITransform, math, tween, Vec3, SpriteFrame, Sprite } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. import { LineLightPk } from './LineLightPk';
  4. import { ParliclePk } from './ParliclePk';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('AddParliclePkBase')
  7. export class AddParliclePkBase extends Component {
  8. @property({ tooltip: "粒子特效父级,不填写默认当前node", type: Node }) public parlicleBox: Node;
  9. @property({ tooltip: "底图", type: Sprite }) public seatLight: Sprite;
  10. @property({ tooltip: "底图", type: Sprite }) public leader: Sprite;
  11. public type = 1;
  12. start() {
  13. if (!this.parlicleBox) {
  14. this.parlicleBox = this.node;
  15. }
  16. }
  17. /**
  18. * 设置图片资源
  19. * @param type 0:红色底座光球,1:蓝色底座光球
  20. */
  21. public async setData(type: number) {
  22. this.type = type;
  23. if (type != 1) {
  24. let img: SpriteFrame = await ResourcesUtils.load("Images/rolesBase/leader" + type + "/spriteFrame", SpriteFrame, this.node);
  25. this.leader.spriteFrame = img;
  26. let img0: SpriteFrame = await ResourcesUtils.load("Images/rolesBase/seatLight" + type + "/spriteFrame", SpriteFrame, this.node);
  27. this.seatLight.spriteFrame = img;
  28. }
  29. this.addParlicle();
  30. this.addLineLight();
  31. this.schedule(this.addParlicle, 0.2)
  32. this.schedule(this.addLineLight, 0.25)
  33. }
  34. public async addParlicle() {
  35. let times = Math.ceil(Math.random() * 5);
  36. for (let i = 0; i < times; i++) {
  37. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/粒子光效pk底座", Prefab, this.node);
  38. let parlicle = instantiate(prefab);
  39. parlicle.parent = this.parlicleBox;
  40. parlicle.getComponent(ParliclePk).setImag(this.type);
  41. let _w = this.parlicleBox.getComponent(UITransform).width - 20;
  42. let _h = this.parlicleBox.getComponent(UITransform).height;
  43. let _x = _w / 2 - (_w * Math.random());
  44. let _y = -_h / 2 * Math.random();
  45. let endX = _x + ((Math.random() - Math.random()) * 100);
  46. let endY = _h / 2 * Math.random();
  47. parlicle.setPosition(new Vec3(_x, _y));
  48. let _scale = Math.random() * 0.5 + 0.5;
  49. parlicle.setScale(new Vec3(_scale, _scale));
  50. let t = tween(parlicle);
  51. t.to(0.65, { position: new Vec3(endX, endY) }).start();
  52. }
  53. }
  54. public async addLineLight() {
  55. let times = Math.ceil(Math.random() * 6);
  56. for (let i = 0; i < times; i++) {
  57. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/光线特效", Prefab, this.node);
  58. let lineLight = instantiate(prefab);
  59. lineLight.parent = this.parlicleBox;
  60. lineLight.getComponent(LineLightPk).setImag(this.type);
  61. let _w = this.parlicleBox.getComponent(UITransform).width - 50;
  62. let _h = this.parlicleBox.getComponent(UITransform).height;
  63. let _x = _w / 2 - (_w * Math.random());
  64. let _y = -_h / 2 + 30;
  65. let endY = _h / 2 * Math.random();
  66. lineLight.setPosition(new Vec3(_x, _y));
  67. let t = tween(lineLight);
  68. t.to(0.65, { position: new Vec3(_x, endY) }).start();
  69. }
  70. }
  71. }