-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblockly-mario.min.js
1 lines (1 loc) · 15.3 KB
/
blockly-mario.min.js
1
var blockly_mario;!function(){function a(){var a='"'+this.getTitleValue("VALUE")+'"';return[a,Blockly.JavaScript.ORDER_ATOMIC]}function b(a,b,c){return Blockly.JavaScript.valueToCode(a,b,c)}Blockly.Language.agent_act={init:function(){this.setColour(290),this.appendValueInput("ACTION").appendTitle("activate"),this.setPreviousStatement(!0),this.setNextStatement(!0),this.setTooltip("Activate or keep active the given action. Actions otherwise deactivate at each time step.\n\nTo actually jump or shoot more than once, it must be inactive between.")}},Blockly.Language.agent_action={init:function(){this.setColour(160),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["jump","JUMP"],["shoot","SHOOT"],["left","LEFT"],["right","RIGHT"],["up","UP"],["down","DOWN"]]),"VALUE"),this.setOutput(!0,String),this.setTooltip("Choose an action. Jump also starts levels. Shoot also runs faster.")}},Blockly.Language.agent_enemies={init:function(){this.setColour(210),this.appendDummyInput().appendTitle("enemies"),this.setOutput(!0,Array),this.setTooltip("A list of all active enemies.")}},Blockly.Language.agent_enemyTypeOption={init:function(){this.setColour(160),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["bullet bill","BULLET_BILL"],["goomba (mushroom enemy)","GOOMBA"],["green koopa (turtle)","GREEN_KOOPA"],["piranha plant","PIRANHA_PLANT"],["red koopa (turtle)","RED_KOOPA"],["shell (green or red)","SHELL"],["spiny (turtle)","SPINY"]]),"VALUE"),this.setOutput(!0,String),this.setTooltip("Choose from the list of possible enemy types.")}},Blockly.Language.agent_mario={init:function(){this.setColour(0),this.appendDummyInput().appendTitle("mario"),this.setOutput(!0,"Sprite"),this.setTooltip("Mario player character.")}},Blockly.Language.agent_mode={init:function(){this.setColour(120),this.appendDummyInput().appendTitle("game mode is").appendTitle(new Blockly.FieldDropdown([["level","LEVEL"],["map","MAP"],["title","TITLE"],["lose","LOSE"],["win","WIN"]]),"MODE"),this.setOutput(!0,Boolean),this.setTooltip("Test the current game mode (screen type).")}},Blockly.Language.agent_powerUps={init:function(){this.setColour(210),this.appendDummyInput().appendTitle("power-ups"),this.setOutput(!0,Array),this.setTooltip("A list of all active power-ups (mushrooms and fire flowers).")}},Blockly.Language.agent_predicate={init:function(){this.setColour(120),this.appendValueInput("SPRITE"),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["on ground","ON_GROUND"],["has fire","HAS_FIRE"],["super (big)","SUPER"],["winged","WINGED"]]),"VALUE"),this.setInputsInline(!0),this.setOutput(!0,Boolean),this.setTooltip("Logical (true or false) condition about the given character.\nSome apply only to Mario or only to enemies. When inapplicable, the value is always false.")}},Blockly.Language.agent_powerUpTypeOption={init:function(){this.setColour(160),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["fire flower","FIRE_FLOWER"],["super mushroom (power-up)","SUPER_MUSHROOM"]]),"VALUE"),this.setOutput(!0,String),this.setTooltip("Choose from the list of possible power-up types.")}},Blockly.Language.agent_spriteType={init:function(){this.setColour(160),this.appendValueInput("SPRITE").setCheck("Sprite").appendTitle("type of"),this.setOutput(!0,String),this.setTooltip("Text identifying the type of any character or power-up.")}},Blockly.Language.agent_tileSize={init:function(){this.setColour(230),this.appendDummyInput().appendTitle("tile").appendTitle(new Blockly.FieldDropdown([["radius","RADIUS"],["size","SIZE"]]),"VALUE"),this.setOutput(!0,Number),this.setTooltip('The tile "radius" or full size (edge length).')}},Blockly.Language.agent_tileTypeAt={init:function(){this.setColour(160),this.appendValueInput("X").setCheck(Number).appendTitle("tile type at x"),this.appendValueInput("Y").setCheck(Number).appendTitle("y"),this.setInputsInline(!0),this.setOutput(!0,String),this.setTooltip("Tile type at pixel offset (x, y) from Mario's center.")}},Blockly.Language.agent_tileTypeOption={init:function(){this.setColour(160),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["air","AIR"],["brick","BRICK"],["coin","COIN"],["off screen","OFF_SCREEN"],["pipe","PIPE"],["question","QUESTION"],["solid","SOLID"],["surface (top only)","SURFACE"]]),"VALUE"),this.setOutput(!0,String),this.setTooltip("Choose from the list of possible tile types.")}},Blockly.Language.agent_value={init:function(){this.setColour(230),this.appendValueInput("SPRITE"),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["facing x","FACING_X"],["offset x","OFFSET_X"],["offset y","OFFSET_Y"],["position x","POSITION_X"],["position y","POSITION_Y"],["radius x","RADIUS_X"],["radius y","RADIUS_Y"],["velocity x","VELOCITY_X"],["velocity y","VELOCITY_Y"]]),"VALUE"),this.setInputsInline(!0),this.setOutput(!0,Number),this.setTooltip("Get the requested value for the given animated character.\nPositions are refer to center points, where right is +X and up is +Y.\nMario's origin point is the center of the screen, and all other characters are relative to Mario.\nOffset is from tile center.\nFacing is -1 (left), 1 (right), or even 0 (for some nonmoving characters).")}},Blockly.JavaScript.agent_act=function(){var a=b(this,"ACTION",Blockly.JavaScript.ORDER_NONE),c=["$$actions[",a,"] = true;\n"].join("");return c},Blockly.JavaScript.agent_action=a,Blockly.JavaScript.agent_enemies=function(){return["$$support.enemies()",Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_enemyTypeOption=a,Blockly.JavaScript.agent_mario=function(){return["Mario.MarioCharacter",Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_mode=function(){var a={LEVEL:"Mario.LevelState",LOSE:"Mario.LoseState",MAP:"Mario.MapState",TITLE:"Mario.TitleState",WIN:"Mario.WinState"}[this.getTitleValue("MODE")],b="$$support.gameStateIs("+a+")";return[b,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_powerUps=function(){return["$$support.powerUps()",Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_powerUpTypeOption=a,Blockly.JavaScript.agent_predicate=function(){var a=b(this,"SPRITE",Blockly.JavaScript.ORDER_COMMA),c=this.getTitleValue("VALUE"),d=["$$support.spritePredicate(",a,", '",c,"')"].join("");return[d,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_spriteType=function(){var a=b(this,"SPRITE",Blockly.JavaScript.ORDER_NONE),c=["$$support.spriteType(",a,")"].join("");return[c,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_tileSize=function(){var a=this.getTitleValue("VALUE"),b=["$$support.tileSize('",a,"')"].join("");return[b,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_tileTypeAt=function(){var a=b(this,"X",Blockly.JavaScript.ORDER_COMMA),c=b(this,"Y",Blockly.JavaScript.ORDER_COMMA),d=["$$support.tileTypeAt(",a,", ",c,")"].join("");return[d,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_tileTypeOption=a,Blockly.JavaScript.agent_value=function(){var a=b(this,"SPRITE",Blockly.JavaScript.ORDER_COMMA),c=this.getTitleValue("VALUE"),d=["$$support.spriteValue(",a,", '",c,"')"].join("");return[d,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.text_print=function(){var a=b(this,"TEXT",Blockly.JavaScript.ORDER_NONE)||'""';return"blockly_mario.log("+a+");\n"}}(blockly_mario||(blockly_mario={}));var blockly_mario;!function(a){var b={X:160,Y:120},c=16,d=function(){function a(a){this.app=a}return a.prototype.enemies=function(){var a=this;if(!this.gameStateIs(Mario.LevelState))return[];var b=this.gameState().Sprites.Objects.filter(function(b){if(b instanceof Mario.BulletBill||b instanceof Mario.Shell)return!0;if(b instanceof Mario.FlowerEnemy){var c=a.getPosition(b,"X"),d=a.getPosition(b,"Y");return"PIPE"!=a.tileTypeAt(c,d+b.Height/2)}return b instanceof Mario.Enemy});return b},a.prototype.gameState=function(){return this.app.stateContext.State},a.prototype.gameStateIs=function(a){return this.gameState()instanceof a},a.prototype.powerUps=function(){if(!this.gameStateIs(Mario.LevelState))return[];var a=this.gameState().Sprites.Objects.filter(function(a){return a instanceof Mario.Mushroom||a instanceof Mario.FireFlower});return a},a.prototype.spritePredicate=function(a,b){var c=!1;switch(b){case"ON_GROUND":c=a.OnGround;break;case"HAS_FIRE":c=a.Fire;break;case"SUPER":c=a.Large;break;case"WINGED":c=a.Winged}return c=Boolean(c)},a.prototype.spriteType=function(a){if(a instanceof Mario.BulletBill)return"BULLET_BILL";if(a instanceof Mario.Character)return"MARIO";if(a instanceof Mario.Enemy){if(a instanceof Mario.FlowerEnemy)return"PIRANHA_PLANT";switch(a.Type){case Mario.Enemy.Goomba:return"GOOMBA";case Mario.Enemy.GreenKoopa:return"GREEN_KOOPA";case Mario.Enemy.RedKoopa:return"RED_KOOPA";case Mario.Enemy.Spiky:return"SPINY"}}else{if(a instanceof Mario.FireFlower)return"FIRE_FLOWER";if(a instanceof Mario.Mushroom)return"SUPER_MUSHROOM";if(a instanceof Mario.Shell)return"SHELL"}return"UNKNOWN"},a.prototype.spriteValue=function(a,b){var c=b.split(/_+/),d=c[0],e=c[c.length-1];switch(d){case"FACING":return this.getFacing(a,e);case"OFFSET":return this.getOffset(a,e);case"POSITION":return this.getPosition(a,e);case"RADIUS":return this.getRadius(a,e);case"VELOCITY":return this.getVelocity(a,e)}},a.prototype.tileSize=function(a){var b=c;return"RADIUS"==a&&(b/=2),b},a.prototype.tileTypeAt=function(a,d){var e=this.gameState();if(e instanceof Mario.LevelState){var f=Mario.MarioCharacter;d*=-1,a+=f.X,d+=f.Y,d-=f.Height/2;var g=e.Camera,h=g.X,i=h+2*b.X-1,j=g.Y,k=j+2*b.Y-1;if(h>a||j>d||a>i||d>k)return"OFF_SCREEN";a=Math.floor(a/c),d=Math.floor(d/c);var l=e.Level,m=l.Map[a][d];switch(m){case 0:return"AIR";case 10:case 11:case 26:case 27:return"PIPE";case 16:case 17:case 18:case 19:return"BRICK";case 20:case 21:case 22:case 23:return"QUESTION";case 32:case 33:case 34:case 35:return"COIN";case 4:case 5:case 6:case 7:case 9:case 12:case 28:case 131:case 145:case 147:return"SOLID"}if(m>=128){var n=Mario.Tile.Behaviors[m];switch(n){case 0:return"AIR";case Mario.Tile.BlockAll:return"SOLID";case Mario.Tile.BlockUpper:return"SURFACE"}}console.log("Unknown type: "+m)}else e instanceof Mario.MapState;return"UNKNOWN"},a.prototype.getFacing=function(a){return+a.Facing},a.prototype.getOffset=function(a,d){var e=this.gameState();if(e instanceof Mario.LevelState){var f=a[d];return"Y"==d&&(f*=-1,f+=2*b.Y,f+=a.Height/2-1),f%=c,f-=c/2}return Number.NaN},a.prototype.getPosition=function(a,c){var d,e=Mario.MarioCharacter,f=this.gameState();return f instanceof Mario.LevelState?(a===e?(d=e[c],"X"==c&&(d-=f.Camera.X),d-={X:b.X,Y:b.Y}[c]):d=a[c]-e[c],"Y"==c&&(d-=a.Height/2)):f instanceof Mario.MapState?a===e&&(d=f[c+"Mario"],"Y"==c&&(d-=2*b[c]),d+=8):d=Number.NaN,"Y"===c&&(d*=-1),d},a.prototype.getRadius=function(a,b){try{return"X"==b?a.Width:a.Height/2}catch(c){return Number.NaN}},a.prototype.getVelocity=function(a,b){var c;return c=this.gameStateIs(Mario.LevelState)?a[b+"v"]:Number.NaN,"Y"===b&&(c*=-1),c},a}();a.Support=d}(blockly_mario||(blockly_mario={}));var blockly_mario;!function(blockly_mario){function log(a){var b=$("console"),c=String(a).replace(/&/g,"&").replace(/</g,"<");if(c===lastMessage){var d=b.lastChild,e=d.firstChild;e instanceof Element||(d.innerHTML='<span class="log-count">1</span>'+d.innerHTML,e=d.firstChild);var f=Number(e.innerHTML);e.innerHTML=String(f+1)}else lastMessage=c,b.innerHTML+="<div>"+c+"</div>";for(;b.childNodes.length>100;)b.removeChild(b.firstChild);b.scrollTop=b.scrollHeight}function $(a){return document.getElementById(a)}function $a(a){return $(a)}function $input(a){return $(a)}function defineFinishCode(a){return function(b){return a(["var $$actions;","return function() {","$$actions = {};",b,"return $$actions;","};"].join("\n"))}}function defineKeyDown(a){return function(b){if(this.IsActive()&&32==b.keyCode){var c=$input("pause");c.checked=!c.checked,handlePause(),b.preventDefault()}else if(this.IsActive()&&13==b.keyCode){var d=$input("ai");d.checked=!d.checked,b.preventDefault()}else a.call(this,b)}}function handleFileChosen(a){var b=a.target.files;if(b.length){var c=new FileReader;c.onload=function(a){loadBlocksXml(a.target.result,loadClears)},c.readAsText(b[0])}}function handleExport(a){Blockly.selected||(alert("You must first select a block to export."),a.preventDefault())}function handleImport(){loadClears=!1,$("file-chooser").click()}function handleOpen(){loadClears=!0,$("file-chooser").click()}function handlePause(){$input("pause").checked?blockly_mario.app.timer.Stop():blockly_mario.app.timer.Start()}function loadBlocksXml(a,b){try{var c=Blockly.Xml.textToDom(a);b&&Blockly.mainWorkspace.clear(),Blockly.Xml.domToWorkspace(Blockly.mainWorkspace,c)}catch(d){throw alert("Failed to open Blockly program."),d}}function redefine(a,b,c){a[b]=c(a[b])}function selectionChanged(){var a="#";if(Blockly.selected){var b=Blockly.Xml.blockToDom_(Blockly.selected),c="<xml>"+Blockly.Xml.domToText(b)+"</xml>";a="data:text/plain,"+encodeURIComponent(c)}$a("export").href=a}function storageKey(a){return window.location.href.split("#")[0]+"#"+a}function updateCode(){var code=Blockly.Generator.workspaceToCode("JavaScript");code=["(function($$support) {",code,"})"].join("\n");try{aiFunction=eval(code)(new blockly_mario.Support(blockly_mario.app)),$input("ai").disabled=!1,$input("update").disabled=!0}catch(e){throw alert("Error building code."),aiFunction=null,$input("ai").checked=!1,$input("ai").disabled=!0,e}}function windowResized(){var a=$("console");a.style.height=$("app").clientHeight-a.offsetTop-12+"px"}function workspaceChanged(){$("update").disabled=!1;var a=Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(Blockly.mainWorkspace));window.localStorage.setItem(storageKey("blocks"),a),$a("save").href="data:text/plain,"+encodeURIComponent(a)}var aiFunction,loadClears=!1;blockly_mario.app;var lastMessage=null;blockly_mario.log=log,window.addEventListener("load",function(){blockly_mario.app=new Enjine.Application,blockly_mario.app.Initialize(new Mario.LoadingState("../mariohtml5/"),320,240),blockly_mario.app.timer.UpdateObject=new AiUpdate(blockly_mario.app),redefine(Enjine.KeyboardInput,"KeyDownEvent",defineKeyDown),Blockly.inject($("blockly"),{path:"../blockly/",toolbox:$("toolbox")}),redefine(Blockly.JavaScript,"finish",defineFinishCode),Blockly.addChangeListener(workspaceChanged),$("ai").onclick=function(){$input("ai").checked||$("canvas").focus()},["ai","pause"].forEach(function(a){$input(a).checked=!1}),$("pause").onclick=handlePause,$("update").onclick=updateCode,$("import").addEventListener("click",handleImport,!1),$("open").addEventListener("click",handleOpen,!1),$("file-chooser").addEventListener("change",handleFileChosen,!1),Blockly.bindEvent_(Blockly.mainWorkspace.getCanvas(),"blocklySelectChange",null,selectionChanged),$("export").addEventListener("click",handleExport,!1),window.addEventListener("resize",windowResized,!1),windowResized();var a=localStorage.getItem(storageKey("blocks"));a&&loadBlocksXml(a,!0),setTimeout(function(){updateCode()},0),$("canvas").focus()});var AiUpdate=function(){function a(a){this.base=a}return a.prototype.Update=function(a){var b=blockly_mario.app.stateContext.State.Sprites;b&&b.Objects.forEach(function(a){a.Xv=a.X-a.OldX,a.Yv=a.Y-a.OldY,a.OldX=a.X,a.OldY=a.Y});var c,d=$input("ai").checked&&Boolean(aiFunction);if(d){var e=aiFunction(),f={},g={DOWN:"Down",LEFT:"Left",JUMP:"S",RIGHT:"Right",SHOOT:"A",UP:"Up"};for(var h in e)e[h]&&(f[Enjine.Keys[g[h]]]=!0);c=Enjine.KeyboardInput.Pressed,Enjine.KeyboardInput.Pressed=f,Enjine.KeyboardInput.Element=null}this.base.Update(a),d&&(Enjine.KeyboardInput.Pressed=c,Enjine.KeyboardInput.Element=$("canvas"))},a}()}(blockly_mario||(blockly_mario={}));