mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
implemented level restart
This commit is contained in:
parent
81611050d2
commit
3edd664412
12 changed files with 104 additions and 47 deletions
|
|
@ -16,16 +16,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
|
||||
function GameController () {
|
||||
this.view = ViewManager.createView();
|
||||
|
||||
Parent.call(this, new PhysicsEngine());
|
||||
|
||||
this.physicsEngine.setCollisionDetector();
|
||||
|
||||
this.initStats();
|
||||
this.me = null;
|
||||
|
||||
this.initStats();
|
||||
|
||||
this.update();
|
||||
Parent.call(this);
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
|
@ -34,6 +28,9 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
this.stats = new Stats();
|
||||
this.stats.setMode(0);
|
||||
document.body.appendChild(this.stats.domElement);
|
||||
var button = document.createElement("button");
|
||||
button.onclick = inspector.resetLevel;
|
||||
document.body.appendChild(button);
|
||||
};
|
||||
|
||||
GameController.prototype.destruct = function() {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/GameObject",
|
||||
"Lib/Utilities/Exception"
|
||||
"Lib/Utilities/Exception",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, Exception) {
|
||||
function (Parent, Exception, NotificationCenter) {
|
||||
|
||||
function GameObject(physicsEngine, uid) {
|
||||
Parent.call(this, physicsEngine, uid);
|
||||
|
|
@ -14,7 +15,6 @@ function (Parent, Exception) {
|
|||
GameObject.prototype = Object.create(Parent.prototype);
|
||||
|
||||
GameObject.prototype.destroy = function() {
|
||||
// view ...
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,11 @@ function (Parent, Settings, NotificationCenter) {
|
|||
);
|
||||
};
|
||||
|
||||
Item.prototype.destroy = function() {
|
||||
NotificationCenter.trigger("view/removeMesh", this.mesh);
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
Item.prototype.render = function() {
|
||||
|
||||
NotificationCenter.trigger("view/updateMesh",
|
||||
|
|
|
|||
|
|
@ -37,6 +37,11 @@ function (Parent, Settings, NotificationCenter) {
|
|||
);
|
||||
};
|
||||
|
||||
Tile.prototype.destroy = function() {
|
||||
NotificationCenter.trigger("view/removeMesh", this.mesh);
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
Tile.prototype.render = function() {
|
||||
|
||||
NotificationCenter.trigger("view/updateMesh",
|
||||
|
|
|
|||
|
|
@ -4,35 +4,50 @@ define([
|
|||
"Game/" + GLOBALS.context + "/Player"
|
||||
],
|
||||
|
||||
function (Engine, Level, Player) {
|
||||
function (PhysicsEngine, Level, Player) {
|
||||
|
||||
function GameController (physicsEngine) {
|
||||
function GameController () {
|
||||
this.players = {};
|
||||
this.gameObjects = {
|
||||
animated: [],
|
||||
fixed: []
|
||||
};
|
||||
|
||||
if (! physicsEngine instanceof Engine) {
|
||||
throw physicsEngine + " is not of type Engine";
|
||||
}
|
||||
this.levelPath = null;
|
||||
|
||||
this.physicsEngine = physicsEngine;
|
||||
this.physicsEngine = new PhysicsEngine();
|
||||
this.physicsEngine.setCollisionDetector();
|
||||
|
||||
this.update();
|
||||
}
|
||||
|
||||
GameController.prototype.update = function() {
|
||||
|
||||
};
|
||||
|
||||
GameController.prototype.getPhysicsEngine = function () {
|
||||
return this.physicsEngine;
|
||||
}
|
||||
|
||||
GameController.prototype.loadLevel = function (path) {
|
||||
this.levelPath = path;
|
||||
|
||||
if (this.level) {
|
||||
this.level.unload();
|
||||
this.level.destroy();
|
||||
this.gameObjects = {
|
||||
animated: [],
|
||||
fixed: []
|
||||
};
|
||||
}
|
||||
|
||||
this.level = new Level(path, this.physicsEngine, this.gameObjects);
|
||||
this.level.loadLevelInToEngine();
|
||||
}
|
||||
|
||||
GameController.prototype.onResetLevel = function() {
|
||||
this.loadLevel(this.levelPath);
|
||||
};
|
||||
|
||||
|
||||
GameController.prototype.destroy = function () {
|
||||
for(var player in this.players) {
|
||||
this.players[player].destroy();
|
||||
|
|
@ -53,6 +68,5 @@ function (Engine, Level, Player) {
|
|||
return new Player(user.id, this.physicsEngine);
|
||||
};
|
||||
|
||||
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ function (Box2D, Exception) {
|
|||
GameObject.prototype.destroy = function() {
|
||||
if(this.body instanceof Box2D.Dynamics.b2Body) {
|
||||
this.body.GetWorld().DestroyBody(this.body);
|
||||
} else {
|
||||
throw new Exception("can not destroy body");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,19 +7,19 @@ define([
|
|||
function (Parent, Box2D, Settings) {
|
||||
|
||||
function Skateboard(physicsEngine, uid, options) {
|
||||
this.physicsEngine = physicsEngine;
|
||||
|
||||
Parent.call(this, physicsEngine, uid, options);
|
||||
|
||||
this.addWheel(
|
||||
options.x + 8,
|
||||
options.y + 1.5
|
||||
);
|
||||
|
||||
this.addWheel(
|
||||
options.x - 8,
|
||||
options.y + 1.5
|
||||
);
|
||||
this.wheels = [
|
||||
this.addWheel(
|
||||
options.x + 8,
|
||||
options.y + 1.5
|
||||
),
|
||||
this.addWheel(
|
||||
options.x - 8,
|
||||
options.y + 1.5
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
Skateboard.prototype = Object.create(Parent.prototype);
|
||||
|
|
@ -60,22 +60,23 @@ function (Parent, Box2D, Settings) {
|
|||
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
||||
var offset = 4,
|
||||
factor = 80;
|
||||
var density = ((this.options.weight + offset) / this.options.width / this.options.height) * factor;
|
||||
var density = ((0.1 + offset) / 3 / 3) * factor;
|
||||
fixtureDef.density = density;
|
||||
fixtureDef.shape = wheelShape;
|
||||
fixtureDef.isSensor = false;
|
||||
|
||||
var wheelBody = this.physicsEngine.getWorld().CreateBody(bodyDef);
|
||||
var wheelBody = this.body.GetWorld().CreateBody(bodyDef);
|
||||
wheelBody.CreateFixture(fixtureDef);
|
||||
|
||||
var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
|
||||
revoluteJointDef.enableMotor = false;
|
||||
|
||||
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter());
|
||||
this.physicsEngine.getWorld().CreateJoint(revoluteJointDef);
|
||||
this.body.GetWorld().CreateJoint(revoluteJointDef);
|
||||
|
||||
// FIXME this means, that we will have bodies in the world, which must not be
|
||||
// updated (wheels) because they are always connected to a body which will be updated.
|
||||
return wheelBody;
|
||||
};
|
||||
|
||||
Skateboard.prototype.flip = function(direction) {
|
||||
|
|
@ -83,6 +84,14 @@ function (Parent, Box2D, Settings) {
|
|||
|
||||
// FIXME: implement body flip if necessary
|
||||
};
|
||||
|
||||
Skateboard.prototype.destroy = function() {
|
||||
for (var i = 0; i < this.wheels.length; i++) {
|
||||
this.body.GetWorld().DestroyBody(this.wheels[i]);
|
||||
}
|
||||
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
return Skateboard;
|
||||
|
||||
|
|
|
|||
|
|
@ -22,9 +22,13 @@ define([
|
|||
this.createItems();
|
||||
}
|
||||
|
||||
Level.prototype.unload = function () {
|
||||
// TODO unload level from engine if necessary
|
||||
// Perhaps just remove all bodies?
|
||||
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.gameObjects[key] = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Private
|
||||
|
|
@ -235,7 +239,7 @@ microwave: 3.744
|
|||
image:'skateboard.gif',
|
||||
type:'skateboard',
|
||||
category:'outdoor',
|
||||
weight: 5,
|
||||
weight: 1.5,
|
||||
width:26,
|
||||
height:6,
|
||||
x:200,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ function(Parent, NotificationCenter, Parser) {
|
|||
* retrieves move (and other) commands from client and executes them at the server
|
||||
*/
|
||||
PlayerController.prototype.applyCommand = function(options) {
|
||||
// FIXME: remove this function and use ProtocolHelper.applyCommand() instead
|
||||
// Don't forget to change the function names to on...
|
||||
var message;
|
||||
if (typeof options == "string") {
|
||||
message = Parser.decode(options);
|
||||
|
|
|
|||
|
|
@ -13,17 +13,16 @@ define([
|
|||
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player, GameObject, Doll) {
|
||||
|
||||
function GameController (channel) {
|
||||
Parent.call(this, new PhysicsEngine());
|
||||
|
||||
this.physicsEngine.setCollisionDetector();
|
||||
|
||||
|
||||
this.channel = channel;
|
||||
|
||||
this.update();
|
||||
Parent.call(this);
|
||||
|
||||
this.updateWorld();
|
||||
|
||||
NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
NotificationCenter.on('user/left', this.userLeft, this);
|
||||
NotificationCenter.on('user/left', this.userLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client
|
||||
NotificationCenter.on('user/resetLevel', this.onResetLevel, this);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
}
|
||||
|
|
@ -48,9 +47,10 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
}
|
||||
|
||||
GameController.prototype.spawnPlayer = function(player) {
|
||||
var x = 150,
|
||||
var x = 150 + Math.random() * 300,
|
||||
y = 50;
|
||||
player.spawn(x, y);
|
||||
this.gameObjects.animated.push(player.getDoll());
|
||||
|
||||
var message = {
|
||||
spawnPlayer: {
|
||||
|
|
@ -128,6 +128,14 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
return spawnedPlayers;
|
||||
};
|
||||
|
||||
GameController.prototype.onResetLevel = function(userId) {
|
||||
Parent.prototype.onResetLevel.call(this);
|
||||
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", {resetLevel:true});
|
||||
for (var key in this.players) {
|
||||
this.spawnPlayer(this.players[key]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Utilities/Protocol/Helper"
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Protocol/Parser",
|
||||
],
|
||||
|
||||
function(Parent, NotificationCenter, ProtocolHelper) {
|
||||
function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
|
||||
|
||||
function User(id, channel) {
|
||||
Parent.call(this, id);
|
||||
|
|
@ -38,7 +39,16 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
|||
// User command callbacks
|
||||
|
||||
User.prototype.onGameCommand = function(command) {
|
||||
this.player.playerController.applyCommand(command);
|
||||
|
||||
if (typeof command == "string") {
|
||||
command = ProtocolParser.decode(command);
|
||||
} // FIXME: move this to Protocol helper as a function
|
||||
|
||||
if(command.hasOwnProperty("resetLevel")) {
|
||||
NotificationCenter.trigger("user/resetLevel", this.id);
|
||||
} else {
|
||||
this.player.playerController.applyCommand(command);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ function (Networker, SocketIO, Settings, Exception, PIXI) {
|
|||
var networker = new Networker(socket);
|
||||
inspector.networker = networker;
|
||||
inspector.settings = Settings;
|
||||
inspector.resetLevel = function() { networker.sendGameCommand("resetLevel"); }
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue