BattleControl.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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(mapId: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(mapId)
  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(mapId:number){
  49. this.clear()
  50. this.map.reset(mapId)
  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. // initAction(actionJson:string){
  91. // let actionList = JSON.parse(actionJson)
  92. // if(actionList){
  93. // this.map.actionMap = actionList
  94. // }
  95. // }
  96. //更新操作结果
  97. private updateAction(){
  98. let actionList = this.map.actionMap.get(this.map.curTurn)
  99. if(actionList == null){
  100. return
  101. }
  102. for(let i = 0; i < actionList.length; i++){
  103. let heroDataChange:BattleUtil.HeroDataChange = actionList[i]
  104. switch(heroDataChange.changeType){
  105. case ObjectValueType.InBattle:{
  106. let startPosID = heroDataChange.changeValueLlist[2]
  107. let startPos = this.map.attackPosDataMap.get(startPosID)
  108. if(startPos){
  109. let heroData = this.map.addHero(heroDataChange.changeValueLlist[0],heroDataChange.changeValueLlist[1],heroDataChange.changeValueLlist[2])
  110. startPos.hero = heroData
  111. }
  112. break
  113. }
  114. case ObjectValueType.OutBattle:{
  115. let startPosID = heroDataChange.changeValueLlist[0]
  116. this.map.removeHero(startPosID)
  117. break
  118. }
  119. case ObjectValueType.PosID:{
  120. let startPosID = heroDataChange.changeValueLlist[0]
  121. let endPosID = heroDataChange.changeValueLlist[1]
  122. let startPos = this.map.attackPosDataMap.get(startPosID)
  123. let endPos = this.map.attackPosDataMap.get(endPosID)
  124. let startHero = startPos?.hero
  125. let endHero = endPos?.hero
  126. if(startPos){
  127. startPos.hero = endHero
  128. endHero.position = startPos.pos
  129. }
  130. if(endPos){
  131. endPos.hero = startHero
  132. endHero.position = endPos.pos
  133. }
  134. break;
  135. }
  136. case ObjectValueType.Level:{
  137. let startPosID = heroDataChange.changeValueLlist[0]
  138. let startPos = this.map.attackPosDataMap.get(startPosID)
  139. if(startPos && startPos.hero){
  140. startPos.hero.levelUp()
  141. }
  142. break;
  143. }
  144. // case ObjectValueType.Attack:{
  145. // // let startPosID = heroDataChange.changeValueLlist[0]
  146. // // let startPos = this.map.attackPosDataMap1.get(startPosID)
  147. // // if(startPos && startPos.hero){
  148. // // startPos.hero.attackDamage = heroDataChange.changeValueLlist[1]
  149. // // }
  150. // break;
  151. // }
  152. // case ObjectValueType.AttackRadius:{
  153. // let startPosID = heroDataChange.changeValueLlist[0]
  154. // let startPos = this.map.attackPosDataMap1[startPosID]
  155. // if(startPos && startPos.hero){
  156. // startPos.hero.attackRadius = heroDataChange.changeValueLlist[1]
  157. // }
  158. // break;
  159. // }
  160. // case ObjectValueType.Speed:{
  161. // let startPosID = heroDataChange.changeValueLlist[0]
  162. // let startPos = this.map.attackPosDataMap1[startPosID]
  163. // if(startPos && startPos.hero){
  164. // startPos.hero.speed = heroDataChange.changeValueLlist[1]
  165. // }
  166. // break;
  167. // }
  168. // case ObjectValueType.Critical:{
  169. // let startPosID = heroDataChange.changeValueLlist[0]
  170. // let startPos = this.map.attackPosDataMap1[startPosID]
  171. // if(startPos && startPos.hero){
  172. // startPos.hero.critical = heroDataChange.changeValueLlist[1]
  173. // }
  174. // break;
  175. // }
  176. // case ObjectValueType.CriticalDamage:{
  177. // let startPosID = heroDataChange.changeValueLlist[0]
  178. // let startPos = this.map.attackPosDataMap1[startPosID]
  179. // if(startPos && startPos.hero){
  180. // startPos.hero.criticalDamage = heroDataChange.changeValueLlist[1]
  181. // }
  182. // break;
  183. // }
  184. }
  185. }
  186. }
  187. //更新子弹
  188. // private updateBullet(){
  189. // }
  190. //更新buff计时
  191. private updateBuff(){
  192. this.buffControl.update()
  193. }
  194. //更新敌人(基础参数,技能,buff)
  195. private updateEnemy(){
  196. this.enemyControl.update()
  197. }
  198. //更新英雄(基础参数,技能,buff)
  199. private updateHero(){
  200. this.heroControl.update()
  201. }
  202. //添加操作步骤
  203. addAction(action:BattleUtil.HeroDataChange){
  204. if(!this.map.actionMap.get(this.map.curTurn+1)){
  205. this.map.actionMap.set(this.map.curTurn+1,[])
  206. }
  207. this.map.actionMap.get(this.map.curTurn+1).push(action)
  208. console.log("添加操作步骤",this.map.curTurn,action)
  209. }
  210. //上阵
  211. addHeroInPos(heroID:number,level:number,index:number){
  212. let posID = this.getPosIDByIndex(index)
  213. if(posID == BattleUtil.PosID_Init){
  214. return
  215. }
  216. let action:BattleUtil.HeroDataChange = {
  217. changeType: ObjectValueType.InBattle,
  218. changeValueLlist: [heroID,level,posID]
  219. }
  220. this.addAction(action)
  221. }
  222. //下阵
  223. removeHeroInPos(index:number){
  224. let posID = this.getPosIDByIndex(index)
  225. let action:BattleUtil.HeroDataChange = {
  226. changeType: ObjectValueType.OutBattle,
  227. changeValueLlist: [posID]
  228. }
  229. this.addAction(action)
  230. }
  231. //升级
  232. levelUp(index:number){
  233. let posID = this.getPosIDByIndex(index)
  234. let action:BattleUtil.HeroDataChange = {
  235. changeType: ObjectValueType.Level,
  236. changeValueLlist: [posID]
  237. }
  238. this.addAction(action)
  239. }
  240. //判断是否结束
  241. get isBattleEnd():boolean{
  242. return this.map.battleEnd
  243. }
  244. getPosIDByIndex(index:number){
  245. return this.map.attackPosition[index] || BattleUtil.PosID_Init
  246. }
  247. //根据id获取坐标 取值范围[0,this.length-1]
  248. getPosition(id:number):BattleUtil.Vector2{
  249. return this.map.getPosition(id);
  250. }
  251. }