MapData.ts 12 KB

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