Added rules and tests for them

This commit is contained in:
2016-02-26 14:21:01 +08:00
parent a82d04af1a
commit 1ceecc417d
13 changed files with 801 additions and 340 deletions
+22 -47
View File
@@ -4,26 +4,7 @@ var Detector = function(){
return {
core:
{
// canvas: null,
// ctx: null
},
//
// events:
// {
// canvas: null,
// ctx: null,
// list: [],
// },
flame:
{
// canvas: null,
// ctx: null
},
elements: new GameObjects.ElementStores(),
elements: new GameObjects.Cards(),
visible: true,
@@ -36,21 +17,10 @@ var Detector = function(){
bubblr: undefined,
init: function(baseSize,element)
init: function()
{
// get canvas
// this.core.canvas = document.getElementById('detector-core');
// if (!this.core.canvas) {
// this.core.canvas=$('<canvas id="detector-core"></canvas>');
// $(element).append(this.core.canvas);
// }
// this.core.ctx = this.core.canvas.getContext('2d');
//
// this.flame.canvas = document.getElementById('detector-flame');
// this.flame.ctx = this.flame.canvas.getContext('2d');
this.initBubbles();
this.initFlame();
// this.initBubbles();
// this.initFlame();
},
initBubbles: function(){
@@ -72,10 +42,10 @@ var Detector = function(){
this.bubblr = bubblrElem.data('plugin_bubblr');
},
initFlame: function(){
var flameElem = $('#detector-flame').flame();
this.flamer = flameElem.data('plugin_flame')
},
// initFlame: function(){
// var flameElem = $('#detector-flame').flame();
// this.flamer = flameElem.data('plugin_flame')
// },
bindOnResize: function(){
// HACK
@@ -114,19 +84,19 @@ var Detector = function(){
/** When a user clicks the detector **/
addEvent: function()
{
this.bubblr.bubble(); // bubble for 500ms, TODO make one bubble
// this.bubblr.bubble(); // bubble for 500ms, TODO make one bubble
},
/** When a worker clicks the detector **/
addEventExternal: function(numWorkers)
{
this.bubblr.genBubbles(numWorkers);
// this.bubblr.genBubbles(numWorkers);
},
/** Draw current events **/
draw: function(duration)
{
this.bubblr.bubble();
// this.bubblr.bubble();
},
/** Clear an element back to element Store **/
@@ -196,9 +166,14 @@ var Detector = function(){
if (newElement && intersectingElements.indexOf(newElement)===-1) intersectingElements.push(newElement);
var observation = this.experiment({inputs:intersectingElements,location:$draggable.offset()},game);
console.log('intersectingElements', intersectingElements.length, observation);
return observation;
var uniqueElems = _(intersectingElements).map('key').uniq().value().length;
if (intersectingElements.length==2 && uniqueElems>1){
var observation = this.experiment({inputs:intersectingElements,location:$draggable.offset()},game);
console.log('intersectingElements', intersectingElements.length, observation);
return observation;
} else {
return;
}
},
@@ -209,7 +184,7 @@ var Detector = function(){
var inputKeys = inputs.map(function(e){return e.key;});
inputKeys.sort(); // this makes reaction be independant of order
var result = game.rules[inputKeys];
var result = game.testRules(inputKeys);
if (result) {
this.reaction(inputs,result.reactants,result.results, options.location, game);
} else {
@@ -232,7 +207,7 @@ var Detector = function(){
// get the uuid from inputs
var ingredient = inputs.filter(function(e){return e.key===reactants[i];})[0];
var j = _.findIndex(this.elements,{uuid:ingredient.uuid});
var removed = this.elements.slice(j,1);
var removed = this.elements.splice(j,1);
}
// TODO use angular effects to remove in puff of fade
@@ -259,7 +234,7 @@ var Detector = function(){
}
// effects
this.bubblr.bubble();
// this.bubblr.bubble();
},
};
-70
View File
@@ -1,70 +0,0 @@
/** Define and draw to canvas particle events, used in detector **/
/**
* Define and draw to canvas particle events, used in detector
* @param {Object} type Object with name 'electron'||'jet'||'muon'
* @param {Number} count Number of particles in this event
* @param {Boolean} external Wether even was caused by player or worker
*/
function ParticleEvent(type, count, external)
{
this.work = typeof external !== 'undefined' ? external : false;
this.type = type;
this.length = 0;
this.radius = 0;
this.direction = 0;
this.sign = (Math.random() - 0.5 >= 0) ? 1 : -1;
this.alpha = this.work ? 0.5 : 1;
this.count = count;
switch (this.type.name)
{
case 'electron':
this.length = detector.radius.siliconSpace * detector.ratio + Math.round((detector.radius.ecal * detector.ratio + 10 - detector.radius.siliconSpace * detector.ratio) * Math.random());
this.direction = Math.random() * Math.PI * 2;
this.radius = 20 + Math.round((100 - 20) * Math.random());
break;
case 'jet':
this.length = detector.radius.ecal * detector.ratio + Math.round((detector.radius.mucal * detector.ratio - detector.radius.ecal * detector.ratio) * Math.random());
this.direction = Math.random() * Math.PI * 2;
this.radius = 40 + Math.round((200 - 40) * Math.random());
break;
case 'muon':
this.length = detector.radius.mucal * detector.ratio + 3 * detector.radius.mucalDark * detector.ratio + Math.round((4 * detector.radius.mucalLight * detector.ratio + 2 * detector.radius.mucalDark * detector.ratio) * Math.random());
this.direction = Math.random() * Math.PI * 2;
this.radius = 200 + Math.round((600 - 200) * Math.random());
break;
}
this.draw(16, true);
};
ParticleEvent.prototype.draw = function(duration, init)
{
init = typeof init !== 'undefined' ? init : false;
var ctx = detector.events.ctx;
var cx = detector.width / 2;
var cy = detector.height / 2;
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.strokeStyle = this.type.color;
ctx.fillStyle = this.type.color;
ctx.lineWidth = 2;
ctx.translate(cx, cy);
ctx.rotate(this.direction);
ctx.translate(-cx, -cy);
ctx.beginPath();
ctx.arc(cx + this.length / 2, cy + this.sign * Math.round(Math.sqrt(Math.abs(this.radius * this.radius - this.length * this.length / 4))), this.radius, - this.sign * Math.PI / 2 - Math.asin(this.length / (2 * this.radius)), - this.sign * Math.PI / 2 + Math.asin(this.length / (2 * this.radius)), false);
ctx.stroke();
ctx.restore();
if (!init) {
this.alpha -= 0.03 / 16 * duration;
}
};