some more renaming

This commit is contained in:
logsol 2012-07-21 21:37:24 +02:00
parent 9de3147406
commit bd45f538c7
34 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,40 @@
define(["Vendor/Box2D", "Chuck/Constants"], function(Box2D, Constants) {
function Detector(me) {
this.me = me;
this.listener = new Box2D.Dynamics.b2ContactListener();
this.listener.chuckDetector = this;
this.listener.BeginContact = this.BeginContact;
this.listener.PostSolve = this.PostSolve;
this.listener.EndContact = this.EndContact;
}
Detector.prototype.getListener = function() {
return this.listener;
}
Detector.prototype.handleStand = function(point, isColliding) {
if (point.GetFixtureA().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR
|| point.GetFixtureB().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR) {
this.me.onFootSensorDetection(isColliding);
}
}
/** Extension **/
Detector.prototype.BeginContact = function(point) {
this.chuckDetector.handleStand(point, true);
}
Detector.prototype.PostSolve = function(point, impulse) {
this.chuckDetector.handleStand(point, true);
}
Detector.prototype.EndContact = function(point) {
this.chuckDetector.handleStand(point, false);
}
return Detector;
});

61
app/Game/Client/Control/Key.js Executable file
View file

@ -0,0 +1,61 @@
define(function(){
function Key () {
this._active = false;
this._activityUpdateStatus = false;
this._activityUpdateNeeded = false;
this._keyDownFunction = null;
this._keyUpFunction = null;
this._keyFrameFunction = null;
}
Key.prototype.setActivityUpdateStatus = function(active) {
this._activityUpdateStatus = active;
}
Key.prototype.getActivityUpdateStatus = function() {
return this._activityUpdateStatus;
}
Key.prototype.setActivityUpdateNeeded = function(need) {
this._activityUpdateNeeded = need;
}
Key.prototype.getActivityUpdateNeeded = function() {
return this._activityUpdateNeeded;
}
Key.prototype.setActive = function(active) {
this._active = active;
}
Key.prototype.getActive = function() {
return this._active;
}
Key.prototype.setKeyDownFunction = function(f) {
this._keyDownFunction = f;
}
Key.prototype.getKeyDownFunction = function() {
return this._keyDownFunction;
}
Key.prototype.setKeyUpFunction = function(f) {
this._keyUpFunction = f;
}
Key.prototype.getKeyUpFunction = function() {
return this._keyUpFunction;
}
Key.prototype.setKeyFrameFunction = function(f) {
this._keyFrameFunction = f;
}
Key.prototype.getKeyFrameFunction = function() {
return this._keyFrameFunction;
}
return Key;
});

View file

@ -0,0 +1,94 @@
define(["Chuck/Control/InputController", "Chuck/Control/KeyboardInput"], function(InputController, KeyboardInput){
function InputControlUnit(me, clientProcessor) {
this.clientProcessor = clientProcessor;
this.inputController = new InputController(me);
this.keyboardInput = new KeyboardInput(this);
var keys = {
w:87,
a:65,
s:83,
d:68,
up: 38,
left: 37,
down: 40,
right: 39
}
this.init(keys);
}
InputControlUnit.prototype.init = function(keys) {
this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop', 'moveLeft');
this.keyboardInput.registerKey(keys.left, 'moveLeft', 'stop', 'moveLeft');
this.keyboardInput.registerKey(keys.d, 'moveRight', 'stop', 'moveRight');
this.keyboardInput.registerKey(keys.right, 'moveRight', 'stop', 'moveRight');
this.keyboardInput.registerKey(keys.w, 'jump', 'jumped', 'jumping');
this.keyboardInput.registerKey(keys.up, 'jump', 'jumped', 'jumping');
this.keyboardInput.registerKey(keys.s, 'duck', 'standUp', 'duck');
this.keyboardInput.registerKey(keys.down, 'duck', 'standUp', 'duck');
this.keyboardInput.registerKey(keys.s, 'activateShift', 'activateShift', 'deactivateShift');
this.keyboardInput.registerKey(keys.down, 'activateShift', 'activateShift', 'deactivateShift');
}
InputControlUnit.prototype.moveLeft = function() {
this.inputController.moveLeft();
this.clientProcessor.sendGameCommand('moveLeft');
}
InputControlUnit.prototype.moveRight = function() {
this.inputController.moveRight();
this.clientProcessor.sendGameCommand('moveRight');
}
InputControlUnit.prototype.stop = function() {
this.inputController.stop();
this.clientProcessor.sendGameCommand('stop');
}
InputControlUnit.prototype.jump = function() {
this.inputController.jump();
this.clientProcessor.sendGameCommand('jump');
}
InputControlUnit.prototype.jumped = function() {
this.inputController.jumped();
}
InputControlUnit.prototype.jumping = function() {
this.inputController.jumping();
}
InputControlUnit.prototype.duck = function() {
this.inputController.duck();
this.clientProcessor.sendGameCommand('duck');
}
InputControlUnit.prototype.standUp = function() {
this.inputController.standUp();
}
InputControlUnit.prototype.activateShift = function() {
this.inputController.activateShift();
this.clientProcessor.sendGameCommand('activateShift');
}
InputControlUnit.prototype.deactivateShift = function() {
this.inputController.deactivateShift();
}
InputControlUnit.prototype.update = function() {
this.keyboardInput.update();
}
return InputControlUnit;
});

