MapData.ts 13 KB

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