Skip to content

Commit

Permalink
Re-organize the server by switching to a model based approach for games.
Browse files Browse the repository at this point in the history
  • Loading branch information
BonsaiDen committed Oct 26, 2010
1 parent 2b03a52 commit 9fddd91
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 124 deletions.
File renamed without changes.
87 changes: 49 additions & 38 deletions server/nodegame.js → server/nodegame/nodegame.js
Expand Up @@ -42,29 +42,66 @@ var MSG_ACTORS_DESTROY = 8;
var MSG_CLIENT_MESSAGE = 9;


// Game Model ------------------------------------------------------------------
// -----------------------------------------------------------------------------
function Model(interval) {
this.interval = interval;
this.game = {};
this.client = {};
this.actors = {};
this.baseActor = function(rate) {
this.updateRate = rate;
this.onCreate = function(data) {};
this.onUpdate = function() {};
this.onDestroy = function() {};
this.onMessage = function(once) {return [];};
};
}

Model.prototype.Game = function() {
return this.game;
};

Model.prototype.Client = function() {
return this.client;
};

Model.prototype.Actor = function(id, rate) {
return this.actors[id] = new this.baseActor(rate);
};

Model.prototype.Server = function(options) {
return new Server(options, this);
};

exports.Model = function(interval) {
return new Model(interval);
};


