import { Role } from "../../../common/InterfaceAddEnum"; import { BattlesConf } from "../conf/BattlesConf"; import { EnemyConf } from "../conf/EnemyConf"; import { MapConf } from "../conf/MapConf"; import { BattleUtil } from "./BattleUtil"; import { BuffData, BuffDataPool } from "./BuffData"; import { BulletData, BulletDataPool } from "./BulletData"; import { DataBase } from "./DataBase"; import { EnemyData, EnemyDataPool } from "./EnemyData"; import { HeroData, HeroDataPool } from "./HeroData"; //英雄站位 export interface PosData { hero: HeroData, posID: number, pos: BattleUtil.Vector2 } enum EnemyBornIndex{ //当前批次第一个出生时间 BornTime = 0, //出生时格子 PosID, //敌人类型 TypeID, //行走路线 RoldID, //出生数量 Count, //间隔时间 Interval, //强度 Level, } export interface EnemyBornData { bornTime: number, positionID: number, typeID: number, level: number, roldID: number } //地图格子 单例 export class MapData { protected static instance:MapData; //-------------------配置数据---------------------- //所属类型编号 章节id typeID: number; //关卡编号 battlesID: number; //地图格子数量,目前固定是10*10 size:BattleUtil.Vector2; //每个格子大小 // sizeUnit:BattleUtil.Vector2; //格子数量 length:number; //每个格子的坐标 从左下角开始 /** * 90 91 92 93 94 95 96 97 98 99 * 80 81 82 83 84 85 86 87 88 89 * 70 71 72 73 74 75 76 77 78 79 * 60 61 62 63 64 65 66 67 68 69 * 50 51 52 53 54 55 56 57 58 59 * 40 41 42 43 44 45 46 47 48 49 * 30 31 32 33 34 35 36 37 38 39 * 20 21 22 23 24 25 26 27 28 29 * 10 11 12 13 14 15 16 17 18 19 * 0 1 2 3 4 5 6 7 8 9 */ //pointList:Array>; //常规路径 normalRold:Array>; //特殊路径 // //捷径 至少有3个点:起点,终点,中间点,其中起点和终点是常规路径上的点 // shortcut:Array>; //终点 endPosition:number; //攻击点 attackPosition:Array; //敌人出生列表> enemyBornMap:Map> = new Map(); //出生时长 bornNeedTime:number = 0; //-------------------动态数据---------------------- // 攻击位 attackPosDataMap: Map = new Map(); //所有存活的敌人 enemyDataList:Array = [] //所有子弹 bulletList:Array = [] // buffMap: Map = new Map(); //地图点对应坐标坐标 mapPosList:Array = [] //当前帧 curTurn:number = 0; //游戏是否结束 protected _battleEnd:boolean = false; protected _bWin:boolean = false; //操作列表 number表示帧数> actionMap:Map> = new Map(); //关卡总血量 从1开始防止除0异常 allEnemyHP:number = 1; static GetInstance():MapData{ if(!MapData.instance){ MapData.instance = new MapData(); } return MapData.instance; } //析构 static Dispose(){ MapData.instance = null; } set bWin(b:boolean){ this._bWin = b; } get bWin():boolean{ return this._bWin; } set battleEnd(b:boolean){ this._battleEnd = b; if(b){ this.bWin = this.enemyDataList.length == 0; let str:string = JSON.stringify(Object.fromEntries(this.actionMap)) console.log(str) } } get battleEnd():boolean{ return this._battleEnd; } init(battlesID:number){ this.size = new BattleUtil.Vector2(10,10); //this.sizeUnit = new BattleUtil.Vector2(70,70); this.length = this.size.x*this.size.y; for(let i = 0; i < this.length; i++){ this.mapPosList.push(this.getPosition(i)) } this.reset(battlesID); } reset(battlesID:number){ this.attackPosDataMap.clear(); //所有存活的敌人 this.enemyDataList.splice(0,this.enemyDataList.length); //所有子弹 this.bulletList.splice(0,this.bulletList.length); this.buffMap.clear(); this.enemyBornMap.clear(); //操作列表 number表示帧数 this.actionMap.clear(); console.log("clear!") //读表 this.battlesID = battlesID; let battlesConf = BattlesConf.data[battlesID.toString()]; if(!battlesConf){ console.error("没有找到关卡配置表,请检查配置表是否正确") return; } let typeID = battlesConf.Chapter; this.typeID = typeID; let mapConf = MapConf.data[typeID.toString()]; this.normalRold = mapConf.RoldJsonArr; this.endPosition = this.normalRold[0][this.normalRold[0].length-1]; this.attackPosition = mapConf.AttackPositionArray; this.allEnemyHP = 1; let EnemyJsonArr = mapConf.EnemyJsonArr; EnemyJsonArr.forEach((enemyObj)=>{ for(let j=0;j{ for(let j=0;j{ //this.pointList[pos.x][pos.y] = 1; let podID = Number(v) let posData:PosData = { hero: null, posID: podID, pos: this.getPosition(podID) } this.attackPosDataMap.set(v,posData); }) this.curTurn = 0; this._battleEnd = false; this._bWin = false; } getNormalRold(id:number):Array{ return this.normalRold[id%this.normalRold.length]; } //根据id获取坐标 取值范围[0,this.length-1] getPosition(id:number):BattleUtil.Vector2{ if(id<0 || id>=this.length) return null; let pos = new BattleUtil.Vector2(0,0); pos.x = id%this.size.x; pos.y = Math.floor(id/this.size.x); return pos; } //添加到地图上 addHero(role:Role,level:number,posID:number){ let hero = HeroDataPool.getObject() hero.init(role,level,posID); console.log(typeof(posID),posID) if(this.attackPosDataMap.has(posID)){ let posData = this.attackPosDataMap.get(posID) let posHero = posData.hero; if(posHero){ posHero.clear(); HeroDataPool.putObject(posHero); } posData.hero = hero hero.position = posData.pos; } return hero; } //移除地图上指定位置的英雄 removeHero(posID:number){ if(this.attackPosDataMap.has(posID)){ let posData = this.attackPosDataMap.get(posID); let posHero = posData.hero; if(posHero){ posHero.clear(); HeroDataPool.putObject(posHero); } posData.hero = null; } } /** *添加敌人 * @param typeID 敌人类型 * @param level 敌人等级 * @param roldID 敌人行走路径 * @param bronPos 敌人在路径上哪个点走 */ addEnemy(typeID:number,level:number,roldID:number,bronPos:number){ let enemy = EnemyDataPool.getObject() let rold = this.getNormalRold(roldID); enemy.init(typeID,level,rold); this.enemyDataList.push(enemy); enemy.position = this.getPosition(bronPos); enemy.speedVector = new BattleUtil.Vector2(0,0); enemy.speedVector = this.getEnemySpeedVector(enemy); enemy.posID = bronPos; return enemy; } //移除地图上指定的敌人 removeEnemy(enemy:EnemyData){ let index = this.enemyDataList.indexOf(enemy); if(index>=0){ this.enemyDataList.splice(index,1); } enemy.clear(); EnemyDataPool.putObject(enemy); } getEnemySpeedVector(enemy:EnemyData):BattleUtil.Vector2{ let oldPos = this.mapPosList[enemy.posID] let newPos = this.mapPosList[enemy.nextPosID] if(newPos){ let direction = BattleUtil.Vector2.Sub(newPos,oldPos) direction.multiply(enemy.speedCur) return direction; } return new BattleUtil.Vector2(0,0); } addBuff(buffID:number,duration:number,valueList:Array,src:DataBase,target:DataBase){ let buff:BuffData = this.buffMap.get(buffID) if(buff == null){ buff = BuffDataPool.getObject() buff.init(buffID,duration,valueList,src,target) this.buffMap.set(buff.ID,buff) if(target instanceof EnemyData){ target.addBuff(buff) } } return buff; } removeBuff(ID:number){ let buff:BuffData = this.buffMap.get(ID) if(buff != null){ let target = buff.targetDataBase if(target instanceof EnemyData){ target.removeBuff(buff) } buff.clear() BuffDataPool.putObject(buff) this.buffMap.delete(ID) } } addBullet(typeID:number,srcHero:HeroData,targetEnemy:EnemyData){ let bullet:BulletData = BulletDataPool.getObject() bullet.init(typeID,srcHero,targetEnemy) this.bulletList.push(bullet) return bullet; } removeBullet(bullet:BulletData){ let index = this.bulletList.indexOf(bullet); if(index>=0){ this.bulletList.splice(index,1); } bullet.clear(); BulletDataPool.putObject(bullet); } clear(){ this.attackPosDataMap.forEach((posData,posID)=>{ if(posData.hero){ posData.hero.clear(); HeroDataPool.putObject(posData.hero); } }) this.attackPosDataMap.clear(); this.enemyDataList.forEach((enemy)=>{ enemy.clear(); EnemyDataPool.putObject(enemy); }) this.enemyDataList = []; // this.bulletList.forEach((bullet)=>{ // bullet.clear(); // BulletDataPool.putObject(bullet); // }) // this.bulletList = []; this.buffMap.forEach((buff)=>{ buff.clear(); BuffDataPool.putObject(buff); }) } }