Skip to content

Commit

Permalink
Gets back to working version
Browse files Browse the repository at this point in the history
  • Loading branch information
m4r00p committed Aug 24, 2011
1 parent 73fc7f9 commit 2fed9dd
Show file tree
Hide file tree
Showing 4,889 changed files with 675 additions and 181 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
*.swo
*.idea
.DS_Store
7 changes: 7 additions & 0 deletions .idea/dictionaries/marekapawlowski.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/app
88 changes: 15 additions & 73 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,25 @@
var express = require('express'),
app = express.createServer(),
server = express.createServer(),
io = require('socket.io');

var gameObject = {};
require('./app/core/object.js');
require('./app/node/game.js');

app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyDecoder());
app.use(express.staticProvider(__dirname + '/../frontend/'));
//init static providers
server.configure(function(){
server.use(express.methodOverride());
server.use(express.bodyParser());
server.use(express.static(__dirname + '/../frontend/'));
});

app.get('/', function (req, res) {

//redirect root
server.get('/', function (req, res) {
res.redirect('/index.html');
});

app.listen(process.env['app_port'] || 3000);


var socket = io.listen(app);
socket.on('connection', function(client) {
var gameEntry = gameObject[client.sessionId] = {};

gameEntry.sessionId = client.sessionId;
gameEntry.name = "subzero";
gameEntry.direction = "right";
gameEntry.state = "stance";
gameEntry.hp = 100;
gameEntry.x = 0;
gameEntry.y = 0;

client.on('message', function(data) {
//console.log("Action: " + data.action + " sessionId: " + client.sessionId);

switch (data.action) {
case 'walk':
gameEntry.direction = data.direction;
gameEntry.state = data.action;
gameEntry.x = data.x;
gameEntry.y = data.y;
break;
case 'stance':
gameEntry.state = data.action;
break;
case 'punch':
case 'kick':
gameEntry.state = data.action;
for (var sid in gameObject) {
if (gameObject.hasOwnProperty(sid)) {
var entry = gameObject[sid];

if (Math.abs(data.x - entry.x) < 70 && sid != client.sessionId) {
entry.state = "beinghit";
entry.hp -= 10;
//init http server
//process.env['app_port'] should be set by nodester
server.listen(process.env['app_port'] || 3000);

setTimeout(function (entr) {
var obj = {};
obj[entr.sessionId] = entr;

entr.state = "stance";
client.broadcast({gameObject: obj});
}.bind(client, entry), 900);
}
}
}
break;
case 'login':
client.send({gameObject: gameObject, sessionId: client.sessionId});
break;
}

var obj = {};
obj[gameObject.sessionId] = gameObject;

client.broadcast({gameObject: obj});

});

client.on('disconnect', function(){
delete gameObject[client.sessionId];
client.broadcast({gameObject: gameObject, remove: client.sessionId});
});
});
new app.node.Game(server, io.listen(server));
4 changes: 2 additions & 2 deletions frontend/app/controller/arena.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ app.core.Object.define("app.controller.Arena", {
constructor: function (model, view) {
arguments.callee.prototype.uper.apply(this, arguments); //call parent constructor
},
static: {},
member: {
statics: {},
members: {
_model: null,
_view: null
}
Expand Down
6 changes: 2 additions & 4 deletions frontend/app/controller/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ app.core.Object.define("app.controller.Character", {
constructor: function (model, view) {
arguments.callee.prototype.uper.apply(this, arguments); //call parent constructor
},
static: {},
member: {
statics: {},
members: {
_freezeTime: null,
_timeout: null,

Expand All @@ -14,8 +14,6 @@ app.core.Object.define("app.controller.Character", {
var code = last && last[1];
var time = +new Date();

console.log(queue);

if (this._freezeTime > time) {
return;
}
Expand Down
94 changes: 68 additions & 26 deletions frontend/app/controller/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ app.core.Object.define("app.controller.Game", {
constructor: function (model, view) {
arguments.callee.prototype.uper.apply(this, arguments); //call parent constructor

this._initArena();

this.__gameObject = {};
this._initArena();
this._initSocket();

this.__characterModelMap = {};
this.__characterViewMap = {};
this.__characterControllerMap = {};
},
static: {},
member: {
statics: {},
members: {
__arena: null,

__characterController: null,
Expand All @@ -33,8 +34,8 @@ app.core.Object.define("app.controller.Game", {
},

_initSocket: function () {
var that = this;
var socket = new io.Socket("localhost", {port: 3000});
var that = this;
var socket = new io.Socket();
this.__socket = socket;

socket.on('connect', function (event) {
Expand All @@ -47,21 +48,28 @@ app.core.Object.define("app.controller.Game", {
}

if (data.gameObject) {
that._update(data.gameObject);
that._initGameObject(data.gameObject);
}

if (data.remove) {
that._remove(data.remove);
that._remove(data.remove);
}

if (data.change) {
that._updateCharacter(data.change);
}

if (data.add) {
that._createCharacter(data.add);
}
});
});

socket.on('disconnect', function (event) {
socket.on('disconnect', function () {
console.log('disconnect');
});

socket.connect();

socket.send({action: "login"});
},

_initArena: function () {
Expand All @@ -72,7 +80,7 @@ app.core.Object.define("app.controller.Game", {
arenaView
);

this.__arena = arenaController;
this.__arena = arenaController;
},

_updateModel: function (model, data) {
Expand All @@ -86,7 +94,7 @@ app.core.Object.define("app.controller.Game", {
model[method](data[property]);
}
else {
console.warn("There is not such setter on model!!!");
console.warn("There is not such setter on model!!!");
}
}
}
Expand All @@ -99,44 +107,44 @@ app.core.Object.define("app.controller.Game", {

_createCharacter: function (data) {
var model = new app.model.Character();
var view = new app.view.character.Subzero(model);
var view = new app.view.Character(model);
var controller = new app.controller.Character(model, view);

this._updateModel(model, data);

this.__characterModelMap[data.sessionId] = model;

this.__characterViewMap[data.sessionId] = view;
this.__characterModelMap[data.sessionId] = model;
this.__characterControllerMap[data.sessionId] = controller;

if (data.sessionId == this.__sessionId) {
this.__characterController = controller;
this.__characterModel = model;
this.__characterModel.addListener('change', this.__onChange.bind(this));
model.addListener('change', this.__onChange.bind(this));
// setInterval(function () {
// model.setX(model.getX() + 1);
// var data = model.getData();
// data.action = "walk";
// this.__socket.send(data);
// }.bind(this),100);
}
},

_remove: function (sessionId) {
console.log("to remove " + sessionId);
console.log(this.__characterViewMap);
this.__characterViewMap[sessionId].remove();

delete this.__characterModelMap[sessionId];
delete this.__characterViewMap[sessionId];
delete this.__characterControllerMap[sessionId];
},

_update: function (data) {
_initGameObject: function (data) {
var id;
this.__gameObject = data;

for (id in data) {
var characterData = data[id];

if (data.hasOwnProperty(id)) {
if (this.__characterModelMap[id]) {
this._updateCharacter(characterData);
}
else {
this._createCharacter(characterData);
}
this._createCharacter(data[id]);
}
}
},
Expand All @@ -152,7 +160,41 @@ app.core.Object.define("app.controller.Game", {
this._keyboard.addListener("press", this._onKeyboardPress.bind(this));
},

_stopPropagation: function (event) {
event.stopPropagation();
},

_onClickLogin: function () {
var input = this.getDocument().getElementById("username");

if (input.value) {
$.ajax({
type: 'POST',
url: '/login',
data: {username: input.value},
success: this._onLoginSucced,
dataType: 'application/json'
});
}
},

_onLoginSucced: function (data) {
console.log('data', data)
},

_input: null,

run: function () {
// var input = this.getDocument().getElementById("username");
// var login = this.getDocument().getElementById("login");
//
// this._input = input;
//
// input.addEventListener("keydown", this._stopPropagation, false);
// input.addEventListener("keyup", this._stopPropagation, false);
//
// login.addEventListener("click", this._onClickLogin.bind(this), false);

this._registerListener();
}
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/controller/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ app.core.Object.define("app.controller.Object", {
this._model = model;
this._view = view;
},
static: {},
member: {
statics: {},
members: {
_model: null,
_view: null
}
Expand Down
Loading

0 comments on commit 2fed9dd

Please sign in to comment.