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;
});