BattleControl.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // case ObjectValueType.Attack:{
  149. // // let startPosID = heroDataChange.changeValueLlist[0]
  150. // // let startPos = this.map.attackPosDataMap1.get(startPosID)
  151. // // if(startPos && startPos.hero){
  152. // // startPos.hero.attackDamage = heroDataChange.changeValueLlist[1]
  153. // // }
  154. // break;
  155. // }
  156. // case ObjectValueType.AttackRadius:{
  157. // let startPosID = heroDataChange.changeValueLlist[0]
  158. // let startPos = this.map.attackPosDataMap1[startPosID]
  159. // if(startPos && startPos.hero){
  160. // startPos.hero.attackRadius = heroDataChange.changeValueLlist[1]
  161. // }
  162. // break;
  163. // }
  164. // case ObjectValueType.Speed:{
  165. // let startPosID = heroDataChange.changeValueLlist[0]
  166. // let startPos = this.map.attackPosDataMap1[startPosID]
  167. // if(startPos && startPos.hero){
  168. // startPos.hero.speed = heroDataChange.changeValueLlist[1]
  169. // }
  170. // break;
  171. // }
  172. // case ObjectValueType.Critical:{
  173. // let startPosID = heroDataChange.changeValueLlist[0]
  174. // let startPos = this.map.attackPosDataMap1[startPosID]
  175. // if(startPos && startPos.hero){
  176. // startPos.hero.critical = heroDataChange.changeValueLlist[1]
  177. // }
  178. // break;
  179. // }
  180. // case ObjectValueType.CriticalDamage:{
  181. // let startPosID = heroDataChange.changeValueLlist[0]
  182. // let startPos = this.map.attackPosDataMap1[startPosID]
  183. // if(startPos && startPos.hero){
  184. // startPos.hero.criticalDamage = heroDataChange.changeValueLlist[1]
  185. // }
  186. // break;
  187. // }
  188. }
  189. }
  190. }
  191. //更新子弹
  192. // private updateBullet(){
  193. // }
  194. //更新buff计时
  195. private updateBuff(){
  196. this.buffControl.update()
  197. }
  198. //更新敌人(基础参数,技能,buff)
  199. private updateEnemy(){
  200. this.enemyControl.update()
  201. }
  202. //更新英雄(基础参数,技能,buff)
  203. private updateHero(){
  204. this.heroControl.update()
  205. }
  206. //添加操作步骤
  207. addAction(action:BattleUtil.HeroDataChange){
  208. if(!this.map.actionMap.get(this.map.curTurn+1)){
  209. this.map.actionMap.set(this.map.curTurn+1,[])
  210. }
  211. this.map.actionMap.get(this.map.curTurn+1).push(action)
  212. console.log("添加操作步骤",this.map.curTurn,action)
  213. }
  214. //上阵
  215. addHeroInPos(heroID:number,level:number,index:number){
  216. let posID = this.getPosIDByIndex(index)
  217. if(posID == BattleUtil.PosID_Init){
  218. return
  219. }
  220. let action:BattleUtil.HeroDataChange = {
  221. changeType: ObjectValueType.InBattle,
  222. changeValueLlist: [heroID,level,posID]
  223. }
  224. this.addAction(action)
  225. }
  226. //下阵
  227. removeHeroInPos(index:number){
  228. let posID = this.getPosIDByIndex(index)
  229. let action:BattleUtil.HeroDataChange = {
  230. changeType: ObjectValueType.OutBattle,
  231. changeValueLlist: [posID]
  232. }
  233. this.addAction(action)
  234. }
  235. //升级
  236. levelUp(index:number){
  237. let posID = this.getPosIDByIndex(index)
  238. let action:BattleUtil.HeroDataChange = {
  239. changeType: ObjectValueType.Level,
  240. changeValueLlist: [posID]
  241. }
  242. this.addAction(action)
  243. }
  244. //判断是否结束
  245. get isBattleEnd():boolean{
  246. return this.map.battleEnd
  247. }
  248. getPosIDByIndex(index:number){
  249. return this.map.attackPosition[index] || BattleUtil.PosID_Init
  250. }
  251. //根据id获取坐标 取值范围[0,this.length-1]
  252. getPosition(id:number):BattleUtil.Vector2{
  253. return this.map.getPosition(id);
  254. }
  255. }