BattleRole.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { _decorator, Component, Sprite, Label, Node, Animation, SpriteFrame, tween, Vec3, UIOpacity, Prefab, instantiate, ProgressBar, ImageAsset, Texture2D, utils } from 'cc';
  2. import { Http } from '../../core/net/Http';
  3. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  4. import { WindowManager } from '../../core/ui/window/WindowManager';
  5. import { Utils } from '../../core/utils/Utils';
  6. import { ConfigData } from '../../Data/ConfigData';
  7. import { g } from '../../Data/g';
  8. import { ISJSB, platform } from '../../Data/platform';
  9. import { FsUtils } from '../../FsUtils/FsUtils';
  10. import { AddParliclePkBase } from '../../SpecialEffects/AddParliclePkBase';
  11. const { ccclass, property } = _decorator;
  12. /**切磋神将 */
  13. @ccclass('BattleRole')
  14. export class BattleRole extends Component {
  15. @property({ type: Node, tooltip: "底坐" })
  16. bottomSeatNode: Node;
  17. @property({ type: Sprite, tooltip: "底红坐光效" })
  18. redLightSprite: Sprite;
  19. @property({ type: Sprite, tooltip: "底蓝坐光效" })
  20. blueLightSprite: Sprite;
  21. @property({ type: Sprite, tooltip: "神将" })
  22. generalSprite: Sprite;
  23. @property({ type: Label, tooltip: "神将等级" })
  24. lvLabel: Label;
  25. @property({ type: Node, tooltip: "邀请好友信息" })
  26. inviteNode: Node;
  27. @property({ type: Node, tooltip: "用户头像组" })
  28. headNode: Node;
  29. @property({ type: Sprite, tooltip: "微信头像" })
  30. userHeadSprite: Sprite;
  31. @property({ type: Node, tooltip: "主将特效节点" })
  32. mainGeneralAnimationNode: Node;
  33. @property({ type: Node, tooltip: "空闲状态时用户名字组" })
  34. userNameFreeNode: Node;
  35. @property({ type: Label, tooltip: "用户名" })
  36. userNameLabel: Label;
  37. @property({ type: Animation, tooltip: "神将台动画" })
  38. animation: Animation;
  39. @property({ tooltip: "是否播放动画" })
  40. isPlayAaimation: Boolean = true;
  41. @property({ type: Http, tooltip: "消息控制" })
  42. http: Http;
  43. @property({ tooltip: "我方血条", type: ProgressBar })
  44. ourHpProgress: ProgressBar;
  45. @property({ type: ProgressBar, tooltip: "敌方血条" })
  46. emenyHpProgress: ProgressBar;
  47. /** 显示状态 */
  48. private _showState: number;
  49. /**战斗状态 */
  50. private _battleState: number;
  51. /**好友神将数据 */
  52. private generalData: any;//{id: number, lv: number, nickName: string, avator: string}好友I,
  53. /**神将基础数据 */
  54. public generalBaseData: any;
  55. /**是否是我方神将 */
  56. public isOur = true;
  57. public speed: number;
  58. public HP: number = 0;
  59. private delay: number = 1;//执行间隔
  60. private time = 0;//当前时间
  61. private lastTime = 0;//上次执行时间点
  62. public startMove = false;//是自动否移动
  63. start() {
  64. if (this.isPlayAaimation) {
  65. this.scheduleOnce(() => {
  66. this.animation.play();
  67. }, Math.random());
  68. }
  69. }
  70. /**
  71. * 设置神将数据
  72. * @param value 用户神将数据{avator,id,lv,nickName}
  73. * @param state 显示状态
  74. * @param isOur 是否是我方神将
  75. */
  76. public async setData(value: any, state: number = 0, isOur: boolean = true) {
  77. this.isOur = isOur;
  78. this.generalData = value;
  79. if (state > 0 || value.lv > 0) {
  80. value.lv = value.lv || 1;
  81. this.generalBaseData = ConfigData.configMap.get("generalBase")[value.lv];
  82. this.speed = this.generalBaseData.speed;
  83. this.HP = this.generalBaseData.HP;
  84. let img: SpriteFrame = await ResourcesUtils.load<SpriteFrame>("Roles/" + value.lv + "/spriteFrame", SpriteFrame, this.node);
  85. this.generalSprite.spriteFrame = img;
  86. this.lvLabel.string = value.lv + '';
  87. this.generalSprite.node.active = true;
  88. }
  89. if (value.id > 0 && state == 0 && value.lv == 0) {
  90. value.lv = value.lv || 1;
  91. let img: SpriteFrame = await ResourcesUtils.load<SpriteFrame>("Roles/" + value.lv + "/spriteFrame", SpriteFrame, this.node);
  92. this.generalSprite.spriteFrame = img;
  93. this.lvLabel.string = '0';
  94. this.generalSprite.node.active = true;
  95. }
  96. if (ISJSB && this.generalData.avator) {
  97. let img = await ResourcesUtils.loadRemote<ImageAsset>(this.generalData.avator, { ext: ".png" }, this);
  98. const spriteFrame = new SpriteFrame();
  99. const texture = new Texture2D();
  100. texture.image = img;
  101. spriteFrame.texture = texture;
  102. this.userHeadSprite.spriteFrame = spriteFrame;
  103. }
  104. this.showState = state;
  105. }
  106. public setHp(value: number) {
  107. if (!value) return;
  108. this.HP = (this.HP * 100 - value * 100) / 100;
  109. this.HP = this.HP < 0 ? 0 : this.HP;
  110. let hpProgress = Number(FsUtils.toFixed(this.HP / this.generalBaseData.HP, 2));
  111. if (this.isOur) {
  112. this.ourHpProgress.progress = hpProgress;
  113. }
  114. else {
  115. this.emenyHpProgress.progress = hpProgress;
  116. }
  117. }
  118. public set showState(value: number) {
  119. this._showState = value;
  120. switch (value) {
  121. case BattleRoleState.Free:
  122. if (this.generalData.id != 0) {
  123. this.userNameFreeNode.active = this.headNode.active = true;
  124. }
  125. this.inviteNode.active = !this.headNode.active;
  126. if (this.headNode.active) {
  127. this.userNameFreeNode.getChildByName("名字").getComponent(Label).string = this.generalData.nickName;
  128. }
  129. break;
  130. case BattleRoleState.Ready:
  131. this.setReadyView();
  132. break;
  133. case BattleRoleState.Fight:
  134. this.setReadyView();
  135. this.showHp();
  136. break;
  137. }
  138. }
  139. public async showHp() {
  140. this.ourHpProgress.node.active = this.isOur;
  141. this.emenyHpProgress.node.active = !this.isOur;;
  142. this.ourHpProgress.progress = 1;
  143. this.emenyHpProgress.progress = 1;
  144. }
  145. public get showState() {
  146. return this._showState;
  147. }
  148. public set battleState(value: number) {
  149. if (this._battleState != value) {
  150. this._battleState = value;
  151. }
  152. }
  153. public get battleState() {
  154. return this._battleState;
  155. }
  156. /**设置神将显示 */
  157. private async setReadyView() {
  158. this.userNameFreeNode.active = false;
  159. if (this.generalData.id) {//==0普通神将
  160. this.headNode.active = true;
  161. this.userNameFreeNode.active = true;
  162. if (this.generalData.id == g.userData.id || this.generalData.id == g.battleData.enemyID) {//主将
  163. let effect = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/底座特效", null, this.mainGeneralAnimationNode);
  164. let effectNode = instantiate(effect);
  165. let effectScript = effectNode.getComponent(AddParliclePkBase);
  166. effectNode.parent = this.mainGeneralAnimationNode;
  167. this.bottomSeatNode.setScale(new Vec3(1.4, 1.4, 0));
  168. this.generalSprite.node.setScale(new Vec3(1, 1, 1));
  169. if (this.generalData.id == g.battleData.enemyID) {
  170. this.userNameLabel.string = this.generalData.nickName;
  171. effectScript.setData(0);
  172. } else {
  173. effectScript.setData(1);
  174. this.userNameLabel.string = g.userData.nickName;
  175. }
  176. } else {// 好友主将
  177. this.userNameLabel.string = this.generalData.nickName;
  178. }
  179. }
  180. this.redLightSprite.node.setScale(new Vec3(1.2, 1, 1));
  181. this.blueLightSprite.node.setScale(new Vec3(1.2, 1, 1));
  182. this.redLightSprite.node.active = !this.isOur;
  183. this.blueLightSprite.node.active = this.isOur;
  184. }
  185. update(delta: number) {
  186. if (this._battleState == BattleRoleState.Move) {
  187. this.time += delta;
  188. if (this.time - this.lastTime >= this.delay) {
  189. this.lastTime += this.delay;
  190. this.move();
  191. }
  192. }
  193. }
  194. public move(pos: Vec3 = null) {
  195. let targetPos = new Vec3(this.node.position.x + (Math.random() > 0.5 ? +10 : -10), this.node.position.y + (Math.random() > 0.5 ? +10 : -10), 0);
  196. tween(this.node).to(1, { position: targetPos }).start();
  197. }
  198. public setMove() {
  199. this.time = 0;
  200. this.lastTime = 0;
  201. this.startMove = true;
  202. }
  203. //邀请
  204. public async onInvite() {
  205. let result = await this.http.send("/api/app/share", {});
  206. if (result.code == 0) {
  207. platform.wxShare(result.data.url, result.data.title, result.data.descroption);
  208. } else {
  209. WindowManager.showTips("分享信息获取失败,请稍后再试");
  210. }
  211. }
  212. }
  213. /**0:UI中状态,1:战斗准备,2战斗 */
  214. export enum BattleRoleState {
  215. Free,
  216. Ready,
  217. Move,
  218. Fight
  219. }