View file

@ -0,0 +1,78 @@
define(["Chuck/Control/Key"], function(Key){
function KeyboardInput (inputControlUnit) {
this._registry = {};
this._inputControlUnit = inputControlUnit;
this.init();
}
KeyboardInput.prototype.init = function() {
// Using window is ok here because it only runs in the browser
window.onkeydown = this._onKeyDown.bind(this);
window.onkeyup = this._onKeyUp.bind(this);
}
KeyboardInput.prototype.registerKey = function(keyCode, onKeyDown, onKeyUp, onKeyFrame) {
var key = new Key();
key.setKeyDownFunction(onKeyDown);
key.setKeyUpFunction(onKeyUp);
key.setKeyFrameFunction(onKeyFrame);
this._registry[keyCode] = key;
}
KeyboardInput.prototype._getKeyByKeyCode = function(keyCode) {
return this._registry[keyCode];
}
KeyboardInput.prototype._onKeyDown = function(e) {
var key = this._getKeyByKeyCode(e.keyCode);
if (key && key.getActive() == false) {
key.setActivityUpdateStatus(true);
key.setActivityUpdateNeeded(true);
}
}
KeyboardInput.prototype._onKeyUp = function(e) {
var key = this._getKeyByKeyCode(e.keyCode);
if (key != null) {
key.setActivityUpdateStatus(false);
key.setActivityUpdateNeeded(true);
}
}
KeyboardInput.prototype.update = function() {
var callback = null;
var self = this;
for (var keyCode in this._registry) {
var key = this._registry[keyCode];
if (key.getActivityUpdateNeeded()) {
if (key.getActivityUpdateStatus() == true) {
callback = key.getKeyDownFunction();
key.setActive(true);
} else {
callback = key.getKeyUpFunction();
key.setActive(false);
}
key.setActivityUpdateNeeded(false);
}
if (callback) {
self._inputControlUnit[callback]();
} else {
if (key.getActive()) {
callback = key.getKeyFrameFunction();
if (callback) {
self._inputControlUnit[callback]();
}
}
}
callback = null;
}
}
return KeyboardInput;
});

102
app/Game/Client/GameController.js Executable file
View file

@ -0,0 +1,102 @@
var requires = [
"Chuck/View/ViewController",
"Chuck/Physics/Engine",
"Chuck/Player",
"Chuck/Control/InputControlUnit",
"Chuck/Settings",
"Vendor/Box2D",
"Chuck/Loader/Level",
"RequestAnimationFrame"
];
define(requires,
function(ViewController, PhysicsEngine, Player, InputControlUnit, Settings, Box2D, Level, requestAnimFrame) {
function ClientProcessor (clientGame) {
this.clientGame = clientGame;
this.init();
};
ClientProcessor.prototype.init = function() {
this.viewController = new ViewController();
this.physicsEngine = new PhysicsEngine();
this.update();
}
ClientProcessor.prototype.loadLevel = function(path) {
if (this.level) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine();
}
ClientProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine;
}
ClientProcessor.prototype.getMe = function() {
return this.me;
}
ClientProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this));
this.physicsEngine.update();
this.viewController.update();
if(this.me) {
this.inputControlUnit.update();
this.me.update();
}
}
ClientProcessor.prototype.destruct = function() {
}
ClientProcessor.prototype.spawnNewPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
return player;
}
ClientProcessor.prototype.spawnMeWithId = function(id) {
this.me = this.spawnNewPlayerWithId(id);
this.inputControlUnit = new InputControlUnit(this.me, this);
}
ClientProcessor.prototype.sendGameCommand = function(command, options) {
this.clientGame.sendGameCommand(command, options);
}
ClientProcessor.prototype.processGameCommand = function(command, options) {
if (command == "worldUpdate") {
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && options[userData]) {
var update = options[userData];
//console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y);
body.SetAwake(true);
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
}
} while (body = body.GetNext());
}
}
return ClientProcessor;
});

View file

