Permalink
Browse files

Lots of little things

  • Loading branch information...
1 parent df28d14 commit 2353d4750a4fc2bfc8054b7e3df51df7af7b3b99 John Conomikes committed Jan 25, 2013
@@ -18,6 +18,7 @@ Copyright 2011, Carnegie Learning
var FractionRenderer = require('/FractionRenderer');
var LabelBG = require('/LabelBG');
var PieChart = require('/PieChart');
+var ImageContent = require('/ImageContent');
// Static Imports
var XML = require('/XML');
@@ -38,23 +39,33 @@ Content.initialize = function () {
Content.registerContent(LabelBG.identifier, LabelBG);
Content.registerContent('Fraction', FractionRenderer);
Content.registerContent('PieChart', PieChart);
+ Content.registerContent('Image', ImageContent);
}
-Content._validateNode = function (xmlNode) {
- if(xmlNode.attributes.hasOwnProperty('TYPE')) {
- if(Content.registeredContent.hasOwnProperty(xmlNode.attributes.TYPE)) {
- return true;
- }
+// Helper function to convert all attributes into a object using attribute names to map values
+var mapper = function(xml) {
+ var map = {};
+ var attributes = xml[0].attributes;
+
+ for (a = 0; a < attributes.length; a++) {
+ map[attributes[a].name] = attributes[a].value;
}
- return false;
+
+ return map;
}
// Build Content subclass from parsed XML
Content.buildFrom = function(xmlNode) {
- if(Content._validateNode(xmlNode)) {
- var cs = XML.getChildByName(xmlNode, 'ContentSettings');
- if(cs) {
- return new Content.registeredContent[xmlNode.attributes.TYPE](cs.attributes);
+ //if(xmlNode.attributes.hasOwnProperty('TYPE')) {
+ if($(xmlNode).attr('TYPE')) {
+ var cs = $(xmlNode).children('ContentSettings');
+
+ var opts = {}
+ if(cs.length > 0) {
+ return new Content.registeredContent[$(xmlNode).attr('TYPE')](mapper($(cs)));
+ }
+ else {
+ return new Content.registeredContent[$(xmlNode).attr('TYPE')](mapper($(xmlNode)));
}
}
@@ -45,10 +45,12 @@ function FractionRenderer (opts) {
opts['fontSize'] = this.fontSize;
this.n = new cocos.nodes.Label(opts);
+ this.n.anchorPoint = new geom.Point(0.5, 0.5);
this.addChild({child: this.n});
opts['string'] = this.denominator;
this.d = new cocos.nodes.Label(opts);
+ this.d.anchorPoint = new geom.Point(0.5, 0.5);
this.addChild({child: this.d});
// Figuring out combined content size
@@ -120,8 +122,8 @@ FractionRenderer.inherit(cocos.nodes.Node, {
context.strokeStyle = this.lineColor;
context.beginPath();
if(this.whole == null) {
- context.moveTo(size.width / -4, 0);
- context.lineTo(size.width / 4, 0);
+ context.moveTo(size.width / -3, 0);
+ context.lineTo(size.width / 3, 0);
}
// Account for offset due to mixed number presence
else {
@@ -0,0 +1,74 @@
+/*
+Copyright 2011, Carnegie Learning
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+var cocos = require('cocos2d');
+var events = require('events');
+
+function ImageContent (opts) {
+ ImageContent.superclass.constructor.call(this, opts);
+
+ //Set properties from the option object
+ var i = -1;
+ while(++i < ImageContent.params.length) {
+ if (opts[ImageContent.params[i]]) {
+ this[ImageContent.params[i]] = opts[ImageContent.params[i]];
+ }
+ }
+
+ this.uid = ImageContent.getUID();
+
+ this.load();
+}
+
+ImageContent.inherit(cocos.nodes.Node, {
+ uid : null,
+ scaleX : null,
+ scaleY : null,
+ src : null,
+ isLoaded: false,
+
+ load: function() {
+ var img = new Image()
+ __jah__.resources[this.uid] = {url: this.src, path: this.uid};
+ __jah__.resources[this.uid].data = img;
+ ImageContent.toLoad++;
+
+ img.onload = function () {
+ ImageContent.toLoad--;
+ __jah__.resources[this.uid].loaded = true;
+ this.isLoaded = true;
+ events.trigger(this, 'ImageLoaded');
+
+ this.addChild({child: new cocos.nodes.Sprite({file: this.uid})});
+ }.bind(this)
+
+ img.onerror = function () {
+ throw new Error('Failed to load resource: ' + this.uid + ' from ' + this.src);
+ }.bind(this)
+
+ img.src = this.src;
+ }
+});
+
+ImageContent.params = ['scaleX','scaleY','src'];
+ImageContent.idNum = 1;
+ImageContent.toLoad = 0;
+
+ImageContent.getUID = function() {
+ return '/resources/__ImageContent_' + (++ImageContent.idNum);
+}
+
+module.exports = ImageContent
@@ -68,6 +68,7 @@ PerspectiveNode.inherit(cocos.nodes.Node, {
// Explicitly unschedules and unsubscribes this node
cleanup: function () {
+ clearTimeout(this.idleing);
cocos.Scheduler.sharedScheduler.unscheduleUpdateForTarget(this);
events.clearInstanceListeners(this);
},
@@ -136,7 +137,7 @@ PerspectiveNode.inherit(cocos.nodes.Node, {
cocos.Scheduler.sharedScheduler.scheduleUpdate({target: this, priority: 0, paused: false});
}
else {
- setTimeout(this.idle, 1000);
+ this.idleing = setTimeout(this.idle, 1000);
}
},
@@ -21,7 +21,6 @@ var geom = require('geometry');
var Content = require('/Content');
var PNode = require('/PerspectiveNode');
var RC = require('/RaceControl');
-var XML = require('/XML');
// Represents a single question to be answered by the player
function Question (node, z) {
@@ -35,20 +34,24 @@ function Question (node, z) {
Question.superclass.constructor.call(this, superOpts);
// Build delimiters for question
+ var that = this, i=0;
this.delimiters = [];
- for(var i=0; i<node.children.length-1; i+=1) {
- this.buildDelim(node.children[i], z, i==0 ? 1 : -1);
- this.delimiters[i].xCoordinate = RC.delimiterSpacing[node.children.length][i];
- }
+
+ //HACK: Remove 3 lane hardcoding
+ $(node).children('Content').each(function() {
+ that.buildDelim(this, z, i==0 ? 1 : -1);
+ that.delimiters[i].xCoordinate = RC.delimiterSpacing[3][i];
+ i += 1;
+ });
- //HACK: need better way of determining/setting this
- RC.curNumLanes = node.children.length;
+ //HACK: Remove 3 lane hardcoding
+ RC.curNumLanes = 3;
- this.correctAnswer = node.children[i].attributes['VALUE'];
+ this.correctAnswer = parseInt($(node).children('ANSWER').attr('VALUE'));
return this;
}
-
+
Question.inherit(PNode, {
correctAnswer : null, // The correct response
answer : null, // The answer provided by the player
@@ -69,18 +72,17 @@ Question.inherit(PNode, {
c.position = new geom.Point(180, -270);
c.bgShow = false;
- var pSet = XML.getChildByName(node, 'PerspectiveSettings');
- pSet = (pSet == null) ? {attributes:{}} : pSet;
+ var pSet = $(node).find('PerspectiveSettings');
// Create option settings
var opts = {
lockY : true,
silent : true,
- minScale : pSet.attributes['minScale'] == null ? 1.2 : pSet.attributes['minScale'],
- maxScale : pSet.attributes['maxScale'] == null ? 3.2 : pSet.attributes['maxScale'],
+ minScale : pSet.attr('minScale') == null ? 1.2 : pSet.attr('minScale'),
+ maxScale : pSet.attr('maxScale') == null ? 3.2 : pSet.attr('maxScale'),
alignH : 0.87,
alignV : 0,
- visibility : pSet.attributes['visibility'] == null ? 5.5 : pSet.attributes['visibility'],
+ visibility : pSet.attr('visibility') == null ? 5.5 : pSet.attr('visibility'),
xCoordinate : 0,
zCoordinate : z,
content : sign
@@ -56,10 +56,8 @@ RaceControl.YTextOff = function(y, fs) {
RaceControl.crossFadeSpeed = 30;
RaceControl.parseAudio = function (xml) {
- var node = XML.getDeepChildByName(xml, 'AudioSettings');
- if(node) {
- RaceControl.helper(RaceControl, 'crossFadeSpeed', node.attributes['crossFadeSpeed']);
- }
+ var node = $(this).find('AudioSettings');
+ RaceControl.helper(RaceControl, 'crossFadeSpeed', node.attr('crossFadeSpeed'));
};
// <MEDALS> ///////////////////////////////////////////////////////////////////////////////////////
@@ -68,24 +66,11 @@ RaceControl.times = [32, 42, 68, 100, 200]; // Holds [min, gold,
RaceControl.medalNames = ['Gold', 'Gold', 'Silver', 'Bronze', ' - '];
RaceControl.parseMedals = function (xml) {
- var node = XML.getDeepChildByName(xml, 'MEDALS');
- if(!node) {
- throw new Error('No medal data found for stage');
- }
- else {
- var id, val;
- for(var i in node.children) {
- id = node.children[i].attributes['Id'];
- val = node.children[i].attributes['MEDAL_THRESHOLD'];
-
- if(id != null && val != null) {
- RaceControl.times[id] = val / 1000;
- }
- else {
- throw new Error('Missing or corrupted medal data');
- }
- }
- }
+ var node = $(xml).find('MEDALS');
+
+ $(node).find('MEDAL').each(function() {
+ RaceControl.times[$(this).attr('Id')] = $(this).attr('MEDAL_THRESHOLD') / 1000;
+ });
};
RaceControl.gold = '#CC9900'; // Color for gold medals
@@ -101,15 +86,12 @@ RaceControl.finishSpacing = 110; // Distance in meter
RaceControl.initialSpacing = 0; // Additional distance in meters before the first question
RaceControl.parseSpacing = function (xml) {
- var node = XML.getDeepChildByName(xml, 'GlobalSpacing');
- if(!node) {
- return;
- }
+ var node = $(xml).find('GlobalSpacing');
- RaceControl.helper(RaceControl, 'intermissionSpacing', node.attributes['IntermissionSpacing']);
- RaceControl.helper(RaceControl, 'questionSpacing' , node.attributes['QuestionSpacing']);
- RaceControl.helper(RaceControl, 'finishSpacing' , node.attributes['FinishSpacing']);
- RaceControl.helper(RaceControl, 'initialSpacing' , node.attributes['InitialSpacing']);
+ RaceControl.helper(RaceControl, 'intermissionSpacing', node.attr('IntermissionSpacing'));
+ RaceControl.helper(RaceControl, 'questionSpacing' , node.attr('QuestionSpacing'));
+ RaceControl.helper(RaceControl, 'finishSpacing' , node.attr('FinishSpacing'));
+ RaceControl.helper(RaceControl, 'initialSpacing' , node.attr('InitialSpacing'));
};
RaceControl.delimiterSpacing = {2: [0], 3: [-1.5, 1.5], 4: [-3, 0, 3]};
@@ -120,13 +102,10 @@ RaceControl.penaltyTime = 15; // Time in seconds l
RaceControl.penaltySpeed = -0.1; // Percentage speed change for an incorrect answer
RaceControl.parsePenalty = function (xml) {
- var node = XML.getDeepChildByName(xml, 'PenaltySettings');
- if(!node) {
- return;
- }
+ var node = $(xml).find('PenaltySettings');
- RaceControl.helper(RaceControl, 'penaltyTime', node.attributes['TimeLost']);
- RaceControl.helper(RaceControl, 'penaltySpeed', node.attributes['SpeedLost']);
+ RaceControl.helper(RaceControl, 'penaltyTime', node.attr('TimeLost'));
+ RaceControl.helper(RaceControl, 'penaltySpeed', node.attr('SpeedLost'));
RaceControl.penaltySpeed *= -1;
};
@@ -143,22 +122,19 @@ RaceControl.maxTimeWindow = 110 / 150.0 * 0.9; // Minimum time betw
RaceControl.maxDistWindow = 300; // Maximum distance coverable in 2 seconds
RaceControl.parseSpeed = function (xml) {
- var node = XML.getDeepChildByName(xml, 'SpeedSettings');
- if(!node) {
- return;
- }
+ var node = $(xml).find('SpeedSettings');
- var max = node.attributes['Max'];
- var min = node.attributes['Min'];
- var speed = node.attributes['Default'];
- var turbo = node.attributes['Turbo'];
+ var max = node.attr('Max');
+ var min = node.attr('Min');
+ var speed = node.attr('Default');
+ var turbo = node.attr('Turbo');
// Set the values on the player
RaceControl.helper(RaceControl, 'maxSpeed', max);
RaceControl.helper(RaceControl, 'minSpeed', min);
RaceControl.helper(RaceControl, 'zVelocity', speed==null ? min : speed);
- RaceControl.helper(RaceControl, 'acceleration', node.attributes['Acceleration']);
- RaceControl.helper(RaceControl, 'deceleration', node.attributes['Deceleration']);
+ RaceControl.helper(RaceControl, 'acceleration', node.attr('Acceleration'));
+ RaceControl.helper(RaceControl, 'deceleration', node.attr('Deceleration'));
RaceControl.helper(RaceControl, 'turboSpeed', turbo==null ? max : turbo);
};
Oops, something went wrong.

0 comments on commit 2353d47

Please sign in to comment.