Files
metacar/dist/metacar.min.js

1 line
43 KiB
JavaScript

window.metacar=function(t){var e={};function i(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,i),o.l=!0,o.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)i.d(n,o,function(e){return t[e]}.bind(null,o));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e){t.exports=window.PIXI},function(t,e,i){"use strict";i.r(e);var n=i(0),o=(n.Application,n.loader),a=(n.loader.resources,n.Sprite),r=n.Graphics,s=n.Container,h="https://metacar-project.com/",c=h+"public/textures/textures.json",l=h+"public/images/",p=60,d={DEFAULT:"car.png",AGENT:"car_agent.png"},y={ROAD:-1,DEFAULT:0,CAR:1,AGENT:1},u={ROADS:{"↕":{image:"road_u.png",orientation:1.5},"↔":{image:"road_r.png",orientation:0},"↱":{image:"road_rotate_ld.png",orientation:1.25},"↰":{image:"road_rotate_ul.png",orientation:.75},"↲":{image:"road_rotate_rl.png",orientation:.25},"↳":{image:"road_rotate_dl.png",orientation:1.75},"↟":{image:"road_up.png",orientation:1.5},"↠":{image:"road_rp.png",orientation:0},"↤":{image:"road_r2.png",orientation:0},"↦":{image:"road_l2.png",orientation:1},"↥":{image:"road_d2.png",orientation:.5},"↧":{image:"road_u2.png",orientation:1.5}},house:{image:"house.png"},house2:{image:"house2.png"},house3:{image:"house3.png"},bench:{image:"bench.png"},tree:{image:"tree.png"}},x={level:{fullCity:{cars:[{mx:2,my:5,line:0,auto:!0},{mx:3,my:4,line:0,auto:!0},{mx:3,my:4,line:1,auto:!0},{mx:6,my:3,line:0,auto:!0},{mx:6,my:3,line:1,auto:!0},{mx:8,my:2,line:0,auto:!0},{mx:8,my:2,line:1,auto:!0},{mx:2,my:7,line:0,auto:!0}],house:[{x:9,y:409},{x:146,y:162},{x:439,y:39}],house2:[{x:425,y:192},{x:431,y:354}],house3:[{x:426,y:281},{x:51,y:157},{x:553,y:34}],bench:[{x:259,y:206},{x:8,y:205}],tree:[{x:1,y:303},{x:54,y:357},{x:215,y:325},{x:230,y:400},{x:282,y:368},{x:182,y:433},{x:285,y:309},{x:576,y:233},{x:581,y:300},{x:519,y:388},{x:582,y:391},{x:256,y:69},{x:151,y:43},{x:77,y:77},{x:26,y:24},{x:189,y:98}],map:[[0,0,0,0,0,0,"↕",0,0,0,0],[0,0,0,0,0,0,"↟",0,0,0,0],[0,0,0,0,0,0,"↦","↔","↔","↠","↔"],[0,0,0,0,0,0,"↕",0,0,0,0],["↔","↔","↧","↔","↠","↔","↤",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0],[0,0,"↟",0,0,0,"↟",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0]]},level1:{cars:[{mx:5,my:3,line:0,auto:!0},{mx:5,my:3,line:1,auto:!0},{mx:5,my:1,line:0,auto:!0}],tree:[{x:120,y:50},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:18.5,y:178},{x:305.5,y:125},{x:360.5,y:122}],asset:[{x:252.5,y:142},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:252.5,y:26},{x:195.5,y:0},{x:307.5,y:1},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:35.5,y:185},{x:18.5,y:178},{x:289.5,y:126},{x:337.5,y:126},{x:305.5,y:125},{x:360.5,y:122},{x:492.5,y:56},{x:503.5,y:136},{x:504.5,y:58}],bench:[{x:252.5,y:142},{x:252.5,y:26}],road:[{x:240,y:60},{x:240,y:60},{x:480,y:0},{x:480,y:60},{x:480,y:120},{x:480,y:180},{x:480,y:240},{x:300,y:180},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:360,y:60},{x:300,y:60},{x:420,y:120}],house3:[{x:503.5,y:136}],house:[{x:504.5,y:58}],map:[[0,0,0,0,0,0,0,0,0,0],[0,0,0,"↱","↠","↔","↔","↰",0,0],[0,0,0,"↕",0,0,0,"↟",0,0],[0,0,0,"↳","↔","↔","↔","↲",0,0],[0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT"]}}}},level0:{cars:[],house:[{x:263,y:108},{x:112,y:258}],house3:[{x:262,y:16}],bench:[{x:135,y:34}],tree:[{x:124,y:134},{x:7,y:11},{x:69,y:10},{x:19,y:238},{x:5,y:133},{x:260,y:211}],map:[[0,0,0,0,0],[0,"↱","↔","↰",0],[0,"↕",0,"↕",0],[0,"↳","↠","↲",0],[0,0,0,0,0]],agent:{mx:2,my:1,line:1,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level2:{cars:[],road:[{x:300,y:360},{x:300,y:360},{x:360,y:360},{x:420,y:360},{x:240,y:360},{x:180,y:360},{x:120,y:360},{x:480,y:360},{x:480,y:360},{x:120,y:360},{x:60,y:360},{x:300,y:300},{x:300,y:240},{x:480,y:360},{x:480,y:360},{x:540,y:360},{x:300,y:180},{x:360,y:180},{x:240,y:180},{x:240,y:180},{x:180,y:180},{x:420,y:180},{x:420,y:120},{x:180,y:120},{x:300,y:60},{x:420,y:60},{x:180,y:60},{x:240,y:60},{x:360,y:60},{x:480,y:60},{x:120,y:60},{x:540,y:60},{x:540,y:120},{x:540,y:180},{x:540,y:240},{x:540,y:300},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:240},{x:360,y:180},{x:480,y:360}],asset:[{x:162,y:264},{x:407,y:264},{x:259,y:126},{x:306,y:33},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:376,y:336},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:-46,y:290},{x:622,y:154},{x:617,y:280}],house:[{x:162,y:264},{x:622,y:154}],house3:[{x:407,y:264},{x:-46,y:290}],tree:[{x:259,y:126},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:617,y:280}],bench:[{x:306,y:33},{x:376,y:336}],map:[[0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↧","↔","↔","↔","↧","↔","↰",0],[0,"↕",0,"↕",0,0,0,"↕",0,"↕",0],[0,"↕",0,"↳","↔","↧","↠","↲",0,"↕",0],[0,"↟",0,0,0,"↕",0,0,0,"↕",0],[0,"↕",0,0,0,"↕",0,0,0,"↕",0],[0,"↳","↔","↔","↔","↥","↔","↔","↠","↲",0],[0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level3:{cars:[{mx:1,my:3,line:0},{mx:1,my:8,line:0},{mx:4,my:8,line:0},{mx:7,my:7,line:1},{mx:9,my:6,line:0},{mx:11,my:4,line:1},{mx:9,my:3,line:1},{mx:7,my:2,line:1},{mx:5,my:1,line:1},{mx:2,my:1,line:1},{mx:6,my:8,line:0},{mx:1,my:6,line:0},{mx:5,my:5,line:1},{mx:4,my:3,line:1},{mx:3,my:4,line:0},{mx:4,my:6,line:0}],road:[{x:120,y:60},{x:180,y:60},{x:240,y:60},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:360},{x:60,y:420},{x:60,y:480},{x:120,y:480},{x:180,y:480},{x:240,y:480},{x:300,y:60},{x:300,y:120},{x:300,y:180},{x:300,y:480},{x:300,y:420},{x:300,y:360},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:240,y:240},{x:300,y:60},{x:360,y:60},{x:240,y:180},{x:300,y:180},{x:300,y:240},{x:300,y:300},{x:240,y:300},{x:180,y:180},{x:180,y:240},{x:180,y:300},{x:360,y:60},{x:420,y:60},{x:420,y:60},{x:420,y:120},{x:420,y:180},{x:420,y:180},{x:480,y:180},{x:540,y:180},{x:660,y:180},{x:600,y:180},{x:660,y:240},{x:660,y:300},{x:660,y:360},{x:600,y:360},{x:540,y:360},{x:480,y:360},{x:420,y:360},{x:300,y:480},{x:360,y:480},{x:360,y:420},{x:360,y:360},{x:420,y:360},{x:420,y:420},{x:420,y:480},{x:360,y:480},{x:180,y:360},{x:240,y:360},{x:300,y:360},{x:300,y:300},{x:300,y:240},{x:180,y:180},{x:240,y:180},{x:300,y:180},{x:180,y:240},{x:180,y:300},{x:240,y:180},{x:180,y:60},{x:60,y:300},{x:300,y:480},{x:240,y:480},{x:600,y:180},{x:540,y:180},{x:660,y:300},{x:480,y:360}],asset:[{x:515,y:262},{x:378,y:254},{x:510,y:89},{x:626,y:86},{x:518,y:445},{x:631,y:436},{x:711,y:434},{x:243,y:295},{x:243,y:246},{x:253,y:153},{x:16,y:521},{x:129,y:214},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:63,y:539},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359},{x:742,y:181},{x:752,y:12}],house3:[{x:515,y:262},{x:626,y:86}],house2:[{x:378,y:254},{x:631,y:436},{x:711,y:434},{x:752,y:12}],house:[{x:510,y:89},{x:518,y:445},{x:742,y:181}],tree:[{x:243,y:295},{x:243,y:246},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359}],bench:[{x:253,y:153}],map:[[0,0,0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↠","↔","↔","↔","↰",0,0,0,0,0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↕",0,"↱","↠","↰",0,"↳","↔","↠","↔","↰",0],[0,"↕",0,"↕",0,"↕",0,0,0,0,0,"↕",0],[0,"↟",0,"↕",0,"↕",0,0,0,0,0,"↟",0],[0,"↕",0,"↳","↔","↲",0,"↱","↠","↔","↔","↲",0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↳","↔","↔","↔","↠","↔","↲",0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:3,my:8,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}}}};function v(t,e){var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",t,!0),i.onreadystatechange=function(){4==i.readyState&&200==i.status&&e(JSON.parse(i.responseText))},i.send(null)}function m(t,e){var i=t.split("//");i=i[1].split("/"),e(x[i[0]][i[1]])}function f(t,e){({"embedded:":m,"http:":v,"https:":v})[t.split("//")[0]](t,e)}function g(t,e){var i=document.createElement("a");document.body.appendChild(i);var n=new Blob([t],{type:"application/octet-binary"}),o=window.URL.createObjectURL(n);i.href=o,i.download=e,i.click(),window.URL.revokeObjectURL(o),i.href=""}function _(t){var e={};return e.code=t,e.isDown=!1,e.isUp=!0,e.press=void 0,e.release=void 0,e.downHandler=function(t){t.keyCode===e.code&&(e.isUp&&e.press&&e.press(),e.isDown=!0,e.isUp=!1),t.preventDefault()},e.upHandler=function(t){t.keyCode===e.code&&(e.isDown&&e.release&&e.release(),e.isDown=!1,e.isUp=!0),t.preventDefault()},window.addEventListener("keydown",e.downHandler.bind(e),!1),window.addEventListener("keyup",e.upHandler.bind(e),!1),e}var I,C=0,w=function(){function t(t,e,i,n){void 0===n&&(n={});var o=this;this.level=t,n.image=n.image||d.DEFAULT,n.lidar=n.lidar||!1,n.lidarInfo=n.lidarInfo||{pts:5,width:4,height:5,pos:0},this.core=new a(i[n.image]),this.info=e,this.core.scale.x=.8,this.core.scale.y=.8,this.core.anchor.x=.5,this.core.anchor.y=.5,this.core.road=void 0,this.core.carId=C,C+=1,this.core.agent=!1,this.core.obstacle=!0,this.core.mapId=y.CAR,n.lidar&&(this.createLidar(n.lidarInfo),this.core.lidar=this.lidar),n.motionEngine?(this.motion=n.motionEngine,this.motion.setUp(this.core,this.lidar),this.core.rotationStep=.5):this.core.rotationStep=.5,this.core.mx=this.info.mx,this.core.my=this.info.my,this.core.x=this.info.mx*p,this.core.y=this.info.my*p,this.core.checkAndsetNewRoad=function(t){return o.checkAndsetNewRoad(t)};var r=this.level.getRoad(this.core.my,this.core.mx);this.core.line=0,r&&r.setCarPosition(this.core,this.info.line)}return t.prototype.checkAndsetNewRoad=function(t){var e=this.core.road;t||(t=this.level.getRoad(this.core.my,this.core.mx)),t!=e&&(e&&e.cars.splice(e.cars.indexOf(this.core.carId),1),this.core.road=t,this.core.haveTurned=!1,this.core.optionalTurn=!1,this.core.turnedRandom=void 0,this.core.next_road=!1,t&&t.cars.push(this.core.carId))},t.prototype.reset=function(){this.core.mx=this.info.mx,this.core.my=this.info.my;var t=this.level.getRoad(this.core.my,this.core.mx);t&&t.setCarPosition(this.core,this.info.line,!0)},t.prototype.getState=function(){var t={},e=[];return this.motion.state.map(function(t){e=e.concat(t)}),e.push(this.core.v),t.linear=e,t.lidar=this.motion.state.map(function(t){return t.slice()}),t.v=this.core.v,t.a=this.core.last_a,t.steering=this.core.last_yaw_rate,t},t.prototype.step=function(t,e){if(void 0===e&&(e=null),null==e)var i=this.motion.step(t),n=i.agentCollisions,o=i.onRoad;else{var a=this.motion.actionStep(t,e);n=a.agentCollisions,o=a.onRoad}return{agentCollisions:n,onRoad:o}},t.prototype.createLidar=function(t){t.pts=t.pts||5,t.width=t.width||4,t.height=t.height||5,t.pos=t.pos||0,this.lidar=new s,this.lidar.lidarPts=[],this.lidar.collisionPts=[];var e=new r;e.pt=!1,e.alpha=.3,e.beginFill(5329233),e.drawRect(0,0,this.core.width*t.width,this.core.height*t.height),e.endFill();var i=new r;i.pt=!1,i.alpha=0,i.beginFill(5308416),i.drawRect(this.core.width-5,e.height/2,3,3),i.endFill();var n=new r;n.pt=!1,n.alpha=0,n.beginFill(5308416),n.drawRect(-1,e.height/2-2,3,3),n.endFill();var o=new r;o.pt=!1,o.alpha=0,o.beginFill(5308416),o.drawRect(this.core.width/2,e.height/2-this.core.height/2+5,3,3),o.endFill();var a=new r;a.pt=!1,a.alpha=0,a.beginFill(5308416),a.drawRect(this.core.width/2,e.height/2+this.core.height/2-8,3,3),a.endFill(),n.x-=t.pos*this.core.width,i.x-=t.pos*this.core.width,a.x-=t.pos*this.core.width,o.x-=t.pos*this.core.width;for(var h=e.width/t.pts,c=e.height/t.pts,l=t.pts-1;l>=0;l--)for(var p=0;p<t.pts;p++){var d=h/4+l*h,y=c/4+p*c,u=new r;u.pt=!0,u.beginFill(16777215),u.drawRect(d,y,5,5),u.endFill(),this.lidar.addChild(u),this.lidar.lidarPts.push(u)}this.lidar.pts=t.pts,this.lidar.addChild(e),this.lidar.addChild(i),this.lidar.addChild(n),this.lidar.addChild(o),this.lidar.addChild(a),this.lidar.collisionPts.push(i),this.lidar.collisionPts.push(n),this.lidar.collisionPts.push(o),this.lidar.collisionPts.push(a),this.lidar.x=this.core.x,this.lidar.y=this.core.y,this.lidar.pivot.y=this.lidar.height/2,this.lidar.pivot.x=this.core.width/2-t.pos*this.core.width,this.lidar.rotation=this.core.rotation},t}(),M=function(){function t(t){this.level=t}return t.prototype.boxesIntersect=function(t,e){var i=t.getBounds(),n=e.getBounds();return i.x+i.width>n.x&&i.x<n.x+n.width&&i.y+i.height>n.y&&i.y<n.y+n.height},t.prototype.carIntersect=function(t,e){for(var i=t.lidar.collisionPts.length,n=0;n<i;n++)if(this.boxesIntersect(t.lidar.collisionPts[n],e))return!0;return!1},t.prototype.carCaptorInObject=function(t,e){for(var i=t.lidar.collisionPts.length,n=0,o=0;o<i;o++)this.boxesIntersect(t.lidar.collisionPts[o],e)&&(n+=1);return n},t.prototype.detectInteractions=function(t,e){void 0===t&&(t=!0),void 0===e&&(e=!1);for(var i=this.level.getEnvs(),n=[],o=[],a=0,r=!1,s=0;s<i.length;s++)i[s]!=this.car&&(Math.abs(i[s].mx-this.car.mx)<2&&Math.abs(i[s].my-this.car.my)<2||e)&&(t&&i[s].obstacle&&this.carIntersect(this.car,i[s])?n.push(i[s]):t&&i[s].mapId==y.ROAD&&(a+=this.carCaptorInObject(this.car,i[s])),this.boxesIntersect(i[s],this.lidar)&&o.push(i[s]));return this.setState(o),(a>=4||!t)&&(r=!0),{agentCollisions:n,onRoad:r}},t.prototype.setState=function(t){for(var e=0,i=0;i<this.lidar.lidarPts.length;i++){this.lidar.lidarPts[i].alpha=.3;var n=Math.floor(e/this.lidar.pts),o=Math.floor(e%this.lidar.pts);this.state[n][o]=y.DEFAULT;for(var a=0;a<t.length;a++)if(t[a]!=this.car){var r=this.boxesIntersect(t[a],this.lidar.lidarPts[i]);r&&t[a].obstacle&&(this.lidar.lidarPts[i].alpha=1),r&&(t[a].mapId>this.state[n][o]||t[a].mapId==y.ROAD&&this.state[n][o]==y.DEFAULT)&&(this.state[n][o]=t[a].mapId)}e+=1}},t}(),b=(I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])},function(t,e){function i(){this.constructor=t}I(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}),R=function(t){function e(e,i){var n=t.call(this,e)||this;return n.maxSpeed=1,n.rotationStep=i.rotationStep||.5,n.actions=i.actions||["UP","LEFT","RIGHT","DOWN","WAIT"],n}return b(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i<e.pts;i++){for(var n=[],o=0;o<e.pts;o++)n.push(y.DEFAULT);this.state.push(n)}this.setUpKeyboard(),this.car.v=0,this.car.a=0,this.car.yaw_rate=0,this.detectInteractions()},e.prototype.setUpKeyboard=function(){var t=this,e=_(37),i=_(38),n=_(39),o=_(40);-1!=this.actions.indexOf("LEFT")&&(e.press=function(){t.turnLeft()}),-1!=this.actions.indexOf("RIGHT")&&(n.press=function(){t.turnRight()}),-1!=this.actions.indexOf("UP")&&(i.press=function(){t.moveForward()},i.release=function(){t.car.v=0}),-1!=this.actions.indexOf("DOWN")&&(o.press=function(){t.moveBackward()},o.release=function(){t.car.v=0})},e.prototype.turnLeft=function(){this.car.rotation-=this.rotationStep*Math.PI,this.lidar.rotation=this.car.rotation},e.prototype.turnRight=function(){this.car.rotation+=this.rotationStep*Math.PI,this.lidar.rotation=this.car.rotation},e.prototype.moveForward=function(){this.car.v=1},e.prototype.moveBackward=function(){this.car.v=-1},e.prototype.actionStep=function(t,e){"LEFT"==this.actions[e]&&this.turnLeft(),"RIGHT"==this.actions[e]&&this.turnRight(),"UP"==this.actions[e]&&this.moveForward(),"DOWN"==this.actions[e]&&this.moveBackward();var i=this.step(t),n=i.agentCollisions,o=i.onRoad;return this.car.v=0,{agentCollisions:n,onRoad:o}},e.prototype.actionSpace=function(){return{type:"Discrete",size:1,range:[0,1,2,3,4]}},e.prototype.step=function(t){this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t,this.car.mx=Math.floor(this.car.x/p),this.car.my=Math.floor(this.car.y/p),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var e=this.detectInteractions(!0,!0),i=e.agentCollisions,n=e.onRoad;return i.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:i,onRoad:n}},e}(M),E=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),S=function(t){function e(e){var i=t.call(this,e)||this;return i.map=i.level.getMap(),i.mapSizeY=i.map.length,i.mapSizeX=i.map[0].length,i.rotationStep=.5,i.rotationToNextCase={},i.rotationToNextCase[0]={mx:1,my:0},i.rotationToNextCase[.5]={mx:0,my:1},i.rotationToNextCase[1]={mx:-1,my:0},i.rotationToNextCase[1.5]={mx:0,my:-1},i.rotationToNextCase[-.5]={mx:0,my:-1},i.rotationToNextCase[-1]={mx:-1,my:0},i.rotationToNextCase[-1.5]={mx:0,my:1},i}return E(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i<e.pts;i++){for(var n=[],o=0;o<e.pts;o++)n.push(y.DEFAULT);this.state.push(n)}this.car.v=0,this.detectInteractions()},e.prototype.turnLeft=function(){this.car.rotation-=this.rotationStep*Math.PI,this.lidar.rotation=this.car.rotation},e.prototype.turnRight=function(){this.car.rotation+=this.rotationStep*Math.PI,this.lidar.rotation=this.car.rotation},e.prototype.moveUp=function(){this.car.v=1},e.prototype.moveDown=function(){this.car.v=-1},e.prototype.isRoad=function(t,e,i){if(void 0===i&&(i=!1),e<0||e>=this.mapSizeY||t<0||t>=this.mapSizeX||0==this.map[e][t])return!1;if(i&&this.level.getRoad(e,t).cars.length>0)return!1;return!0},e.prototype.autoRotation=function(){var t=p+p/3,e=p-p/3.5,i=p;if(this.car.next_road){var n=Math.abs((this.car.next_road.outroad.y+p/2-this.car.y)*this.car.next_road.outroad_np.my),o=Math.abs((this.car.next_road.outroad.x+p/2-this.car.x)*this.car.next_road.outroad_np.mx);if(0==this.car.next_road.rotation_type&&n<t&&o<t)return this.car.haveTurned=!0,this.car.previousRotation=this.car.rotation,this.car.rotation=this.car.rotation+=Math.PI/2,this.car.rotation=this.car.rotation%(2*Math.PI),void(this.car.next_road=void 0);if(1==this.car.next_road.rotation_type&&n<e&&o<e)return this.car.previousRotation=this.car.rotation,this.car.haveTurned=!0,this.car.rotation=this.car.rotation-=Math.PI/2,this.car.rotation=this.car.rotation%(2*Math.PI),void(this.car.next_road=void 0);if(2==this.car.next_road.rotation_type&&n<i&&o<i)return this.car.haveTurned=!0,this.car.previousRotation=this.car.rotation,this.car.rotation=this.car.rotation-=Math.PI,this.car.rotation=this.car.rotation%(2*Math.PI),0!=n&&this.car.x<this.car.next_road.outroad.x+p/2?(this.car.x=this.car.next_road.outroad.x+p/2+p/4,this.car.y=this.car.next_road.outroad.y):0!=n&&this.car.x>this.car.next_road.outroad.x+p/2?(this.car.x=this.car.next_road.outroad.x+p-p/2-p/4,this.car.y=this.car.next_road.outroad.y):0!=o&&this.car.y<this.car.next_road.outroad.y+p/2?(this.car.y=this.car.next_road.outroad.y+p/2+p/4,this.car.x=this.car.next_road.outroad.x):0!=o&&this.car.y>this.car.next_road.outroad.y+p/2&&(this.car.y=this.car.next_road.outroad.y+p-p/2-p/4,this.car.x=this.car.next_road.outroad.x),void(this.car.next_road=void 0)}else{var a=this.car.rotation/Math.PI,r=(this.car.rotation/Math.PI+.5)%2,s=(this.car.rotation/Math.PI-.5)%2,h=this.rotationToNextCase[a],c=this.rotationToNextCase[r],l=this.rotationToNextCase[s],d=!1,y=this.car.mx+h.mx,u=this.car.my+h.my;if(this.isRoad(y,u)?this.car.haveTurned||this.car.turnedRandom?!this.car.haveTurned&&this.car.turnedRandom<.5&&this.isRoad(this.car.mx+c.mx,this.car.my+c.my)?(d=!0,this.car.optionalTurn=!0):!this.car.haveTurned&&this.car.turnedRandom>=.5&&this.isRoad(this.car.mx+l.mx,this.car.my+l.my)&&(d=!0,this.car.optionalTurn=!0):this.car.turnedRandom=Math.random():(d=!0,this.car.optionalTurn=!1),d)for(var x=[(a+.5)%2,(a-.5)%2,null],v=0;v<x.length;v++){var m=void 0;m=null!=x[v]?this.rotationToNextCase[x[v]]:{mx:0,my:0};var f=this.car.mx+m.mx,g=this.car.my+m.my;if(this.isRoad(f,g))return void(this.car.next_road={outroad:{y:u*p,x:y*p},outroad_np:h,rotation_type:v})}}},e.prototype.step=function(t){-1!=this.state.toString().indexOf(","+y.CAR.toString())?(this.car.v=0,0==this.car.v&&this.car.haveTurned&&1==this.car.optionalTurn&&(this.car.rotation=this.car.previousRotation)):(this.car.v=.8,this.autoRotation()),this.level.isCarsMoving&&(this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t),this.car.mx=Math.floor(this.car.x/p),this.car.my=Math.floor(this.car.y/p),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation,this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var e=this.detectInteractions(!1);return{agentCollisions:e.agentCollisions,onRoad:e.onRoad}},e}(M),L=function(){function t(t){this.assets=[],this.agentMotionEngine=R,this.agentMotionOptions={},this.agentLidarInfo={},this.level=t}return t.prototype.setAgentLidar=function(t){this.agentLidarInfo=t},t.prototype.setAgentMotion=function(t,e){this.agentMotionEngine=t,this.agentMotionOptions=e},t.prototype.createRoadSide=function(t){var e=t.mx*p,i=t.my*p,n=new r;n.beginFill(15263976),n.drawRoundedRect(0,0,p+30,p+30,void 0),n.x=e-15,n.y=i-15,n.endFill(),this.level.addChild(n)},t.prototype.createRoad=function(t,e,i){var n=this,o=new a(e[u.ROADS[t.type].image]);o.arrow=i,o.x=t.mx*p,o.y=t.my*p,o.obstacle=!1,o.mapId=y.ROAD,o.orientation=u.ROADS[t.type].orientation,o.setCarPosition=function(t,e,i){return void 0===i&&(i=!1),n.setCarOnRoad(o,t,e,i)},o.cars=[],o.mx=t.mx,o.my=t.my,this.level.addRoad(o)},t.prototype.setCarOnRoad=function(t,e,i,n){void 0===n&&(n=!1),null==i&&(i=Math.min(1,Math.floor(2*Math.random())));var o=this.level.findCarById(t.cars[0]);!n&&t.cars.length>=1&&o&&o.core.line==i&&(i=0==i?1:0),e.line=i,e.a=0,e.v=0,e.x=t.x,e.y=t.y,e.x+=p/2,e.y+=p/2;var a=1*p/4;t.orientation=Math.floor(t.orientation/e.rotationStep)*e.rotationStep;var r=-t.orientation*Math.PI,s=0*Math.cos(r)-a*Math.sin(r),h=Math.cos(r)*a+0*Math.sin(r),c=0==i?1:-1,l=0==i?0:Math.PI;e.x+=c*Math.round(s),e.y+=c*Math.round(h),e.mx=Math.floor(e.x/p),e.my=Math.floor(e.y/p),e.rotation=r+l,e.checkAndsetNewRoad(t)},t.prototype.createAsset=function(t,e,i,n){var o=new a(i[t]);o.obstacle=!1,o.type=n,o.mapId=0,o.x=e.x,o.y=e.y,this.level.addChild(o),this.assets.push(o)},t.prototype.createMap=function(t,e,i,n){if(void 0===n&&(n=!0),n)for(var o=0;o<t.length;o++)for(var a=0;a<t[o].length;a++)u.ROADS[t[o][a]]&&this.createRoadSide({mx:a,my:o,type:t[o][a]});for(o=0;o<t.length;o++)for(a=0;a<t[o].length;a++)u.ROADS[t[o][a]]&&this.createRoad({mx:a,my:o,type:t[o][a]},i,t[o][a]);for(var r in u)if("ROADS"!=r&&e[r])for(var s=0;s<e[r].length;s++)this.createAsset(u[r].image,e[r][s],i,r)},t.prototype.createCars=function(t,e){for(var i in t.cars){var n={lidar:!0,lidarInfo:{pts:2,width:.5,height:1,pos:1}};n.lidar=!0,n.motionEngine=new S(this.level);var o=new w(this.level,t.cars[i],e,n);this.level.addCar(o)}},t.prototype.createAgent=function(t,e,i){void 0===i&&(i=!0);var n=new w(this.level,t.agent,e,{image:d.AGENT,lidar:!0,lidarInfo:this.agentLidarInfo,motionEngine:new this.agentMotionEngine(this.level,this.agentMotionOptions)});return i&&this.level.addChild(n.lidar),this.level.addCar(n),n.core.agent=!0,n},t.prototype.addAsset=function(t){this.assets.push(t)},t.prototype.exportMap=function(t,e,i,n){for(var o={cars:[]},a=[],r=this.level.getEnvs(),s=0;s<e;s++){for(var h=[],c=0;c<t;c++)h.push(0);a.push(h)}for(var l=0;l<r.length;l++){(p=r[l]).isremove||(p.mapId==y.CAR&&!p.agent&&p.my<e&&p.mx<t?o.cars.push({mx:p.mx,my:p.my,line:p.line}):p.mapId==y.ROAD&&p.my<e&&p.mx<t&&(a[p.my][p.mx]=p.arrow))}for(l=0;l<this.assets.length;l++){var p;(p=this.assets[l]).isremove||"car"!=p.type&&"agent"!=p.type&&(o[p.type]||(o[p.type]=[]),o[p.type].push({x:p.x,y:p.y}))}o.map=a,this.level.agent&&(o.agent={mx:this.level.agent.core.mx,my:this.level.agent.core.my,line:this.level.agent.core.line,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}});var d=JSON.stringify(o,null,4);return n&&g(d,i),o},t}(),T=function(){function t(t,e){this.agent=null,this.app=null,this.map=null,this.info=null,this.envs=[],this.roads={},this.cars=[],this.steping=!0,this.info=t,this.canvasId=e}return t.prototype.load=function(t){var e=this;return this.loop=t,new Promise(function(t){e.createLevel(e.info).then(function(){return t()})})},t.prototype.createLevel=function(t){var e=this;return this.map=t.map,this.app=new PIXI.Application({width:this.map[0].length*p,height:this.map.length*p,backgroundColor:8437566}),document.getElementById(this.canvasId).appendChild(this.app.view),new Promise(function(i){o.add([c,"https://metacar-project.com/public/textures/textures.png"],{crossOrigin:!0}).load(function(){e._setup(t),i()})})},t.prototype._setup=function(t){console.warn("This method should be implemented.",t)},t.prototype.getRoads=function(){return this.roads},t.prototype.findCarById=function(t){return this.cars.find(function(e){return e.core.carId==t})},t.prototype.getRoad=function(t,e){return this.roads[[t.toString(),e.toString()].toString()]},t.prototype.addRoad=function(t){this.roads[[t.my.toString(),t.mx.toString()].toString()]=t,this.envs.push(t),this.app.stage.addChild(t)},t.prototype.addCar=function(t){this.cars.push(t),this.app.stage.addChild(t.core),this.envs.push(t.core)},t.prototype.getEnvs=function(){return this.envs},t.prototype.getMap=function(){return this.map},t.prototype.addChild=function(t){this.app.stage.addChild(t)},t.prototype.render=function(t){t?(this.app.ticker.start(),this.steping=!0):(this.app.ticker.stop(),this.steping=!1)},t.prototype.reset=function(){},t.prototype.resize=function(t,e){this.app.renderer.resize(t*p,e*p)},t.prototype.getMousePosition=function(){return this.app.renderer.plugins.interaction.mouse.global},t.prototype.setSteping=function(t){this.steping=t},t.prototype.shuffleCarsPositions=function(){for(var t=0;t<this.cars.length;t++){var e=this.roads,i=Object.keys(e);for(var n in i.sort(function(){return Math.random()-.5}),i){var o=e[i[n]];if(0==o.cars.length){o.setCarPosition(this.cars[t].core);break}}}},t}(),A=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),O=function(t){function e(e,i){var n=t.call(this,e,i)||this;return n.isCarsMoving=!0,n.lastReward=0,n.rewardFunction=null,n.map=n.info.map,n.am=new L(n),n}return A(e,t),e.prototype.setRewardFunction=function(t){this.rewardFunction=t},e.prototype.carsMoving=function(t){this.isCarsMoving=t},e.prototype.setAgentMotion=function(t,e){this.am.setAgentMotion(t,e)},e.prototype.setAgentLidar=function(t){this.am.setAgentLidar(t)},e.prototype._setup=function(t){var e=this,i=o.resources[c].textures;this.am.createMap(this.map,t,i),this.am.createCars(t,i),t.agent&&(this.agent=this.am.createAgent(t,i)),this.app.ticker.add(function(t){return e.loop(t)})},e.prototype.reset=function(){this.agent.reset();for(var t=0;t<this.cars.length;t++)this.cars[t].reset()},e.prototype.setReward=function(t,e,i){var n;return n=isNaN(i)||null==i?0+Math.max(0,this.agent.core.v)/this.agent.motion.maxSpeed:0==i?1:0,t.length>0?n=-1:e||(n=-1),n},e.prototype.getLastReward=function(){return this.lastReward},e.prototype.step=function(t,e,i){if(void 0===e&&(e=null),void 0===i&&(i=!0),!i||this.steping){for(var n=0;n<this.cars.length;n++)this.cars[n].lidar&&!this.cars[n].core.agent&&this.cars[n].step(t);if(this.agent){var o=this.agent.step(t,e),a=o.agentCollisions,r=o.onRoad,s=void 0;return s=this.rewardFunction?this.rewardFunction(a,r,e):this.setReward(a,r,e),this.lastReward=s,s}return 0}},e.prototype.stopRender=function(){this.app.ticker.stop()},e}(T),P=function(){function t(t,e){this.level=t,this.canvasId=e;var i=document.getElementById(e),n=document.createElement("div");n.classList.add("metacar_buttons_container"),n.id="metacar_"+e+"_buttons_container",i.parentNode.insertBefore(n,i.nextSibling),this.buttonsContainer=n}return t.prototype.isPlaying=function(){return this.playing},t.prototype.getEditorSize=function(){return{width:this.editorWidth,height:this.editorHeight}},t.prototype._createButton=function(t,e){var i=document.createElement("button");return i.classList.add("metacar_button_train"),i.id="metacar_"+this.canvasId+"_button_"+e,e=e.replace(/_/g," "),i.innerHTML=e.charAt(0).toUpperCase()+e.slice(1),t.appendChild(i),i},t.prototype.onTrain=function(t){var e=this;this._createButton(this.buttonsContainer,"train").addEventListener("click",function(){e.level.render(!1),e.level.setSteping(!1),t&&t()})},t.prototype.onPlay=function(t){var e=this;this._createButton(this.buttonsContainer,"play").addEventListener("click",function(){t&&(e.playing=!0,e.playCallback=t)})},t.prototype.onStop=function(t){var e=this;this._createButton(this.buttonsContainer,"stop").addEventListener("click",function(){e.playing=!1,e.playCallback=void 0,t&&t()})},t.prototype.onResetEnv=function(t){var e=this;this._createButton(this.buttonsContainer,"reset_env").addEventListener("click",function(){e.level.reset(),t&&t()})},t.prototype.onCustomEvent=function(t,e){this._createButton(this.buttonsContainer,t).addEventListener("click",function(){e&&e()})},t.prototype.onSaveEditor=function(t,e){var i=this;this._createButton(this.buttonsContainer,"save").addEventListener("click",function(){var n=i.getEditorSize(),o=i.level.exportMap(n.width,n.height,e.name,e.download);t&&t(o)})},t.prototype.onLoad=function(t,e){void 0===e&&(e=Object()),e.local=e.local||!1;var i=this._createButton(this.buttonsContainer,"load_trained_agent"),n=document.createElement("input");n.type="file",n.accept="*/*",n.style.display="none",n.classList.add("metacar_button_input_file"),n.id="metacar_"+this.canvasId+"_button_input_file",this.buttonsContainer.appendChild(n),n.addEventListener("change",function(e){!function(t,e){var i=t.target.files[0],n=new FileReader;n.readAsText(i,"UTF-8"),n.onload=function(t){e(t.target.result)}}(e,function(e){t&&t(e)})}),i.addEventListener("click",function(){e.local?n.click():t&&t()})},t.prototype._createRangeBar=function(t){var e=document.createElement("input");return e.type="range",e.min="5",e.max="15",e.value="5",e.classList.add("metacar_"+t+"_input"),e.id="metacar_"+this.canvasId+"_"+t+"_input",e},t.prototype._createSpan=function(t,e){var i=document.createElement("span");return i.classList.add("metacar_"+t+"_span"),i.id="metacar_"+this.canvasId+"_"+t+"_span",i.innerHTML=e,i},t.prototype._createImage=function(t,e){var i=document.createElement("img");return i.classList.add("metacar_img"),i.id="metacar_"+this.canvasId+"_"+t+"_img",i.src=e,i},t.prototype._listenRangeChanges=function(t,e){var i=this;t.onchange=function(){var n=parseInt(t.value),o=parseInt(e.value);i.level.resize(n,o),i.editorWidth=n,i.editorHeight=o},e.onchange=function(){var n=parseInt(t.value),o=parseInt(e.value);i.level.resize(n,o),i.editorWidth=n,i.editorHeight=o}},t.prototype._listenCanvasEditor=function(){var t=this;document.getElementById(this.canvasId).addEventListener("contextmenu",function(t){return t.preventDefault()}),document.getElementById(this.canvasId).addEventListener("click",function(){t.level.getSelectedItems()&&t.level.setCurrentItem()})},t.prototype._listenEditorImgs=function(){for(var t=document.getElementsByClassName("metacar_img"),e=this,i=0;i<t.length;i++)t[i].addEventListener("click",function(){e.level.selectedItem({image:this.getAttribute("data-img"),type:this.getAttribute("data-type"),data:this.getAttribute("data-data")})},!1)},t.prototype.createEditorElementsEvents=function(){var t=this._createSpan("width","Width"),e=this._createRangeBar("width"),i=this._createSpan("height","Height"),n=this._createRangeBar("height");this.buttonsContainer.appendChild(t),this.buttonsContainer.appendChild(e),this.buttonsContainer.appendChild(i),this.buttonsContainer.appendChild(n),this._listenRangeChanges(e,n),this._listenCanvasEditor();var o=this.level.getMap(),a=o[0].length,r=o.length;e.value=a.toString(),n.value=r.toString(),this.editorWidth=a,this.editorHeight=r;var s=this._createImage("car_agent",l+"car_agent.png");s.dataset.type="agent",s.dataset.img="car_agent.png",this.buttonsContainer.appendChild(s);var h=this._createImage("car_agent",l+"car.png");h.dataset.type="car",h.dataset.img="car.png",this.buttonsContainer.appendChild(h);var c=u.ROADS;for(var p in c){(y=this._createImage(c[p].image.replace(".png",""),l+c[p].image)).dataset.type="road",y.dataset.img=c[p].image,y.dataset.data=p,this.buttonsContainer.appendChild(y)}for(var d in u){var y;if("ROADS"!=d)(y=this._createImage(d,l+u[d].image)).dataset.img=u[d].image,y.dataset.data=d,y.dataset.type="asset",this.buttonsContainer.appendChild(y)}this._listenEditorImgs()},t}(),k=function(){function t(t,e){this.eventList=["train","play","stop","reset_env","load"],this.agentMotionEngine=R,this.agentMotionOptions={},this.isCarsMoving=!0,this.loopCallback=void 0,t&&e||console.error("You must specify the canvasId and the levelToLoad"),this.canvasId=t,this.levelToLoad=e}return t.prototype.setRewardFunction=function(t){this.level.setRewardFunction(t)},t.prototype.carsMoving=function(t){this.isCarsMoving=t},t.prototype.getLastReward=function(){return this.level.getLastReward()},t.prototype.setAgentLidar=function(t){this.agentLidarInfo=t},t.prototype.setAgentMotion=function(t,e){this.agentMotionEngine=t,this.agentMotionOptions=e},t.prototype.addEvent=function(t,e,i){var n=this.eventList.indexOf(t);-1!=n?"load"!=this.eventList[n]?this.eventCallback[n](e):this.eventCallback[n](e,i):this.event.onCustomEvent(t,e)},t.prototype.render=function(t){this.level.render(t)},t.prototype.steping=function(t){this.level.setSteping(t)},t.prototype.save=function(t,e){g(t,e)},t.prototype.actionSpace=function(){return this.level.agent.motion.actionSpace()},t.prototype.getState=function(){return this.level.agent.getState()},t.prototype.step=function(t){return this.level.step(1,t,!1)},t.prototype.reset=function(){this.level.reset()},t.prototype.shuffle=function(t){if((t=t||{cars:!0,agent:!0}).cars=null==t.cars||t.cars,t.agent=null==t.agent||t.agent,t.agent){var e=this.level.getRoads(),i=Object.keys(e);for(var n in i.sort(function(){return Math.random()-.5}),i){var o=e[i[n]];if(0==o.cars.length){o.setCarPosition(this.level.agent.core);break}}}t.cars&&this.level.shuffleCarsPositions()},t.prototype.loop=function(t){this.loopCallback=t},t.prototype._loop=function(t){this.event.isPlaying()?this.event.playCallback():this.level.step(t),this.loopCallback&&this.loopCallback()},t.prototype._setEvents=function(){var t=this;this.event=new P(this.level,this.canvasId),this.eventCallback=[function(e){return t.event.onTrain(e)},function(e){return t.event.onPlay(e)},function(e){return t.event.onStop(e)},function(e){return t.event.onResetEnv(e)},function(e,i){return t.event.onLoad(e,i)}]},t.prototype.load=function(){var t=this;return new Promise(function(e){"string"==typeof t.levelToLoad?f(t.levelToLoad,function(i){t.level=new O(i,t.canvasId),t.level.setAgentMotion(t.agentMotionEngine,t.agentMotionOptions),t.level.setAgentLidar(t.agentLidarInfo),t.level.carsMoving(t.isCarsMoving),t._setEvents(),t.level.load(function(e){return t._loop(e)}).then(function(){e()})}):(t.level=new O(t.levelToLoad,t.canvasId),t.level.setAgentMotion(t.agentMotionEngine,t.agentMotionOptions),t.level.setAgentLidar(t.agentLidarInfo),t.level.carsMoving(t.isCarsMoving),t._setEvents(),t.level.load(function(e){return t._loop(e)}).then(function(){e()}))})},t}(),F=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),D=function(t){function e(e,i){var n=t.call(this,e,i)||this;return n.selectedItems=null,n.am=new L(n),n}return F(e,t),e.prototype._setEvents=function(){},e.prototype._setup=function(t){var e=this;this._setEvents();var i=o.resources[c].textures;this.am.createMap(this.map,t,i,!1),this.am.createCars(t,i),t.agent&&(this.agent=this.am.createAgent(t,i,!1));for(var n=this,a=0;a<this.envs.length;a++)this.envs[a].interactive=!0,this.envs[a].on("rightclick",function(){n.removeItem(this)});for(a=0;a<this.am.assets.length;a++)this.am.assets[a].interactive=!0,this.am.assets[a].on("rightclick",function(){n.removeItem(this)});this.app.ticker.add(function(t){return e.loop(t)})},e.prototype.resize=function(t,e){this.app.renderer.resize(t*p,e*p)},e.prototype.getSelectedItems=function(){return this.selectedItems},e.prototype.selectedItem=function(t){this.selectedItems&&this.app.stage.removeChild(this.selectedItems);var e=o.resources[c].textures;this.selectedItems=new a(e[t.image]),this.selectedItems.type=t.type,this.selectedItems.image=t.image,this.selectedItems.data=t.data,this.selectedItems.x=0,this.selectedItems.y=0,this.app.stage.addChild(this.selectedItems)},e.prototype.removeItem=function(t){t.lidar&&this.app.stage.removeChild(t.lidar),t.mapId==y.CAR&&t.road&&t.road.cars.splice(t.road.cars.indexOf(t.carId),1),t.agent&&(this.agent=void 0),this.app.stage.removeChild(t),t.isremove=!0},e.prototype.exportMap=function(t,e,i,n){return this.am.exportMap(t,e,i,n)},e.prototype.setCurrentItem=function(){this.am.addAsset(this.selectedItems);var t=o.resources[c].textures,e=this;"road"==this.selectedItems.type?(this.am.createRoad({mx:Math.floor(this.selectedItems.x/p),my:Math.floor(this.selectedItems.y/p),type:this.selectedItems.data},t,this.selectedItems.data),this.envs[this.envs.length-1].interactive=!0,this.envs[this.envs.length-1].on("rightclick",function(){e.removeItem(this)})):"car"==this.selectedItems.type?(this.am.createCars({cars:[{mx:Math.floor(this.selectedItems.x/p),my:Math.floor(this.selectedItems.y/p),rotation:0,line:0}]},t),this.envs[this.envs.length-1].interactive=!0,this.envs[this.envs.length-1].on("rightclick",function(){e.removeItem(this)})):"agent"==this.selectedItems.type&&null==this.agent?(this.agent=this.am.createAgent({agent:{mx:Math.floor(this.selectedItems.x/p),my:Math.floor(this.selectedItems.y/p),rotation:0,line:0,motion:{type:"BasicMotionEngine",options:{rotation_step:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},t),this.envs[this.envs.length-1].interactive=!0,this.envs[this.envs.length-1].on("rightclick",function(){e.removeItem(this)})):"asset"==this.selectedItems.type&&(this.am.createAsset(this.selectedItems.image,{x:this.selectedItems.x,y:this.selectedItems.y},t,this.selectedItems.data),this.am.assets[this.am.assets.length-1].interactive=!0,this.am.assets[this.am.assets.length-1].on("rightclick",function(){e.removeItem(this)})),this.app.stage.removeChild(this.selectedItems),this.selectedItems=null},e}(T),N=function(){function t(t,e){this.eventList=["save"],t&&!this.levelToLoad||console.error("You must specify the canvasId and the levelToLoad"),this.canvasId=t,this.levelToLoad=e}return t.prototype.load=function(){var t=this;return new Promise(function(e){"string"==typeof t.levelToLoad?f(t.levelToLoad,function(i){t.level=new D(i,t.canvasId),t.level.load(function(){return t.loop()}),t.event=new P(t.level,t.canvasId),t.event.createEditorElementsEvents(),t.eventCallback=[function(e,i){return t.event.onSaveEditor(e,i)}],e()}):(t.level=new D(t.levelToLoad,t.canvasId),t.level.load(function(){return t.loop()}),t.event=new P(t.level,t.canvasId),t.event.createEditorElementsEvents(),t.eventCallback=[function(e,i){return t.event.onSaveEditor(e,i)}],e())})},t.prototype.save=function(t,e){g(t,e)},t.prototype.addEvent=function(t,e,i){var n=this.eventList.indexOf(t);-1!=n?this.eventCallback[n](e,i):this.event.onCustomEvent(t,e)},t.prototype.loop=function(){var t=this.level.getMousePosition(),e=this.level.getSelectedItems();e&&"asset"==e.type?(e.x=t.x,e.y=t.y):e&&(e.x=Math.floor(t.x/p)*p,e.y=Math.floor(t.y/p)*p)},t}(),U=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),B=function(t){function e(e){var i=t.call(this,e)||this;return i.maxSpeed=2,i.level=e,i.actions=["SteeringAngle","Throttle"],i}return U(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i<e.pts;i++){for(var n=[],o=0;o<e.pts;o++)n.push(y.DEFAULT);this.state.push(n)}this.setUpKeyboard(),this.car.v=0,this.car.a=0,this.car.yaw_rate=0,this.detectInteractions()},e.prototype.setUpKeyboard=function(){var t=this,e=_(37),i=_(38),n=_(39),o=_(40);e.press=function(){t.turn(-.7)},e.release=function(){t.turn(0)},n.press=function(){t.turn(.7)},n.release=function(){t.turn(0)},i.press=function(){t.move(1)},i.release=function(){t.car.a=0},o.press=function(){t.move(-1)},o.release=function(){t.car.a=0}},e.prototype.turn=function(t){this.car.yaw_rate=t},e.prototype.move=function(t){this.car.a=t},e.prototype.actionStep=function(t,e){this.car.a=Math.min(Math.max(e[0],-1),1),this.car.yaw_rate=Math.min(Math.max(e[1],-1),1);var i=this.step(t),n=i.agentCollisions,o=i.onRoad;return this.car.a=0,this.car.yaw_rate=0,{agentCollisions:n,onRoad:o}},e.prototype.actionSpace=function(){return{type:"Continous",size:2,range:[[0,1],[-1,1]]}},e.prototype.step=function(t){if(this.car.last_a=this.car.a,this.car.last_yaw_rate=this.car.yaw_rate,this.car.v>0&&0==this.car.a?this.car.v=Math.max(0,this.car.v-.01):this.car.v<0&&0==this.car.a&&(this.car.v=Math.min(0,this.car.v+.01)),this.car.a>0&&this.car.v>=0?this.car.v=Math.min(this.maxSpeed,this.car.v+.005*this.car.a):this.car.a>0&&this.car.v<0&&(this.car.v=Math.min(this.maxSpeed,this.car.v+.03*this.car.a)),this.car.a<0&&this.car.v<=0?this.car.v=Math.max(-this.maxSpeed,this.car.v+.005*this.car.a):this.car.a<0&&this.car.v>0&&(this.car.v=Math.max(-this.maxSpeed,this.car.v+.03*this.car.a)),0==this.car.yaw_rate)this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t;else{var e=.01*this.car.yaw_rate*(this.car.v+.001)*Math.PI;this.car.x+=this.car.v/e*(Math.sin(this.car.rotation+e*t)-Math.sin(this.car.rotation)),this.car.y+=this.car.v/e*(Math.cos(this.car.rotation)-Math.cos(this.car.rotation+e*t)),this.car.rotation+=e*t}this.car.mx=Math.floor(this.car.x/p),this.car.my=Math.floor(this.car.y/p),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var i=this.detectInteractions(!0,!0),n=i.agentCollisions,o=i.onRoad;return n.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:n,onRoad:o}},e}(M),j={env:k,editor:N,level:{fullCity:"embedded://level/fullCity",level1:"embedded://level/level1",level2:"embedded://level/level2",level3:"embedded://level/level3",level0:"embedded://level/level0"},motion:{BasicMotion:R,ControlMotion:B}};e.default=j}]).default;