@ -0,0 +1,104 @@
define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientGame) {
function Networker(socketLink) {
this.socketLink = socketLink;
this.clientGame = null;
this.init();
}
Networker.prototype.init = function() {
var self = this;
this.socketLink.on('connect', function() {
self.onConnect();
});
this.socketLink.on('message', function(message) {
self.onMessage(message);
});
this.socketLink.on('disconnect', function() {
self.onDisconnect();
});
}
Networker.prototype.onConnect = function() {
this.join('dungeon');
}
Networker.prototype.onMessage = function(message) {
var self = this;
ProtocolHelper.runCommands(message, function(command, options) {
self.processControlCommand(command, options);
});
}
Networker.prototype.onDisconnect = function() {
this.clientGame.destruct();
this.clientGame = null;
}
Networker.prototype.join = function(channelName){
this.sendCommand('join', channelName);
}
Networker.prototype.sendCommand = function(command, options) {
var message = ProtocolHelper.encodeCommand(command, options);
this.socketLink.send(message);
}
Networker.prototype.onJoinSuccess = function(options) {
this.clientGame = new ClientGame(this, options.id);
this.clientGame.loadLevel("default.json")
console.log("Joined " + options.channelName);
if (options.userIds && options.userIds.length > 0) {
for(var i = 0; i < options.userIds.length; i++) {
this.clientGame.userJoined(options.userIds[i])
}
}
}
Networker.prototype.onUserJoined = function(userId) {
this.clientGame.userJoined(userId);
console.log("User " + userId + " joined");
}
Networker.prototype.sendGameCommand = function(command, options) {
this.sendCommand('gameCommand', ProtocolHelper.assemble(command, options));
}
Networker.prototype.onUserLeft = function(userId) {
this.clientGame.userLeft(userId);
}
Networker.prototype.processControlCommand = function(command, options) {
switch(command) {
case 'joinSuccess':
this.onJoinSuccess(options);
break;
case 'gameCommand':
for(var gameCommand in options) {
this.clientGame.processGameCommand(gameCommand, options[gameCommand]);
}
break;
case 'userJoined':
this.onUserJoined(options);
break;
case 'userLeft':
this.onUserLeft(options);
break;
default:
break;
}
}
return Networker;
});

View file

@ -0,0 +1,50 @@
define(['Vendor/Three', 'Chuck/Settings'], function(Three, Settings) {
function CameraController(isOrthographic) {
isOrthographic = typeof isOrthographic == 'undefined'
? true
: isOrthographic;
if(isOrthographic) {
this.camera = new Three.OrthographicCamera(
-Settings.STAGE_WIDTH/2,
Settings.STAGE_WIDTH/2,
Settings.STAGE_HEIGHT/2,
-Settings.STAGE_HEIGHT/2,
-2000,
1000
);
} else {
this.camera = new Three.PerspectiveCamera(
45,
Settings.STAGE_WIDTH / Settings.STAGE_HEIGHT,
-2000,
1000
);
}
this.camera.position.z = 481;
}
CameraController.prototype.getCamera = function(){
return this.camera;
}
CameraController.prototype.setPosition = function(x, y){
this.camera.position.x = x;
this.camera.position.y = y;
}
CameraController.prototype.setZoom = function(z){
this.camera.position.z = z;
}
return CameraController;
});

View file

@ -0,0 +1,53 @@
define(['Chuck/Settings'], function(Settings) {
var Dom = {
canvas: null,
debugCanvas: null
};
Dom.getCanvasContainer = function(){
var container = document.getElementById(Settings.CANVAS_DOM_ID);
if(container) {
return container;
} else {
throw 'Canvas Container missing: #' + Settings.CANVAS_DOM_ID;
}
}
Dom.getCanvas = function(){
return Dom.canvas;
}
Dom.setCanvas = function(canvas){
var container = Dom.getCanvasContainer();
if(Dom.canvas){
container.removeChild(Dom.canvas);
}
Dom.canvas = canvas;
container.appendChild(canvas);
}
Dom.getDebugCanvas = function(){
return Dom.debugCanvas;
}
Dom.createDebugCanvas = function(){
var container = Dom.getCanvasContainer();
if(Dom.debugCanvas){
container.removeChild(Dom.debugCanvas);
}
var canvas = document.createElement('canvas');
canvas.width = Settings.STAGE_WIDTH;
canvas.height = Settings.STAGE_HEIGHT;
Dom.debugCanvas = canvas;
container.appendChild(canvas);
}
return Dom;
});

View file

