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