This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.Engine.min.js
1 lines (1 loc) · 9.99 KB
/
class.Engine.min.js
1
if('undefined'==typeof OGX){}OGX.StarEngine.Engine=class{constructor(a){this.config=a,this.camera=a.camera,this.map=null,this.view={x:null,y:null,offset:0,width:a.width,height:a.height,zoom:1,tile:{}},this.container=null,this.stage=null,this.background=null,this.foreground=null,this.topground=null,this.renderer=null,this.renderTexture=null,this.add0=null,this.add1=null,this.pt1={x:0,y:0},this.pt2={x:0,y:0},this.bt=null,this.addTiles={},this.removeTiles={},this.tiles0={},this.tiles1={},this.tile=null,this.tile1=null,this.tileRec={x:0,y:0,width:0,height:0},this.new_tile={x:0,y:0},this.changeX=null,this.changeY=null,this.dirX=null,this.dirY=null,this.stats=null,this.resize=null,this.view.w=a.width,this.view.h=a.height,this.state=OGX.StarEngine.Engine.STATE_STOPPED,this.initPixi(),this.loadTextures()}static get STATE_RUNNING(){return'running'}static get STATE_STOPPED(){return'stopped'}static get TEXTURES_LOADED(){return'texturesLoaded'}setCamera(a){var b=Math.ceil,c=Math.floor;this.camera=a,this.view.tile={x:c(this.camera.x/+this.map.tileSize),y:c(this.camera.y/+this.map.tileSize)},this.view.x=this.camera.x,this.view.y=this.camera.y,this.camera.viz={},this.camera.viz.x=b(this.view.width/(this.config.tileSize/2)/2)+1,this.camera.viz.y=b(this.view.height/(this.config.tileSize/4)/2)+1}setMap(a){this.map=a}setZoom(a){this.renderZoom(a),this.resize.scale={x:a,y:a}}renderView(){this.renderBaseView(),this.updateBaseView(),this.renderTopView(),this.updateTopView(),this.renderer.render(this.stage)}clearView(){}start(){this.camera&&this.map&&this.state===OGX.StarEngine.Engine.STATE_STOPPED&&(this.state=OGX.StarEngine.Engine.STATE_RUNNING,this.run(!0))}stop(){this.state===OGX.StarEngine.Engine.STATE_RUNNING&&(this.state=OGX.StarEngine.Engine.STATE_STOPPED,this.run(!1))}renderBaseView(){var a=Math.floor;this.view.x=this.camera.x,this.view.y=this.camera.y,this.view.tile.x=a(this.camera.x/this.config.tileSize),this.view.tile.y=a(this.camera.y/this.config.tileSize),this.camera.tile={x:this.view.tile.x,y:this.view.tile.y};let b=this.view.tile.x,c=1,d=this.view.tile.x,e=this.view.tile.x-this.camera.viz.x,f=this.view.tile.y-this.camera.viz.y,g=this.view.tile.x+this.camera.viz.x,h=this.view.tile.y+this.camera.viz.y;for(let a=f;a<h+1;a++){for(let c=e;c<g+1;c++)this.add0=!1,this.add1=!1,this.pt1.x=c*this.config.tileSize,this.pt1.y=a*this.config.tileSize,this.pt2=this.convert_2D_ISO(this.pt1),c<b||c>d||(this.map.levels[1].tiles['t_'+c+'_'+a]?(this.add1=!0,this.add0=this.map.levels[1].tiles['t_'+c+'_'+a].border):this.add0=!0,this.add1&&(this.addTiles['t_'+c+'_'+a]={pt2d:this.pt1,ptIso:this.pt2}),this.add0&&this.addGroundTile(c,a));b-=c,d+=c,b===this.view.tile.x-this.camera.viz.x&&(c*=-1)}return this.camera.tile=this.view.tile,this.pt1.x=-this.camera.x,this.pt1.y=-this.camera.y,this.pt1=this.convert_2D_ISO(this.pt1),this.container.position.x=this.pt1.x+this.config.width*this.view.zoom/2,this.container.position.y=this.pt1.y+this.config.height*this.view.zoom/2,this.sortZ(this.foreground),!0}updateBaseView(){var a=Math.ceil,b=Math.floor;if(this.new_tile={x:b(this.camera.x/this.config.tileSize),y:b(this.camera.y/this.config.tileSize)},this.changeY=this.new_tile.y!==this.view.tile.y,this.changeX=this.new_tile.x!==this.view.tile.x,this.dirX=-1*a(this.view.tile.x-this.new_tile.x),this.dirY=-1*a(this.view.tile.y-this.new_tile.y),this.changeX&&this.changeY){let a={x:this.view.tile.x,y:this.view.tile.y+-1*(this.dirY*this.camera.viz.y)},b={x:this.new_tile.x,y:this.new_tile.y+this.camera.viz.y*this.dirY},c=2*this.camera.viz.x+1,d=b.x,e=b.y,f=a.x,g=a.y,h=0;for(let a=0;a<c;a++)this.add0=!1,this.add1=!1,this.pt1.x=d*this.config.tileSize,this.pt1.y=e*this.config.tileSize,this.tileRec={x:this.pt1.x,y:this.pt1.y,width:this.config.tileSize,height:this.config.tileSize},this.map.levels[1].tiles['t_'+d+'_'+e]?(this.add1=!0,this.add0=this.map.levels[1].tiles['t_'+d+'_'+e].border):this.add0=!0,this.add1&&(this.addTiles['t_'+d+'_'+e]={pt2d:this.pt1,ptIso:this.convert_2D_ISO(this.pt1)}),this.add0&&this.addGroundTile(d,e),this.tiles1['t_'+f+'_'+g]&&(this.removeTiles['t_'+f+'_'+g]=!0),this.tiles0['t_'+f+'_'+g]&&(this.tiles0['t_'+f+'_'+g].destroy(),delete this.tiles0['t_'+f+'_'+g]),0==h?(e+=-1*this.dirY,g+=this.dirY,h=1):(d+=this.dirX,f+=-1*this.dirX,h=0)}else if(this.changeX){let a=this.camera.tile.y-this.camera.viz.y,b=this.new_tile.y+this.camera.viz.y,c=this.camera.tile.x+this.dirX,d=c+this.camera.viz.x*this.dirX,e=this.camera.tile.x;for(let f=a;f<b+1;f++)this.add0=!1,this.add1=!1,this.pt1.x=c*this.config.tileSize,this.pt1.y=f*this.config.tileSize,this.tileRec={x:this.pt1.x,y:this.pt1.y,width:this.config.tileSize,height:this.config.tileSize},this.map.levels[1].tiles['t_'+c+'_'+f]?(this.add1=!0,this.add0=this.map.levels[1].tiles['t_'+c+'_'+f].border):(this.add0=!0,this.add1=!1),this.add1&&(this.addTiles['t_'+c+'_'+f]={pt2d:this.pt1,ptIso:this.convert_2D_ISO(this.pt1)}),this.add0&&this.addGroundTile(c,f),this.tiles0['t_'+e+'_'+f]&&(this.tiles0['t_'+e+'_'+f].destroy(),delete this.tiles0['t_'+e+'_'+f]),this.tiles1['t_'+e+'_'+f]&&(this.removeTiles['t_'+e+'_'+f]=!0),c+this.dirX===d+this.dirX&&(this.dirX*=-1),c+=this.dirX,e-=this.dirX}else if(this.changeY){let a=this.camera.tile.x-this.camera.viz.x,b=this.new_tile.x+this.camera.viz.x,c=this.camera.tile.y+this.dirY,d=c+this.camera.viz.y*this.dirY,e=this.camera.tile.y;for(let f=a;f<b+1;f++)this.add0=!1,this.add1=!1,this.pt1.x=f*this.config.tileSize,this.pt1.y=c*this.config.tileSize,this.tileRec={x:this.pt1.x,y:this.pt1.y,width:this.config.tileSize,height:this.config.tileSize},this.map.levels[1].tiles['t_'+f+'_'+c]?(this.add1=!0,this.add0=this.map.levels[1].tiles['t_'+f+'_'+c].border):this.add0=!0,this.add1&&(this.addTiles['t_'+f+'_'+c]={pt2d:this.pt1,ptIso:this.convert_2D_ISO(this.pt1)}),this.add0&&this.addGroundTile(f,c),this.tiles0['t_'+f+'_'+e]&&(this.tiles0['t_'+f+'_'+e].destroy(),delete this.tiles0['t_'+f+'_'+e]),this.tiles1['t_'+f+'_'+e]&&(this.removeTiles['t_'+f+'_'+e]=!0),c+this.dirY===d+this.dirY&&(this.dirY*=-1),c+=this.dirY,e-=this.dirY}return(this.changeX||this.changeY)&&this.sortZ(this.foreground),this.camera.tile=this.new_tile,this.view.tile=this.new_tile,this.view.x=this.camera.x,this.view.y=this.camera.y,this.camera.tile=this.view.tile,this.pt1.x=-this.camera.x,this.pt1.y=-this.camera.y,this.pt1=this.convert_2D_ISO(this.pt1),this.container.position.x=this.pt1.x+this.config.width/this.view.zoom/2,this.container.position.y=this.pt1.y+this.config.height/this.view.zoom/2,!0}renderTopView(){for(let b in this.topground.position.y=-83,this.addTiles)this.map.levels[1].tiles[b]&&(this.tile1=new OGX.StarEngine.Tile(this.config.tileSize),this.tile1.setTexture(this.map.levels[1].tiles[b].texture),this.tile1.tile=this.addTiles[b].pt2d,this.tile1.x=this.tile1.img.x=this.addTiles[b].ptIso.x,this.tile1.y=this.tile1.img.y=this.addTiles[b].ptIso.y-this.tile1.img.height,this.tile1.img.z=this.tile1.img.y+this.tile1.img.height,this.tile1.name=b,this.topground.addChild(this.tile1.img),this.tiles1[b]=this.tile1);return this.addTiles={},this.sortZ(this.topground),!0}updateTopView(){for(var b in this.addTiles)this.map.levels[1].tiles[b]&&(this.tiles1[b]||(this.bt=this.map.levels[1].tiles[b],this.tile=new OGX.StarEngine.Tile(this.config.tileSize),this.tile.setTexture(this.map.levels[1].tiles[b].texture),this.tile.tile=this.addTiles[b].pt2d,this.tile.x=this.tile.img.x=this.addTiles[b].ptIso.x,this.tile.y=this.tile.img.y=this.addTiles[b].ptIso.y-this.tile.img.height,this.tile.img.z=this.tile.img.y+this.tile.img.height,this.tile.name=b,this.topground.addChild(this.tile.img),this.tiles1[b]=this.tile));for(b in this.removeTiles)this.tiles1[b].destroy(),delete this.tiles1[b];return this.addTiles={},this.removeTiles={},b&&this.sortZ(this.topground),!0}clearView(){for(var b in this.tiles0)this.tiles0[b].destroy(),delete this.tiles0[b];for(b in this.tiles0={},this.tiles1)this.tiles1[b].destroy(),delete this.tiles1[b];this.tiles0={},this.tiles1={}}convert_2D_ISO(a){let b={x:0,y:0},c=a.x/2,d=-1*(a.x/4),e=a.y/2,f=-1*(a.y/4);return b.x=c+e,b.y=d-f,b}addGroundTile(a,b){if(this.map.levels[0].tiles['t_'+a+'_'+b]){switch(this.bt=this.map.levels[0].tiles['t_'+a+'_'+b],this.tile=new OGX.StarEngine.Tile(this.config.tileSize),this.tile.setTexture(this.bt.texture),this.tile.tile.x=a,this.tile.tile.y=b,this.tile.x=this.tile.img.x=this.bt.posIso.x,this.tile.y=this.tile.img.y=this.bt.posIso.y-this.tile.img.height,this.tile.img.z=this.tile.img.y+this.tile.img.height,this.bt.type){case OGX.StarEngine.BaseTile.TILE_TYPE_STATIC_Z:case OGX.StarEngine.BaseTile.TILE_TYPE_DYNAMIC_Z:this.foreground.addChild(this.tile.img);break;case OGX.StarEngine.BaseTile.TILE_TYPE_STATIC:case OGX.StarEngine.BaseTile.TILE_TYPE_DYNAMIC:this.background.addChild(this.tile.img);}this.tiles0['t_'+a+'_'+b]=this.tile}}sortZ(a){a.children.sort(this.depthCompare)}depthCompare(a,b){return a.z<b.z?-1:a.z>b.z?1:0}run(a){a&&requestAnimationFrame(()=>this.animate())}animate(){this.state===OGX.StarEngine.Engine.STATE_RUNNING&&(this.updateBaseView(),this.updateTopView(),this.renderer.render(this.stage),requestAnimationFrame(()=>this.animate()))}loadTextures(){for(let a=0;a<this.config.textures.length;a++)PIXI.loader.add(this.config.textures[a]+'.json'),PIXI.loader.add(this.config.textures[a]+'.png');PIXI.loader.load(this.onTexturesLoaded)}onTexturesLoaded(){var a=new Event(OGX.StarEngine.Engine.TEXTURES_LOADED);document.dispatchEvent(a)}initPixi(){this.stage=new PIXI.Container,this.renderer=new PIXI.WebGLRenderer(this.config.width,this.config.height,null),this.renderer.view.style.width=this.config.width+'px',this.renderer.view.style.height=this.config.height+'px',this.renderer.view.style.display='block',document.getElementById(this.config.container).appendChild(this.renderer.view),this.renderTexture=new PIXI.RenderTexture.create(this.config.width,this.config.height),this.resize=new PIXI.Container,this.container=new PIXI.Container,this.resize.addChild(this.container),this.background=new PIXI.Container,this.container.addChild(this.background),this.foreground=new PIXI.Container,this.container.addChild(this.foreground),this.topground=new PIXI.Container,this.container.addChild(this.topground),this.stage.addChild(this.resize)}};