@ -0,0 +1,106 @@
define(["Client/Dom", "Vendor/Three", "Chuck/Settings", "Chuck/View/CameraController"], function(Dom, Three, Settings, CameraController){
function ViewController(){
this.mesh = null;
this.scene = null;
this.renderer = null;
this.cameraController = new CameraController();
this.init();
}
function isWebGlEnabled () {
try {
return !! window.WebGLRenderingContext && !! document.createElement( 'canvas' ).getContext( 'experimental-webgl' );
} catch(e) {
return false;
}
}
ViewController.prototype.init = function(){
var self = this;
var rendererOptions = {
antialias: true,
preserveDrawingBuffer: true
};
if(isWebGlEnabled()) {
this.renderer = new Three.WebGLRenderer(rendererOptions);
} else {
this.renderer = new Three.CanvasRenderer(rendererOptions);
}
this.renderer.setClearColorHex(0x333333, 1);
this.renderer.setSize(Settings.STAGE_WIDTH, Settings.STAGE_HEIGHT);
Dom.setCanvas(this.renderer.domElement);
if(Settings.DEBUG_MODE){
Dom.createDebugCanvas();
}
this.scene = new Three.Scene();
this.scene.add(this.cameraController.getCamera());
var ambientLight = new Three.AmbientLight(0xffffff);
this.scene.add(ambientLight);
var directionalLight = new Three.DirectionalLight(0xffffff);
directionalLight.position.set(1, 0, 10).normalize();
this.scene.add(directionalLight);
this.createMesh(100, 100, 100, 100, 'static/img/100.png', function(mesh){
self.mesh = mesh;
self.scene.add(mesh);
});
/*
this.createMesh(50, 50, 200, 100, 'static/img/100.png', function(mesh){
self.scene.add(mesh);
});
*/
//this.animate(this);
}
ViewController.prototype.update = function() {
if(this.mesh) {
this.mesh.rotation.z += .01;
this.mesh.position.z += 1;
this.mesh.position.x += .4;
this.mesh.position.y += .4;
}
this.render();
}
ViewController.prototype.render = function() {
this.renderer.render(this.scene, this.cameraController.getCamera());
}
ViewController.prototype.createMesh = function(width, height, x, y, imgPath, callback) {
var textureImg = new Image();
textureImg.onload = function(){
var material = new Three.MeshLambertMaterial({
map: Three.ImageUtils.loadTexture(imgPath)
});
var mesh = new Three.Mesh(new Three.PlaneGeometry(width, height), material);
mesh.overdraw = true;/*
mesh.position.z = 0;
mesh.position.x = x;
mesh.position.y = y;
*/
callback(mesh);
};
textureImg.src = imgPath;
}
return ViewController;
});

50
app/Game/Config/Settings.js Executable file
View file

@ -0,0 +1,50 @@
define({
STAGE_WIDTH: 600,
STAGE_HEIGHT: 400,
// BOX2D INITIALATORS
RATIO: 35,
BOX2D_WORLD_AABB_SIZE: 3000,
BOX2D_ALLOW_SLEEP: true,
BOX2D_GRAVITY: 16,
BOX2D_VELOCITY_ITERATIONS: 5,
BOX2D_POSITION_ITERATIONS: 5,
BOX2D_TIME_STEP: 1 / 60,
// GRAPHIC PATHS
GRAPHICS_PATH: 'static/img/',
GRAPHICS_SUBPATH_ITEMS: 'items/',
GRAPHICS_SUBPATH_CHARACTERS: 'characters/',
TILE_SIZE: 15,
// GAME PLAY
WALK_SPEED: 2.5,
RUN_SPEED: 4.0,
FLY_SPEED: 3.2,
JUMP_SPEED: 3.0,
JUMP_UPLIFT: 0.05,
// restitution: bouncyness, friction: rubbing, density: mass
TILE_FRICTION: 0.99,
TILE_RESTITUTION: 0.1,
PLAYER_DENSITY: 0.96,
PLAYER_FRICTION: 5,
PLAYER_MOTION_FRICTION: 0.1,
PLAYER_RESTITUTION: 0.0,
PLAYER_LINEAR_DAMPING: .5,
ITEM_DENSITY: 0.9,
ITEM_FRICTION: 0.99,
ITEM_RESTITUTION: 0.02,
// BROWSER
CANVAS_DOM_ID: 'canvasContainer',
IS_BROWSER_ENVIRONMENT: typeof window !== 'undefined',
DEBUG_MODE: true,
// NETWORKING
WORLD_UPDATE_BROADCAST_INTERVAL: 15
})

View file

View file

@ -0,0 +1,59 @@
define(function(){
function InputController(player) {
this.player = player;
this._shift;
this._isJumping;
}
InputController.prototype.moveLeft = function() {
this.player.move(-1);
}
InputController.prototype.moveRight = function() {
this.player.move(1);
}
InputController.prototype.stop = function() {
this.player.stop();
}
InputController.prototype.jump = function() {
this._isJumping = true;
this.player.jump();
}
InputController.prototype.jumped = function() {
this._isJumping = false;
}
InputController.prototype.jumping = function() {
if (this._isJumping) {
this.player.jumping();
}
}
InputController.prototype.duck = function() {
this.player.duck();
}
InputController.prototype.standUp = function() {
this.player.standUp();
}
InputController.prototype.activateShift = function() {
this._shift = true;
}
InputController.prototype.deactivateShift = function() {
this._shift = false;
}
InputController.prototype.update = function() {
}
return InputController;
});

