chuck.js/app/Game/Core/Loader/Level.js
logsol 806a9d8194 Refactores the GLOBALS.context to use more expressive naming
Combines GLOBALS and Chuck namespace into App. Also increases
number of stack trace steps to be printed when an error occurs.

I experimented with the "replace" plugin from require.js, which
works but it would mean that our context switchable
"import-statements" would look like the example below, which I
decided against at least for now, just because of the looks.

The downside of not using the plugin is that we cannot use the
"use strict" statement in the channel.js entry script and also
cannot put a "var" in front of App variable there.

For example: "replace!Game/:AppContext/Physics/Engine",
Instead of:  "Game/" + App.context + "/Physics/Engine",

Fixes #86
2016-10-11 23:05:33 +02:00

102 lines
No EOL
3.1 KiB
JavaScript
Executable file

define([
"Game/Config/Settings",
"Lib/Vendor/Box2D",
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Abstract",
"Game/" + App.context + "/Collision/Detector",
"Game/" + App.context + "/GameObjects/Tile",
"Game/" + App.context + "/GameObjects/Item",
"Game/" + App.context + "/GameObjects/Items/Skateboard",
"Game/" + App.context + "/GameObjects/Items/RagDoll",
"Game/" + App.context + "/GameObjects/Items/RubeDoll"
], function (Settings, Box2D, nc, Abstract, CollisionDetector, Tile, Item, Skateboard, RagDoll, RubeDoll) {
"use strict";
function Level (uid, engine) {
this.uid = uid;
this.engine = engine;
this.levelObject = null;
this.isLoaded = false;
this.load(this.uid);
this.spawnPoints = null;
}
Level.prototype.load = function (uid) {
var self = this;
// FIXME: check if theres a security hazard here (user injected path)
var path = Settings.MAPS_PATH + uid + ".json";
this.loadLevelDataFromPath(path, function (levelData) {
self.setup(levelData);
});
};
Level.prototype.setup = function(levelData) { // jshint unused:false
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":
return new Skateboard(this.engine, uid, options);
case "ragdoll":
return new RagDoll(this.engine, uid, options);
case "rubedoll":
return new RubeDoll(this.engine, uid, options);
default:
return new Item(this.engine, uid, options);
}
};
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.setupLayer = function(options, behind, referenceId) { // jshint unused:false
// will be extended (so far only in client)
};
Level.prototype.createContainer = function(options) { // jshint unused:false
// nothing to do here yet, in the future perhaps synchronize day/night graphics
};
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;
};
return Level;
});