Merge branch 'master' of github.com:logsol/chuck.js

This commit is contained in:
logsol 2012-07-17 11:38:04 +02:00
commit 1d16f29bf0
13 changed files with 84 additions and 71 deletions

View file

@ -7,13 +7,11 @@ Follow the development at http://chuck-game.tumblr.com/
How to run: How to run:
* git clone https://github.com/logsol/chuck.js.git * ```git clone https://github.com/logsol/chuck.js.git```
* cd chuck.js * ```cd chuck.js```
* npm install * ```npm install```
* node server.js * ```node server.js [port] [log level]```
Open in browser http://localhost:1234 Open in browser http://localhost:1234
This game is licensed under the GPLv3 licence http://www.gnu.org/licenses/gpl-3.0.html This game is licensed under the GPLv3 licence http://www.gnu.org/licenses/gpl-3.0.html

View file

@ -1,4 +1,4 @@
define(["Vendor/Box2D"], function(Box2D) { define(["Vendor/Box2D", "Chuck/Constants"], function(Box2D, Constants) {
function Detector(me) { function Detector(me) {
this.me = me; this.me = me;
@ -15,7 +15,9 @@ define(["Vendor/Box2D"], function(Box2D) {
} }
Detector.prototype.handleStand = function(point, isColliding) { Detector.prototype.handleStand = function(point, isColliding) {
if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') { if (point.GetFixtureA().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR
|| point.GetFixtureB().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR) {
this.me.onFootSensorDetection(isColliding); this.me.onFootSensorDetection(isColliding);
} }
} }

View file

@ -1 +1,15 @@
Constants.js define(function() {
var Constants = {
COLLISION_IDENTIFIER_PLAYER: 'player',
COLLISION_IDENTIFIER_PLAYER_HEAD: 'head',
COLLISION_IDENTIFIER_PLAYER_CHEST: 'chest',
COLLISION_IDENTIFIER_PLAYER_LEGS: 'legs',
COLLISION_IDENTIFIER_PLAYER_FOOT_SENSOR: 'footsensor',
COLLISION_IDENTIFIER_TILE: 'tile'
}
return Constants;
});

View file

@ -1,30 +0,0 @@
define(["Chuck/Settings", "Chuck/Processors/ServerProcessor", "Chuck/Processors/ClientProcessor"], function(Settings, ServerProcessor, ClientProcessor){
function Game(networker){
this.networker = networker;
this.processor = this.createProcessor();
this.processor.loadLevel("default.json");
}
Game.prototype.createProcessor = function(){
var processor;
if(Settings.IS_BROWSER_ENVIRONMENT){
processor = new ClientProcessor();
} else {
processor = new ServerProcessor();
}
return processor;
}
Game.prototype.processGameCommand = function(command, options){
console.log('(not implemented) processGameCommand:', command, options);
}
Game.prototype.destruct = function(){
this.processor.destruct();
}
return Game;
});

View file

@ -1,4 +1,4 @@
define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) { define(["Chuck/Settings", "Chuck/Constants", "Vendor/Box2D"], function(Settings, Constants, Box2D) {
// Public // Public
function Level(path, engine) { function Level(path, engine) {
@ -50,7 +50,7 @@ define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) {
fixtureDef.friction = Settings.TILE_FRICTION; fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'tile'; fixtureDef.userData = Constants.COLLISION_IDENTIFIER_TILE;
this.engine.createBody(bodyDef).CreateFixture(fixtureDef); this.engine.createBody(bodyDef).CreateFixture(fixtureDef);
} }

View file

@ -1,4 +1,4 @@
define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ define(["Vendor/Box2D", "Chuck/Constants", "Chuck/Settings"], function(Box2D, Constants, Settings){
function Doll (physicsEngine, id){ function Doll (physicsEngine, id){
this.id = id; this.id = id;
@ -18,7 +18,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
bodyDef.fixedRotation = true; bodyDef.fixedRotation = true;
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.userData = 'player-' + this.id; bodyDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER + '-' + this.id;
this.body = world.CreateBody(bodyDef); this.body = world.CreateBody(bodyDef);
@ -32,14 +32,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
fixtureDef.shape = headShape; fixtureDef.shape = headShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myHead-' + this.id; fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_HEAD;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO)); bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
fixtureDef.shape = bodyShape; fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myBody-' + this.id; fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_CHEST;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
@ -48,7 +48,8 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
fixtureDef.shape = legsShape; fixtureDef.shape = legsShape;
fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myLegs-' + this.id; fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_LEGS;
this.legs = this.body.CreateFixture(fixtureDef); this.legs = this.body.CreateFixture(fixtureDef);
var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
@ -56,7 +57,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO)); feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
fixtureDef.shape = feetShape; fixtureDef.shape = feetShape;
fixtureDef.isSensor = true; fixtureDef.isSensor = true;
fixtureDef.userData = 'myFeet-' + this.id; fixtureDef.userData = Constants.COLLISION_IDENTIFIER_FOOTSENSOR;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
this.body.SetActive(false); this.body.SetActive(false);
@ -74,8 +75,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
Doll.prototype.setFriction = function (friction) { Doll.prototype.setFriction = function (friction) {
if(!friction) friction = -1; if(!friction) friction = -1;
if (this.legs.GetFriction() != friction) if (this.legs.GetFriction() != friction) {
{
this.legs.SetFriction(friction); this.legs.SetFriction(friction);
} }
} }

View file

@ -70,6 +70,7 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, InputController,
ServerProcessor.prototype.userIdLeft = function(id) { ServerProcessor.prototype.userIdLeft = function(id) {
var player = this.players[id].player; var player = this.players[id].player;
player.destroy(); player.destroy();
delete this.players[id];
} }
ServerProcessor.prototype.updateWorld = function() { ServerProcessor.prototype.updateWorld = function() {

View file

@ -67,9 +67,7 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG
} }
Networker.prototype.sendGameCommand = function(command, options) { Networker.prototype.sendGameCommand = function(command, options) {
var message = {}; this.sendCommand('gameCommand', ProtocolHelper.assemble(command, options));
message[command] = options || true;
this.sendCommand('gameCommand', message);
} }
Networker.prototype.onUserLeft = function(userId) { Networker.prototype.onUserLeft = function(userId) {

View file

@ -8,7 +8,7 @@ define(["Protocol/Parser"], function(Parser) {
Helper.assemble = function(command, options){ Helper.assemble = function(command, options){
var commands = {}; var commands = {};
commands[command] = options; commands[command] = options || null;
return commands; return commands;
} }

24
lib/Server/Factory.js Normal file
View file

@ -0,0 +1,24 @@
define(['Server/NotificationCenter'], function(NotificationCenter) {
function Factory() {
this.notificationCenter = new NotificationCenter();
}
Factory.prototype.new = function () {
if (arguments.length < 1)
throw 'Too fiew arguments';
if (typeof arguments[0] != 'function')
throw arguments[0] + ' is not a function';
var type = arguments[0];
var object = new (type.bind.apply(type,arguments))();
object.notificationCenter = this.notificationCenter;
object.factory = this;
return object;
}
return Factory;
});

View file

@ -0,0 +1,7 @@
define(function() {
function NotificationCenter() {
}
return NotificationCenter;
});

View file

@ -1,18 +1,21 @@
define(['socket.io'], function(io) { define(['socket.io'], function(io) {
function Socket(server, coordinator) { function Socket(server, options, coordinator) {
options.logLevel = typeof options.logLevel != 'undefined'
? options.logLevel
: 0;
this.coordinator = coordinator; this.coordinator = coordinator;
this.socket = io.listen(server); this.socket = io.listen(server);
this.init(server); this.init(options);
} }
Socket.prototype.init = function(){ Socket.prototype.init = function(options){
var self = this; var self = this;
this.socket.configure('development', function(){ this.socket.configure('development', function(){
this.set('log level', 0); this.set('log level', options.logLevel);
}); });
this.socket.on('connection', function(user){ this.socket.on('connection', function(user){

View file

@ -13,28 +13,24 @@ var requirements = [
"Server/Coordinator" "Server/Coordinator"
]; ];
var port = process.argv[2]
|| process.env.PORT
|| process.env.npm_package_config_port;
requirejs(requirements, function(HttpServer, Socket, Coordinator) { requirejs(requirements, function(HttpServer, Socket, Coordinator) {
var options = { var options = {
port: process.env.npm_package_config_port, port: port,
rootDirectory: './', rootDirectory: './',
caching: false caching: false,
logLevel: process.argv[3]
}; };
var coordinator = new Coordinator(); var coordinator = new Coordinator();
var httpServer = new HttpServer(options); var httpServer = new HttpServer(options);
var socket = new Socket(httpServer.getServer(), coordinator); var socket = new Socket(httpServer.getServer(), options, coordinator);
inspector.coordinator = coordinator; inspector.coordinator = coordinator;
}); });
exports = module.exports = inspector; exports = module.exports = inspector;
/*
belongs to channel.js
var chuck;
requirejs(["Chuck/Chuck"], function(Chuck) {
Chuck.init();
chuck = Chuck;
});
*/