define(['jquery'], function($) {
var Map = Class.extend({
init: function(game) {
this.game = game;
this.data = [];
this.isLoaded = false;
this.mapLoaded = false;
this.tilesetLoaded = false;
this._loadMap();
this._initTileset();
},
ready: function(f) {
this.ready_func = f;
},
_checkReady: function() {
if(this.mapLoaded && this.tilesetLoaded) {
this.isLoaded = true;
if(this.ready_func) {
this.ready_func();
}
}
},
_loadMap: function() {
var self = this;
log.info('맵 데이터 로딩중... (웹 워커)');
var worker = new Worker('/js/mapworker.js');
worker.postMessage(1);
worker.onmessage = function(event) {
var map = event.data;
self._initMap(map);
self.grid = map.grid;
self.plateauGrid = map.plateauGrid;
self.mapLoaded = true;
log.info('맵 데이터 로딩 완료.');
self._checkReady();
}
},
_initMap: function(map) {
this.width = map.width;
this.height = map.height;
this.tilesize = map.tilesize;
this.data = map.data;
this.blocking = map.blocking || [];
this.plateau = map.plateau || [];
this.collisions = map.collisions;
this.hight = map.high;
this.animated = map.animated;
},
_initTileset: function() {
var tilePath = "/img/tilesheet.png";
this.tileset = this._loadTileset(tilePath);
},
_loadTileset: function(tilePath) {
var self = this;
var tileset = new Image();
tileset.src = tilePath;
log.info("타일 이미지 로딩중... => " + tilePath);
tileset.onload = function() {
log.info("타일 이미지 로딩 완료.");
self.tilesetLoaded = true;
self._checkReady();
};
return tileset;
},
tileIndexToGridPosition: function(tileNum) {
var x = 0;
var y = 0;
var getX = function(num, w) {
if(num == 0) {
return 0;
}
return (num % w == 0) ? w - 1 : (num % w) - 1;
};
x = getX(tileNum + 1, this.width);
y = Math.floor(tileNum / this.width);
return {x: x, y: y};
},
gridPositionToTileIndex: function(x, y) {
return (y * this.width) + x + 1;
},
isOutOfBounds: function(x, y) {
return isInt(x) && isInt(y)
&& (x < 0 || x >= this.width || y < 0 || y >= this.height);
},
isHighTile: function(id) {
return _.indexOf(this.high, id+1) >= 0;
},
isAnimatedTile: function(id) {
return id+1 in this.animated;
},
getTileAnimationLength: function(id) {
return this.animated[id+1].l;
},
getTileAnimationDelay: function(id) {
var animProperties = this.animated[id+1];
if(animProperties.d) {
return animProperties.d;
} else {
return 100;
}
},
isCollding: function(x, y) {
if(this.isOutOfBounds(x, y) || !this.grid) {
return false;
}
return (this.grid[y][x] === 1);
}
});
return Map;
});
var Map = Class.extend({
init: function(game) {
this.game = game;
this.data = [];
this.isLoaded = false;
this.mapLoaded = false;
this.tilesetLoaded = false;
this._loadMap();
this._initTileset();
},
ready: function(f) {
this.ready_func = f;
},
_checkReady: function() {
if(this.mapLoaded && this.tilesetLoaded) {
this.isLoaded = true;
if(this.ready_func) {
this.ready_func();
}
}
},
_loadMap: function() {
var self = this;
log.info('맵 데이터 로딩중... (웹 워커)');
var worker = new Worker('/js/mapworker.js');
worker.postMessage(1);
worker.onmessage = function(event) {
var map = event.data;
self._initMap(map);
self.grid = map.grid;
self.plateauGrid = map.plateauGrid;
self.mapLoaded = true;
log.info('맵 데이터 로딩 완료.');
self._checkReady();
}
},
_initMap: function(map) {
this.width = map.width;
this.height = map.height;
this.tilesize = map.tilesize;
this.data = map.data;
this.blocking = map.blocking || [];
this.plateau = map.plateau || [];
this.collisions = map.collisions;
this.hight = map.high;
this.animated = map.animated;
},
_initTileset: function() {
var tilePath = "/img/tilesheet.png";
this.tileset = this._loadTileset(tilePath);
},
_loadTileset: function(tilePath) {
var self = this;
var tileset = new Image();
tileset.src = tilePath;
log.info("타일 이미지 로딩중... => " + tilePath);
tileset.onload = function() {
log.info("타일 이미지 로딩 완료.");
self.tilesetLoaded = true;
self._checkReady();
};
return tileset;
},
tileIndexToGridPosition: function(tileNum) {
var x = 0;
var y = 0;
var getX = function(num, w) {
if(num == 0) {
return 0;
}
return (num % w == 0) ? w - 1 : (num % w) - 1;
};
x = getX(tileNum + 1, this.width);
y = Math.floor(tileNum / this.width);
return {x: x, y: y};
},
gridPositionToTileIndex: function(x, y) {
return (y * this.width) + x + 1;
},
isOutOfBounds: function(x, y) {
return isInt(x) && isInt(y)
&& (x < 0 || x >= this.width || y < 0 || y >= this.height);
},
isHighTile: function(id) {
return _.indexOf(this.high, id+1) >= 0;
},
isAnimatedTile: function(id) {
return id+1 in this.animated;
},
getTileAnimationLength: function(id) {
return this.animated[id+1].l;
},
getTileAnimationDelay: function(id) {
var animProperties = this.animated[id+1];
if(animProperties.d) {
return animProperties.d;
} else {
return 100;
}
},
isCollding: function(x, y) {
if(this.isOutOfBounds(x, y) || !this.grid) {
return false;
}
return (this.grid[y][x] === 1);
}
});
return Map;
});
댓글 0