View file

View file

@ -0,0 +1,263 @@
define(["Chuck/Settings", "Chuck/Constants", "Vendor/Box2D"], function(Settings, Constants, Box2D) {
// Public
function Level(path, engine) {
this.path = path;
this.engine = engine;
this.levelObject = null;
}
Level.prototype.loadLevelInToEngine = function() {
this.loadLevelObjectFromPath(this.path);
this.createPhysicTiles();
}
Level.prototype.unload = function() {
// TODO unload level from engine if necessary
// Perhaps just remove all bodies?
}
// Private
Level.prototype.createPhysicTiles = 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]);
}
}
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 = Constants.COLLISION_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
// s: shape
// x: x-position
// y: y-position
// r: rotation (optional)
// Shapes:
// 1
// o o o
// o o o
// o o o
// 2
// o
// o o
// o o o
// 3
// o
// o
// o o
// 4
// o
// o o o
// o o o
// 5
// o
// o
// o o
// 6
// o
// o o o
// o o o
// 7
//
// o
// o o
// 8
// o o
// o o o
// o o o
this.levelObject = {
tiles: [
{s:1, x:3, y:18},
{s:1, x:37, y:27},
{s:1, x:20, y:24},
{s:1, x:24, y:27},
{s:1, x:37, y:26},
{s:1, x:9, y:18},
{s:2, x:32, y:25, r:1},
{s:1, x:23, y:27},
{s:3, x:34, y:24, r:1},
{s:1, x:35, y:28},
{s:4, x:17, y:21},
{s:2, x:21, y:24},
{s:2, x:42, y:23, r:3},
{s:3, x:30, y:24, r:3},
{s:2, x:22, y:25},
{s:1, x:40, y:25},
{s:1, x:38, y:26},
{s:1, x:8, y:18},
{s:1, x:38, y:25},
{s:1, x:28, y:28},
{s:1, x:36, y:27},
{s:1, x:7, y:18},
{s:2, x:20, y:23},
{s:2, x:43, y:23, r:1},
{s:6, x:31, y:24},
{s:1, x:16, y:21},
{s:1, x:1, y:18},
{s:1, x:31, y:29},
{s:2, x:30, y:25, r:2},
{s:4, x:11, y:18},
{s:1, x:28, y:27},
{s:1, x:28, y:26},
{s:1, x:28, y:29},
{s:1, x:19, y:23},
{s:5, x:12, y:18, r:1},
{s:1, x:42, y:24},
{s:6, x:33, y:24, r:2},
{s:1, x:39, y:25},
{s:1, x:33, y:29},
{s:1, x:29, y:29},
{s:1, x:21, y:25},
{s:1, x:27, y:27},
{s:5, x:16, y:20, r:1},
{s:1, x:5, y:18},
{s:5, x:18, y:21, r:1},
{s:4, x:13, y:19},
{s:1, x:14, y:20},
{s:1, x:30, y:29},
{s:1, x:4, y:18},
{s:1, x:6, y:18},
{s:1, x:2, y:18},
{s:1, x:32, y:24},
{s:1, x:34, y:29},
{s:1, x:32, y:29},
{s:2, x:1, y:16},
{s:1, x:10, y:18},
{s:1, x:42, y:25},
{s:2, x:28, y:25, r:3},
{s:2, x:0, y:16, r:2},
{s:1, x:22, y:27},
{s:1, x:25, y:27},
{s:1, x:31, y:25},
{s:5, x:14, y:19, r:1},
{s:1, x:41, y:25},
{s:1, x:36, y:28},
{s:4, x:15, y:20},
{s:2, x:19, y:22},
{s:3, x:26, y:26, r:3},
{s:1, x:26, y:27},
{s:1, x:18, y:22},
{s:6, x:27, y:26},
{s:1, x:22, y:26},
{s:1, x:1, y:17},
{s:1, x:35, y:29},
{s:1, x:12, y:19}
]
}
}
return Level;
})

View file

