Refactored abstract method creation

This commit is contained in:
Jeena 2014-07-27 13:03:40 +02:00
parent 1a71fa38f9
commit 7c783d19e8
6 changed files with 72 additions and 163 deletions

View file

@ -13,6 +13,12 @@ function (Parent, Settings, Nc, PIXI) {
Level.prototype = Object.create(Parent.prototype); 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) { Level.prototype.loadLevelDataFromPath = function (path, callback) {
var self = this; var self = this;
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();

View file

@ -1,11 +1,12 @@
define([ define([
"Lib/Utilities/Abstract",
"Game/Client/View/DomController", "Game/Client/View/DomController",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Exception", "Lib/Utilities/Exception",
"Lib/Utilities/NotificationCenter" "Lib/Utilities/NotificationCenter"
], ],
function (DomController, Settings, Exception, Nc) { function (Abstract, DomController, Settings, Exception, Nc) {
function AbstractView () { function AbstractView () {
this.me = null; this.me = null;
@ -37,10 +38,29 @@ function (DomController, Settings, Exception, Nc) {
Nc.on(Nc.ns.client.view.preloadBar.update, this.onUpdateLoader, this), Nc.on(Nc.ns.client.view.preloadBar.update, this.onUpdateLoader, this),
]; ];
} }
Abstract.prototype.addMethod.call(AbstractView, 'render');
Abstract.prototype.addMethod.call(AbstractView, 'createMesh', ['texturePath', 'callback', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'createAnimatedMesh', ['texturePaths', 'callback', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'addMesh', ['mesh']);
Abstract.prototype.addMethod.call(AbstractView, 'removeMesh', ['mesh']);
Abstract.prototype.addMethod.call(AbstractView, 'updateMesh', ['mesh', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'addFilter', ['mesh', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'removeFilter', ['mesh', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'setCameraPosition', ['x', 'y']);
Abstract.prototype.addMethod.call(AbstractView, 'setCameraZoom', ['z']);
Abstract.prototype.addMethod.call(AbstractView, 'onZoomIn');
Abstract.prototype.addMethod.call(AbstractView, 'onZoomIn');
Abstract.prototype.addMethod.call(AbstractView, 'onZoomReset');
Abstract.prototype.addMethod.call(AbstractView, 'toggleInfo', ['show', 'string']);
Abstract.prototype.addMethod.call(AbstractView, 'onCreateAndAddPlayerInfo', ['options']);
Abstract.prototype.addMethod.call(AbstractView, 'onCreateAndAddPlayerArrow', ['options']);
Abstract.prototype.addMethod.call(AbstractView, 'onUpdatePlayerArrow', ['options']);
Abstract.prototype.addMethod.call(AbstractView, 'onUpdatePlayerInfo', ['mesh', 'options']);
Abstract.prototype.addMethod.call(AbstractView, 'onRemovePlayerInfo', ['mesh']);
Abstract.prototype.addMethod.call(AbstractView, 'onUpdateLoader', ['progress']);
AbstractView.prototype.isWebGlEnabled = function () { AbstractView.prototype.isWebGlEnabled = function () {
try { try {
return !! window.WebGLRenderingContext && !! document.createElement( 'canvas' ).getContext( 'experimental-webgl' ); return !! window.WebGLRenderingContext && !! document.createElement( 'canvas' ).getContext( 'experimental-webgl' );
@ -50,67 +70,14 @@ function (DomController, Settings, Exception, Nc) {
} }
AbstractView.prototype.initCanvas = function (canvas) { AbstractView.prototype.initCanvas = function (canvas) {
this.canvas = canvas; this.canvas = canvas;
DomController.initCanvas(canvas); DomController.initCanvas(canvas);
} }
AbstractView.prototype.loadPlayerMesh = function(player) {
throw new Exception('Abstract Function loadPlayerMesh not overwritten');
};
AbstractView.prototype.loadMeshes = function(objects) {
throw new Exception('Abstract Function loadMeshes not overwritten');
};
AbstractView.prototype.render = function () {
throw new Exception('Abstract Function render not overwritten');
}
AbstractView.prototype.createMesh = function (texturePath, callback, options) {
throw new Exception('Abstract Function createMesh not overwritten');
}
AbstractView.prototype.createAnimatedMesh = function (texturePaths, callback, options) {
throw new Exception('Abstract Function createAnimatedMesh not overwritten');
}
AbstractView.prototype.addMesh = function(mesh) {
throw new Exception('Abstract Function addMesh not overwritten');
};
AbstractView.prototype.removeMesh = function(mesh) {
throw new Exception('Abstract Function removeMesh not overwritten');
};
AbstractView.prototype.updateMesh = function(mesh, options) {
throw new Exception('Abstract Function updateMesh not overwritten');
};
AbstractView.prototype.addFilter = function(mesh, options) {
throw new Exception('Abstract Function addFilter not overwritten');
};
AbstractView.prototype.removeFilter = function(mesh, options) {
throw new Exception('Abstract Function removeFilter not overwritten');
};
AbstractView.prototype.setMe = function(player) { AbstractView.prototype.setMe = function(player) {
this.me = player; this.me = player;
}; };
AbstractView.prototype.addPlayer = function(player) {
throw new Exception('Abstract Function addPlayer not overwritten');
};
AbstractView.prototype.removPlayer = function(player) {
throw new Exception('Abstract Function removPlayer not overwritten');
};
AbstractView.prototype.setCameraPosition = function (x, y) {
throw new Exception('Abstract Function setCameraPosition not overwritten');
}
AbstractView.prototype.calculateCameraPosition = function() { AbstractView.prototype.calculateCameraPosition = function() {
var reference = this.me.getPosition(); var reference = this.me.getPosition();
var pos = {}; var pos = {};
@ -127,22 +94,6 @@ function (DomController, Settings, Exception, Nc) {
return pos; return pos;
}; };
AbstractView.prototype.setCameraZoom = function (z) {
throw new Exception('Abstract Function setCameraZoom not overwritten');
};
AbstractView.prototype.onZoomIn = function () {
throw new Exception('Abstract Function onZoomIn not overwritten');
};
AbstractView.prototype.onZoomOut = function () {
throw new Exception('Abstract Function onZoomOut not overwritten');
};
AbstractView.prototype.onZoomReset = function () {
throw new Exception('Abstract Function onZoomReset not overwritten');
};
AbstractView.prototype.onFullscreenChange = function(isFullScreen) { AbstractView.prototype.onFullscreenChange = function(isFullScreen) {
if (!isFullScreen) { if (!isFullScreen) {
@ -164,34 +115,6 @@ function (DomController, Settings, Exception, Nc) {
this.debugMode = debugMode; this.debugMode = debugMode;
}; };
AbstractView.prototype.toggleInfo = function(show, string) {
throw new Exception('Abstract Function showInfo not overwritten');
};
AbstractView.prototype.onCreateAndAddPlayerInfo = function(options) {
throw new Exception('Abstract Function onCreateAndAddPlayerInfo not overwritten');
};
AbstractView.prototype.onCreateAndAddPlayerArrow = function(options) {
throw new Exception('Abstract Function onCreateAndAddPlayerArrow not overwritten');
};
AbstractView.prototype.onUpdatePlayerArrow = function(options) {
throw new Exception('Abstract Function onUpdatePlayerArrow not overwritten');
};
AbstractView.prototype.onUpdatePlayerInfo = function(playerInfo, options) {
throw new Exception('Abstract Function onUpdatePlayerInfo not overwritten');
};
AbstractView.prototype.onRemovePlayerInfo = function(playerInfo) {
throw new Exception('Abstract Function onRemovePlayerInfo not overwritten');
};
AbstractView.prototype.onUpdateLoader = function(progress) {
throw new Exception('Abstract Function onUpdateLoader not overwritten');
};
AbstractView.prototype.destroy = function() { AbstractView.prototype.destroy = function() {
for (var i = 0; i < this.ncTokens.length; i++) { for (var i = 0; i < this.ncTokens.length; i++) {
Nc.off(this.ncTokens[i]); Nc.off(this.ncTokens[i]);

View file

@ -65,7 +65,7 @@ define(function() {
USE_WEBGL: true, USE_WEBGL: true,
// NETWORKING // NETWORKING
NETWORK_UPDATE_INTERVAL: 70, NETWORK_UPDATE_INTERVAL: 70, // in milliseconds
NETWORK_LOG_INCOMING: false, NETWORK_LOG_INCOMING: false,
NETWORK_LOG_OUTGOING: false, NETWORK_LOG_OUTGOING: false,
NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'], NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'],

View file

@ -2,6 +2,7 @@ define([
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Lib/Utilities/NotificationCenter", "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Abstract",
"Game/" + GLOBALS.context + "/Collision/Detector", "Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Tile", "Game/" + GLOBALS.context + "/GameObjects/Tile",
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
@ -9,7 +10,7 @@ define([
"Game/" + GLOBALS.context + "/GameObjects/Items/RagDoll", "Game/" + GLOBALS.context + "/GameObjects/Items/RagDoll",
"Game/" + GLOBALS.context + "/GameObjects/Items/Rube" "Game/" + GLOBALS.context + "/GameObjects/Items/Rube"
], function (Settings, Box2D, Nc, CollisionDetector, Tile, Item, Skateboard, RagDoll, Rube) { ], function (Settings, Box2D, Nc, Abstract, CollisionDetector, Tile, Item, Skateboard, RagDoll, Rube) {
function Level (uid, engine) { function Level (uid, engine) {
this.uid = uid; this.uid = uid;
@ -19,19 +20,26 @@ define([
this.load(this.uid); this.load(this.uid);
} }
Abstract.prototype.addMethod.call(Level, "createTiles");
Abstract.prototype.addMethod.call(Level, "createItems");
Abstract.prototype.addMethod.call(Level, "addBackground");
Level.prototype.load = function (uid) { Level.prototype.load = function (uid) {
var self = this; var self = this;
var path = Settings.MAPS_PATH + uid + ".json" var path = Settings.MAPS_PATH + uid + ".json"
this.loadLevelDataFromPath(path, function (levelData) { this.loadLevelDataFromPath(path, function (levelData) {
self.levelData = levelData; self.setup(levelData);
self.addBackground();
self.createTiles();
self.createItems();
self.isLoaded = true;
Nc.trigger(Nc.ns.core.game.events.level.loaded);
}); });
} }
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.createItem = function(uid, options) { Level.prototype.createItem = function(uid, options) {
switch(options.type) { switch(options.type) {
//case 'skateboard': //case 'skateboard':
@ -54,55 +62,8 @@ define([
}; };
Level.prototype.destroy = function () { Level.prototype.destroy = function () {
/*
for (var key in this.gameObjects) {
for (var i = 0; i < this.gameObjects[key].length; i++) {
this.gameObjects[key][i].destroy();
}
}
*/
this.isLoaded = false; this.isLoaded = false;
} }
/* Extended by TiledLevel
Level.prototype.createTiles = function () {
if (!this.levelData || !this.levelData.tiles || this.levelData.tiles.length < 1) {
throw "Level: Can't create physic tiles, no tiles found";
}
var tiles = this.levelData.tiles;
for (var i = 0; i < tiles.length; i++) {
var options = tiles[i];
//options.m = this.tileAtPositionExists(options.x, options.y - 1) ? "Soil" : "GrassSoil";
options.m = "Soil";
//this.gameObjects.fixed.push(
new Tile(this.engine, "tile-" + i, options);
//);
}
}
*/
/* Extended by TiledLevel
Level.prototype.createItems = function() {
if (!this.levelData || !this.levelData.items) {
return;
}
var items = this.levelData.items;
for (var i = 0; i < items.length; i++) {
var options = items[i];
var uid = "item-" + i;
var item = this.createItem(uid, options);
//this.gameObjects.animated.push(item);
};
};
// Extended by TiledLevel
Level.prototype.addBackground = function() {
}
*/
return Level; return Level;
}) })

View file

@ -0,0 +1,18 @@
define([
"Lib/Utilities/Exception"
],
function (Exception) {
function Abstract() {
}
Abstract.prototype.addMethod = function(methodName, params) {
this.prototype[methodName] = function() {
throw new Exception("Abstract method", this, methodName + "(" + params.join(', ') + ") not overwritten.");
}
}
return Abstract;
});

View file

@ -1,7 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Chuck Lobby</title> <meta charset="utf-8">
<title>Chuck</title>
<link rel="stylesheet" href="/static/css/screen.css" type="text/css" media="screen"> <link rel="stylesheet" href="/static/css/screen.css" type="text/css" media="screen">
</head> </head>
<body> <body>