PVPHero.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { _decorator, Component, Node, sp, Vec3, v3, tween, Label, ProgressBar, Prefab, instantiate, UITransform, Animation, clamp } from 'cc';
  2. import { MissionData } from '../battle/MissionData';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  5. import { Sound } from '../core/sound/Sound';
  6. import { ConfigData } from '../data/ConfigData';
  7. import { PVPCardData } from './PVP';
  8. import { PVPBullet } from './PVPBullet';
  9. import { PVPHeroUI } from './PVPHeroUI';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('PVPHero')
  12. export class PVPHero extends Component {
  13. @property({ type: ResourceLoader, tooltip: "资源加载器" }) res: ResourceLoader;
  14. @property({ type: sp.Skeleton, tooltip: "spine组件" }) spine: sp.Skeleton;
  15. @property({ type: Prefab, tooltip: "子弹预制体" }) bulletPrefab: Prefab;
  16. @property({ type: Animation, tooltip: "动画组件" }) animation: Animation;
  17. private data: PVPCardData;
  18. private dir: number;
  19. private hp: number;
  20. private sound: Sound;
  21. private ui: PVPHeroUI;
  22. start() {
  23. this.sound = this.getComponent(Sound);
  24. }
  25. update() {
  26. this.ui && this.ui.node.setPosition(this.node.position);
  27. }
  28. onDestroy() {
  29. this.ui.node && this.ui.node.destroy();
  30. }
  31. public setUI(ui: PVPHeroUI): void {
  32. this.ui = ui;
  33. }
  34. public async setData(data: PVPCardData, dir: number, to: Vec3, cb: Function) {
  35. this.hp = 100;
  36. this.data = data;
  37. this.dir = dir;
  38. let spData = await this.res.load<sp.SkeletonData>("spine/hero/" + DataSystem.getData(ConfigData)["general"][data.id]["hero_res"], sp.SkeletonData);
  39. this.spine.skeletonData = spData;
  40. this.spine.setAnimation(0, "run", true);
  41. this.spine.node.setScale(v3(dir, 1, 1));
  42. this.ui.starLabel.string = data.star + "";
  43. this.ui.lvLabel.string = "LV." + data.lv;
  44. this.ui.nameLabel.string = DataSystem.getData(ConfigData)["general"][data.id]['name'];
  45. tween(this.node).to(0.5, { position: to }).call(async () => {
  46. this.spine.setAnimation(0, "idle", true);
  47. if (cb) {
  48. cb();
  49. }
  50. }).start();
  51. }
  52. public attack(to: Node, cb: Function): void {
  53. this.spine.setAnimation(0, "attack", false);
  54. let self = this;
  55. this.spine.setEventListener(() => {
  56. let node = instantiate(self.bulletPrefab);
  57. node.setPosition(self.node.position.clone().add(v3(-self.dir * self.node.getComponent(UITransform).width / 2, self.node.getComponent(UITransform).height / 2 - 30, 0)));
  58. node.setParent(self.node.parent);
  59. node.getComponent(PVPBullet).setData(self.data, self.dir, to.position.clone().add(v3(0, self.node.getComponent(UITransform).height / 2 - 30, 0)), cb);
  60. if (this.sound) {
  61. switch (DataSystem.getData(ConfigData)["general"][this.data.id]["atk_res"]) {
  62. case 1: case 2:
  63. this.sound.play(2);
  64. break;
  65. case 3:
  66. this.sound.play(0);
  67. break;
  68. case 4: case 5:
  69. this.sound.play(1);
  70. break;
  71. }
  72. }
  73. });
  74. this.spine.setCompleteListener(() => {
  75. self.spine.setAnimation(0, "idle", true);
  76. });
  77. }
  78. public onHit(hit: number, cb: Function): void {
  79. let i = 0;
  80. this.hp -= hit;
  81. this.ui.bloodLabel.string = clamp(this.hp, 0, 100,) + "%";
  82. this.ui.bloodProgress.progress = this.hp / 100;
  83. if (this.hp > 0) {
  84. this.animation.play(this.animation.clips[0].name);
  85. this.animation.on(Animation.EventType.FINISHED, () => {
  86. if (!i && cb) {
  87. i++;
  88. cb();
  89. }
  90. }, this);
  91. } else {
  92. cb && cb();
  93. }
  94. }
  95. public die(cb: Function): void {
  96. let i = 0;
  97. this.animation.play(this.animation.clips[0].name);
  98. this.animation.on(Animation.EventType.FINISHED, () => {
  99. if (!i && cb) {
  100. i++;
  101. this.node.destroy();
  102. cb();
  103. }
  104. }, this);
  105. }
  106. }