@ -0,0 +1,114 @@
define(["Vendor/Box2D", "Chuck/Constants", "Chuck/Settings"], function(Box2D, Constants, Settings){
function Doll (physicsEngine, id){
this.id = id;
this.physicsEngine = physicsEngine;
this.body;
this.legs;
this.contactPoint;
this.init(this.physicsEngine.getWorld());
}
Doll.prototype.init = function (world) {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.position.x = 220 / Settings.RATIO;
bodyDef.position.y = 0 / Settings.RATIO;
bodyDef.fixedRotation = true;
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER + '-' + this.id;
this.body = world.CreateBody(bodyDef);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.density = Settings.PLAYER_DENSITY;
fixtureDef.friction = 0;
fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
var headShape = new Box2D.Collision.Shapes.b2CircleShape();
headShape.SetRadius(5 / Settings.RATIO);
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
fixtureDef.shape = headShape;
fixtureDef.isSensor = false;
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_HEAD;
this.body.CreateFixture(fixtureDef);
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));
fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false;
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_CHEST;
this.body.CreateFixture(fixtureDef);
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
legsShape.SetRadius(5 / Settings.RATIO);
legsShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -5 / Settings.RATIO));
fixtureDef.shape = legsShape;
fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false;
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_LEGS;
this.legs = this.body.CreateFixture(fixtureDef);
var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
feetShape.SetRadius(4 / Settings.RATIO);
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
fixtureDef.shape = feetShape;
fixtureDef.isSensor = true;
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_FOOTSENSOR;
this.body.CreateFixture(fixtureDef);
this.body.SetActive(false);
}
Doll.prototype.spawn = function (x, y) {
this.body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
this.body.SetActive(true);
}
Doll.prototype.getBody = function () {
return this.body;
}
Doll.prototype.setFriction = function (friction) {
if(!friction) friction = -1;
if (this.legs.GetFriction() != friction) {
this.legs.SetFriction(friction);
}
}
Doll.prototype.move = function (direction, speed) {
this.setFriction(Settings.PLAYER_MOTION_FRICTION);
this.body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y);
this.body.SetLinearVelocity(vector);
}
Doll.prototype.stop = function () {
this.setFriction(Settings.PLAYER_FRICTION);
}
Doll.prototype.jump = function () {
this.body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
this.body.ApplyImpulse(vector, this.body.GetPosition());
// maybe change to a constant force instead of applying of force?
// to prevent higher jumping running uphill, etc.
}
Doll.prototype.jumping = function () {
var vector = new Box2D.Common.Math.b2Vec2(0, -0.05);
this.body.ApplyImpulse(vector, this.body.GetPosition());
}
Doll.prototype.destroy = function() {
this.body.GetWorld().DestroyBody(this.body);
}
return Doll;
});

63
app/Game/Core/Physics/Engine.js Executable file
View file

@ -0,0 +1,63 @@
define(["Chuck/Settings", "Client/Dom", "Vendor/Box2D", "Chuck/Collision/Detector"], function(Settings, Dom, Box2D, CollisionDetector){
function Engine () {
this.world;
this.init();
}
Engine.prototype.init = function() {
this.world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP);
if(Settings.IS_BROWSER_ENVIRONMENT && Settings.DEBUG_MODE) {
this.setupDebugDraw();
}
}
Engine.prototype.getWorld = function() {
return this.world;
}
Engine.prototype.setCollisionDetector = function(me) {
var detector = new CollisionDetector(me);
this.world.SetContactListener(detector.getListener());
}
Engine.prototype.setupDebugDraw = function() {
//var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
var debugSprite = Dom.getDebugCanvas().getContext("2d");
// set debug draw
var debugDraw = new Box2D.Dynamics.b2DebugDraw();
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(Settings.RATIO);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(null
| Box2D.Dynamics.b2DebugDraw.e_shapeBit
| Box2D.Dynamics.b2DebugDraw.e_jointBit
//| Box2D.Dynamics.b2DebugDraw.e_coreShapeBit
//| Box2D.Dynamics.b2DebugDraw.e_aabbBit
//| Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit
//| Box2D.Dynamics.b2DebugDraw.e_obbBit
//| Box2D.Dynamics.b2DebugDraw.e_pairBit
);
this.world.SetDebugDraw(debugDraw);
this.world.SetWarmStarting(true);
}
Engine.prototype.createBody = function(bodyDef) {
return this.world.CreateBody(bodyDef);
}
Engine.prototype.update = function() {
this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
this.world.ClearForces();
this.world.DrawDebugData();
}
return Engine;
})

188
app/Game/Core/Player.js Normal file
View file

