Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Abacus/Abacus
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgoddard committed Nov 27, 2011
2 parents 0cce500 + 61ce48c commit b3924a1
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 28 deletions.
17 changes: 13 additions & 4 deletions Jakefile.js
Expand Up @@ -48,8 +48,10 @@ var // Program References
evil: true,
forin: false,
maxerr: 100,
eqnull: true
// "curly": true,
eqnull: true,
curly: true,
browser: true
// onevar: true,
// "eqnull": true,
// "immed": true,
// "newcap": true,
Expand Down Expand Up @@ -198,7 +200,14 @@ function uglify( src ) {

// Return deflated src input.
function gzip( src ) {
return zlib.deflate( new Buffer( src ) );

if ( zlib.Deflate ) {
return zlib.gzip( src, function( err, buffer ) {
ok( "Compressed size: " + (buffer.length + "").yellow + " bytes gzipped (" + ( src.length + "" ).yellow + " bytes minified)." );
});
}

ok( "Compressed size: " + (zlib.deflate( src ).length + "").yellow + " bytes gzipped (" + ( src.length + "" ).yellow + " bytes minified)." );
}

// Jake Tasks
Expand Down Expand Up @@ -281,7 +290,7 @@ task( "min", function() {
min = intro + min;

if ( writeFile( minpath, min, false ) ) {
ok( "Compressed size: " + (gzip( min ).length + "").yellow + " bytes gzipped (" + ( min.length + "" ).yellow + " bytes minified)." );
gzip( min );
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion build.json
Expand Up @@ -5,7 +5,8 @@
"src/abacus.js",
"src/abacus.timer.js",
"src/abacus.tween.js",
"src/abacus.animation.js"
"src/abacus.animation.js",
"src/abacus.entity.js"
],
"prehint": true,
"posthint": true,
Expand Down
11 changes: 7 additions & 4 deletions index.html
@@ -1,10 +1,13 @@

<!DOCTYPE html>
<html>
<head>
<meta>
<title>Boilerplate demo</title>
<script src="src/boilerplate.js"></script>
<title>Abacus</title>
<!-- Abacus -->
<script src="src/abacus.js"></script>
<script src="src/abacus.tween.js"></script>
<script src="src/abacus.timer.js"></script>
<script src="src/abacus.animation.js"></script>
<script src="src/abacus.entity.js"></script>
</head>
<body>

Expand Down
16 changes: 8 additions & 8 deletions readme.md
@@ -1,14 +1,14 @@
#Abacus.js
Abacus is an Open Source HTML5 Game Framework. Abacus is currently in the early pre 0.1 stage of development. Take a look at the milestones to see our progress: https://github.com/boazsender/Abacus/issues/milestones
Abacus is an Open Source HTML5 Game Framework. Abacus is currently in the early pre 0.1 stage of development. Take a look at the milestones to see our progress: https://github.com/Abacus/Abacus/issues/milestones

Please see Dependancies, Documentation, Project goals, Target User, Roadmap, W3C API requests, Style Guide, Contributing Guide, and Target Browsers below.

If you are interested in contributing to Open Web Standards connected to games, make sure to join the W3C Games Community Group http://www.w3.org/community/games.

## Dependencies
### One time setup:
* Node.js
* NPM
### One time setup for build process:
* Node.js (installation: https://github.com/joyent/node/wiki/Installation)
* NPM (installation: ```curl http://npmjs.org/install.sh | sh```)

### Setup
Once the dependencies are installed:
Expand All @@ -24,18 +24,18 @@ Future re-builds require _only_:


## Documentation
Documentation for the APIs we have implemented so far is available on our wiki: https://github.com/boazsender/Abacus/wiki/Documentation
Documentation for the APIs we have implemented so far is available on our wiki: https://github.com/Abacus/Abacus/wiki/Documentation

## Project goals
The project goals are available on our wiki: https://github.com/boazsender/Abacus/wiki/goals
The project goals are available on our wiki: https://github.com/Abacus/Abacus/wiki/goals

## Target User
* Games developers
* Indie game developers
* Large game studios

## Roadmap
The project roadmap is available on our wiki: https://github.com/boazsender/Abacus/wiki/roadmap
The project roadmap is available on our wiki: https://github.com/Abacus/Abacus/wiki/roadmap


## What we really want from browser vendors:
Expand All @@ -57,7 +57,7 @@ See Also: https://github.com/rwldrn/idiomatic.js
## Contributing Guide
We encourage you to fork, branch and make pull requests! Join us in #Abacus on irc.freenode.net.

Issues are also really helpful, please head over to the issues for this project to give input on the project goals/scope, the API so far, or to submit a feature request or bug: https://github.com/boazsender/Abacus/issues
Issues are also really helpful, please head over to the issues for this project to give input on the project goals/scope, the API so far, or to submit a feature request or bug: https://github.com/Abacus/Abacus/issues

## Target Browsers
* IE 9
Expand Down
101 changes: 101 additions & 0 deletions src/abacus.entity.js
@@ -0,0 +1,101 @@
(function( window, Abacus ) {

var entities = {};

// Express the Entity function
function Entity( options ) {

var hooks;

// If no options.type, default to player
options.type = options.type || 'player';

// If no options.id, provide default
options.id = options.id || Abacus.guid();

hooks = Entity.hooks[ options.type ] || {};

// Extend a reasonable set of defaults with
// any hooks that may exist, with the
// options argument onto fresh target object
this.attributes = Abacus.extend({}, hooks, options );
}

// Setup the Entity prototype with methods for opperating
// on entity instances
Entity.prototype = {
// Serialize the entity
toJSON: function() {
return JSON.stringify( this.attributes );
},
// Get the value of an entity's property.
get: function( prop ) {
return this.attributes[ prop ];
},
// Returns `true` if the property contains a value that is not null
// or undefined.
has: function( prop ) {
// Use native syntax or raw speed
return typeof this.attributes[ prop ] !== 'undefined';
},
// Set a property on an entity's attributes
set: function( prop, value ) {

// Check arg type and set accordingly
if ( typeof prop === 'object' ) {
// Directly extend `this.attributes` if `prop` arg is an
// object argument of key/vals
Abacus.extend( this.attributes, prop );
} else {
this.attributes[ prop ] = value;
}

return this;
},
// Remove a property from the entity
unset: function( prop ) {
delete this.attributes[prop];
return this;
}
};

// Special methods and data to extend specific types
Entity.hooks = {
// type: { methods }
player: {
achievements: []
}
};

Abacus.entity = function( options ) {
// Ensure that id duplication is prohibited
if ( options.id && entities[ options.id ] ) {
throw new Error( 'Cannot create entity; id in use: ' + options.id );
}

// Create new Entity instance
var entity = new Entity( options );

// Cache entity reference in memory
entities[ options.id ] = entity;

// Return new entity
return entity;
};

// Allow for custom hooks
Abacus.entity.hooks = Entity.hooks;

// Attach static functions

// Returns instances from cache by id
Abacus.entity.get = function( id ) {
return id ? entities[ id ] : entities;
};

// Removes all instances from cache
Abacus.entity.flush = function() {
entities = [];
};

})( this, this.Abacus );
19 changes: 8 additions & 11 deletions src/abacus.js
Expand Up @@ -35,7 +35,7 @@
val = obj[ i ];

// If array item is an object (including arrays), derive new value by cloning
if ( typeof val === "object" ) {
if ( typeof val === 'object' ) {
val = Abacus.clone( val );
}

Expand All @@ -57,17 +57,14 @@
return Object.create({}, (function( src ) {

// Store reference to non-inherited properties,
// Initialize
var ownPropertyNames = Object.getOwnPropertyNames( src ),
properties = {};
var properties = {};

ownPropertyNames.forEach(function( name ) {
Object.getOwnPropertyNames( src ).forEach(function( name ) {

var descriptor = Object.getOwnPropertyDescriptor( src, name ),
tmp;
var descriptor = Object.getOwnPropertyDescriptor( src, name );

// Recurse on properties whose value is an object or array
if ( typeof src[ name ] === "object" ) {
if ( typeof src[ name ] === 'object' ) {
descriptor.value = Abacus.clone( src[ name ] );
}

Expand Down Expand Up @@ -112,9 +109,9 @@
// Abacus.prefix
// This user agent's vendor prefix
Abacus.prefix = (function( window ) {
return [ "webkit", "moz", "ms", "o" ].filter(function( val ) {
return val + "RequestAnimationFrame" in window;
})[ 0 ] || "";
return [ 'webkit', 'moz', 'ms', 'o' ].filter(function( val ) {
return val + 'RequestAnimationFrame' in window;
})[ 0 ] || '';
})( window );

// Expose global Abacus object
Expand Down
2 changes: 2 additions & 0 deletions test/index.html
Expand Up @@ -12,11 +12,13 @@
<script src="../src/abacus.tween.js"></script>
<script src="../src/abacus.timer.js"></script>
<script src="../src/abacus.animation.js"></script>
<script src="../src/abacus.entity.js"></script>
<!-- tests -->
<script src="unit/abacus.js"></script>
<script src="unit/abacus.tween.js"></script>
<script src="unit/abacus.timer.js"></script>
<script src="unit/abacus.animation.js"></script>
<script src="unit/abacus.entity.js"></script>
</head>
<body>
<h1 id="qunit-header">Abacus Unit Tests</h1>
Expand Down
43 changes: 43 additions & 0 deletions test/unit/abacus.entity.js
@@ -0,0 +1,43 @@
module('Entity');

test('Abacus.entity() exists and is a function', 2, function() {
ok( Abacus.entity, 'Abacus.entity exists' );
equal( typeof Abacus.entity, 'function', 'Abacus.entity is a function' );
});

test('Abacus.entity() creates entities', 4, function() {

var attributes = { name: 'player', type: 'enemy', id: 'asd' },
player = Abacus.entity(attributes);

ok( player, 'Abacus.entity({}) does not return null/undefined' );

Object.keys( attributes ).forEach(function( key ) {
equal( player.get( key ), attributes[ key ], 'Instance has correct value per key' );
});
});

test('Abacus.entity constructs a player correctly', 18, function() {
var player = Abacus.entity({ name: 'player' });

equal( typeof player.attributes.achievements, 'object', 'achievements exists on player and is an object' );
equal( typeof player.get, 'function', 'get exists on player and is a function' );
equal( typeof player.has, 'function', 'has exists on player and is a function' );
equal( typeof player.set, 'function', 'set exists on player and is a function' );
equal( typeof player.toJSON, 'function', 'toJSON exists on player and is a function' );
equal( typeof player.unset, 'function', 'unset exists on player and is a function' );
equal( player.attributes.id.length, 36, 'id exists on player and is 36 characters long' );
ok( player.attributes.name, 'the name property was properly merged onto the player object' );

equal( player.get('name'), 'player', 'Test get method to return correct value' );
equal( player.get('id').length, 36, 'test that get id returns a 36 char string' );
equal( player.has('name'), true, 'check that has method returns true for prop that is expected to exist' );
equal( player.set('foo', 'bar').get('foo'), 'bar', 'check that setting foo as bar and then getting foo returns bar' );
equal( player.has('foo'), true, 'check that has foo is true' );
equal( player.get('foo'), 'bar', 'check that get foo returns bar' );
equal( player.unset('foo').has('foo'), false, 'check that unset foo and then has foo is false' );
equal( player.has('foo'), false, 'check that has foo is false' );
equal( typeof player.toJSON(), 'string', 'check that toJSON returns a string' );

deepEqual( JSON.parse( player.toJSON() ), player.attributes, 'JSON roundstrip' );
});

0 comments on commit b3924a1

Please sign in to comment.