From fe0d4a66e289a25e73b975df5e7785f7bc35746e Mon Sep 17 00:00:00 2001 From: Jeena Date: Thu, 19 Dec 2013 15:16:15 +0100 Subject: [PATCH] first attempt to implement GameObject --- app/Game/Client/GameController.js | 4 + app/Game/Client/GameObjects/GameObject.js | 31 ++++++ app/Game/Client/GameObjects/Tile.js | 48 ++++++++ app/Game/Client/View/Views/AbstractView.js | 4 + app/Game/Client/View/Views/PixiView.js | 34 +++--- app/Game/Client/View/Views/ThreeView.js | 19 +++- app/Game/Config/Settings.js | 5 +- app/Game/Core/GameController.js | 8 +- app/Game/Core/GameObject.js | 23 ++-- app/Game/Core/GameObjects/GameObject.js | 29 +++++ app/Game/Core/GameObjects/Tile.js | 122 +++++++++++++++++++++ app/Game/Core/Loader/Level.js | 112 ++----------------- app/Game/Server/GameObjects/GameObject.js | 9 ++ app/Game/Server/GameObjects/Tile.js | 9 ++ 14 files changed, 328 insertions(+), 129 deletions(-) create mode 100644 app/Game/Client/GameObjects/GameObject.js create mode 100644 app/Game/Client/GameObjects/Tile.js create mode 100644 app/Game/Core/GameObjects/GameObject.js create mode 100644 app/Game/Core/GameObjects/Tile.js create mode 100644 app/Game/Server/GameObjects/GameObject.js create mode 100644 app/Game/Server/GameObjects/Tile.js diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index c609fda..42feeb7 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -72,6 +72,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat this.me.update(); } + for (var i = 0; i < this.gameObjects.animated.length; i++) { + this.gameObjects.animated[i].render(); + } + this.view.render(); this.stats.end(); diff --git a/app/Game/Client/GameObjects/GameObject.js b/app/Game/Client/GameObjects/GameObject.js new file mode 100644 index 0000000..116139c --- /dev/null +++ b/app/Game/Client/GameObjects/GameObject.js @@ -0,0 +1,31 @@ +define([ + "Game/Core/GameObjects/GameObject" +], + +function(Parent) { + + function GameObject(physicsEngine, view) { + this.view = view; + Parent.call(this, physicsEngine); + this.createMesh(); + this.render(); + } + + GameObject.prototype = Object.create(Parent.prototype); + + GameObject.prototype.destroy = function() { + // view ... + Parent.prototype.destroy.call(this); + }; + + GameObject.prototype.render = function() { + throw new Exception('Abstract method GameObject.render not overwritten'); + } + + GameObject.prototype.createMesh = function() { + throw new Exception('Abstract method GameObject.getMesh not overwritten'); + }; + + return GameObject; + +}); \ No newline at end of file diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js new file mode 100644 index 0000000..c85f3c8 --- /dev/null +++ b/app/Game/Client/GameObjects/Tile.js @@ -0,0 +1,48 @@ +define([ + "Game/Core/GameObjects/Tile", + "Game/Config/Settings" +], + +function (Parent, Settings) { + + function Tile(physicsEngine, view, options) { + Parent.call(this, physicsEngine, view, options); + } + + Tile.prototype = Object.create(Parent.prototype); + + Tile.prototype.createMesh = function() { + var self = this; + + var material = "Stones"; + var imgPath = Settings.GRAPHICS_PATH + + Settings.GRAPHICS_SUBPATH_TILES + + material + '/' + + this.options.s + '' + + this.options.r + '.gif'; + + var callback = function(mesh) { + self.mesh = mesh; + self.view.addMesh(mesh); + } + + this.view.createMesh( + Settings.TILE_SIZE, + Settings.TILE_SIZE, + 0, + 0, + imgPath, + callback + ); + }; + + Tile.prototype.render = function() { + this.view.updateMesh(this.mesh, { + x: this.options.x * Settings.TILE_SIZE, + y: this.options.y * Settings.TILE_SIZE, + }) + } + + return Tile; + +}); \ No newline at end of file diff --git a/app/Game/Client/View/Views/AbstractView.js b/app/Game/Client/View/Views/AbstractView.js index 59b382b..2287659 100644 --- a/app/Game/Client/View/Views/AbstractView.js +++ b/app/Game/Client/View/Views/AbstractView.js @@ -29,6 +29,10 @@ function (DomController, Settings, Exception) { } } + AbstractView.prototype.addMesh = function(mesh) { + throw new Exception('Abstract Function addMesh not overwritten '); + }; + AbstractView.prototype.loadPlayerMesh = function(player) { throw new Exception('Abstract Function loadPlayerMesh not overwritten '); }; diff --git a/app/Game/Client/View/Views/PixiView.js b/app/Game/Client/View/Views/PixiView.js index b510f49..60e398a 100755 --- a/app/Game/Client/View/Views/PixiView.js +++ b/app/Game/Client/View/Views/PixiView.js @@ -47,7 +47,7 @@ function (Parent, DomController, PIXI, Settings) { var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil"; var callback = function(mesh) { - self.container.addChild(mesh); + self.addMesh(mesh); //console.log("img height:", mesh.material.map.image.height); //mesh.rotation.z = rad; }; @@ -62,48 +62,56 @@ function (Parent, DomController, PIXI, Settings) { this.setCameraPosition(pos.x, pos.y); } + /* for (var i = 0; i < this.movableObjects.length; i++) { var obj = this.movableObjects[i]; var pos = obj.player.getPosition(); obj.mesh.position.x = pos.x * Settings.RATIO + 4 ; obj.mesh.position.y = pos.y * Settings.RATIO - 34; // weirdly a different magic number as for three } + */ this.renderer.render(this.stage); } - PixiView.prototype.createMesh = function (width, height, x, y, imgPath, callback, isTile) { + PixiView.prototype.addMesh = function(mesh) { + this.container.addChild(mesh); + }; + + PixiView.prototype.createMesh = function (width, height, x, y, imgPath, callback) { var texture = PIXI.Texture.fromImage(imgPath); - var mesh; - //if(isTile) { - // mesh = new PIXI.TilingSprite(texture, width, height); - //} else { - - mesh = new PIXI.Sprite(texture); + var mesh = new PIXI.Sprite(texture); mesh.width = width; mesh.height = height; - - //} - mesh.position.x = x; mesh.position.y = y; callback(mesh); } + PixiView.prototype.updateMesh = function(mesh, options) { + if (options.x) mesh.position.x = options.x; + if (options.y) mesh.position.y = options.y; + if (options.rotation) mesh.rotation = options.rotation; + if (options.xScale) mesh.scale.x = options.xScale; + if (options.yScale) mesh.scale.y = options.yScale; + if (options.width) mesh.width = options.width; + if (options.height) mesh.height = options.height; + }; + PixiView.prototype.addPlayer = function(player) { var self = this; var mesh = null; var pos = player.getPosition(); var size = {w:10, h:42}; var callback = function(mesh) { - self.container.addChild(mesh); + self.addMesh(mesh); self.movableObjects.push({ player: player, mesh: mesh - }); + }); } this.createMesh(size.w, size.h, pos.x, pos.y, "static/img/Characters/Chuck/chuck.png", callback, false); }; diff --git a/app/Game/Client/View/Views/ThreeView.js b/app/Game/Client/View/Views/ThreeView.js index 55670d1..d24963f 100755 --- a/app/Game/Client/View/Views/ThreeView.js +++ b/app/Game/Client/View/Views/ThreeView.js @@ -61,7 +61,7 @@ function (Parent, DomController, Three, Settings) { var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil"; self.createMesh(Settings.TILE_SIZE, Settings.TILE_SIZE, x, y, 'static/img/Tiles/' + material + '/' + o.s + '' + o.r + '.gif', function(mesh) { - self.scene.add(mesh); + self.addMesh(mesh); //console.log("img height:", mesh.material.map.image.height); //mesh.rotation.z = rad; }); @@ -86,6 +86,10 @@ function (Parent, DomController, Three, Settings) { this.renderer.render(this.scene, this.camera); } + ThreeView.prototype.addMesh = function(mesh) { + this.scene.add(mesh); + }; + ThreeView.prototype.createMesh = function (width, height, x, y, imgPath, callback) { var mesh; var material = new Three.MeshLambertMaterial({ @@ -101,13 +105,24 @@ function (Parent, DomController, Three, Settings) { mesh.position.y = y; } + ThreeView.prototype.updateMesh = function(mesh, options) { + if (options.x) mesh.position.x = options.x; + if (options.y) mesh.position.y = options.y; + if (options.rotation) mesh.rotation.z = options.rotation * (Math.PI / 180); + if (options.xScale) mesh.width *= options.xScale; + if (options.yScale) mesh.height *= options.yScale; + if (options.width) mesh.width = options.width; + if (options.height) mesh.height = options.height; + }; + + ThreeView.prototype.addPlayer = function(player) { var self = this; var mesh = null; var pos = player.getPosition(); var size = {w:10, h:42}; var callback = function(mesh) { - self.scene.add(mesh); + self.addMesh(mesh); self.movableObjects.push({ player: player, mesh: mesh diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index c70c178..ac087a7 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -12,8 +12,9 @@ define({ // GRAPHIC PATHS GRAPHICS_PATH: 'static/img/', - GRAPHICS_SUBPATH_ITEMS: 'items/', - GRAPHICS_SUBPATH_CHARACTERS: 'characters/', + GRAPHICS_SUBPATH_ITEMS: 'Items/', + GRAPHICS_SUBPATH_CHARACTERS: 'Characters/', + GRAPHICS_SUBPATH_TILES: 'Tiles/', RATIO: 35, TILE_SIZE: 15, //15 diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index b729822..4136f39 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -8,7 +8,11 @@ function (Engine, Level, Player) { function GameController (physicsEngine) { this.players = {}; - + this.gameObjects = { + animated: [], + fixed: [] + }; + if (! physicsEngine instanceof Engine) { throw physicsEngine + " is not of type Engine"; } @@ -25,7 +29,7 @@ function (Engine, Level, Player) { this.level.unload(); } - this.level = new Level(path, this.physicsEngine); + this.level = new Level(path, this.physicsEngine, this.gameObjects); this.level.loadLevelInToEngine(); } diff --git a/app/Game/Core/GameObject.js b/app/Game/Core/GameObject.js index e438349..9fd8f49 100644 --- a/app/Game/Core/GameObject.js +++ b/app/Game/Core/GameObject.js @@ -1,16 +1,25 @@ define([ + "Lib/Vendor/Box2D", + "Lib/Utilities/Exception" ], -function() { +function (Box2D, Exception) { - function GameObject() { - this.body = null; - } - - GameObject.prototype.render = function() { - return null; + function GameObject(physicsEngine) { + var def = this.getBodyDef(); + this.body = physicsEngine.getWorld().CreateBody(def); } + GameObject.prototype.getBodyDef = function() { + throw new Exception('Abstract method GameObject.getBodyDef not overwritten'); + }; + + GameObject.prototype.destroy = function() { + if(this.body instanceof Box2D.Dynamics.b2Body) { + this.body.GetWorld().DestroyBody(this.body); + } + }; + GameObject.prototype.getBody = function() { return this.body; }; diff --git a/app/Game/Core/GameObjects/GameObject.js b/app/Game/Core/GameObjects/GameObject.js new file mode 100644 index 0000000..9fd8f49 --- /dev/null +++ b/app/Game/Core/GameObjects/GameObject.js @@ -0,0 +1,29 @@ +define([ + "Lib/Vendor/Box2D", + "Lib/Utilities/Exception" +], + +function (Box2D, Exception) { + + function GameObject(physicsEngine) { + var def = this.getBodyDef(); + this.body = physicsEngine.getWorld().CreateBody(def); + } + + GameObject.prototype.getBodyDef = function() { + throw new Exception('Abstract method GameObject.getBodyDef not overwritten'); + }; + + GameObject.prototype.destroy = function() { + if(this.body instanceof Box2D.Dynamics.b2Body) { + this.body.GetWorld().DestroyBody(this.body); + } + }; + + GameObject.prototype.getBody = function() { + return this.body; + }; + + return GameObject; + +}); \ No newline at end of file diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js new file mode 100644 index 0000000..9b259cd --- /dev/null +++ b/app/Game/Core/GameObjects/Tile.js @@ -0,0 +1,122 @@ +define([ + "Game/" + GLOBALS.context + "/GameObjects/GameObject", + "Lib/Vendor/Box2D", + "Game/Config/Settings", + "Game/" + GLOBALS.context + "/Collision/Detector" +], + +function (Parent, Box2D, Settings, CollisionDetector) { + + function Tile(physicsEngine, options) { + this.options = options; + Parent.call(this, physicsEngine); + this.createPhysicTile(this.options); + } + + Tile.prototype = Object.create(Parent.prototype); + + Tile.prototype.getBodyDef = function() { + + var bodyDef = new Box2D.Dynamics.b2BodyDef(); + bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; + bodyDef.position.x = this.options.x * Settings.TILE_SIZE / Settings.RATIO; + bodyDef.position.y = this.options.y * Settings.TILE_SIZE / Settings.RATIO; + bodyDef.angle = this.options.r * 90 * Math.PI / 180; + + return bodyDef; + } + + Tile.prototype.createPhysicTile = function (tile) { + tile.r = tile.r || 0; + var vertices = this.createVertices(tile); + + var tileShape = new Box2D.Collision.Shapes.b2PolygonShape(); + + tileShape.SetAsArray(vertices, vertices.length); + + var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); + fixtureDef.shape = tileShape; + fixtureDef.density = 0; + fixtureDef.friction = Settings.TILE_FRICTION; + fixtureDef.restitution = Settings.TILE_RESTITUTION; + fixtureDef.isSensor = false; + fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE; + + this.body.CreateFixture(fixtureDef); + } + + Tile.prototype.createVertices = function (tile) { + var vs = []; + + switch(tile.s) { + case 1: + this.addVec(vs, -1, -1); // o o o + this.addVec(vs, 1, -1); // o o o + this.addVec(vs, 1, 1); // o o o + this.addVec(vs, -1, 1); + break; + + case 2: + this.addVec(vs, -1, -1); // o + this.addVec(vs, 1, 1); // o o + this.addVec(vs, -1, 1); // o o o + break; + + case 3: + this.addVec(vs, -1, -1); // o + this.addVec(vs, 0, 1); // o + this.addVec(vs, -1, 1); // o o + break; + + case 4: + this.addVec(vs, -1, -1); // o + this.addVec(vs, 1, 0); // o o o + this.addVec(vs, 1, 1); // o o o + this.addVec(vs, -1, 1); + break; + + case 5: + this.addVec(vs, 1, -1); // o + this.addVec(vs, 1, 1); // o + this.addVec(vs, 0, 1); // o o + break; + + case 6: + this.addVec(vs, 1, -1); // o + this.addVec(vs, 1, 1); // o o o + this.addVec(vs, -1, 1); // o o o + this.addVec(vs, -1, 0); + break; + + case 7: + this.addVec(vs, -1, 0); // + this.addVec(vs, 0, 1); // o + this.addVec(vs, -1, 1); // o o + break; + + case 8: + this.addVec(vs, -1, -1); // o o + this.addVec(vs, 0, -1); // o o o + this.addVec(vs, 1, 0); // o o o + this.addVec(vs, 1, 1); + this.addVec(vs, -1, 1); + break; + + default: + break; + } + + return vs; + } + + Tile.prototype.mkArg = function (multiplier) { + return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier; + } + + Tile.prototype.addVec = function (vs, m1, m2) { + return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2))); + } + + return Tile; + +}); \ No newline at end of file diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index a970a58..aa46f5b 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -1,20 +1,23 @@ define([ "Game/Config/Settings", "Lib/Vendor/Box2D", - "Game/" + GLOBALS.context + "/Collision/Detector" + "Game/" + GLOBALS.context + "/Collision/Detector", + "Game/" + GLOBALS.context + "/GameObjects/Tile" -], function (Settings, Box2D, CollisionDetector) { +], function (Settings, Box2D, CollisionDetector, Tile) { // Public - function Level (path, engine) { + function Level (path, engine, gameObjects, view) { this.path = path; this.engine = engine; this.levelObject = null; + this.gameObjects = gameObjects; + this.view = view; } Level.prototype.loadLevelInToEngine = function () { this.loadLevelObjectFromPath(this.path); - this.createPhysicTiles(); + this.createTiles(); } Level.prototype.unload = function () { @@ -24,114 +27,17 @@ define([ // Private - Level.prototype.createPhysicTiles = function () { + Level.prototype.createTiles = function () { if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) { throw "Level: Can't create physic tiles, no tiles found"; } var tiles = this.levelObject.tiles; for (var i = tiles.length - 1; i >= 0; i--) { - this.createPhysicTile(tiles[i]); + this.gameObjects.fixed.push(new Tile(this.engine, tiles[i])); } } - Level.prototype.createPhysicTile = function (tile) { - tile.r = tile.r || 0; - var vertices = this.createVertices(tile); - - var bodyDef = new Box2D.Dynamics.b2BodyDef(); - bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; - bodyDef.position.x = tile.x * Settings.TILE_SIZE / Settings.RATIO; - bodyDef.position.y = tile.y * Settings.TILE_SIZE / Settings.RATIO; - bodyDef.angle = tile.r * 90 * Math.PI / 180; - - var tileShape = new Box2D.Collision.Shapes.b2PolygonShape(); - - tileShape.SetAsArray(vertices, vertices.length); - - var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); - fixtureDef.shape = tileShape; - fixtureDef.density = 0; - fixtureDef.friction = Settings.TILE_FRICTION; - fixtureDef.restitution = Settings.TILE_RESTITUTION; - fixtureDef.isSensor = false; - fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE; - - this.engine.createBody(bodyDef).CreateFixture(fixtureDef); - } - - Level.prototype.createVertices = function (tile) { - var vs = []; - - switch(tile.s) { - case 1: - this.addVec(vs, -1, -1); // o o o - this.addVec(vs, 1, -1); // o o o - this.addVec(vs, 1, 1); // o o o - this.addVec(vs, -1, 1); - break; - - case 2: - this.addVec(vs, -1, -1); // o - this.addVec(vs, 1, 1); // o o - this.addVec(vs, -1, 1); // o o o - break; - - case 3: - this.addVec(vs, -1, -1); // o - this.addVec(vs, 0, 1); // o - this.addVec(vs, -1, 1); // o o - break; - - case 4: - this.addVec(vs, -1, -1); // o - this.addVec(vs, 1, 0); // o o o - this.addVec(vs, 1, 1); // o o o - this.addVec(vs, -1, 1); - break; - - case 5: - this.addVec(vs, 1, -1); // o - this.addVec(vs, 1, 1); // o - this.addVec(vs, 0, 1); // o o - break; - - case 6: - this.addVec(vs, 1, -1); // o - this.addVec(vs, 1, 1); // o o o - this.addVec(vs, -1, 1); // o o o - this.addVec(vs, -1, 0); - break; - - case 7: - this.addVec(vs, -1, 0); // - this.addVec(vs, 0, 1); // o - this.addVec(vs, -1, 1); // o o - break; - - case 8: - this.addVec(vs, -1, -1); // o o - this.addVec(vs, 0, -1); // o o o - this.addVec(vs, 1, 0); // o o o - this.addVec(vs, 1, 1); - this.addVec(vs, -1, 1); - break; - - default: - break; - } - - return vs; - } - - Level.prototype.mkArg = function (multiplier) { - return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier; - } - - Level.prototype.addVec = function (vs, m1, m2) { - return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2))); - } - Level.prototype.loadLevelObjectFromPath = function (path) { // TODO: load JSON levelObject from path diff --git a/app/Game/Server/GameObjects/GameObject.js b/app/Game/Server/GameObjects/GameObject.js new file mode 100644 index 0000000..03b2a9b --- /dev/null +++ b/app/Game/Server/GameObjects/GameObject.js @@ -0,0 +1,9 @@ +define([ + "Game/Core/GameObjects/GameObject" +], + +function(Parent) { + + return Parent; + +}); \ No newline at end of file diff --git a/app/Game/Server/GameObjects/Tile.js b/app/Game/Server/GameObjects/Tile.js new file mode 100644 index 0000000..837802b --- /dev/null +++ b/app/Game/Server/GameObjects/Tile.js @@ -0,0 +1,9 @@ +define([ + "Game/Core/GameObjects/Tile" +], + +function(Parent) { + + return Parent; + +}); \ No newline at end of file