@ -0,0 +1,188 @@
define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
function Player (physicsEngine, id, repository) {
this.physicsEngine = physicsEngine;
this.id = id;
this.repository = repository;
this.standing = false;
this.doll;
this.mc;
this.currentAnimationState = 'stand';
this.lookDirection = 1;
this.moveDirection = 0;
this.init(id);
}
Player.prototype.init = function(id) {
this.doll = new Doll(this.physicsEngine, id);
//this.mc = EmbedHandler.load(EmbedHandler.CHUCK);
//this.mc.stop();
//var mclp = new MovieClipLabelParser();
//mclp.parse(this.mc);
}
Player.prototype.spawn = function(x, y) {
//this.repository.createModel(this.mc, this.doll.getBody());
this.doll.spawn(x, y);
}
Player.prototype.getDoll = function() {
return this.doll;
}
Player.prototype.getBody = function() {
return this.doll.getBody();
}
Player.prototype.setStanding = function(isStanding) {
var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) {
this.animate('stand');
}
this.standing = isStanding;
}
Player.prototype.isStanding = function() {
return this.standing;
}
Player.prototype.move = function(direction) {
this.moveDirection = direction;
switch(true) {
case direction == this.lookDirection && this.isStanding():
this.doll.move(direction, Settings.RUN_SPEED);
break;
case !this.isStanding():
this.doll.move(direction, Settings.FLY_SPEED);
break;
default:
this.doll.move(direction, Settings.WALK_SPEED);
break;
}
if (this.isStanding()) {
this.animate(this.calculateWalkAnimation());
}
}
Player.prototype.stop = function() {
this.moveDirection = 0;
this.doll.stop();
if (this.isWalking() || this.standing) {
this.animate('stand');
}
}
Player.prototype.jump = function() {
if (this.isStanding()) {
this.doll.jump();
this.animate('jump');
this.setStanding(false);
}
}
Player.prototype.jumping = function() {
if (!this.isStanding()) {
this.doll.jumping();
}
}
Player.prototype.duck = function() {
if (this.standing && !this.isWalking()) {
this.animate('duck');
}
}
Player.prototype.standUp = function() {
if (this.standing) {
this.animate('standup');
}
}
Player.prototype.animate = function(type) {
if (type == this.currentAnimationState) {
return;
}
//this.mc.gotoAndPlay(type);
this.currentAnimationState = type;
}
Player.prototype.calculateWalkAnimation = function() {
if (this.moveDirection == this.lookDirection) {
return 'run';
}
return 'walkback';
}
Player.prototype.look = function(x, y) {
/*
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
var lastLookDirection = this.lookDirection;
if (x < Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = -1;
this.lookDirection = -1;
degree = (-45 + degree / 2);
this.mc.head.rotation = degree;
} else if (x >= Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = 1;
this.lookDirection = 1;
degree = (45 + -degree / 2) - 90;
this.mc.head.rotation = degree;
}
if (this.lookDirection != lastLookDirection && this.isWalking()) {
this.animate(this.calculateWalkAnimation());
}*/
}
Player.prototype.isWalking = function() {
var states = ['walk', 'walkback', 'run'];
if (states.indexOf(this.currentAnimationState) >= 0) {
return true;
}
return false;
}
// called by CollisionDetection
Player.prototype.onFootSensorDetection = function(isColliding) {
if(isColliding) {
if(this.doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) {
return;
}
this.setStanding(true);
} else {
// TODO This needs some more thought to it.
// maybe take a look at collision groups for collision detection,
// to group all tiles together
//this.setStanding(false);
//this.animate('jumploop');
}
}
Player.prototype.update = function() {
//this.mc.head.y = this.mc.head_posmask.y;
if (this.doll.getBody().GetLinearVelocity().x == 0 && this.isWalking()) {
this.stop();
}
if (!this.doll.getBody().IsAwake()) {
this.setStanding(true);
}
}
Player.prototype.destroy = function() {
this.doll.destroy();
}
return Player;
});

View file

@ -0,0 +1,25 @@
define(["Protocol/Parser"], function(Parser) {
var Helper = {}
Helper.encodeCommand = function(command, options){
return Parser.encode(Helper.assemble(command, options));
}
Helper.assemble = function(command, options){
var commands = {};
commands[command] = options || null;
return commands;
}
Helper.runCommands = function(message, callback){
var commands = Parser.decode(message);
for(var command in commands) {
callback(command, commands[command]);
}
}
return Helper;
});

View file

@ -0,0 +1,14 @@
define(function() {
var Parser = {};
Parser.encode = function(message){
return JSON.stringify(message);
}
Parser.decode = function(message){
return JSON.parse(message);
}
return Parser;
});

View file

@ -0,0 +1,58 @@
define(["Chuck/ServerGame"], function(ServerGame) {
function Channel(name) {
this.name = name;
this.users = {};
this.serverGame = this.factory.new(ServerGame, this);
console.log("server game " + this.serverGame);
this.serverGame.loadLevel("default.json");
var self = this;
this.notificationCenter.on("processGameCommandFromUser", function(topic, args) {
self.processGameCommandFromUser.apply(self, args);
});
}
Channel.validateName = function(name){
return true;
}
Channel.prototype.addUser = function(user){
var userIds = Object.keys(this.users);
this.users[user.id] = user;
user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds});
this.sendCommandToAllUsersExcept('userJoined', user.id, user);
this.serverGame.createPlayerForUser(user)
}
Channel.prototype.releaseUser = function(user) {
this.serverGame.userIdLeft(user.id);
this.sendCommandToAllUsersExcept("userLeft", user.id, user);
delete this.users[user.id];
}
Channel.prototype.sendCommandToAllUsers = function(command, options) {
for(var id in this.users) {
this.users[id].sendCommand(command, options);
}
}
Channel.prototype.sendCommandToAllUsersExcept = function(command, options, except_user) {
for(var id in this.users) {
if (id != except_user.id) {
this.users[id].sendCommand(command, options);
}
}
}
Channel.prototype.processGameCommandFromUser = function(command, options, user) {
this.serverGame.progressGameCommandFromUser(command, options, user);
}
return Channel;
});

