Role.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { _decorator, Node, Component, Sprite, Label, Animation, SpriteFrame, AnimationState, systemEvent, Prefab, instantiate, UITransform, Vec3, tween } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. import { Sound } from '../core/sound/Sound';
  4. import { BitmapFont } from '../core/ui/BitmapFont';
  5. import { ConfigData } from '../Data/ConfigData';
  6. import { g } from '../Data/g';
  7. import { FsUtils } from '../FsUtils/FsUtils';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('Role')
  10. export class Role extends Component {
  11. @property({ type: Animation, tooltip: "角色特效" }) animation: Animation;
  12. @property({ type: Label, tooltip: "等级" }) roleLVLabel: Label;
  13. @property({ type: BitmapFont, tooltip: "金币" }) addMoneyBitmapFont: BitmapFont;
  14. @property({ tooltip: "金币增长间隔" }) space: number = 2;
  15. @property({ tooltip: "特效父级", type: Node }) public particleBox: Node;
  16. @property({ tooltip: "角色图片", type: Sprite }) public roleImg: Sprite;
  17. @property({ type: Sound, tooltip: "加金币音效" })
  18. addMoneySound: Sound;
  19. public generalBaseData: any;//神将数据
  20. private addMoney: number = 0;
  21. public generalLv: number = 0;
  22. public async setData(data: any) {
  23. this.generalLv = data.lv;
  24. this.generalBaseData = ConfigData.configMap.get("generalBase")[data.lv];
  25. this.addMoney = this.generalBaseData.output * this.space;
  26. let img: SpriteFrame = await ResourcesUtils.load("Roles/" + this.generalBaseData.lv + "/spriteFrame", SpriteFrame, this.node);
  27. // this.node.getChildByName("roleImg").getComponent(Sprite).spriteFrame = img;
  28. this.roleImg.spriteFrame = img;
  29. this.roleLVLabel.string = this.generalBaseData.lv;
  30. this.addMoneyBitmapFont.string = '+' + FsUtils.moneyFormat(this.addMoney);
  31. let state = this.animation.getState("role");
  32. state.wrapMode = 2;
  33. }
  34. private onPlayOver() {
  35. if (g.gameData.playAddMoneySound) {
  36. this.addMoneySound.play();
  37. }
  38. }
  39. public play() {
  40. this.animation.play("role");
  41. }
  42. public playPrize() {
  43. this.animation.play("prizeRole");
  44. }
  45. //添加粒子特效
  46. public async addParticle(num: number, starY: number = 100, endYMix: number = 500) {
  47. for (let i = 0; i < num; i++) {
  48. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/粒子光效", null, this.node);
  49. let particle = instantiate(prefab);
  50. particle.parent = this.particleBox;
  51. let _w = this.particleBox.getComponent(UITransform).width;
  52. let tempx = Math.random() * (_w * 0.8);
  53. let _x = tempx > ((_w * 0.8) / 2) ? ((_w * 0.8) / 2) - tempx : tempx;
  54. particle.setPosition(new Vec3(_x, starY + Math.random() * 200));
  55. let _scale = Math.random() * 0.5 + 0.5;
  56. particle.scale = new Vec3(_scale, _scale, 1);
  57. let endY = Math.random() * 300 + endYMix;
  58. let endX = Math.random() * (_w + 100);
  59. endX = endX < ((_w + 100) / 2) ? endX : (_w + 100) / 2 - endX;
  60. let t = tween(particle);
  61. t.to(1.5, { position: new Vec3(endX, endY, 0) }).start();
  62. }
  63. }
  64. public roleState(isPrize: boolean) {
  65. g.gameData.isPlayPrizeRoleAnim = isPrize;
  66. }
  67. //#region 新增合成动画、
  68. //播放合成动画
  69. public playMerge() {
  70. this.animation.play("mergeRole");
  71. }
  72. //#endregion
  73. }