From d29c64385d400f1066108a0ebb1ca62e440ff7c6 Mon Sep 17 00:00:00 2001 From: Jeena Date: Sun, 27 Jul 2014 17:21:37 +0200 Subject: [PATCH] added layers --- app/Game/Client/Loader/Level.js | 15 ++- app/Game/Client/Loader/TiledLevel.js | 43 +++++++- app/Game/Core/Loader/Level.js | 44 +++++--- app/Game/Core/Loader/TiledLevel.js | 148 +++++++++++---------------- 4 files changed, 142 insertions(+), 108 deletions(-) diff --git a/app/Game/Client/Loader/Level.js b/app/Game/Client/Loader/Level.js index 9f73b06..794b79a 100755 --- a/app/Game/Client/Loader/Level.js +++ b/app/Game/Client/Loader/Level.js @@ -13,11 +13,6 @@ function (Parent, Settings, Nc, PIXI) { Level.prototype = Object.create(Parent.prototype); - Level.prototype.setup = function(levelData) { - this.levelData = levelData; - this.addBackground(); - Parent.prototype.setup.call(this, levelData); - }; Level.prototype.loadLevelDataFromPath = function (path, callback) { var self = this; @@ -93,5 +88,15 @@ function (Parent, Settings, Nc, PIXI) { return paths; }; + Level.prototype.createItems = function(options) { + Nc.trigger(); // FIXME + Parent.prototype.createItems.call(this, options); + }; + + Level.prototype.createTiles = function(first_argument) { + Nc.trigger(); // FIXME + Parent.prototype.createItems.call(this, options); + }; + return Level; }); \ No newline at end of file diff --git a/app/Game/Client/Loader/TiledLevel.js b/app/Game/Client/Loader/TiledLevel.js index 0505f5f..c70618f 100644 --- a/app/Game/Client/Loader/TiledLevel.js +++ b/app/Game/Client/Loader/TiledLevel.js @@ -10,7 +10,21 @@ function (Parent, Settings) { } TiledLevel.prototype = Object.create(Parent.prototype); - + + TiledLevel.prototype.setup = function(levelData) { + /* + FIXME: find a name for this shit! + for (var i = 0; i < levelData.layers.length; i++) { + var layerOptions = levelData.layers[i]; + layerOptions.z = i; + if(!this.layerMapping[layerOptions.name]) { + this.createLayer(layerOptions); + } + }; + */ + Parent.prototype.setup.call(this, levelData); + }; + TiledLevel.prototype.getAssetPaths = function(levelData) { var paths = Parent.prototype.getAssetPaths.call(this, levelData); @@ -44,9 +58,32 @@ function (Parent, Settings) { paths.push(Settings.MAPS_PATH + background.image); return paths; - } - + + TiledLevel.prototype.addBackground = function(options) { + var texturePath = Settings.GRAPHICS_PATH + options.image; + + var callback = function (mesh) { + Nc.trigger(Nc.ns.client.view.mesh.add, mesh, 0); // FIXME: add at z layer -1 or so + } + Nc.trigger(Nc.ns.client.view.mesh.create, texturePath, callback, { + width: 4000, + height: 2959, + x: -(4000 - Settings.STAGE_WIDTH) / 2, + y: -(2959 + Settings.STAGE_HEIGHT + 700) / 2 + }); + } + + TiledLevel.prototype.getLayer = function(levelData, name) { + for (var i = 0; i < levelData.layers.length; i++) { + if(levelData.layers[i].name === name) { + return levelData.layers[i]; + } + } + + throw "Layer '" + name + "' not found."; + }; + return TiledLevel; }); \ No newline at end of file diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index fe042c2..529fde5 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -18,12 +18,9 @@ define([ this.levelObject = null; this.isLoaded = false; this.load(this.uid); + this.spawnPoints = null; } - Abstract.prototype.addMethod.call(Level, "createTiles"); - Abstract.prototype.addMethod.call(Level, "createItems"); - Abstract.prototype.addMethod.call(Level, "addBackground"); - Level.prototype.load = function (uid) { var self = this; var path = Settings.MAPS_PATH + uid + ".json" @@ -33,13 +30,17 @@ define([ } Level.prototype.setup = function(levelData) { - this.levelData = levelData; - this.createTiles(); - this.createItems(); this.isLoaded = true; Nc.trigger(Nc.ns.core.game.events.level.loaded); }; + Level.prototype.createItems = function(options) { + for (var i = 0; i < options.length; i++) { + var uid = "item-" + i; + this.createItem(uid, options[i]); + }; + }; + Level.prototype.createItem = function(uid, options) { switch(options.type) { //case 'skateboard': @@ -53,14 +54,33 @@ define([ } }; - Level.prototype.getRandomSpawnPoint = function() { - throw new Error("Level not loaded."); - return { - x: 150 + Math.random() * 300, - y: -500 + Level.prototype.createTiles = function(options) { + for (var i = 0; i < options.length; i++) { + new Tile(this.engine, "tile-" + i, options[i]); }; }; + Level.prototype.createSpawnPoints = function(points) { + this.spawnPoints = points; + }; + + Level.prototype.getRandomSpawnPoint = function() { + if(!this.spawnPoints) { + return { + x: 150 + Math.random() * 300, + y: -500 + }; + } + + var size = this.spawnPoints.length; + var object = this.spawnPoints[parseInt(Math.random() * (size -1), 10)]; + + return { + x: object.x / Settings.TILE_RATIO, + y: object.y / Settings.TILE_RATIO + } + }; + Level.prototype.destroy = function () { this.isLoaded = false; } diff --git a/app/Game/Core/Loader/TiledLevel.js b/app/Game/Core/Loader/TiledLevel.js index 0788f64..cc80943 100755 --- a/app/Game/Core/Loader/TiledLevel.js +++ b/app/Game/Core/Loader/TiledLevel.js @@ -13,83 +13,83 @@ define([ ], function (Parent, Settings, ItemSettings, Box2D, Options, Exception, Nc, CollisionDetector, Tile, Item, Skateboard) { - // Public function TiledLevel (path, engine) { + this.layerMapping = { + tiles: this.createTiles.bind(this), + items: this.createItems.bind(this), + spawnpoints: this.createSpawnPoints.bind(this) + }; + this.levelData = null; Parent.call(this, path, engine); } TiledLevel.prototype = Object.create(Parent.prototype); - TiledLevel.prototype.createTiles = function () { - if (!this.levelData) { - throw "Level: Can't create level, nothing found"; - } - var collisionLayer = this.getLayer(this.levelData, "collision"); - - if(collisionLayer) { - - for (var i = 0; i < collisionLayer.data.length; i++) { - - var gid = collisionLayer.data[i]; - if(gid === 0) continue; - - var imagePath = this.getTileImagePath(gid); - - - var parts = imagePath.split("/"); - var tileType = parts[parts.length - 1].split(".")[0].split(""); - - // FIXME rename s to shape, r to rotation etc. - - var options = { - s: parseInt(tileType[0], 10), - r: parseInt(tileType[1], 10), - t: imagePath, - x: i % collisionLayer.width, - y: parseInt(i / collisionLayer.height , 10) - } - - //this.gameObjects.fixed.push( - new Tile(this.engine, "tile-" + i, options); - //); + TiledLevel.prototype.setup = function(levelData) { + this.levelData = levelData; + for (var i = 0; i < levelData.layers.length; i++) { + var layerOptions = levelData.layers[i]; + layerOptions.z = i; + if(this.layerMapping[layerOptions.name]) { + this.layerMapping[layerOptions.name](layerOptions); } - - } else { - console.warn("Level: No collision Layer given"); - } - } - - TiledLevel.prototype.createItems = function() { - var objects = this.getLayer(this.levelData, "items").objects; - - for (var i = 0; i < objects.length; i++) { - var object = objects[i]; - - var options = this.gatherOptions(object); - - var uid = "item-" + i; - var item = this.createItem(uid, options); - //this.gameObjects.animated.push(item); }; + + Parent.prototype.setup.call(this, levelData); }; - TiledLevel.prototype.addBackground = function(path) { + TiledLevel.prototype.createTiles = function(options) { - var texturePath = Settings.GRAPHICS_PATH + "Backgrounds/starnight.png"; - var callback = function (mesh) { - Nc.trigger(Nc.ns.client.view.mesh.add, mesh, 0); // FIXME: add at z layer -1 or so + var data = options.data; + var tilesOptions = []; + for (var i = 0; i < data.length; i++) { + + var gid = data[i]; + if(gid === 0) continue; + + var imagePath = this.getTileImagePath(gid); + + var parts = imagePath.split("/"); + var tileType = parts[parts.length - 1].split(".")[0].split(""); + + // FIXME rename s to shape, r to rotation etc. + + var tileOptions = { + s: parseInt(tileType[0], 10), + r: parseInt(tileType[1], 10), + t: imagePath, + x: i % options.width, + y: parseInt(i / options.height , 10) + } + + tilesOptions.push(tileOptions); } - Nc.trigger(Nc.ns.client.view.mesh.create, texturePath, callback, { - width: 4000, - height: 2959, - x: -(4000 - Settings.STAGE_WIDTH) / 2, - y: -(2959 + Settings.STAGE_HEIGHT + 700) / 2 - }); + + Parent.prototype.createTiles.call(this, tilesOptions); } + TiledLevel.prototype.createItems = function(options) { + var objects = options.objects; + var itemsOptions = [] + for (var i = 0; i < objects.length; i++) { + var options = this.gatherOptions(objects[i]); + itemsOptions.push(options); + }; + + Parent.prototype.createItems.call(this, itemsOptions); + }; + + TiledLevel.prototype.createSpawnPoints = function(options) { + var points = options.objects.map(function(o) { + return { x: o.x, y: o.y }; + }); + + Parent.prototype.createSpawnPoints(this, points); + }; + TiledLevel.prototype.gatherOptions = function(tiledObject) { var options = {}; @@ -106,7 +106,6 @@ define([ var defaultOptions = this.getDefaultItemSettingsByName(options.name); options = Options.merge(options, defaultOptions); - //options = Options.merge(tiledObject.properties, options); return options; }; @@ -139,32 +138,5 @@ define([ } } - TiledLevel.prototype.getRandomSpawnPoint = function() { - if(!this.levelData) { - return Parent.prototype.getRandomSpawnPoint.call(this); - } else { - - var spawnLayer = this.getLayer(this.levelData, "spawnpoints"); - - var size = spawnLayer.objects.length; - var object = spawnLayer.objects[parseInt(Math.random() * (size -1), 10)]; - - return { - x: object.x / Settings.TILE_RATIO, - y: object.y / Settings.TILE_RATIO - } - } - }; - - TiledLevel.prototype.getLayer = function(levelData, name) { - for (var i = 0; i < levelData.layers.length; i++) { - if(levelData.layers[i].name === name) { - return levelData.layers[i]; - } - } - - throw "Layer '" + name + "' not found."; - }; - return TiledLevel; }) \ No newline at end of file