View file

106
app/Game/Server/GameController.js Executable file
View file

@ -0,0 +1,106 @@
var requires = [
"Chuck/Physics/Engine",
"Chuck/Settings",
"Chuck/Player",
"Vendor/Box2D",
"Chuck/Loader/Level",
"Chuck/Control/InputController",
"RequestAnimationFrame"
];
define(requires, function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame){
function ServerProcessor (serverGame) {
this.serverGame = serverGame;
this.players = {};
this.init();
}
ServerProcessor.prototype.init = function() {
this.physicsEngine = this.factory.new(PhysicsEngine);
this.update();
this.updateWorld();
}
ServerProcessor.prototype.loadLevel = function(path) {
if (this.level) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine();
}
ServerProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine;
}
ServerProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this));
this.physicsEngine.update();
for(var id in this.players) {
this.players[id].player.update();
}
}
ServerProcessor.prototype.destruct = function() {
}
ServerProcessor.prototype.createPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
this.players[id] = {
player: player,
inputController: new InputController(player)
};
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
}
ServerProcessor.prototype.progressGameCommandFromId = function(command, options, id) {
var inputController = this.players[id].inputController;
if (typeof inputController[command] == 'function') {
inputController[command](options);
}
}
ServerProcessor.prototype.userIdLeft = function(id) {
var player = this.players[id].player;
player.destroy();
delete this.players[id];
}
ServerProcessor.prototype.updateWorld = function() {
var update = {};
var isUpdateNeeded = false;
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && body.IsAwake()){
update[userData] = {
p: body.GetPosition(),
a: body.GetAngle(),
lv: body.GetLinearVelocity(),
av: body.GetAngularVelocity()
};
isUpdateNeeded = true;
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
//this.serverGame.updateClientsWorld(update);
this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
}
return ServerProcessor;
});

View file

@ -0,0 +1,53 @@
define(function() {
function NotificationCenter() {
this.topics = {};
this.subUid = -1;
}
NotificationCenter.prototype.trigger = function(topic, args) {
if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger.";
}
var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0;
while (len--) {
subscribers[len].func(topic, args);
}
return this;
}
NotificationCenter.prototype.on = function(topic, func) {
if (!this.topics[topic]) {
this.topics[topic] = [];
}
var token = ( ++this.subUid ).toString();
this.topics[topic].push({
token: token,
func: func
});
return token;
}
NotificationCenter.prototype.off = function(token) {
for(var m in this.topics) {
if (this.topics[m]) {
for(var i = 0, j = this.topics[m].length; i < j; i++) {
if (this.topics[m][i].token === token) {
this.topics[m].splice(i, 1);
return token;
}
}
}
}
return this;
}
return NotificationCenter;
});

89
app/Game/Server/User.js Normal file
View file

@ -0,0 +1,89 @@
define(["Protocol/Helper"], function(ProtocolHelper) {
function User(socketLink, coordinator) {
this.id = socketLink.id;
this.socketLink = socketLink;
this.coordinator = coordinator;
this.channel = null;
this.init(socketLink);
}
User.prototype.init = function(socketLink){
var self = this;
socketLink.on('message', function(message){
self.onMessage(message);
});
socketLink.on('disconnect', function(){
self.onDisconnect();
});
}
User.prototype.setChannel = function(channel) {
if (this.notificationCenter) {
this.notificationCenter.off("updateClientsWorld");
}
this.channel = channel;
// Use the right factory and nc
this.notificationCenter = this.channel.notificationCenter;
this.factory = this.channel.factory;
var self = this;
this.notificationCenter.on("sendCommandToAllUsers", function(topic, args) {
self.sendCommand.apply(self, args);
});
}
User.prototype.sendCommand = function(command, options) {
var message = ProtocolHelper.encodeCommand(command, options);
this.socketLink.send(message);
}
User.prototype.onMessage = function(message){
var self = this;
ProtocolHelper.runCommands(message, function(command, options){
self.processControlCommand(command, options);
});
}
User.prototype.onDisconnect = function(){
this.coordinator.removeUser(this);
}
User.prototype.processControlCommand = function(command, options){
switch(command) {
case 'join':
this.coordinator.assignUserToChannel(this, options);
break;
case 'leave':
this.coordinator.assignUserToLobby(this);
break;
case 'gameCommand':
for(var gameCommand in options) {
this.notificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]);
//this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this);
}
break;
default:
break;
}
}
User.prototype.toString = function() {
return "[User " + this.id + "]";
};
return User;
});