BattleControl.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { RoleData } from "../../../data/RoleData";
  2. import { BattleBase } from "../base/BattleBase";
  3. import { BattleEventManager } from "../base/BattleEventManager";
  4. import { BattleEventData_Over, BattleEventTarget, BattleEventType } from "../base/BattleEventUtil";
  5. import { ObjectValueType } from "../data/BattleEnum";
  6. import { BattleUtil } from "../data/BattleUtil";
  7. import { MapData } from "../data/MapData";
  8. import { BuffControl } from "./BuffControl";
  9. import { EnemyControl } from "./EnemyControl";
  10. import { HeroControl } from "./HeroControl";
  11. import { SkillControl } from "./SkillControl";
  12. //战斗控制类
  13. export class BattleControl extends BattleBase{
  14. map:MapData = null
  15. //英雄控制类
  16. heroControl:HeroControl = null
  17. //敌人控制类
  18. enemyControl:EnemyControl = null
  19. //buff控制类
  20. buffControl:BuffControl = null
  21. battleEventManager:BattleEventManager = null
  22. private static instance:BattleControl;
  23. static GetInstance():BattleControl{
  24. if(!BattleControl.instance){
  25. BattleControl.instance = new BattleControl();
  26. }
  27. return BattleControl.instance;
  28. }
  29. //初始化
  30. init(battleID:number){
  31. this.battleEventManager = BattleEventManager.instance
  32. this.map = MapData.GetInstance()
  33. this.heroControl = new HeroControl()
  34. this.enemyControl = new EnemyControl()
  35. this.buffControl = new BuffControl()
  36. this.map.init(battleID)
  37. this.buffControl.init()
  38. this.heroControl.init()
  39. this.enemyControl.init()
  40. SkillControl.GetInstance().init()
  41. // for(let i = 0; i < BattleUtil.TurnMax; i++){
  42. // this.update()
  43. // if(this.map.battleEnd){
  44. // break;
  45. // }
  46. // }
  47. // this.map.battleEnd = true
  48. }
  49. reset(battleID:number){
  50. this.clear()
  51. this.map.reset(battleID)
  52. this.buffControl.init()
  53. this.heroControl.init()
  54. this.enemyControl.init()
  55. SkillControl.GetInstance().init()
  56. }
  57. clear(){
  58. this.map.clear()
  59. }
  60. //每次加一帧
  61. update(){
  62. this.map.curTurn++
  63. if(this.map.battleEnd){
  64. return
  65. }
  66. //更新操作结果
  67. this.updateAction()
  68. //更新子弹
  69. // this.updateBullet()
  70. this.updateBuff()
  71. //更新敌人(基础参数,技能,buff)
  72. this.updateEnemy()
  73. //更新英雄(基础参数,技能,buff)
  74. this.updateHero()
  75. this.buffControl.updateNewBuff()
  76. //判断是否结束
  77. if((this.map.enemyDataList.length == 0) && (this.map.bornNeedTime < this.map.curTurn)){
  78. this.map.battleEnd = true
  79. this.map.bWin = true
  80. let eventData:BattleEventData_Over = {
  81. eventType: BattleEventType.Over,
  82. bWin: true
  83. }
  84. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  85. }
  86. }
  87. get mapID():number{
  88. return this.map.typeID
  89. }
  90. //敌人总血量
  91. get allEnemyHP():number{
  92. return this.map.allEnemyHP
  93. }
  94. //初始化操作列表,服务器才用的
  95. // initAction(actionJson:string){
  96. // let actionList = JSON.parse(actionJson)
  97. // if(actionList){
  98. // this.map.actionMap = actionList
  99. // }
  100. // }
  101. //更新操作结果
  102. private updateAction(){
  103. let actionList = this.map.actionMap.get(this.map.curTurn)
  104. if(actionList == null){
  105. return
  106. }
  107. for(let i = 0; i < actionList.length; i++){
  108. let heroDataChange:BattleUtil.HeroDataChange = actionList[i]
  109. switch(heroDataChange.changeType){
  110. case ObjectValueType.InBattle:{
  111. let startPosID = heroDataChange.changeValueLlist[2]
  112. let startPos = this.map.attackPosDataMap.get(startPosID)
  113. if(startPos){
  114. let role = RoleData.getRoleDataByID(heroDataChange.changeValueLlist[0])
  115. let heroData = this.map.addHero(role,heroDataChange.changeValueLlist[1],heroDataChange.changeValueLlist[2])
  116. startPos.hero = heroData
  117. }
  118. break
  119. }
  120. case ObjectValueType.OutBattle:{
  121. let startPosID = heroDataChange.changeValueLlist[0]
  122. this.map.removeHero(startPosID)
  123. break
  124. }
  125. case ObjectValueType.PosID:{
  126. let startPosID = heroDataChange.changeValueLlist[0]
  127. let endPosID = heroDataChange.changeValueLlist[1]
  128. let startPos = this.map.attackPosDataMap.get(startPosID)
  129. let endPos = this.map.attackPosDataMap.get(endPosID)
  130. let startHero = startPos?.hero
  131. let endHero = endPos?.hero
  132. if(startPos){
  133. startPos.hero = endHero
  134. endHero.position = startPos.pos
  135. }
  136. if(endPos){
  137. endPos.hero = startHero
  138. endHero.position = endPos.pos
  139. }
  140. break;
  141. }
  142. case ObjectValueType.Level:{
  143. let startPosID = heroDataChange.changeValueLlist[0]
  144. let startPos = this.map.attackPosDataMap.get(startPosID)
  145. if(startPos && startPos.hero){
  146. startPos.hero.levelUp()
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. //更新buff计时
  154. private updateBuff(){
  155. this.buffControl.update()
  156. }
  157. //更新敌人(基础参数,技能,buff)
  158. private updateEnemy(){
  159. this.enemyControl.update()
  160. }
  161. //更新英雄(基础参数,技能,buff)
  162. private updateHero(){
  163. this.heroControl.update()
  164. }
  165. //添加操作步骤
  166. addAction(action:BattleUtil.HeroDataChange){
  167. if(!this.map.actionMap.get(this.map.curTurn+1)){
  168. this.map.actionMap.set(this.map.curTurn+1,[])
  169. }
  170. this.map.actionMap.get(this.map.curTurn+1).push(action)
  171. console.log("添加操作步骤",this.map.curTurn,action)
  172. }
  173. //上阵
  174. addHeroInPos(heroID:number,level:number,index:number){
  175. let posID = this.getPosIDByIndex(index)
  176. if(posID == BattleUtil.PosID_Init){
  177. return
  178. }
  179. let action:BattleUtil.HeroDataChange = {
  180. changeType: ObjectValueType.InBattle,
  181. changeValueLlist: [heroID,level,posID]
  182. }
  183. this.addAction(action)
  184. }
  185. //下阵
  186. removeHeroInPos(index:number){
  187. let posID = this.getPosIDByIndex(index)
  188. let action:BattleUtil.HeroDataChange = {
  189. changeType: ObjectValueType.OutBattle,
  190. changeValueLlist: [posID]
  191. }
  192. this.addAction(action)
  193. }
  194. //升级
  195. levelUp(index:number){
  196. let posID = this.getPosIDByIndex(index)
  197. let action:BattleUtil.HeroDataChange = {
  198. changeType: ObjectValueType.Level,
  199. changeValueLlist: [posID]
  200. }
  201. this.addAction(action)
  202. }
  203. //判断是否结束
  204. get isBattleEnd():boolean{
  205. return this.map.battleEnd
  206. }
  207. getPosIDByIndex(index:number){
  208. return this.map.attackPosition[index] || BattleUtil.PosID_Init
  209. }
  210. //根据id获取坐标 取值范围[0,this.length-1]
  211. getPosition(id:number):BattleUtil.Vector2{
  212. return this.map.getPosition(id);
  213. }
  214. }