UEBattleView.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, FightBase, fightLogOne, FightStart } from "../../shared/base";
  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(fightInfo: FightBase) {
  93. GameDataCenter.battle.setFightInfo(fightInfo); //设置战斗数据
  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. async produceRole() {
  103. //生产角色
  104. let fightType: FightType = this.type;
  105. let teams = this.fightStart?.teams;
  106. if (gameMethod.isEmpty(teams)) {
  107. console.error("teams is empty");
  108. return;
  109. }
  110. for (const key in teams) {
  111. if (Object.prototype.hasOwnProperty.call(teams, key)) {
  112. let teamInfo = teams[key];
  113. let roleNode = AssetMgr.instantiate(this.cellLayer, this.rolePrefab);
  114. let pos = this.cellLayer.getChildByName(teamInfo?.seat?.toString())?.position;
  115. roleNode.getComponent(UEBattleRole).setRole(pos, teamInfo, fightType);
  116. roleNode.scale = 0.6;
  117. roleNode.scaleX = pos.x > 0 ? roleNode.scale * -1 : roleNode.scale * 1;
  118. GameDataCenter.battle.addRole(key, roleNode.getComponent(UEBattleRole));
  119. }
  120. }
  121. }
  122. //播放战斗日志
  123. playLog() {
  124. this.TxtRound.string = "第" + this.huihe + "回合";
  125. let curLog = this.fightLogList?.[this.huihe]?.[this.curIndex];
  126. // console.error("curLog:", curLog)
  127. let actionType: ActionType = curLog.aType;
  128. switch (actionType) {
  129. case ActionType.round:
  130. case ActionType.buff:
  131. this.nextLog();
  132. break;
  133. case ActionType.atk:
  134. this.playAtk(curLog);
  135. break;
  136. // case ActionType.skill:
  137. // this.playAtk(curLog);
  138. // break;
  139. case ActionType.over:
  140. this.nextLog();
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. //播放下一条日志
  147. nextLog() {
  148. this.curIndex++;
  149. if (this.fightLogList?.[this.huihe]?.[this.curIndex] == null) {
  150. this.huihe++;
  151. if (this.fightLogList?.[this.huihe] != null) {
  152. this.curIndex = 0;
  153. this.playLog();
  154. } else {
  155. this.scheduleOnce(() => {
  156. console.log("战斗结束");
  157. }, 1);
  158. }
  159. } else {
  160. this.playLog();
  161. }
  162. }
  163. playAtk(curLog: fightLogOne) {
  164. let atker = GameDataCenter.battle.battleRoleList[curLog.atker.iid];
  165. let target0 = curLog.target.length > 0 ? GameDataCenter.battle.battleRoleList[curLog.target[0].iid]
  166. : Object.values(GameDataCenter.battle.battleRoleList)[0] //没有target 默认取一个防错
  167. let delay = 0.5;
  168. let hitPos = this.cellLayer.getChildByName(curLog?.seat?.toString())?.position;
  169. cc.tween(atker.node)
  170. .delay(0.15 / GameDataCenter.battle.realBattleSpeed)
  171. .to(delay / GameDataCenter.battle.realBattleSpeed, { x: hitPos.x, y: hitPos.y })
  172. .call(() => {
  173. atker.playAni("atk1", false);
  174. // GameDataCenter.audio.playEffect(atker.atkAudio);
  175. })
  176. .delay((delay + 0.3) / GameDataCenter.battle.realBattleSpeed)
  177. .call(() => {
  178. //飘血
  179. console.log("curLog.target:", curLog.target);
  180. for (let i = 0; i < curLog.target.length; i++) {
  181. let target = curLog.target[i];
  182. let trgetNode = GameDataCenter.battle.battleRoleList[target?.iid]
  183. let effectList = target?.effect;
  184. for (let i = 0; i < effectList?.length; i++) {
  185. let effect = effectList[i];
  186. console.log("effect:", effect);
  187. trgetNode.ShowSubHp(effect);
  188. }
  189. }
  190. })
  191. .delay(0.1 / GameDataCenter.battle.realBattleSpeed)
  192. .call(() => {
  193. //显示特效以及连击等特殊效果
  194. })
  195. .delay(0.1 / GameDataCenter.battle.realBattleSpeed)
  196. .call(() => {
  197. atker.playAni("walk", true);
  198. // GameDataCenter.audio.playEffect(atker.atkAudio);
  199. })
  200. .to(delay / GameDataCenter.battle.realBattleSpeed, { x: atker.posX, y: atker.posY })//回到初始位置
  201. .call(() => {
  202. atker.playAni("stand", true);
  203. let atkerEffectList = curLog.atker?.effect;
  204. for (let i = 0; i < atkerEffectList?.length; i++) {
  205. let effect = atkerEffectList[i];
  206. console.log("atkerEffect:", effect);
  207. atker.ShowSubHp(effect);
  208. }
  209. // GameDataCenter.audio.playEffect(atker.atkAudio);
  210. })
  211. .delay(0.3 / GameDataCenter.battle.realBattleSpeed)
  212. .call(() => {
  213. this.nextLog();
  214. })
  215. .start();
  216. }
  217. }