Raspsnir/public_html/browserquest/js/map.js

1 line
4.1 KiB
JavaScript
Raw Normal View History

define(["jquery","area"],function(e,t){var n=Class.extend({init:function(e,t){this.game=t,this.data=[],this.isLoaded=!1,this.tilesetsLoaded=!1,this.mapLoaded=!1,this.loadMultiTilesheets=e;var n=!this.game.renderer.mobile&&!this.game.renderer.tablet;this._loadMap(n),this._initTilesets()},_checkReady:function(){this.tilesetsLoaded&&this.mapLoaded&&(this.isLoaded=!0,this.ready_func&&this.ready_func())},_loadMap:function(t){var n=this,r="maps/world_client.json";if(t){log.info("Loading map with web worker.");var i=new Worker("js/mapworker.js");i.postMessage(1),i.onmessage=function(e){var t=e.data;n._initMap(t),n.grid=t.grid,n.plateauGrid=t.plateauGrid,n.mapLoaded=!0,n._checkReady()}}else log.info("Loading map via Ajax."),e.get(r,function(e){n._initMap(e),n._generateCollisionGrid(),n._generatePlateauGrid(),n.mapLoaded=!0,n._checkReady()},"json")},_initTilesets:function(){var e,t,n;this.loadMultiTilesheets?this.game.renderer.mobile||this.game.renderer.tablet?(this.tilesetCount=1,t=this._loadTileset("img/2/tilesheet.png")):(this.tilesetCount=2,t=this._loadTileset("img/2/tilesheet.png"),n=this._loadTileset("img/3/tilesheet.png")):(this.tilesetCount=1,e=this._loadTileset("img/1/tilesheet.png")),this.tilesets=[e,t,n]},_initMap:function(e){this.width=e.width,this.height=e.height,this.tilesize=e.tilesize,this.data=e.data,this.blocking=e.blocking||[],this.plateau=e.plateau||[],this.musicAreas=e.musicAreas||[],this.collisions=e.collisions,this.high=e.high,this.animated=e.animated,this.doors=this._getDoors(e),this.checkpoints=this._getCheckpoints(e)},_getDoors:function(e){var t={},n=this;return _.each(e.doors,function(e){var r;switch(e.to){case"u":r=Types.Orientations.UP;break;case"d":r=Types.Orientations.DOWN;break;case"l":r=Types.Orientations.LEFT;break;case"r":r=Types.Orientations.RIGHT;break;default:r=Types.Orientations.DOWN}t[n.GridPositionToTileIndex(e.x,e.y)]={x:e.tx,y:e.ty,orientation:r,cameraX:e.tcx,cameraY:e.tcy,portal:e.p===1}}),t},_loadTileset:function(e){var t=this,n=new Image;return n.src=e,log.info("Loading tileset: "+e),n.onload=function(){if(n.width%t.tilesize>0)throw Error("Tileset size should be a multiple of "+t.tilesize);log.info("Map tileset loaded."),t.tilesetCount-=1,t.tilesetCount===0&&(log.debug("All map tilesets loaded."),t.tilesetsLoaded=!0,t._checkReady())},n},ready:function(e){this.ready_func=e},tileIndexToGridPosition:function(e){var t=0,n=0,r=function(e,t){return e==0?0:e%t==0?t-1:e%t-1};return e-=1,t=r(e+1,this.width),n=Math.floor(e/this.width),{x:t,y:n}},GridPositionToTileIndex:function(e,t){return t*this.width+e+1},isColliding:function(e,t){return this.isOutOfBounds(e,t)||!this.grid?!1:this.grid[t][e]===1},isPlateau:function(e,t){return this.isOutOfBounds(e,t)||!this.plateauGrid?!1:this.plateauGrid[t][e]===1},_generateCollisionGrid:function(){var e=0,t=this;this.grid=[];for(var n,r=0;r<this.height;r++){this.grid[r]=[];for(n=0;n<this.width;n++)this.grid[r][n]=0}_.each(this.collisions,function(e){var n=t.tileIndexToGridPosition(e+1);t.grid[n.y][n.x]=1}),_.each(this.blocking,function(e){var n=t.tileIndexToGridPosition(e+1);t.grid[n.y]!==undefined&&(t.grid[n.y][n.x]=1)}),log.info("Collision grid generated.")},_generatePlateauGrid:function(){var e=0;this.plateauGrid=[];for(var t,n=0;n<this.height;n++){this.plateauGrid[n]=[];for(t=0;t<this.width;t++)_.include(this.plateau,e)?this.plateauGrid[n][t]=1:this.plateauGrid[n][t]=0,e+=1}log.info("Plateau grid generated.")},isOutOfBounds:function(e,t){return isInt(e)&&isInt(t)&&(e<0||e>=this.width||t<0||t>=this.height)},isHighTile:function(e){return _.indexOf(this.high,e+1)>=0},isAnimatedTile:function(e){return e+1 in this.animated},getTileAnimationLength:function(e){return this.animated[e+1].l},getTileAnimationDelay:function(e){var t=this.animated[e+1];return t.d?t.d:100},isDoor:function(e,t){return this.doors[this.GridPositionToTileIndex(e,t)]!==undefined},getDoorDestination:function(e,t){return this.doors[this.GridPositionToTileIndex(e,t)]},_getCheckpoints:function(e){var n=[];return _.each(e.checkpoints,function(e){var r=new t(e.x,e.y,e.w,e.h);r.id=e.id,n.push(r