diff --git a/package.json b/package.json index d6c8c47..a4f21c5 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,8 @@ }, "devDependencies": { "interleave": "*", - "uglify-js": "*" + "uglify-js": "*", + "mocha": "*", + "chai": "*" } } \ No newline at end of file diff --git a/pkg/amd/GamebaseTypes.js b/pkg/amd/GamebaseTypes.js index 5788b99..6e5b986 100644 --- a/pkg/amd/GamebaseTypes.js +++ b/pkg/amd/GamebaseTypes.js @@ -75,7 +75,6 @@ define('GamebaseTypes', ['underscore'], function(underscore) { } return results; } - function Tile(opts) { opts = opts || {}; @@ -108,7 +107,6 @@ define('GamebaseTypes', ['underscore'], function(underscore) { Tile.prototype.getTerrain = function() { return this.tn; } - /** A terrain layer contains an array containing all the tiles **/ @@ -172,12 +170,163 @@ define('GamebaseTypes', ['underscore'], function(underscore) { TerrainLayer.prototype.toJSON = function() { return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; } + /** + A tiled object layer holds a tile layer, where a collection of tiles + may comprises an object + + For example: A tree may occupy tiles (donoted by X, with a collision grid given by +) + XXX + XXX + + X + + + This layer will have a reference to the tree, the tiles it occupies, + and any potential collisions. + + Additionally, the TiledObjectLayer prevents any other object in the layer + from occupying any tile that is currently taken by any other object + **/ + function TiledObjectLayer(id, size, opts) { + this.id = id; + this.size = size; + this.type = 'tiledobject'; + this.tiles = new Array(size.width * size.height); + this.objects = {}; + } - function randomInRange(min, max) { - return Math.round(min+ (Math.random() * (max - min))); + /** + Returns the tile at x,y + **/ + TiledObjectLayer.prototype.getTile = function(x, y) { + if (arguments.length == 1) { + y = x.y; + x = x.x; + } + var index = this.getTileIndex(x,y); + return (index < 0) ? null : this.tiles[index]; + } + + /** + Sets the tile at x,y + **/ + TiledObjectLayer.prototype.setTile = function(x, y, tile) { + this.tiles[this.getTileIndex(x,y)] = tile; } + /** + Returns the array index of the tile + **/ + TiledObjectLayer.prototype.getTileIndex = function(x, y) { + return (y * this.size.width) + x; + } + /** + Converts a tile index to x,y tile coordinates + **/ + TiledObjectLayer.prototype.getIndexPosition = function(tileIndex) { + return { + x: tileIndex % this.size.width, + y: Math.floor(tileIndex / this.size.width) + } + } + + /** + Return surrounds + **/ + TiledObjectLayer.prototype.getSurrounds = function(x, y) { + return [ + this.getTile(x-1, y-1), this.getTile(x, y-1), this.getTile(x + 1, y - 1), + this.getTile(x-1, y), this.getTile(x, y), this.getTile(x+1, y), + this.getTile(x-1, y+1), this.getTile(x, y+1), this.getTile(x+1, y+1) + ]; + } + + /** + Checks that the object's proposed bounds to do not impinge + on an existing object + **/ + TiledObjectLayer.prototype.checkBounds = function(object, options) { + var opts = options || {}, + position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Check that the object fits within the layer bounds + if (position.x < 0 || position.x + width >= this.size.width + || position.y < 0 || position.y + height >= this.size.height) { + return false; + } + + // Ignore the check for collisions if instructed + if (opts && opts.allowCollisions === true) { + return true; + } + + // Check for collisions with other objects + while (numTiles--) { + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = this.getTile(layerPosition); + + if (tile && tile.obj && tile.obj != object.id) { + return false; + } + } + return true; + } + + /** + Adds an object with the given id at the position starting from the + tile topLeft given + **/ + TiledObjectLayer.prototype.addObject = function(object, options) { + + // Check that we can add an object there + if (!this.checkBounds(object, options)) { + return false; + } + + var id = object.id || Date.now(); + // Check if the object already exists + if (this.objects[id] != null) { + return false; + } + + this.objects[id] = object; + + var position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Set the object tiles + while (numTiles--) { + + var objectTile = object.tiles[numTiles]; + + // Ignore empty tiles + if (!objectTile || objectTile === -1) continue; + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = new Tile({terrain: objectTile}); + + tile.obj = id; + this.setTile(layerPosition.x, layerPosition.y, tile); + } + return true; + } + + /** + Exports the terrain layer to JSON + **/ + TiledObjectLayer.prototype.toJSON = function() { + return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; + } + function randomInRange(min, max) { + return Math.round(min+ (Math.random() * (max - min))); + } var BasicColors = { 'blue' : ['\033[34m', '\033[39m'], @@ -225,7 +374,6 @@ define('GamebaseTypes', ['underscore'], function(underscore) { } } - function GamebaseTypes() { } @@ -233,6 +381,7 @@ define('GamebaseTypes', ['underscore'], function(underscore) { GamebaseTypes.Map2D = Map2D; GamebaseTypes.Tile = Tile; GamebaseTypes.TerrainLayer = TerrainLayer; + GamebaseTypes.TiledObjectLayer = TiledObjectLayer; GamebaseTypes.ASCIIVisualizer = ASCIIVisualizer; GamebaseTypes.randomInRange = randomInRange; GamebaseTypes.Unicode = { diff --git a/pkg/cjs/GamebaseTypes.js b/pkg/cjs/GamebaseTypes.js index ddf3a24..68b95d5 100644 --- a/pkg/cjs/GamebaseTypes.js +++ b/pkg/cjs/GamebaseTypes.js @@ -76,7 +76,6 @@ Map2D.prototype.toJSON = function() { } return results; } - function Tile(opts) { opts = opts || {}; @@ -109,7 +108,6 @@ Tile.prototype.setTerrain = function(terrain) { Tile.prototype.getTerrain = function() { return this.tn; } - /** A terrain layer contains an array containing all the tiles **/ @@ -173,12 +171,163 @@ TerrainLayer.prototype.getSurrounds = function(x, y) { TerrainLayer.prototype.toJSON = function() { return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; } +/** + A tiled object layer holds a tile layer, where a collection of tiles + may comprises an object + + For example: A tree may occupy tiles (donoted by X, with a collision grid given by +) + XXX + XXX + + X + + + This layer will have a reference to the tree, the tiles it occupies, + and any potential collisions. + + Additionally, the TiledObjectLayer prevents any other object in the layer + from occupying any tile that is currently taken by any other object + **/ +function TiledObjectLayer(id, size, opts) { + this.id = id; + this.size = size; + this.type = 'tiledobject'; + this.tiles = new Array(size.width * size.height); + this.objects = {}; +} -function randomInRange(min, max) { - return Math.round(min+ (Math.random() * (max - min))); +/** + Returns the tile at x,y + **/ +TiledObjectLayer.prototype.getTile = function(x, y) { + if (arguments.length == 1) { + y = x.y; + x = x.x; + } + var index = this.getTileIndex(x,y); + return (index < 0) ? null : this.tiles[index]; +} + +/** + Sets the tile at x,y + **/ +TiledObjectLayer.prototype.setTile = function(x, y, tile) { + this.tiles[this.getTileIndex(x,y)] = tile; } +/** + Returns the array index of the tile + **/ +TiledObjectLayer.prototype.getTileIndex = function(x, y) { + return (y * this.size.width) + x; +} +/** + Converts a tile index to x,y tile coordinates + **/ +TiledObjectLayer.prototype.getIndexPosition = function(tileIndex) { + return { + x: tileIndex % this.size.width, + y: Math.floor(tileIndex / this.size.width) + } +} + +/** + Return surrounds + **/ +TiledObjectLayer.prototype.getSurrounds = function(x, y) { + return [ + this.getTile(x-1, y-1), this.getTile(x, y-1), this.getTile(x + 1, y - 1), + this.getTile(x-1, y), this.getTile(x, y), this.getTile(x+1, y), + this.getTile(x-1, y+1), this.getTile(x, y+1), this.getTile(x+1, y+1) + ]; +} + +/** + Checks that the object's proposed bounds to do not impinge + on an existing object + **/ +TiledObjectLayer.prototype.checkBounds = function(object, options) { + var opts = options || {}, + position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Check that the object fits within the layer bounds + if (position.x < 0 || position.x + width >= this.size.width + || position.y < 0 || position.y + height >= this.size.height) { + return false; + } + + // Ignore the check for collisions if instructed + if (opts && opts.allowCollisions === true) { + return true; + } + + // Check for collisions with other objects + while (numTiles--) { + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = this.getTile(layerPosition); + + if (tile && tile.obj && tile.obj != object.id) { + return false; + } + } + return true; +} + +/** + Adds an object with the given id at the position starting from the + tile topLeft given + **/ +TiledObjectLayer.prototype.addObject = function(object, options) { + + // Check that we can add an object there + if (!this.checkBounds(object, options)) { + return false; + } + + var id = object.id || Date.now(); + // Check if the object already exists + if (this.objects[id] != null) { + return false; + } + + this.objects[id] = object; + + var position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Set the object tiles + while (numTiles--) { + + var objectTile = object.tiles[numTiles]; + + // Ignore empty tiles + if (!objectTile || objectTile === -1) continue; + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = new Tile({terrain: objectTile}); + + tile.obj = id; + this.setTile(layerPosition.x, layerPosition.y, tile); + } + return true; +} + +/** + Exports the terrain layer to JSON + **/ +TiledObjectLayer.prototype.toJSON = function() { + return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; +} +function randomInRange(min, max) { + return Math.round(min+ (Math.random() * (max - min))); +} var BasicColors = { 'blue' : ['\033[34m', '\033[39m'], @@ -226,7 +375,6 @@ ASCIIVisualizer.prototype.visualizeTo = function(map, output) { } } - function GamebaseTypes() { } @@ -234,6 +382,7 @@ function GamebaseTypes() { GamebaseTypes.Map2D = Map2D; GamebaseTypes.Tile = Tile; GamebaseTypes.TerrainLayer = TerrainLayer; +GamebaseTypes.TiledObjectLayer = TiledObjectLayer; GamebaseTypes.ASCIIVisualizer = ASCIIVisualizer; GamebaseTypes.randomInRange = randomInRange; GamebaseTypes.Unicode = { diff --git a/pkg/oldschool/GamebaseTypes.js b/pkg/oldschool/GamebaseTypes.js index e9af636..10f3534 100644 --- a/pkg/oldschool/GamebaseTypes.js +++ b/pkg/oldschool/GamebaseTypes.js @@ -77,7 +77,6 @@ } return results; } - function Tile(opts) { opts = opts || {}; @@ -110,7 +109,6 @@ Tile.prototype.getTerrain = function() { return this.tn; } - /** A terrain layer contains an array containing all the tiles **/ @@ -174,12 +172,163 @@ TerrainLayer.prototype.toJSON = function() { return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; } + /** + A tiled object layer holds a tile layer, where a collection of tiles + may comprises an object + + For example: A tree may occupy tiles (donoted by X, with a collision grid given by +) + XXX + XXX + + X + + + This layer will have a reference to the tree, the tiles it occupies, + and any potential collisions. + + Additionally, the TiledObjectLayer prevents any other object in the layer + from occupying any tile that is currently taken by any other object + **/ + function TiledObjectLayer(id, size, opts) { + this.id = id; + this.size = size; + this.type = 'tiledobject'; + this.tiles = new Array(size.width * size.height); + this.objects = {}; + } - function randomInRange(min, max) { - return Math.round(min+ (Math.random() * (max - min))); + /** + Returns the tile at x,y + **/ + TiledObjectLayer.prototype.getTile = function(x, y) { + if (arguments.length == 1) { + y = x.y; + x = x.x; + } + var index = this.getTileIndex(x,y); + return (index < 0) ? null : this.tiles[index]; + } + + /** + Sets the tile at x,y + **/ + TiledObjectLayer.prototype.setTile = function(x, y, tile) { + this.tiles[this.getTileIndex(x,y)] = tile; } + /** + Returns the array index of the tile + **/ + TiledObjectLayer.prototype.getTileIndex = function(x, y) { + return (y * this.size.width) + x; + } + /** + Converts a tile index to x,y tile coordinates + **/ + TiledObjectLayer.prototype.getIndexPosition = function(tileIndex) { + return { + x: tileIndex % this.size.width, + y: Math.floor(tileIndex / this.size.width) + } + } + + /** + Return surrounds + **/ + TiledObjectLayer.prototype.getSurrounds = function(x, y) { + return [ + this.getTile(x-1, y-1), this.getTile(x, y-1), this.getTile(x + 1, y - 1), + this.getTile(x-1, y), this.getTile(x, y), this.getTile(x+1, y), + this.getTile(x-1, y+1), this.getTile(x, y+1), this.getTile(x+1, y+1) + ]; + } + + /** + Checks that the object's proposed bounds to do not impinge + on an existing object + **/ + TiledObjectLayer.prototype.checkBounds = function(object, options) { + var opts = options || {}, + position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Check that the object fits within the layer bounds + if (position.x < 0 || position.x + width >= this.size.width + || position.y < 0 || position.y + height >= this.size.height) { + return false; + } + + // Ignore the check for collisions if instructed + if (opts && opts.allowCollisions === true) { + return true; + } + + // Check for collisions with other objects + while (numTiles--) { + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = this.getTile(layerPosition); + + if (tile && tile.obj && tile.obj != object.id) { + return false; + } + } + return true; + } + + /** + Adds an object with the given id at the position starting from the + tile topLeft given + **/ + TiledObjectLayer.prototype.addObject = function(object, options) { + + // Check that we can add an object there + if (!this.checkBounds(object, options)) { + return false; + } + + var id = object.id || Date.now(); + // Check if the object already exists + if (this.objects[id] != null) { + return false; + } + + this.objects[id] = object; + + var position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Set the object tiles + while (numTiles--) { + + var objectTile = object.tiles[numTiles]; + + // Ignore empty tiles + if (!objectTile || objectTile === -1) continue; + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = new Tile({terrain: objectTile}); + + tile.obj = id; + this.setTile(layerPosition.x, layerPosition.y, tile); + } + return true; + } + + /** + Exports the terrain layer to JSON + **/ + TiledObjectLayer.prototype.toJSON = function() { + return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; + } + function randomInRange(min, max) { + return Math.round(min+ (Math.random() * (max - min))); + } var BasicColors = { 'blue' : ['\033[34m', '\033[39m'], @@ -227,7 +376,6 @@ } } - function GamebaseTypes() { } @@ -235,6 +383,7 @@ GamebaseTypes.Map2D = Map2D; GamebaseTypes.Tile = Tile; GamebaseTypes.TerrainLayer = TerrainLayer; + GamebaseTypes.TiledObjectLayer = TiledObjectLayer; GamebaseTypes.ASCIIVisualizer = ASCIIVisualizer; GamebaseTypes.randomInRange = randomInRange; GamebaseTypes.Unicode = { diff --git a/pkg/raw/GamebaseTypes.js b/pkg/raw/GamebaseTypes.js index bffae68..a025838 100644 --- a/pkg/raw/GamebaseTypes.js +++ b/pkg/raw/GamebaseTypes.js @@ -75,7 +75,6 @@ Map2D.prototype.toJSON = function() { } return results; } - function Tile(opts) { opts = opts || {}; @@ -108,7 +107,6 @@ Tile.prototype.setTerrain = function(terrain) { Tile.prototype.getTerrain = function() { return this.tn; } - /** A terrain layer contains an array containing all the tiles **/ @@ -172,12 +170,163 @@ TerrainLayer.prototype.getSurrounds = function(x, y) { TerrainLayer.prototype.toJSON = function() { return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; } +/** + A tiled object layer holds a tile layer, where a collection of tiles + may comprises an object + + For example: A tree may occupy tiles (donoted by X, with a collision grid given by +) + XXX + XXX + + X + + + This layer will have a reference to the tree, the tiles it occupies, + and any potential collisions. + + Additionally, the TiledObjectLayer prevents any other object in the layer + from occupying any tile that is currently taken by any other object + **/ +function TiledObjectLayer(id, size, opts) { + this.id = id; + this.size = size; + this.type = 'tiledobject'; + this.tiles = new Array(size.width * size.height); + this.objects = {}; +} -function randomInRange(min, max) { - return Math.round(min+ (Math.random() * (max - min))); +/** + Returns the tile at x,y + **/ +TiledObjectLayer.prototype.getTile = function(x, y) { + if (arguments.length == 1) { + y = x.y; + x = x.x; + } + var index = this.getTileIndex(x,y); + return (index < 0) ? null : this.tiles[index]; +} + +/** + Sets the tile at x,y + **/ +TiledObjectLayer.prototype.setTile = function(x, y, tile) { + this.tiles[this.getTileIndex(x,y)] = tile; } +/** + Returns the array index of the tile + **/ +TiledObjectLayer.prototype.getTileIndex = function(x, y) { + return (y * this.size.width) + x; +} +/** + Converts a tile index to x,y tile coordinates + **/ +TiledObjectLayer.prototype.getIndexPosition = function(tileIndex) { + return { + x: tileIndex % this.size.width, + y: Math.floor(tileIndex / this.size.width) + } +} + +/** + Return surrounds + **/ +TiledObjectLayer.prototype.getSurrounds = function(x, y) { + return [ + this.getTile(x-1, y-1), this.getTile(x, y-1), this.getTile(x + 1, y - 1), + this.getTile(x-1, y), this.getTile(x, y), this.getTile(x+1, y), + this.getTile(x-1, y+1), this.getTile(x, y+1), this.getTile(x+1, y+1) + ]; +} + +/** + Checks that the object's proposed bounds to do not impinge + on an existing object + **/ +TiledObjectLayer.prototype.checkBounds = function(object, options) { + var opts = options || {}, + position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Check that the object fits within the layer bounds + if (position.x < 0 || position.x + width >= this.size.width + || position.y < 0 || position.y + height >= this.size.height) { + return false; + } + + // Ignore the check for collisions if instructed + if (opts && opts.allowCollisions === true) { + return true; + } + + // Check for collisions with other objects + while (numTiles--) { + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = this.getTile(layerPosition); + + if (tile && tile.obj && tile.obj != object.id) { + return false; + } + } + return true; +} + +/** + Adds an object with the given id at the position starting from the + tile topLeft given + **/ +TiledObjectLayer.prototype.addObject = function(object, options) { + + // Check that we can add an object there + if (!this.checkBounds(object, options)) { + return false; + } + + var id = object.id || Date.now(); + // Check if the object already exists + if (this.objects[id] != null) { + return false; + } + + this.objects[id] = object; + + var position = object.topLeft, + width = object.width, + height = Math.ceil(object.tiles.length / width), + numTiles = object.tiles.length; + + // Set the object tiles + while (numTiles--) { + + var objectTile = object.tiles[numTiles]; + + // Ignore empty tiles + if (!objectTile || objectTile === -1) continue; + + var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, + layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, + tile = new Tile({terrain: objectTile}); + + tile.obj = id; + this.setTile(layerPosition.x, layerPosition.y, tile); + } + return true; +} + +/** + Exports the terrain layer to JSON + **/ +TiledObjectLayer.prototype.toJSON = function() { + return {id: this.id, type: this.type, size: this.size, tiles: this.tiles}; +} +function randomInRange(min, max) { + return Math.round(min+ (Math.random() * (max - min))); +} var BasicColors = { 'blue' : ['\033[34m', '\033[39m'], @@ -225,7 +374,6 @@ ASCIIVisualizer.prototype.visualizeTo = function(map, output) { } } - function GamebaseTypes() { } @@ -233,6 +381,7 @@ function GamebaseTypes() { GamebaseTypes.Map2D = Map2D; GamebaseTypes.Tile = Tile; GamebaseTypes.TerrainLayer = TerrainLayer; +GamebaseTypes.TiledObjectLayer = TiledObjectLayer; GamebaseTypes.ASCIIVisualizer = ASCIIVisualizer; GamebaseTypes.randomInRange = randomInRange; GamebaseTypes.Unicode = { diff --git a/src/map/layer/tiledobject.js b/src/map/layer/tiledobject.js index eabf515..bbf9273 100644 --- a/src/map/layer/tiledobject.js +++ b/src/map/layer/tiledobject.js @@ -72,8 +72,9 @@ TiledObjectLayer.prototype.getSurrounds = function(x, y) { Checks that the object's proposed bounds to do not impinge on an existing object **/ -TiledObjectLayer.prototype.checkBounds = function(object) { - var position = object.topLeft, +TiledObjectLayer.prototype.checkBounds = function(object, options) { + var opts = options || {}, + position = object.topLeft, width = object.width, height = Math.ceil(object.tiles.length / width), numTiles = object.tiles.length; @@ -82,7 +83,12 @@ TiledObjectLayer.prototype.checkBounds = function(object) { if (position.x < 0 || position.x + width >= this.size.width || position.y < 0 || position.y + height >= this.size.height) { return false; - } + } + + // Ignore the check for collisions if instructed + if (opts && opts.allowCollisions === true) { + return true; + } // Check for collisions with other objects while (numTiles--) { @@ -102,10 +108,10 @@ TiledObjectLayer.prototype.checkBounds = function(object) { Adds an object with the given id at the position starting from the tile topLeft given **/ -TiledObjectLayer.prototype.addObject = function(object) { +TiledObjectLayer.prototype.addObject = function(object, options) { // Check that we can add an object there - if (!this.checkBounds(object)) { + if (!this.checkBounds(object, options)) { return false; } @@ -122,17 +128,20 @@ TiledObjectLayer.prototype.addObject = function(object) { height = Math.ceil(object.tiles.length / width), numTiles = object.tiles.length; - // Check for collisions with other objects + // Set the object tiles while (numTiles--) { var objectTile = object.tiles[numTiles]; // Ignore empty tiles - if (!objectTile) continue; + if (!objectTile || objectTile === -1) continue; var relativePosition = { x: numTiles % width, y: Math.floor(numTiles / width) }, layerPosition = { x: position.x + relativePosition.x, y: position.y + relativePosition.y }, - tile = this.setTile(layerPosition.x, layerPosition.y, new Tile({terrain: objectTile, obj: id})); + tile = new Tile({terrain: objectTile}); + + tile.obj = id; + this.setTile(layerPosition.x, layerPosition.y, tile); } return true; } diff --git a/tests/test_tiledobjects.js b/tests/test_tiledobjects.js index f82c89e..acf6daf 100644 --- a/tests/test_tiledobjects.js +++ b/tests/test_tiledobjects.js @@ -1,4 +1,6 @@ -var gbtypes = require('../pkg/cjs/GamebaseTypes'), +var _ = require('underscore'), + expect = require('chai').expect, + gbtypes = require('../pkg/cjs/GamebaseTypes'), object = { id: 'tree', topLeft: {x: 2, y: 2}, @@ -15,7 +17,18 @@ describe('Tiled object layer', function() { var layer = new gbtypes.TiledObjectLayer('testobjects', {width: 10, height: 10}); layer.addObject(object); - console.log(layer.tiles); + var ok = layer.addObject({ + id: 'tree1', + width: 3, + tiles: [1, 1, 1, + 1, 1, 1, + null, 1, null], + topLeft: {x: 3, y: 2}}); + expect(ok).to.equal(false); + + ok = layer.addObject(_.extend(_.clone(object), {id: 'tree2', topLeft: {x: 6, y: 2}})); + expect(ok).to.equal(true); + done(); });