made with requirebin
Created
October 8, 2015 08:13
-
-
Save kirbysayshi/46b6d01c1208d6b10901 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Pocket = require('pocket-ces'); | |
var arbit = require('arbit'); | |
var pkt = new Pocket(); | |
var random = arbit(); | |
var uiHTML = '' | |
+ '<button onclick="handlePlantSeedClick()">Plant a seed</button>' | |
+ '<button onclick="handleTickClick()">Walk through the fields</button>' | |
+ '<button onclick="handleHarvestClick()">Harvest!</button>' | |
+ '<button onclick="handleNapClick()">Take a relaxing nap</button>' | |
+ '<div id="status-pane"></div>' | |
document.body.innerHTML = uiHTML; | |
pkt.cmpType('life', function (cmp, opts) { | |
cmp.age = cmp.age || 1; | |
}) | |
pkt.cmpType('growth', function (cmp, opts) { | |
cmp.height = cmp.height || 0; | |
}); | |
pkt.cmpType('bag', function (cmp, opts) { | |
cmp.seeds = cmp.seeds || 1; | |
}); | |
pkt.systemForEach( | |
'plants-grow', | |
['life', 'growth'], | |
function (pkt, key, life, growth) { | |
growth.height += Math.log(life.age); | |
life.age += 1; | |
}); | |
pkt.systemForEach( | |
'plant-harvester', | |
['life', 'growth'], | |
function (pkt, key, life, growth) { | |
var shouldHarvest = pkt.keysMatching('cmd:should-harvest'); | |
if (!shouldHarvest.length) return; | |
shouldHarvest.forEach(function(key) { pkt.destroyKey(key) }); | |
var bag = pkt.firstData('bag'); | |
if (life.age > 20) { | |
bag.seeds += Math.floor(growth.height / life.age); | |
pkt.destroyKey(key); | |
} | |
}); | |
pkt.system( | |
'system-printer', | |
[], | |
function (pkt) { | |
var pane = document.querySelector('#status-pane'); | |
var bag = pkt.firstData('bag'); | |
var lifes = pkt.indexedData('life'); | |
var growths = pkt.indexedData('growth'); | |
var keys = pkt.keysMatching('life', 'growth'); | |
if (!keys.length) { | |
pane.innerHTML = 'Your fields are empty.'; | |
} else { | |
pane.innerHTML = keys.map(function(key) { | |
return '<b>height:</b> ' | |
+ growths[key].height | |
+ ', <b>age:</b> ' | |
+ lifes[key].age; | |
}).join('<br>') | |
} | |
pane.innerHTML += '<p>Your bag has ' + bag.seeds + ' seeds.</p>'; | |
}); | |
pkt.key({ | |
bag: null | |
}); | |
window.handlePlantSeedClick = function (e) { | |
var bag = pkt.firstData('bag'); | |
if (bag.seeds > 0) { | |
bag.seeds--; | |
pkt.key({ | |
life: null, | |
growth: null | |
}); | |
pkt.tick(16); | |
} | |
} | |
window.handleHarvestClick = function (e) { | |
var shouldHarvest = pkt.keysMatching('cmd:should-harvest'); | |
if (!shouldHarvest.length) { | |
pkt.key({ 'cmd:should-harvest': null }); | |
pkt.tick(16); | |
pkt.tick(16); | |
} | |
} | |
window.handleTickClick = function (e) { | |
pkt.tick(16); | |
} | |
window.handleNapClick = function (e) { | |
var count = random.nextInt(1,101); | |
while(count--) pkt.tick(16); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({arbit:[function(require,module,exports){function alea(s0,s1,c){var f=function aleaStep(){var t=2091639*s0+c*2.3283064365386963e-10;s0=s1;return s1=t-(c=t|0)};f.getState=function aleaGetState(){return[s0,s1,c]};f.nextFloat=aleaNextFloat;f.nextInt=aleaNextInt;return f}function aleaNextFloat(opt_minOrMax,opt_max){var value=this();var min,max;if(typeof opt_max=="number"){min=opt_minOrMax;max=opt_max}else if(typeof opt_minOrMax=="number"){min=0;max=opt_minOrMax}else{return value}return min+value*(max-min)}function aleaNextInt(minOrMax,opt_max){return Math.floor(this.nextFloat(minOrMax,opt_max))}function aleaFromSeed(seed){var s0,s1,h,n=4022871197,v;seed="X"+(seed||+new Date);for(var i=0;i<2;i++){for(var j=0;j<seed.length;j++){n+=seed.charCodeAt(j);h=.02519603282416938*n;n=h>>>0;h-=n;h*=n;n=h>>>0;h-=n;n+=h*4294967296}v=(n>>>0)*2.3283064365386963e-10;if(i===0)s0=v;else s1=v}return alea(s0,s1,1)}aleaFromSeed.fromState=function aleaFromState(state){return alea.apply(null,state)};module.exports=aleaFromSeed},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var System=require("./system");function Pocket(){this.componentTypes={};this.systems=[];this.components={};this.keys={};this.labels={};this.idCounter=0;this.keysToDestroy={};this.indexedData=this.indexedData.bind(this)}Pocket.prototype.nextId=function(){return++this.idCounter};Pocket.prototype.tick=function(dt){var self=this;Object.keys(this.keysToDestroy).forEach(function(id){self.immediatelyDestroyKey(id);delete self.keysToDestroy[id]});this.dt=dt;for(var i=0;i<this.systems.length;i++){var system=this.systems[i];var datas=system.requiredComponents.map(this.indexedData);var keys=this.keysMatching.apply(this,system.requiredComponents);if(!keys.length&&system.requiredComponents.length>0)continue;datas.unshift(keys);datas.unshift(this);system.action.apply(system,datas)}};Pocket.prototype.system=function(name,requirements,fn){return this.systems.push(new System(name,requirements,fn))};Pocket.prototype.systemForEach=function(name,requirements,fn){var action=fn;fn=function(pkt,keys){for(var i=0,args=[];i<arguments.length;i++)args[i]=arguments[i];var components=args.slice(0);var key;for(var i=0;i<keys.length;i++){key=keys[i];args[1]=key;for(var j=2;j<components.length;j++){args[j]=components[j][key]}action.apply(this,args)}};return this.systems.push(new System(name,requirements,fn))};Pocket.prototype.systemFromObject=Pocket.prototype.sysFromObj=function(obj){if(obj.actionEach)return this.systemForEach(obj.name,obj.reqs,obj.actionEach);return this.system(obj.name,obj.reqs,obj.action)};Pocket.prototype.cmpType=Pocket.prototype.componentType=function(name,initializer){this.componentTypes[name]=typeof initializer=="object"?initializer:{init:initializer,dalloc:function(){},malloc:function(){return{}}}};Pocket.prototype.key=function(componentsValues){var id=componentsValues.id?componentsValues.id:this.nextId();if(componentsValues.id&&this.keys[componentsValues.id]){console.warn("discarding component id "+componentsValues.id);id=this.nextId()}this.keys[id]=id;Object.keys(componentsValues).forEach(function(cmpName){this.addComponentTokey(id,cmpName,componentsValues[cmpName])},this);return id};Pocket.prototype.destroyKey=function(id){this.keysToDestroy[id]=true};Pocket.prototype.immediatelyDestroyKey=function(id){var self=this;var found=this.keys[id];if(!found){throw new Error("key with id "+id+" already destroyed")}delete this.keys[id];Object.keys(this.components).forEach(function(name){delete self.components[name][id]})};Pocket.prototype.addComponentTokey=function(id,componentName,opt_props){var key=this.keys[id];if(!key){throw new Error('Could not find key with id "'+id+'"')}var others=this.components[componentName]||(this.components[componentName]={});var cmp=others[id];if(!cmp){cmp=others[id]={};var componentDef=this.componentTypes[componentName];if(componentDef){componentDef.init(cmp,opt_props||{})}else if(!this.labels[componentName]){this.labels[componentName]=true;console.log("Found no component initializer for "+'"'+componentName+'"'+", assuming it is a label.")}}};Pocket.prototype.indexedData=function(name){return this.components[name]||{}};Pocket.prototype.firstData=function(name){var data=this.components[name]||{};return data[Object.keys(data)[0]]};Pocket.prototype.dataFor=function(id,name){return this.components[name][id]};Pocket.prototype.firstkey=function(name1,name2,nameN){for(var i=0,args=[];i<arguments.length;i++)args[i]=arguments[i];var keys=this.keysMatching.apply(this,args);return keys[0]};Pocket.prototype.keysMatching=function(name0,nameN){var matching=[];var table0=this.components[name0];if(!table0)return matching;var ids=Object.keys(table0);for(var i=0;i<ids.length;i++){var id=ids[i];var found=true;for(var j=1;j<arguments.length;j++){var name=arguments[j];var tableN=this.components[name];if(!tableN||!tableN[id]){found=false;break}}if(found){matching.push(this.keys[id])}}return matching};module.exports=Pocket},{"./system":2}],2:[function(require,module,exports){function System(name,requiredComponents,action){this.name=name;this.action=action;this.requiredComponents=requiredComponents}module.exports=System},{}],"pocket-ces":[function(require,module,exports){module.exports=require("./lib/pocket")},{"./lib/pocket":1}]},{},[]);var Pocket=require("pocket-ces");var arbit=require("arbit");var pkt=new Pocket;var random=arbit();var uiHTML=""+'<button onclick="handlePlantSeedClick()">Plant a seed</button>'+'<button onclick="handleTickClick()">Walk through the fields</button>'+'<button onclick="handleHarvestClick()">Harvest!</button>'+'<button onclick="handleNapClick()">Take a relaxing nap</button>'+'<div id="status-pane"></div>';document.body.innerHTML=uiHTML;pkt.cmpType("life",function(cmp,opts){cmp.age=cmp.age||1});pkt.cmpType("growth",function(cmp,opts){cmp.height=cmp.height||0});pkt.cmpType("bag",function(cmp,opts){cmp.seeds=cmp.seeds||1});pkt.systemForEach("plants-grow",["life","growth"],function(pkt,key,life,growth){growth.height+=Math.log(life.age);life.age+=1});pkt.systemForEach("plant-harvester",["life","growth"],function(pkt,key,life,growth){var shouldHarvest=pkt.keysMatching("cmd:should-harvest");if(!shouldHarvest.length)return;shouldHarvest.forEach(function(key){pkt.destroyKey(key)});var bag=pkt.firstData("bag");if(life.age>20){bag.seeds+=Math.floor(growth.height/life.age);pkt.destroyKey(key)}});pkt.system("system-printer",[],function(pkt){var pane=document.querySelector("#status-pane");var bag=pkt.firstData("bag");var lifes=pkt.indexedData("life");var growths=pkt.indexedData("growth");var keys=pkt.keysMatching("life","growth");if(!keys.length){pane.innerHTML="Your fields are empty."}else{pane.innerHTML=keys.map(function(key){return"<b>height:</b> "+growths[key].height+", <b>age:</b> "+lifes[key].age}).join("<br>")}pane.innerHTML+="<p>Your bag has "+bag.seeds+" seeds.</p>"});pkt.key({bag:null});window.handlePlantSeedClick=function(e){var bag=pkt.firstData("bag");if(bag.seeds>0){bag.seeds--;pkt.key({life:null,growth:null});pkt.tick(16)}};window.handleHarvestClick=function(e){var shouldHarvest=pkt.keysMatching("cmd:should-harvest");if(!shouldHarvest.length){pkt.key({"cmd:should-harvest":null});pkt.tick(16);pkt.tick(16)}};window.handleTickClick=function(e){pkt.tick(16)};window.handleNapClick=function(e){var count=random.nextInt(1,101);while(count--)pkt.tick(16)}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"arbit": "0.1.1", | |
"pocket-ces": "1.0.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> | |
<canvas id="cvs"></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment