Skip to content

Commit

Permalink
aqua.gameService()
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgoddard committed Feb 21, 2012
1 parent 2eccc04 commit 2ea7ad0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Game = function() {
this.objects = [];
this.services = [];
this.tasks = aqua.taskList({priorities: Game.Priorities});

this.task({callback: this.call.bind(this, 'update')});
Expand All @@ -26,6 +27,21 @@ Game.prototype = {
}).bind(this), priority: 'GARBAGE', once: true});
},

_destroyservice: function(service) {
this.task({callback: (function() {
var index = this.sevices.indexOf(service);

if (index != -1) {
if (service.ongamedestroy)
service.ongamedestroy(this);

service.game = null;

this.services.splice(index, 1);
}
}).bind(this), priority: 'GARBAGE', once: true});
},

// add - add a gameobject to the game
add: function(object) {
object.game = this;
Expand All @@ -36,6 +52,15 @@ Game.prototype = {
object.call('ongameadd', object, this);
},

// addService
addService: function(service) {
service.game = this;
this.services.push(service);

if (service.ongameadd)
service.ongameadd(this);
},

call: function(method) {
var args = Array.prototype.slice.call(arguments, 1),
objects = this.objects,
Expand Down Expand Up @@ -89,6 +114,29 @@ aqua.extend(Game, {
}
});

var GameService = function() {
this.tasks = [];
};

GameService.prototype = {
name: "service",

destroy: function() {
this.game._destroyservice(this);
},

ongameadd: function(game) {

},

ongamedestroy: function(game) {
var i = -1;
while (i++ < this.tasks.length) {
game.tasks.remove(this.tasks[i]);
}
}
};

var GameObject = function() {
this.components = [];
};
Expand Down Expand Up @@ -189,6 +237,9 @@ Component.prototype = aqua.extend(
aqua.game = function() {
return new Game();
};
aqua.gameService = function() {
return new GameService();
};
aqua.gameObject = function() {
return new GameObject();
};
Expand Down

0 comments on commit 2ea7ad0

Please sign in to comment.