UEBattleView.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { gameMethod } from "../../common/gameMethod";
  2. import { BattleTeamId, FightType } from "../../data/const/TypeConst";
  3. import GameDataCenter from "../../data/GameDataCenter";
  4. import UEBase from "../../frameWork/compment/UEBase";
  5. import { eg } from "../../frameWork/eg";
  6. import { ActionType, fightLogOne, FightStart } from "../../shared/fight/PtlFightTest";
  7. import AssetMgr from "../../utils/AssetMgr";
  8. import { BattleGridConstant } from "./BattleGridConstant";
  9. import UEBattleRole from "./UEBattleRole";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class UEBattleView extends UEBase {
  13. @property(cc.Prefab)
  14. rolePrefab: cc.Prefab = null;
  15. @property(cc.Prefab)
  16. subHpPrefab: cc.Prefab = null;
  17. @property(cc.Prefab)
  18. cellPrefab: cc.Prefab = null!;
  19. @property(cc.Node)
  20. roleContent: cc.Node = null;
  21. @property(cc.Node)
  22. hpLayer: cc.Node = null
  23. @property(cc.Node)
  24. cellLayer: cc.Node = null;
  25. fightStart: FightStart;
  26. fightLogList: {
  27. //战斗日志列表
  28. //回合ID:单回合内战斗日志
  29. [huihe: string]: fightLogOne[];
  30. };
  31. static readonly BundleKey: string = "battle";
  32. static readonly PrefabUrl: string = "UEBattleView";
  33. static readonly CLS: string = "UEBattleView";
  34. type: FightType;
  35. huihe: number;
  36. curIndex: number;
  37. static s_ins: UEBattleView;
  38. TxtRound: cc.Label;
  39. static getInstance() {
  40. return this.s_ins
  41. }
  42. Init() {
  43. UEBattleView.s_ins = this;
  44. this.TxtRound = this.node.getChildByName("ImgDi").getChildByName("TxtRound").getComponent(cc.Label);
  45. this.initEvent();
  46. this.LoadCell();
  47. }
  48. initEvent(): void {
  49. }
  50. //关闭界面
  51. onDisable(): void {
  52. this.node.destroy();
  53. }
  54. /** 加载地图数据 */
  55. private LoadCell() {
  56. for (let i = 0; i < BattleGridConstant.COL; i++) {
  57. for (let j = 0; j < BattleGridConstant.ROW; j++) {
  58. this.CreateCell(i, j);
  59. }
  60. }
  61. }
  62. /** 创建格子 */
  63. private CreateCell(i: number, j: number) {
  64. let cell = cc.instantiate(this.cellPrefab);
  65. this.cellLayer.addChild(cell);
  66. cell.width = BattleGridConstant.CELL_WIDTH;
  67. cell.height = BattleGridConstant.CELL_HEIGHT;
  68. let pos = this.GetPosByIdx(i, j);
  69. cell.setPosition(pos);
  70. cell.name = `${(i + 1) * 10 + (j + 1)}`;
  71. cell.getChildByName("txtIndex").getComponent(cc.Label).string = `${(i + 1) * 10 + (j + 1)}`;
  72. return cell;
  73. }
  74. /** 根据索引获取实际像素坐标 */
  75. private GetPosByIdx(i: number, j: number): cc.Vec3 {
  76. const startX = -(BattleGridConstant.ROW * BattleGridConstant.CELL_WIDTH) / 2;
  77. const startY = (BattleGridConstant.COL * BattleGridConstant.CELL_HEIGHT) / 2;
  78. return cc.v3(
  79. startX + j * BattleGridConstant.CELL_WIDTH + BattleGridConstant.CELL_WIDTH / 2,
  80. startY - i * BattleGridConstant.CELL_HEIGHT - BattleGridConstant.CELL_HEIGHT / 2
  81. )
  82. }
  83. /** 获取扣血飘字表现层 */
  84. GetHpLayer() {
  85. return this.hpLayer;
  86. }
  87. /** 获取扣血飘字预制体 */
  88. GetSubHpPrefab() {
  89. return this.subHpPrefab;
  90. }
  91. //开始战斗
  92. async onStartFight() {
  93. GameDataCenter.battle.SendTestFight(async () => {
  94. eg.poolManager.GetPool("subHPPool").clear();
  95. this.huihe = 1;
  96. this.curIndex = 0;
  97. this.fightStart = GameDataCenter.battle.fightInfo?.fightStart;
  98. this.fightLogList = GameDataCenter.battle.fightInfo?.log;
  99. await this.produceRole();
  100. this.playLog();
  101. })
  102. }
  103. async produceRole() {
  104. //生产角色
  105. let fightType: FightType = this.type;
  106. let teams = this.fightStart?.teams;
  107. if (gameMethod.isEmpty(teams)) {
  108. console.error("teams is empty");
  109. return;
  110. }
  111. for (const key in teams) {
  112. if (Object.prototype.hasOwnProperty.call(teams, key)) {
  113. let teamInfo = teams[key];
  114. let roleNode = AssetMgr.instantiate(this.cellLayer, this.rolePrefab);
  115. let pos = this.cellLayer.getChildByName(teamInfo?.seat?.toString())?.position;
  116. roleNode.getComponent(UEBattleRole).setRole(pos, teamInfo, fightType);
  117. roleNode.scale = 0.6;
  118. roleNode.scaleX = pos.x > 0 ? roleNode.scale * -1 : roleNode.scale * 1;
  119. GameDataCenter.battle.addRole(key, roleNode.getComponent(UEBattleRole));
  120. }
  121. }
  122. }
  123. //播放战斗日志
  124. playLog() {
  125. this.TxtRound.string = "第" + this.huihe + "回合";
  126. let curLog = this.fightLogList?.[this.huihe]?.[this.curIndex];
  127. // console.error("curLog:", curLog)
  128. let actionType: ActionType = curLog.aType;
  129. switch (actionType) {
  130. case ActionType.round:
  131. case ActionType.buff:
  132. this.nextLog();
  133. break;
  134. case ActionType.atk:
  135. this.playAtk(curLog);
  136. break;
  137. // case ActionType.skill:
  138. // this.playAtk(curLog);
  139. // break;
  140. case ActionType.over:
  141. this.nextLog();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. //播放下一条日志
  148. nextLog() {
  149. this.curIndex++;
  150. if (this.fightLogList?.[this.huihe]?.[this.curIndex] == null) {
  151. this.huihe++;
  152. if (this.fightLogList?.[this.huihe] != null) {
  153. this.curIndex = 0;
  154. this.playLog();
  155. } else {
  156. this.scheduleOnce(() => {
  157. console.log("战斗结束");
  158. }, 1);
  159. }
  160. } else {
  161. this.playLog();
  162. }
  163. }
  164. playAtk(curLog: fightLogOne) {
  165. let atker = GameDataCenter.battle.battleRoleList[curLog.atker.iid];
  166. let target0 = curLog.target.length > 0 ? GameDataCenter.battle.battleRoleList[curLog.target[0].iid]
  167. : Object.values(GameDataCenter.battle.battleRoleList)[0] //没有target 默认取一个防错
  168. let delay = 0.5;
  169. let hitPos = this.cellLayer.getChildByName(curLog?.seat?.toString())?.position;
  170. cc.tween(atker.node)
  171. .delay(0.15 / GameDataCenter.battle.realBattleSpeed)
  172. .to(delay / GameDataCenter.battle.realBattleSpeed, { x: hitPos.x, y: hitPos.y })
  173. .call(() => {
  174. atker.playAni("atk1", false);
  175. // GameDataCenter.audio.playEffect(atker.atkAudio);
  176. })
  177. .delay((delay + 0.3) / GameDataCenter.battle.realBattleSpeed)
  178. .call(() => {
  179. //飘血
  180. console.log("curLog.target:", curLog.target);
  181. for (let i = 0; i < curLog.target.length; i++) {
  182. let target = curLog.target[i];
  183. let trgetNode = GameDataCenter.battle.battleRoleList[target?.iid]
  184. let effectList = target?.effect;
  185. for (let i = 0; i < effectList?.length; i++) {
  186. let effect = effectList[i];
  187. console.log("effect:", effect);
  188. trgetNode.ShowSubHp(effect);
  189. }
  190. }
  191. })
  192. .delay(0.1 / GameDataCenter.battle.realBattleSpeed)
  193. .call(() => {
  194. //显示特效以及连击等特殊效果
  195. })
  196. .delay(0.1 / GameDataCenter.battle.realBattleSpeed)
  197. .call(() => {
  198. atker.playAni("walk", true);
  199. // GameDataCenter.audio.playEffect(atker.atkAudio);
  200. })
  201. .to(delay / GameDataCenter.battle.realBattleSpeed, { x: atker.posX, y: atker.posY })//回到初始位置
  202. .call(() => {
  203. atker.playAni("stand", true);
  204. let atkerEffectList = curLog.atker?.effect;
  205. for (let i = 0; i < atkerEffectList?.length; i++) {
  206. let effect = atkerEffectList[i];
  207. console.log("atkerEffect:", effect);
  208. atker.ShowSubHp(effect);
  209. }
  210. // GameDataCenter.audio.playEffect(atker.atkAudio);
  211. })
  212. .delay(0.3 / GameDataCenter.battle.realBattleSpeed)
  213. .call(() => {
  214. this.nextLog();
  215. })
  216. .start();
  217. }
  218. }