MapData.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import { BattlesConf } from "../conf/BattlesConf";
  2. import { EnemyConf } from "../conf/EnemyConf";
  3. import { MapConf } from "../conf/MapConf";
  4. import { BattleUtil } from "./BattleUtil";
  5. import { BuffData, BuffDataPool } from "./BuffData";
  6. import { BulletData, BulletDataPool } from "./BulletData";
  7. import { DataBase } from "./DataBase";
  8. import { EnemyData, EnemyDataPool } from "./EnemyData";
  9. import { HeroData, HeroDataPool } from "./HeroData";
  10. //英雄站位
  11. export interface PosData {
  12. hero: HeroData,
  13. posID: number,
  14. pos: BattleUtil.Vector2
  15. }
  16. enum EnemyBornIndex{
  17. //当前批次第一个出生时间
  18. BornTime = 0,
  19. //出生时格子
  20. PosID,
  21. //敌人类型
  22. TypeID,
  23. //行走路线
  24. RoldID,
  25. //出生数量
  26. Count,
  27. //间隔时间
  28. Interval,
  29. //强度
  30. Level,
  31. }
  32. export interface EnemyBornData {
  33. bornTime: number,
  34. positionID: number,
  35. typeID: number,
  36. level: number,
  37. roldID: number
  38. }
  39. //地图格子 单例
  40. export class MapData {
  41. protected static instance:MapData;
  42. //-------------------配置数据----------------------
  43. //所属类型编号 章节id
  44. typeID: number;
  45. //关卡编号
  46. battlesID: number;
  47. //地图格子数量,目前固定是10*10
  48. size:BattleUtil.Vector2;
  49. //每个格子大小
  50. // sizeUnit:BattleUtil.Vector2;
  51. //格子数量
  52. length:number;
  53. //每个格子的坐标 从左下角开始
  54. /**
  55. * 90 91 92 93 94 95 96 97 98 99
  56. * 80 81 82 83 84 85 86 87 88 89
  57. * 70 71 72 73 74 75 76 77 78 79
  58. * 60 61 62 63 64 65 66 67 68 69
  59. * 50 51 52 53 54 55 56 57 58 59
  60. * 40 41 42 43 44 45 46 47 48 49
  61. * 30 31 32 33 34 35 36 37 38 39
  62. * 20 21 22 23 24 25 26 27 28 29
  63. * 10 11 12 13 14 15 16 17 18 19
  64. * 0 1 2 3 4 5 6 7 8 9
  65. */
  66. //pointList:Array<Array<number>>;
  67. //常规路径
  68. normalRold:Array<Array<number>>;
  69. //特殊路径
  70. // //捷径 至少有3个点:起点,终点,中间点,其中起点和终点是常规路径上的点
  71. // shortcut:Array<Array<number>>;
  72. //终点
  73. endPosition:number;
  74. //攻击点
  75. attackPosition:Array<number>;
  76. //敌人出生列表<turn,Array<EnemyBornData>>
  77. enemyBornMap:Map<number,Array<EnemyBornData>> = new Map();
  78. //出生时长
  79. bornNeedTime:number = 0;
  80. //-------------------动态数据----------------------
  81. // 攻击位<posID,PosData>
  82. attackPosDataMap: Map<number,PosData> = new Map();
  83. //所有存活的敌人
  84. enemyDataList:Array<EnemyData> = []
  85. //所有子弹
  86. bulletList:Array<BulletData> = []
  87. //<ID,BuffData>
  88. buffMap: Map<number,BuffData> = new Map();
  89. //地图点对应坐标坐标
  90. mapPosList:Array<BattleUtil.Vector2> = []
  91. //当前帧
  92. curTurn:number = 0;
  93. //游戏是否结束
  94. protected _battleEnd:boolean = false;
  95. protected _bWin:boolean = false;
  96. //操作列表 number表示帧数<turn,Array<BattleUtil.HeroDataChange>>
  97. actionMap:Map<number,Array<BattleUtil.HeroDataChange>> = new Map();
  98. //关卡总血量 从1开始防止除0异常
  99. allEnemyHP:number = 1;
  100. static GetInstance():MapData{
  101. if(!MapData.instance){
  102. MapData.instance = new MapData();
  103. }
  104. return MapData.instance;
  105. }
  106. //析构
  107. static Dispose(){
  108. MapData.instance = null;
  109. }
  110. set bWin(b:boolean){
  111. this._bWin = b;
  112. }
  113. get bWin():boolean{
  114. return this._bWin;
  115. }
  116. set battleEnd(b:boolean){
  117. this._battleEnd = b;
  118. if(b){
  119. this.bWin = this.enemyDataList.length == 0;
  120. let str:string = JSON.stringify(Object.fromEntries(this.actionMap))
  121. console.log(str)
  122. }
  123. }
  124. get battleEnd():boolean{
  125. return this._battleEnd;
  126. }
  127. init(battlesID:number){
  128. this.size = new BattleUtil.Vector2(10,10);
  129. //this.sizeUnit = new BattleUtil.Vector2(70,70);
  130. this.length = this.size.x*this.size.y;
  131. for(let i = 0; i < this.length; i++){
  132. this.mapPosList.push(this.getPosition(i))
  133. }
  134. this.reset(battlesID);
  135. }
  136. reset(battlesID:number){
  137. this.attackPosDataMap.clear();
  138. //所有存活的敌人
  139. this.enemyDataList.splice(0,this.enemyDataList.length);
  140. //所有子弹
  141. this.bulletList.splice(0,this.bulletList.length);
  142. this.buffMap.clear();
  143. this.enemyBornMap.clear();
  144. //操作列表 number表示帧数
  145. this.actionMap.clear();
  146. console.log("clear!")
  147. //读表
  148. this.battlesID = battlesID;
  149. let battlesConf = BattlesConf.data[battlesID.toString()];
  150. if(!battlesConf){
  151. console.error("没有找到关卡配置表,请检查配置表是否正确")
  152. return;
  153. }
  154. let typeID = battlesConf.Chapter;
  155. this.typeID = typeID;
  156. let mapConf = MapConf.data[typeID.toString()];
  157. this.normalRold = mapConf.RoldJsonArr;
  158. this.endPosition = this.normalRold[0][this.normalRold[0].length-1];
  159. this.attackPosition = mapConf.AttackPositionArray;
  160. this.allEnemyHP = 1;
  161. let EnemyJsonArr = mapConf.EnemyJsonArr;
  162. EnemyJsonArr.forEach((enemyObj)=>{
  163. for(let j=0;j<enemyObj[EnemyBornIndex.Count];j++){
  164. let enemyBornData:EnemyBornData = {
  165. bornTime: enemyObj[EnemyBornIndex.BornTime] + enemyObj[EnemyBornIndex.Interval]*j,
  166. positionID: enemyObj[EnemyBornIndex.PosID],
  167. typeID: enemyObj[EnemyBornIndex.TypeID],
  168. level: battlesConf.EnemyLevel,
  169. roldID: enemyObj[EnemyBornIndex.RoldID],
  170. }
  171. this.allEnemyHP += enemyBornData.level*EnemyConf.data[typeID].Life;
  172. if(this.enemyBornMap.has(enemyBornData.bornTime)){
  173. this.enemyBornMap.get(enemyBornData.bornTime).push(enemyBornData);
  174. }else{
  175. if(this.bornNeedTime < enemyBornData.bornTime){
  176. this.bornNeedTime = enemyBornData.bornTime;
  177. }
  178. this.enemyBornMap.set(enemyBornData.bornTime,[enemyBornData]);
  179. }
  180. }
  181. })
  182. //初始化boss
  183. let bossJsonArr = battlesConf.BossJsonArr;
  184. if(bossJsonArr){
  185. bossJsonArr.forEach((enemyObj)=>{
  186. for(let j=0;j<enemyObj[EnemyBornIndex.Count];j++){
  187. let enemyBornData:EnemyBornData = {
  188. bornTime: enemyObj[EnemyBornIndex.BornTime] + enemyObj[EnemyBornIndex.Interval]*j,
  189. positionID: enemyObj[EnemyBornIndex.PosID],
  190. typeID: enemyObj[EnemyBornIndex.TypeID],
  191. level: enemyObj[EnemyBornIndex.Level],
  192. roldID: enemyObj[EnemyBornIndex.RoldID],
  193. }
  194. this.allEnemyHP += enemyBornData.level*EnemyConf.data[typeID].Life;
  195. if(this.enemyBornMap.has(enemyBornData.bornTime)){
  196. this.enemyBornMap.get(enemyBornData.bornTime).push(enemyBornData);
  197. }else{
  198. if(this.bornNeedTime < enemyBornData.bornTime){
  199. this.bornNeedTime = enemyBornData.bornTime;
  200. }
  201. this.enemyBornMap.set(enemyBornData.bornTime,[enemyBornData]);
  202. }
  203. }
  204. })
  205. }
  206. this.attackPosDataMap.clear()
  207. this.attackPosition.forEach((v,k)=>{
  208. //this.pointList[pos.x][pos.y] = 1;
  209. let podID = Number(v)
  210. let posData:PosData = {
  211. hero: null,
  212. posID: podID,
  213. pos: this.getPosition(podID)
  214. }
  215. this.attackPosDataMap.set(v,posData);
  216. })
  217. this.curTurn = 0;
  218. this._battleEnd = false;
  219. this._bWin = false;
  220. }
  221. getNormalRold(id:number):Array<number>{
  222. return this.normalRold[id%this.normalRold.length];
  223. }
  224. //根据id获取坐标 取值范围[0,this.length-1]
  225. getPosition(id:number):BattleUtil.Vector2{
  226. if(id<0 || id>=this.length) return null;
  227. let pos = new BattleUtil.Vector2(0,0);
  228. pos.x = id%this.size.x;
  229. pos.y = Math.floor(id/this.size.x);
  230. return pos;
  231. }
  232. //添加到地图上
  233. addHero(typeID:number,level:number,posID:number){
  234. let hero = HeroDataPool.getObject()
  235. hero.init(typeID,level,posID);
  236. console.log(typeof(posID),posID)
  237. if(this.attackPosDataMap.has(posID)){
  238. let posData = this.attackPosDataMap.get(posID)
  239. let posHero = posData.hero;
  240. if(posHero){
  241. posHero.clear();
  242. HeroDataPool.putObject(posHero);
  243. }
  244. posData.hero = hero
  245. hero.position = posData.pos;
  246. }
  247. return hero;
  248. }
  249. //移除地图上指定位置的英雄
  250. removeHero(posID:number){
  251. if(this.attackPosDataMap.has(posID)){
  252. let posData = this.attackPosDataMap.get(posID);
  253. let posHero = posData.hero;
  254. if(posHero){
  255. posHero.clear();
  256. HeroDataPool.putObject(posHero);
  257. }
  258. posData.hero = null;
  259. }
  260. }
  261. /**
  262. *添加敌人
  263. * @param typeID 敌人类型
  264. * @param level 敌人等级
  265. * @param roldID 敌人行走路径
  266. * @param bronPos 敌人在路径上哪个点走
  267. */
  268. addEnemy(typeID:number,level:number,roldID:number,bronPos:number){
  269. let enemy = EnemyDataPool.getObject()
  270. let rold = this.getNormalRold(roldID);
  271. enemy.init(typeID,level,rold);
  272. this.enemyDataList.push(enemy);
  273. enemy.position = this.getPosition(bronPos);
  274. enemy.speedVector = new BattleUtil.Vector2(0,0);
  275. enemy.speedVector = this.getEnemySpeedVector(enemy);
  276. enemy.posID = bronPos;
  277. return enemy;
  278. }
  279. //移除地图上指定的敌人
  280. removeEnemy(enemy:EnemyData){
  281. let index = this.enemyDataList.indexOf(enemy);
  282. if(index>=0){
  283. this.enemyDataList.splice(index,1);
  284. }
  285. enemy.clear();
  286. EnemyDataPool.putObject(enemy);
  287. }
  288. getEnemySpeedVector(enemy:EnemyData):BattleUtil.Vector2{
  289. let oldPos = this.mapPosList[enemy.posID]
  290. let newPos = this.mapPosList[enemy.nextPosID]
  291. if(newPos){
  292. let direction = BattleUtil.Vector2.Sub(newPos,oldPos)
  293. direction.multiply(enemy.speedCur)
  294. return direction;
  295. }
  296. return new BattleUtil.Vector2(0,0);
  297. }
  298. addBuff(buffID:number,duration:number,valueList:Array<number>,src:DataBase,target:DataBase){
  299. let buff:BuffData = this.buffMap.get(buffID)
  300. if(buff == null){
  301. buff = BuffDataPool.getObject()
  302. buff.init(buffID,duration,valueList,src,target)
  303. this.buffMap.set(buff.ID,buff)
  304. if(target instanceof EnemyData){
  305. target.addBuff(buff)
  306. }
  307. }
  308. return buff;
  309. }
  310. removeBuff(ID:number){
  311. let buff:BuffData = this.buffMap.get(ID)
  312. if(buff != null){
  313. let target = buff.targetDataBase
  314. if(target instanceof EnemyData){
  315. target.removeBuff(buff)
  316. }
  317. buff.clear()
  318. BuffDataPool.putObject(buff)
  319. this.buffMap.delete(ID)
  320. }
  321. }
  322. addBullet(typeID:number,srcHero:HeroData,targetEnemy:EnemyData){
  323. let bullet:BulletData = BulletDataPool.getObject()
  324. bullet.init(typeID,srcHero,targetEnemy)
  325. this.bulletList.push(bullet)
  326. return bullet;
  327. }
  328. removeBullet(bullet:BulletData){
  329. let index = this.bulletList.indexOf(bullet);
  330. if(index>=0){
  331. this.bulletList.splice(index,1);
  332. }
  333. bullet.clear();
  334. BulletDataPool.putObject(bullet);
  335. }
  336. clear(){
  337. this.attackPosDataMap.forEach((posData,posID)=>{
  338. if(posData.hero){
  339. posData.hero.clear();
  340. HeroDataPool.putObject(posData.hero);
  341. }
  342. })
  343. this.attackPosDataMap.clear();
  344. this.enemyDataList.forEach((enemy)=>{
  345. enemy.clear();
  346. EnemyDataPool.putObject(enemy);
  347. })
  348. this.enemyDataList = [];
  349. // this.bulletList.forEach((bullet)=>{
  350. // bullet.clear();
  351. // BulletDataPool.putObject(bullet);
  352. // })
  353. // this.bulletList = [];
  354. this.buffMap.forEach((buff)=>{
  355. buff.clear();
  356. BuffDataPool.putObject(buff);
  357. })
  358. }
  359. }