123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- 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<Array<number>>;
- //常规路径
- normalRold:Array<Array<number>>;
- //特殊路径
- // //捷径 至少有3个点:起点,终点,中间点,其中起点和终点是常规路径上的点
- // shortcut:Array<Array<number>>;
- //终点
- endPosition:number;
- //攻击点
- attackPosition:Array<number>;
- //敌人出生列表<turn,Array<EnemyBornData>>
- enemyBornMap:Map<number,Array<EnemyBornData>> = new Map();
- //出生时长
- bornNeedTime:number = 0;
- //-------------------动态数据----------------------
- // 攻击位<posID,PosData>
- attackPosDataMap: Map<number,PosData> = new Map();
- //所有存活的敌人
- enemyDataList:Array<EnemyData> = []
- //所有子弹
- bulletList:Array<BulletData> = []
- //<ID,BuffData>
- buffMap: Map<number,BuffData> = new Map();
- //地图点对应坐标坐标
- mapPosList:Array<BattleUtil.Vector2> = []
- //当前帧
- curTurn:number = 0;
- //游戏是否结束
- protected _battleEnd:boolean = false;
- protected _bWin:boolean = false;
- //操作列表 number表示帧数<turn,Array<BattleUtil.HeroDataChange>>
- actionMap:Map<number,Array<BattleUtil.HeroDataChange>> = 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<enemyObj[EnemyBornIndex.Count];j++){
- let enemyBornData:EnemyBornData = {
- bornTime: enemyObj[EnemyBornIndex.BornTime] + enemyObj[EnemyBornIndex.Interval]*j,
- positionID: enemyObj[EnemyBornIndex.PosID],
- typeID: enemyObj[EnemyBornIndex.TypeID],
- level: battlesConf.EnemyLevel,
- roldID: enemyObj[EnemyBornIndex.RoldID],
- }
- this.allEnemyHP += enemyBornData.level*EnemyConf.data[typeID].Life;
- if(this.enemyBornMap.has(enemyBornData.bornTime)){
- this.enemyBornMap.get(enemyBornData.bornTime).push(enemyBornData);
- }else{
- if(this.bornNeedTime < enemyBornData.bornTime){
- this.bornNeedTime = enemyBornData.bornTime;
- }
- this.enemyBornMap.set(enemyBornData.bornTime,[enemyBornData]);
- }
- }
-
- })
- //初始化boss
- let bossJsonArr = battlesConf.BossJsonArr;
- if(bossJsonArr){
- bossJsonArr.forEach((enemyObj)=>{
- for(let j=0;j<enemyObj[EnemyBornIndex.Count];j++){
- let enemyBornData:EnemyBornData = {
- bornTime: enemyObj[EnemyBornIndex.BornTime] + enemyObj[EnemyBornIndex.Interval]*j,
- positionID: enemyObj[EnemyBornIndex.PosID],
- typeID: enemyObj[EnemyBornIndex.TypeID],
- level: enemyObj[EnemyBornIndex.Level],
- roldID: enemyObj[EnemyBornIndex.RoldID],
- }
- this.allEnemyHP += enemyBornData.level*EnemyConf.data[typeID].Life;
- if(this.enemyBornMap.has(enemyBornData.bornTime)){
- this.enemyBornMap.get(enemyBornData.bornTime).push(enemyBornData);
- }else{
- if(this.bornNeedTime < enemyBornData.bornTime){
- this.bornNeedTime = enemyBornData.bornTime;
- }
- this.enemyBornMap.set(enemyBornData.bornTime,[enemyBornData]);
- }
- }
-
- })
- }
- this.attackPosDataMap.clear()
- this.attackPosition.forEach((v,k)=>{
- //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<number>{
- 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<number>,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);
- })
- }
- }
|