-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatenea.min.js
1 lines (1 loc) · 6.97 KB
/
atenea.min.js
1
(function(){var Util = {StringToArray: function(words, sprt){(sprt == null) && (sprt=',');return words.replace(REXP_WORDS_SPACE, '').split(sprt);}};var KeyEventManager = function(){var EVENT_MAP = {u: 'UP', up:'UP',d: 'DOWN', down: 'DOWN',p: 'PRESSED', press: 'PRESSED',r: 'RELEASED', release: 'RELEASED'};var SPECIAL_MAP = {tab: 9,shift: 16,control: 17, ctrl: 17,alt: 18,escape: 27, esc: 27,space: 32,left: 37, arrow_left: 37,up: 38, arrow_up: 38,right: 39, arrow_right: 39,down: 40, arrow_down: 40};var self = this;var keys = {};var events = [];var keydown = function(evt){var key = evt.keyCode;events.push([key, 0]);};var keyup = function(evt){var key = evt.keyCode;events.push([key, 1]);};self.update = function(){copy = events.slice();events = [];for (var i=0; i<copy.length; i++){key = copy[i][0];evt = copy[i][1];if (keys[key] === undefined){keys[key] = 'UP';}if (evt == 0){ switch(keys[key]){case 'UP':keys[key] = 'DOWN';break;case 'DOWN':keys[key] = 'PRESSED';break;}} else { switch(keys[key]){case 'DOWN':case 'PRESSED':keys[key] = 'RELEASED';events.push([key, 1]);break;case 'RELEASED':keys[key] = 'UP';break;}}}};self.parseString = function(string){var result = [];if (string.indexOf('+') != -1){result = Util.StringToArray(string, '+');var limit = result.length;if (result[result.length-1].indexOf('-')){split = Util.StringToArray(result[result.length-1], '-');result[result.length-1] = keyCode(split[0]);result.push(EVENT_MAP[split[1]]);limit -= 1;}for (var i=0; i<limit; i++){result[i] = keyCode(result[i]);}}else if(string.indexOf('-') != -1){result = Util.StringToArray(string, '-');result.push( keyCode(result[0]) );result.push( EVENT_MAP[result[1]] );} else {result.push(keyCode(string));}return result;};var keyCode = function(key){return SPECIAL_MAP[key] || key.toUpperCase().charCodeAt(0);};self.key = function(key){typeof(key)=='string' && ( key=keyCode(key) );return keys[key];};window.addEventListener('keydown', keydown);window.addEventListener('keyup', keyup);};var Atenea = function(){REXP_MODEL_ENTITY = /^[A-Z][a-z]+([A-Z][a-z]+)*/;REXP_MODEL_SCENE = /^[a-z]+$/;REXP_MODEL_LOGIC = /^[A-Z]+$/;REXP_WORDS_SPACE = /\s+/g;var self = this;var activeScene = undefined;var scenes = {};var models = {};var keys = new KeyEventManager();var logics = {};self.init = function(w, h, domParent){canvas.init(w, h, domParent);window.requestAnimFrame = (function(){return window.requestAnimationFrame ||window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||function( callback ){window.setTimeout(callback, 1000 / 60);};})();};self.register = function(id, model){var newModel = {};extend(newModel, model);if(id.match(REXP_MODEL_ENTITY)){newModel.logic && parseLogic(newModelspar.logic);models[id] = newModel;}else if(id.match(REXP_MODEL_SCENE)){scenes[id] = newModel;}else if(id.match(REXP_MODEL_LOGIC)){parseLogic(newModel);logics[id] = newModel;}};self.parse = function(type, object){var e = {};object && (e=object);var models = Util.StringToArray(type);for (n in models){if (models[n].match(REXP_MODEL_ENTITY)){add(e, models[n]);}else if(models[n].match(REXP_MODEL_LOGIC)){self.logic(e, logics[models[n]], false);}}return e;};self.scene = function(id, scene){activeScene = scenes[id];};self.start = function(id){activeScene = scenes[id];animLoop();};self.logic = function(e, model, parse){(parse === undefined) && (parse=true);var attributes = ['sensor', 'controller', 'actuator'];e.logic || (e.logic = {});parse && parseLogic(model);for(var i=0; i<attributes.length; i++){var attr = attributes[i];model[attr] && (e.logic[attr] || (e.logic[attr]={}));extend(e.logic[attr], model[attr]);}};var parseLogic = function(logic){var controllers = logic.controller;var attributes = ['sensor', 'actuator'];for (var n in controllers){var ctrl = controllers[n];ctrl.operator = ctrl.operator.toUpperCase();for (var i=0; i<attributes.length; i++){var attr = attributes[i];(typeof(ctrl[attr]) == 'string') &&(ctrl[attr] = Util.StringToArray(ctrl[attr]));}}if ( logic.hasOwnProperty('sensor') ){for (var name in logic.sensor){logic.sensor[name] = keys.parseString(logic.sensor[name]);}}};var extend = function(dst, src){for (f in src){if( ! dst.hasOwnProperty(f) ){if ( src[f].constructor.name == 'Object' ){dst[f] = {};extend(dst[f], src[f]);}else {dst[f] = src[f];}}}};var add = function(e, model){if(model in models){var model_data = models[model];extend(e, model_data);model_data.logic && self.logic(e, model_data.logic, false);model_data.init && model_data.init.call(e);}};var draw = function(){var entities = activeScene.entities;canvas.context.fillStyle = 'white';canvas.context.fillRect(0, 0, canvas.size().width, canvas.size().height);for (var i=0; i<entities.length; i++){entities[i].draw.apply(entities[i], [canvas.context]);}};var processLogic = function(){for(var i=0; i<activeScene.entities.length; i++){var e = activeScene.entities[i];var all_sensors = getSensorState(e.logic.sensor);for (var name in e.logic.controller){var c = e.logic.controller[name];var sensors = [];var operator = c.operator;var output = false;for (var n in c.sensor){sensors.push(all_sensors[c.sensor[n]]);}if ( operator == 'AND'){output = sensors.indexOf(false) == -1;}else if(operator == 'OR'){output = sensors.indexOf(true) != -1;}else if(operator == 'NAND'){output = sensors.indexOf(true) == -1;}else if(operator == 'NOR'){output = sensors.indexOf(false) != -1;}else if (operator == 'XOR'){output = sensors.filter( function(v){return v;} ).length == 1;}else if(operator == 'XNOR'){output = sensors.filter( function(v){return !v;} ).length == 1;}if (output){for (var n in c.actuator){e.logic.actuator[c.actuator[n]].call(e);}}}}};var getSensorState = function(sensors){var sensor_status = {};key_sensor = getKeySensorState(sensors);extend(sensor_status, key_sensor);return sensor_status;};var getKeySensorState = function(sensors){key_sensor_status = {};for (var name in sensors){var sensor = sensors[name];if (sensor.length == 1){key_sensor_status[name] =['DOWN', 'PRESSED'].indexOf(keys.key(sensor[0])) != -1;} else {key_sensor_status[name] = true;var state = ['DOWN', 'PRESSED'];var limit = sensor.length;if (typeof sensor[sensor.length-1] == 'string'){state = [sensor[sensor.length-1]];limit -= 1;}for (var s=0; s<limit; s++){key_status = keys.key(sensor[s]);if(state.indexOf(key_status) == -1){key_sensor_status[name] = false;break;}}}}return key_sensor_status;};var update = function(){keys.update();processLogic();};var animLoop = function(){requestAnimFrame(animLoop);update();draw();};var canvas = new (function(){var self = this;self.init = function(w, h, domParent){self.domElement = document.createElement('canvas');self.context = self.domElement.getContext('2d');self.domElement.width = w;self.domElement.height = h;if(domParent !== undefined){domParent.appendChild(self.domElement);}else{document.body.appendChild(self.domElement);}};self.size = function(){if(arguments.length == 2){self.domElement.width = arguments[0];self.domElement.height = arguments[1];}else if (arguments.length == 0){return {width:self.domElement.width,height:self.domElement.height};}};})();};window.Atenea = new Atenea();})();