// Server ----------------------------------------------------------------------
// -----------------------------------------------------------------------------
function Server(options) {
function Server(options, model) {
this.maxChars = options.maxChars || 128;
this.maxClients = options.maxClients || 64;
this.port = options.port || 8000;
this.showStatus = options.status === false ? false : true;

// Server
this.model = model;
this.fields = {};
this.fieldsChanged = false;
this.logs = [];

// Client
this.client = {};
this.clientCount = 0;
this.clients = {};
this.clientID = 0;

// Actors
this.actorCount = 0;
this.actorID = 0;
this.actorTypes = {};
this.actorTypes = model.actors;
this.actors = {};

this.bytesSend = 0;
Expand Down Expand Up @@ -115,26 +152,20 @@ function Server(options) {

// Hey Listen!
this.$.listen(this.port);
this.run();
}
exports.Server = Server;


// General ---------------------------------------------------------------------
Server.prototype.run = function() {
var that = this;
process.nextTick(function() {
that.start();
});
};

Server.prototype.start = function() {
var that = this;
for(var i in this.actorTypes) {
this.actors[i] = [];
}
this.startTime = new Date().getTime();
this.time = new Date().getTime();
this.log('>> Server started');
this.$$ = new Game(this);
this.$$.start();
this.status();

Expand Down Expand Up @@ -191,15 +222,6 @@ Server.prototype.saveRecording = function() {
}
};

Server.prototype.Game = function(interval) {
this.$$ = new Game(this, interval);
return this.$$;
};

Server.prototype.Client = function() {
return this.client;
};


// Helpers ---------------------------------------------------------------------
Server.prototype.getTime = function() {
Expand Down Expand Up @@ -331,18 +353,6 @@ Server.prototype.createActor = function(clas, data) {
return a;
};

Server.prototype.createActorType = function(id, rate) {
function ActorType(rate) {
this.updateRate = rate;
this.onCreate = function(data) {};
this.onUpdate = function() {};
this.onDestroy = function() {};
this.onMessage = function(once) {return [];};
}
this.actorTypes[id] = new ActorType(rate);
return this.actorTypes[id];
};

Server.prototype.getActors = function(clas) {
return this.actors[clas];
};
Expand Down Expand Up @@ -463,9 +473,12 @@ Server.prototype.getFields = function(id, force) {

// Game ------------------------------------------------------------------------
// -----------------------------------------------------------------------------
function Game(srv, interval) {
function Game(srv) {
this.$ = srv;
this.$interval = Math.round(1000 / interval);
this.$interval = Math.round(1000 / this.$.model.interval);
for(var m in this.$.model.game) {
this[m] = this.$.model.game[m];
}
}

Game.prototype.start = function() {
Expand Down Expand Up @@ -558,10 +571,8 @@ function Client(srv, conn, record) {
this.$.actors[t][i].$emit(MSG_ACTORS_INIT);
}
}

// Extend
for(var m in this.$.client) {
this[m] = this.$.client[m];
for(var m in this.$.model.client) {
this[m] = this.$.model.client[m];
}
this.onInit();
}
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions server/server.js
@@ -0,0 +1,41 @@
/*
NodeGame: Shooter
Copyright (c) 2010 Ivo Wetzel.
All rights reserved.
NodeGame: Shooter is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NodeGame: Shooter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
NodeGame: Game. If not, see <http://www.gnu.org/licenses/>.
*/


// Server ----------------------------------------------------------------------
// -----------------------------------------------------------------------------
var NodeGame = require('./nodegame/nodegame');

// Setup Game Model
Shooter = NodeGame.Model(20);
require('./shooter/game');
require('./shooter/client');
require('./shooter/actors');

// Start a Server
Shooter.Server({
'port': Math.abs(process.argv[2]) || 28785,
'status': true,
'recordFile': './../record[date].js',
'record': false
});

45 changes: 25 additions & 20 deletions server/actors.js → server/shooter/actors.js
Expand Up @@ -27,7 +27,7 @@ var Polygon2D = require('./polygon').Polygon2D;

// Actors ----------------------------------------------------------------------
// -----------------------------------------------------------------------------
var ActorPlayer = Server.createActorType('player', 2);
var ActorPlayer = Shooter.Actor('player', 2);
ActorPlayer.baseShape = [[0, -12], [10, 12], [-10, 12], [0, -12]];
ActorPlayer.shape = new Shape2D(ActorPlayer.baseShape, 2.5);
ActorPlayer.shapeArmor = new Shape2D(ActorPlayer.baseShape, 6.5);
Expand Down Expand Up @@ -83,19 +83,7 @@ ActorPlayer.onCreate = function(data) {

ActorPlayer.onUpdate = function() {
this.r = this.$$.wrapAngle(this.r + this.mr);
var maxSpeed = this.boost ? 4.5 : 3;
var r = Math.atan2(this.mx, this.my);

var speed = Math.sqrt(Math.pow(this.x - (this.x + this.mx), 2)
+ Math.pow(this.y - (this.y + this.my), 2));

if (speed > maxSpeed) {
speed = maxSpeed;
}
this.mx = Math.sin(r) * speed;
this.my = Math.cos(r) * speed;
this.speed = speed;

this.limitSpeed();
this.x += this.mx;
this.y += this.my;
this.polygon.transform(this.x, this.y, this.r);
Expand Down Expand Up @@ -152,6 +140,8 @@ ActorPlayer.onUpdate = function() {
}
this.boosting = false;
this.boost = false;
this.limitSpeed();
this.update();
}

// Camouflage
Expand Down Expand Up @@ -194,6 +184,21 @@ ActorPlayer.onUpdate = function() {
}
};

ActorPlayer.limitSpeed = function() {
var maxSpeed = this.boost ? 4.5 : 3;
var r = Math.atan2(this.mx, this.my);

var speed = Math.sqrt(Math.pow(this.x - (this.x + this.mx), 2)
+ Math.pow(this.y - (this.y + this.my), 2));

if (speed > maxSpeed) {
speed = maxSpeed;
}
this.mx = Math.sin(r) * speed;
this.my = Math.cos(r) * speed;
this.speed = speed;
}

ActorPlayer.enableArmor = function() {
this.armorHP = 16;
this.armor = true;
Expand Down Expand Up @@ -271,7 +276,7 @@ ActorPlayer.onMessage = function(once) {


// Missile ---------------------------------------------------------------------
var ActorMissile = Server.createActorType('missile', 2);
var ActorMissile = Shooter.Actor('missile', 2);
ActorMissile.onCreate = function(data) {
this.time = this.getTime();
this.player = data.player;
Expand Down Expand Up @@ -367,7 +372,7 @@ ActorMissile.onMessage = function(once) {


// Bullet ----------------------------------------------------------------------
var ActorBullet = Server.createActorType('bullet', 6);
var ActorBullet = Shooter.Actor('bullet', 6);
ActorBullet.onCreate = function(data) {
this.time = this.getTime();
this.player = data.player;
Expand Down Expand Up @@ -395,7 +400,7 @@ ActorBullet.onMessage = function(once) {


// Bomb ------------------------------------------------------------------------
var ActorBomb = Server.createActorType('bomb', 6);
var ActorBomb = Shooter.Actor('bomb', 6);
ActorBomb.onCreate = function(data) {
this.time = this.getTime();
this.range = 120;
Expand Down Expand Up @@ -467,7 +472,7 @@ ActorBomb.onMessage = function(once) {


// PowerUp ---------------------------------------------------------------------
var ActorPowerUp = Server.createActorType('powerup', 0);
var ActorPowerUp = Shooter.Actor('powerup', 0);
ActorPowerUp.onCreate = function(data) {
this.$$.randomPosition(this, this.$$.sizePowerUp);
this.type = data.type;
Expand Down Expand Up @@ -505,7 +510,7 @@ ActorPowerUp.onMessage = function(once) {


// Player Defender -------------------------------------------------------------
var ActorPlayerDef = Server.createActorType('player_def', 6);
var ActorPlayerDef = Shooter.Actor('player_def', 6);
ActorPlayerDef.onCreate = function(data) {
this.player = data.player;
this.player.defender = this;
Expand Down Expand Up @@ -569,7 +574,7 @@ ActorPlayerDef.onMessage = function(once) {


// Asteroid --------------------------------------------------------------------
var ActorAsteroid = Server.createActorType('asteroid', 6);
var ActorAsteroid = Shooter.Actor('asteroid', 6);
ActorAsteroid.shapes = [
new Shape2D([[-1, -6], [-7, -4], [-6, 4], [2, 5], [6, -2]], 2.4),
new Shape2D([[-2, -13], [-13 , -8], [-12, 8], [-2, 12], [11, 10], [12, -8]],
Expand Down
2 changes: 1 addition & 1 deletion server/clients.js → server/shooter/client.js
Expand Up @@ -23,7 +23,7 @@

// Client handlers -------------------------------------------------------------
// -----------------------------------------------------------------------------
var Client = Server.Client();
var Client = Shooter.Client();

Client.onInit = function() {
this.playerName = '';
Expand Down

0 comments on commit 9fddd91

Please sign in to comment.