BattleControl.ts 7.5 KB

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