Permalink
Browse files

Summary: Started sprites. Merged elements and wrap.

  • Loading branch information...
1 parent 031eece commit 2f0545969f19c3af2226ce2b000598e60260814f Robert Hurst committed Mar 22, 2012
Showing with 95 additions and 18 deletions.
  1. +17 −18 red-locomotive/elements.js
  2. +78 −0 red-locomotive/sprite.js
View
35 red-locomotive/elements.js
@@ -26,7 +26,9 @@ define(function() {
return api;
function Element(id, spriteId, x, y, z, width, height) {
- var element;
+ var element, eI, api, emitter, canvas, context, updated;
+
+ updated = true;
//set defaults
if(typeof spriteId === 'number') {
@@ -52,6 +54,16 @@ define(function() {
if(typeof z !== 'number') { throw new Error('Cannot create element. The z position must be a number.'); }
if(typeof width !== 'number') { throw new Error('Cannot create element. The width must be a number.'); }
if(typeof height !== 'number') { throw new Error('Cannot create element. The height must be a number.'); }
+ for(eI = 0; eI < elements.length; eI += 1) {
+ if(elements[eI].id === id) {
+ throw new Error('Cannot create element. The id must be unique.');
+ }
+ }
+
+ //create the emitter and the canvas
+ emitter = engine.emitter();
+ canvas = document.createElement('canvas');
+ context = canvas.getContext('2d');
//create the element
element = {
@@ -68,28 +80,15 @@ define(function() {
"width": width,
"height": height,
"spriteId": spriteId,
- "parentId": null
+ "parentId": null,
+ "canvas": canvas,
+ "context": context,
+ "emitter": emitter
};
//add the element to the elements object
elements.push(element);
- //wrap the element
- return wrap(element);
- }
-
- function wrap(element) {
- var api, emitter, canvas, context, updated;
-
- /*
- If the element was imported via json it will not have an emitter
- or canvas. It such a case they will need to be created.
- */
- emitter = element.emitter = element.emitter || engine.emitter();
- canvas = element.canvas = element.canvas || document.createElement('canvas');
- context = element.context = element.context || canvas.getContext('2d');
- updated = true;
-
//create the api
api = {};
api.move = elementPosition;
View
78 red-locomotive/sprite.js
@@ -0,0 +1,78 @@
+define(function() {
+
+ //Set the namespace
+ init.namespace = '';
+
+ //return the init
+ return init;
+
+ /**
+ * Sprite init
+ */
+ function init(engine, data) {
+ var api, sprites;
+
+ sprites = data.sprites = [];
+
+ api = {
+ "sprite": Sprite
+ };
+
+ function Sprite(id, source) {
+ var sprite, image, elementEmitter, canvas, context, api;
+
+ if(typeof id !== 'string') { throw new Error('Cannot create sprite. The sprite id must be a string.'); }
+ if(typeof source !== 'string' && typeof source !== 'function') { throw new Error('Cannot create sprite. The sprite source must be a string or a function.'); }
+
+ canvas = document.createElement('canvas');
+ context = canvas.getContext('2d');
+
+ sprite = {
+ "id": id,
+ "canvas": canvas
+ };
+
+ sprites.push(sprite);
+
+ if(typeof source === 'string') {
+
+ image = document.createElement('img');
+
+ elementEmitter = engine.emitter();
+ elementEmitter.pipe(image);
+ elementEmitter.on('load', function() {
+
+ Sprite(id, function(canvas, context) {
+ canvas.width = image.width;
+ canvas.height = image.height;
+ context.drawImage(image, 0, 0, image.width, image.height);
+ });
+
+ });
+
+ image.src = source;
+
+ } else if(typeof source === 'function') {
+ source(canvas, context);
+ sprite.width = canvas.width;
+ sprite.height = canvas.height;
+ }
+
+ api = {
+ "update": update
+ };
+
+ return api;
+
+ function update(callback) {
+
+ if(typeof callback !== 'function') { throw new Error('Cannot update sprite. The callback must be a function.'); }
+
+ callback(canvas, context);
+
+ sprite.width !== canvas.width && (sprite.width = canvas.width);
+ sprite.height !== canvas.height && (sprite.height = canvas.height);
+ }
+ }
+ }
+});

0 comments on commit 2f05459

Please sign in to comment.