UNPKG

meta-client

Version:
1 lines 8.97 kB
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();var _config=require('../../../config');var _config2=_interopRequireDefault(_config);var _log=require('../../Utilities/log');var _log2=_interopRequireDefault(_log);var _three=require('three');var _Space=require('../Space');var _Space2=_interopRequireDefault(_Space);var _Graphics=require('./Graphics/Graphics');var _Graphics2=_interopRequireDefault(_Graphics);var _Existence=require('./Existence/Existence');var _Existence2=_interopRequireDefault(_Existence);var _oimo=require('oimo');var OIMO=_interopRequireWildcard(_oimo);function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj}else{var newObj={};if(obj!=null){for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key))newObj[key]=obj[key]}}newObj.default=obj;return newObj}}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var name='Meta';var scope=void 0;var world=new OIMO.World;var bodies=[];var Meta=function(){function Meta(props){_classCallCheck(this,Meta);scope=this;this.name='Meta';this.lifes=[];this.events=[];this._entered=null;this._touched=false;this.rotation={x:0,y:0,z:0};this.position={x:0,y:0,z:0};this.existence=props!==undefined&&props.existence!==undefined?props.existence:new _Existence2.default;this.graphics=props!==undefined&&props.graphics!==undefined?props.graphics:new _Graphics2.default;this.physics=props!==undefined&&props.physics!==undefined?props.physics:true;this.setExistence();this.setPresence();return this}_createClass(Meta,[{key:'setExistence',value:function setExistence(){if(this.existence.start===null&&this.existence.end===null)_Space2.default.get().start(scope)}},{key:'setPresence',value:function setPresence(){_Space2.default.get().add(this)}},{key:'o',value:function o(eventName,callback){return this.on(eventName,callback)}},{key:'on',value:function on(eventName,callback){if(typeof eventName=='function'){callback=eventName;eventName='p'}if(eventName=='t')eventName='touch';if(eventName=='r')eventName='release';if(eventName=='p')eventName='point';if(this.events[eventName]){this.events[eventName].push(callback)}else{this.events[eventName]=[callback]}return this}},{key:'go',value:function go(eventName){var _this=this;for(var _len=arguments.length,rest=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){rest[_key-1]=arguments[_key]}if(this.events[eventName]){this.events[eventName].forEach(function(cb){cb.apply(undefined,rest.concat([_this]))})}return this}},{key:'m',value:function m(direction,amount){return this.move(direction,amount)}},{key:'move',value:function move(direction,amount){switch(direction){default:case 0:case'f':case'forward':case'forwards':this.setPosition({x:this.position.x,y:this.position.y,z:this.position.z+amount});break;case 1:case'b':case'backward':case'backwards':this.setPosition({x:this.position.x,y:this.position.y,z:this.position.z-amount});break;case 2:case'l':case'left':this.setPosition({x:this.position.x-amount,y:this.position.y,z:this.position.z});break;case 3:case'r':case'right':this.setPosition({x:this.position.x+amount,y:this.position.y,z:this.position.z});break;case 4:case'd':case'down':this.setPosition({x:this.position.x,y:this.position.y-amount,z:this.position.z});break;case 5:case'u':case'up':this.setPosition({x:this.position.x,y:this.position.y+amount,z:this.position.z});break;}return this}},{key:'s',value:function s(props){return this.set(props)}},{key:'set',value:function set(position){this.setPosition(position);return this}},{key:'setPosition',value:function setPosition(position){this.graphics.setPosition(position);this.position={x:this.graphics.mesh.position.x,y:this.graphics.mesh.position.y,z:this.graphics.mesh.position.z};(0,_log2.default)('[this.position]'+this.position);return this}},{key:'r',value:function r(direction,amount){return this.rotate(direction,amount)}},{key:'rotate',value:function rotate(direction,amount){switch(direction){default:case 0:case'f':case'forward':case'forwards':this.setRotation({x:this.rotation.x+amount,y:0,z:0});break;case 1:case'b':case'backward':case'backwards':this.setRotation({x:this.rotation.x-amount,y:0,z:0});break;case 2:case'l':case'left':this.setRotation({x:0,y:this.rotation.y-amount,z:0});break;case 3:case'r':case'right':this.setRotation({x:0,y:this.rotation.y+amount,z:0});break;case 4:case'd':case'down':this.setRotation({x:0,y:0,z:this.rotation.z+amount});break;case 5:case'u':case'up':this.setRotation({x:0,y:0,z:this.rotation.z-amount});break;}return this}},{key:'setRotation',value:function setRotation(rotation){this.graphics.setRotation(rotation);this.rotation={x:this.graphics.mesh.rotation.x,y:this.graphics.mesh.rotation.y,z:this.graphics.mesh.rotation.z};(0,_log2.default)(JSON.stringify(this.rotation),name);return this}},{key:'setSize',value:function setSize(){var box3=new _three.Box3().setFromObject(this.graphics.mesh);var x=box3.max.x-box3.min.x;var y=box3.max.y-box3.min.y;var z=box3.max.z-box3.min.z;switch(this.graphics.type){case'box':this.size=[x,y,z];break;case'sphere':this.size=[x/2];break;case'cylinder':this.size=[x/2,y,z/2];break;}}},{key:'p',value:function p(m){return this.setPhysics(m)}},{key:'setPhysics',value:function setPhysics(physics){this.setSize();if(this.body!==undefined&&physics===this.physics)return;physics=physics!==undefined?physics:this.physics;if(physics===null){this.physics=null;return this}var mode=physics;if(this.body!==undefined){this.body.remove()}var body={type:this.graphics.type,size:this.size,pos:[this.position.x,this.position.y,this.position.z],rot:[this.rotation.x,this.rotation.y,this.rotation.z],move:mode,density:mode!==undefined&&mode.density!==undefined?mode.density:1,friction:mode!==undefined&&mode.friction!==undefined?mode.friction:.5,restitution:mode!==undefined&&mode.restitution!==undefined?mode.restitution:.5,belongsTo:mode!==undefined&&mode.belongsTo!==undefined?mode.belongsTo:1,collidesWith:mode!==undefined&&mode.collidesWith!==undefined?mode.collidesWith:4294967295};this.body=world.add(body);this.physics=physics;return this}},{key:'l',value:function l(){var mode=arguments.length>0&&arguments[0]!==undefined?arguments[0]:'on';var life=arguments[1];return this.live(mode='on',life)}},{key:'live',value:function live(){var mode=arguments.length>0&&arguments[0]!==undefined?arguments[0]:'on';var life=arguments[1];switch(mode){case'on':case'o':case 1:this.lifes.push(life);break;case'again':case'a':case 0:this.lifes=[];this.lifes.push(life);break;}return this}},{key:'appear',value:function appear(){return this.end()}},{key:'start',value:function start(){_Space2.default.get().start(this);this.existence.started=true}},{key:'die',value:function die(){return this.end()}},{key:'disappear',value:function disappear(){return this.end()}},{key:'end',value:function end(){_Space2.default.get().end(this);this.existence.ended=true}},{key:'life',value:function life(){if(this.existence.start!==null)if(new Date(this.existence.start)<new Date){if(!this.existence.started)this.start()}if(this.existence.end!==null)if(new Date>new Date(this.existence.start)){if(new Date>new Date(this.existence.end)){if(!this.existence.ended)this.end()}}}},{key:'c',value:function c(color){return this.color(color)}},{key:'color',value:function color(){var _color=arguments.length>0&&arguments[0]!==undefined?arguments[0]:Math.random()*16777215;switch(_color){case 0:case'g':case'gold':_color=16766720;break;case 1:case'r':case'red':_color=16711680;break;case 2:case'g':case'green':_color=65280;break;case 3:case'b':case'blue':_color=255;break;case 4:case'y':case'yellow':_color=16776960;break;case 5:case'w':case'white':_color=16777215;break;}this.graphics.color(_color);this._color=_color;return this}},{key:'add',value:function add(instance){this.graphics=instance}},{key:'i',value:function i(ms,f){return this.in(ms,f)}},{key:'in',value:function _in(ms,f){setTimeout(function(){f()},ms)}},{key:'e',value:function e(ms,f){return this.every(ms,f)}},{key:'ev',value:function ev(ms,f){return this.every(ms,f)}},{key:'every',value:function every(ms,f){f();setInterval(function(){f()},ms)}}],[{key:'getWorld',value:function getWorld(){return world}},{key:'getBodies',value:function getBodies(){return bodies}},{key:'clear',value:function clear(){bodies=[];world=new OIMO.World}}]);return Meta}();exports.default=Meta;