Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Showing with 149 additions and 3 deletions.
  1. +72 −0 README.rst
  2. +1 −1 grunt.js
  3. 0 {src → lib}/_fill.js
  4. +3 −0 {src → lib}/base.js
  5. +60 −2 {src → lib}/object.js
  6. +13 −0 test/tests/base.js
View
72 README.rst
@@ -0,0 +1,72 @@
+======
+AquaJS
+======
+
+Aqua wants to provide rudimentary code for developing games in JavaScript. It isn't an engine but it provides work to help produce one.
+
+Requires
+========
+
+For building::
+
+ - grunt
+ - uglifyjs
+
+Examples
+========
+
+A Game
+------
+
+.. sourcecode :: javascript
+
+ var myGame = aqua.game(),
+ myGameObject = aqua.gameObject();
+
+ // setup rendering, physics, etc services
+ myGame.addService(someService());
+ ...
+
+ // its a good idea to have a component type for storing position, rotation, and other location in world state
+ var transformComponent = function() {
+ aqua.base(this).constructor.call(this);
+
+ this.position = vector(0, 0);
+ };
+ transformComponent = aqua.extend(
+ aqua.component(),
+ {
+ // transform methods
+ });
+
+ myGameObject.add(transformComponent());
+ myGameObject.add(renderingComponent());
+ myGameObject.add(...);
+ myGame.add(myGameObject);
+
+ myGame.main();
+
+GameService
+-----------
+
+This is an example service that replicates the tasks Game objects has by default.
+
+.. sourcecode :: javascript
+
+ var UpdateService = function() {
+ aqua.base(this).constructor.call(this);
+ };
+
+ UpdateService.prototype = aqua.extend(
+ aqua.gameService(),
+ {
+ ongameadd: function(game) {
+ this.tasks.push(
+ game.task({callback: game.call.bind(game, 'update')}));
+ this.tasks.push(
+ game.task({
+ callback: game.call.bind(game, 'lateUpdate'),
+ priority: 'LATE_UPDATE'}));
+ }
+ }
+ );
View
2 grunt.js
@@ -14,7 +14,7 @@ config.init({
index: ['test/index.html']
},
lint: {
- files: ['src/base.js', 'src/object.js']
+ files: ['lib/base.js', 'lib/object.js']
}
});
View
0 src/_fill.js → lib/_fill.js
File renamed without changes.
View
3 src/base.js → lib/base.js
@@ -170,6 +170,9 @@ aqua.query = function(name, _default) {
}
return _default;
};
+aqua.base = function(context) {
+ return Object.getPrototypeOf(Object.getPrototypeOf(context));
+};
aqua.requestAnimFrame = (function(){
// thanks paul irish
return window.requestAnimationFrame ||
View
62 src/object.js → lib/object.js
@@ -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')});
@@ -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;
@@ -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,
@@ -60,15 +85,22 @@ Game.prototype = {
},
main: function() {
+ var self = this;
+
// put common timing and rAF loop logic in here
+ aqua.requestAnimFrame(function loop() {
+ aqua.requestAnimFrame(loop);
+
+ self.step();
+ });
},
pause: function() {
-
+ // un-implemented
},
resume: function() {
-
+ // un-implemented
}
};
@@ -82,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 = [];
};
@@ -182,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();
};
View
13 test/tests/base.js
@@ -81,3 +81,16 @@ test('emitter emits', 1, function() {
emitter.off('call', handler);
emitter.emit('call');
});
+
+test('base gets super proto', 1, function() {
+ var base = function(){
+ this.x = 1;
+ };
+
+ var type = function() {
+ aqua.base(this).constructor.call(this);
+ };
+ type.prototype = new base();
+
+ deepEqual(new type(), new base(), 'equals super type');
+});

No commit comments for this range

Something went wrong with that request. Please try again.