Last active
January 16, 2016 17:54
-
-
Save SammyVimes/261821c4972407a3e8fd to your computer and use it in GitHub Desktop.
xx
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
function update(){fpsCount++,bOnSurface&&(sprite.src=hoizontal_motion_direction==MOTION_RIGHT?GRAPHICS.standing_right:GRAPHICS.standing_left),keysDown.indexOf(DOWN)<0&&(bAttemptingToWarp=!1);for(key in keysDown)switch(keysDown[key]){case RIGHT:posX+=current_speed,stage.scrollLeft<WORLD_SIZE&&(stage.scrollLeft=sprite.offsetLeft-stage.offsetWidth/2+sprite.offsetWidth/2),sprite.src=GRAPHICS.running_right,hoizontal_motion_direction=MOTION_RIGHT;break;case JUMP:bOnSurface&&bCanJump&&(bCanJump=!1,setTimeout(function(){bCanJump=!0},GROUNDED_TIMER),gravity_const=-jump_height,posY-=jump_height,bOnSurface=!1,sprite.src=hoizontal_motion_direction==MOTION_RIGHT?GRAPHICS.jumping_right:GRAPHICS.jumping_left);break;case LEFT:sprite.src=GRAPHICS.running_left,posX-=current_speed,stage.scrollLeft=sprite.offsetLeft-stage.offsetWidth/2+sprite.offsetWidth/2,hoizontal_motion_direction=MOTION_LEFT;break;case DOWN:bOnSurface&&(bAttemptingToWarp=!0)}theta++;for(e in elevators)elevators[e].style.top=175+45*Math.sin(theta/80)+"px";collisionAdjust(),render()}function isObtainable(e){return mushrooms.indexOf(e)>-1?(removeFromCollection(mushrooms,e),animateFormChange(sprite,sprite.className,"mario_big",8),!0):coins.indexOf(e)>-1?(removeFromCollection(coins,e),removeFromCollection(collidables,e),animatePoints(e,200),stage.removeChild(e),takeCoin(),!0):!1}function playHitAnimation(e){if(hitables.indexOf(e)>-1){var t=e.offsetTop;e.style.top=t-5+"px",setTimeout(function(){e.style.top=t+"px"},200)}if(coinboxes.indexOf(e)>-1){var o=document.createElement("img");stage.appendChild(o),o.src=GRAPHICS.coin,o.style.position="absolute",o.style.top=e.offsetTop+"px",o.style.left=e.offsetLeft+e.offsetWidth/2-o.offsetWidth/2+"px",e.style.zIndex=1e3,animatePoints(o,200),takeCoin(),animateUp(o,8,1,function(){stage.removeChild(o)})}if(mushroomboxes.indexOf(e)>-1){removeFromCollection(mushroomboxes,e);var o=document.createElement("img");stage.appendChild(o),o.src=GRAPHICS.mushroom_head,o.style.position="absolute",o.style.top=e.offsetTop+"px",o.style.left=e.offsetLeft+e.offsetWidth/2-o.offsetWidth/2+"px",e.style.zIndex=1e3,mushrooms.push(o),collidables.push(o),coins.push(o),animateUp(o,8,1,function(){})}}function animatePoints(e,t){var o=document.createElement("div");o.className="points",stage.appendChild(o),o.innerHTML=t,o.style.top=e.offsetTop+"px",o.style.left=e.offsetLeft+"px",animateUp(o,8,1,function(){stage.removeChild(o)})}function animateFormChange(e,t,o,i){0!=i&&(e.className=i%2==0?t:o,setTimeout(function(){animateFormChange(e,t,o,--i)},100))}function animateUp(e,t,o,i){return o>=t?void i():(e.style.top=e.offsetTop-o+"px",void setTimeout(function(){animateUp(e,t,o+1,i)},70))}function animateDown(e,t,o,i){return o>=t?void i():(e.style.top=e.offsetTop+o+"px",void setTimeout(function(){animateDown(e,t,o+1,i)},70))}function animateHorizontal(e,t){e.style.left=e.offsetLeft+t+"px",setTimeout(function(){animateHorizontal(e,t)},30)}function isWarpPipe(e){warppipes.indexOf(e)>-1&&(clearTimeout(update_interval),animateDown(sprite,15,1,function(){window.location="https://jsfiddle.net/v9xr5t1f/embedded/result"}))}function takeCoin(){coin_counter.innerHTML=parseInt(coin_counter.innerHTML)+1}function collisionAdjust(){posX<stage.offsetLeft&&(posX=5),posX+sprite.offsetWidth>WORLD_SIZE&&(posX=WORLD_SIZE-sprite.offsetWidth),bOnSurface=!1,collideCount=0;for(c in collidables)if(isCloseToCharacter(collidables[c]))switch(collideCount++,getSideColliding(collidables[c])){case OBJ_ABOVE:if(isObtainable(collidables[c]))break;if(posY=collidables[c].offsetTop-sprite.offsetHeight,bOnSurface=!0,isWarpPipe(collidables[c]))break;current_speed=BASE_SPEED;break;case OBJ_LEFT:if(isObtainable(collidables[c]))break;posX=collidables[c].offsetLeft-sprite.offsetWidth,bCanJump||(current_speed=1);break;case OBJ_RIGHT:if(isObtainable(collidables[c]))break;posX=collidables[c].offsetLeft+collidables[c].offsetWidth,bCanJump||(current_speed=1);break;case OBJ_BELOW:if(playHitAnimation(collidables[c]),isObtainable(collidables[c]))break;posY=collidables[c].offsetTop+collidables[c].offsetHeight,gravity_const=BOUNCE_FACTOR;break;default:sprite.style.backgroundColor="transparent"}}function isCloseToCharacter(e){return Math.abs(e.offsetLeft-sprite.offsetLeft)<100&&Math.abs(e.offsetTop-sprite.offsetTop)<100}function getSideColliding(e){return posY+sprite.offsetHeight>e.offsetTop&&posX+sprite.offsetWidth/2>e.offsetLeft&&posX+sprite.offsetWidth/2<e.offsetLeft+e.offsetWidth&&posY<e.offsetTop&&posY+sprite.offsetHeight<e.offsetTop+e.offsetHeight?OBJ_ABOVE:posX+sprite.offsetWidth>e.offsetLeft&&posY+sprite.offsetHeight/2<e.offsetTop+e.offsetHeight&&posX<e.offsetLeft&&posY+sprite.offsetHeight/2>e.offsetTop?OBJ_LEFT:posX<e.offsetLeft+e.offsetWidth&&posX+sprite.offsetWidth>e.offsetLeft+e.offsetWidth&&posY+sprite.offsetHeight/2>e.offsetTop&&posY+sprite.offsetHeight/2<e.offsetTop+e.offsetHeight?OBJ_RIGHT:posY>e.offsetTop&&posY<e.offsetTop+e.offsetHeight&&posX+sprite.offsetWidth/2>e.offsetLeft&&posX+sprite.offsetWidth/2<e.offsetLeft+e.offsetWidth?OBJ_BELOW:0}function render(){sprite.style.left=posX+"px",sprite.style.top=posY+"px"}function renderWorld(){stage=document.getElementById("stage"),sprite=document.getElementById("sprite"),coin_counter=document.getElementById("coin_counter");for(var e=[],t=0;24>t;t++)e.push(dropGroundUnit(null,GRAPHICS.ground_brick,100*t,stage.offsetHeight-32)),collidables.push(e[t]);setTimeout(function(){var t=dropGroundUnit(e[9],GRAPHICS.ground_pipe),o=dropGroundUnit(e[8],GRAPHICS.question_block,-15,-140);hitables.push(o),coinboxes.push(o);for(var i=0;3>i;i++){var n=dropGroundUnit(e[5],GRAPHICS.block_brick,32*i,-45);hitables.push(n),collidables.push(n),1==i&&mushroomboxes.push(n)}for(var i=0;3>i;i++){var n=dropGroundUnit(e[7],GRAPHICS.block_brick,32*i,-45);hitables.push(n),collidables.push(n)}var s=dropGroundUnit(e[10],GRAPHICS.moving_block),r=dropGroundUnit(e[17],GRAPHICS.moving_block);elevators.push(s),elevators.push(r);for(var i=0;3>i;i++){var a=dropGroundUnit(e[7],GRAPHICS.coin,32*i,-10);collidables.push(a),coins.push(a)}for(var i=0;4>i;i++){var a=dropGroundUnit(e[6],GRAPHICS.coin,32*i+5,-100-32*i);collidables.push(a),coins.push(a)}for(var i=0;3>i;i++){var a=dropGroundUnit(e[10],GRAPHICS.coin,32*i,-232);collidables.push(a),coins.push(a)}for(var i=1;4>i;i++){var n;3==i?(n=dropGroundUnit(e[15],GRAPHICS.question_block,32*i,32*-i),mushroomboxes.push(n)):n=dropGroundUnit(e[15],GRAPHICS.block_brick,32*i,-64),hitables.push(n),collidables.push(n);var a=dropGroundUnit(r,GRAPHICS.coin,12,32*-i-140);collidables.push(a),coins.push(a)}for(var p=0;9>p;p++)for(var i=0;11>i;i++)if(!(p>=i)){var a=dropGroundUnit(e[20],GRAPHICS.small_brick,20*i,20*-p);collidables.push(a)}var f=dropGroundUnit(e[12],GRAPHICS.ground_pipe);collidables.push(f);var l=dropGroundUnit(e[23],GRAPHICS.ground_pipe);warppipes.push(l),collidables.push(l),collidables.push(t),collidables.push(o),collidables.push(s),collidables.push(r)},1e3)}function dropGroundUnit(e,t,o,i){var n=document.createElement("img");return n.src=t,stage.appendChild(n),n.style.position="absolute",n.style.top=(null!=e?e.offsetTop-n.offsetHeight+(i||0):i)+"px",n.style.left=(null!=e?e.offsetLeft+(o||0):o)+"px",n}function gravity(){bOnSurface||(posY+=gravity_const),gravity_const++,gravity_const>GRAVITY_CAP&&(gravity_const=GRAVITY_CAP)}function removeFromCollection(e,t){e.indexOf(t)>-1&&e.splice(e.indexOf(t),1)}function onkeyDown(e){var t=window.event||e,o=e.charCode||e.keyCode;return t.preventDefault?t.preventDefault():t.returnValue=!1,keysDown.indexOf(o)>-1?void 0:(keysDown.push(o),!1)}function onKeyUp(e){var t=(window.event||e,e.charCode||e.keyCode);removeFromCollection(keysDown,t)}function loadGraphics(e){for(g in GRAPHICS){var t=new Image;t.src=GRAPHICS[g],t.onload=function(e,t){return function(){t=e}}(t.src,GRAPHICS[g])}e()}function debugUpdate(){var e="FPS: "+fpsCount+"\nCollision space: "+collideCount+"\n";e+="Collidable: "+collidables.length,debug.value=e,fpsCount=0}var update_interval,gravity_interval,posX=200,posY=200,BASE_SPEED=3,current_speed=3,jump_height=12,gravity_const=1,GRAVITY_CAP=8,WORLD_SIZE=2400,JUMP=38,LEFT=37,RIGHT=39,DOWN=40,keysDown=[],stage,sprite,coin_counter,collidables=[],OBJ_ABOVE=1,OBJ_BELOW=2,OBJ_LEFT=3,OBJ_RIGHT=4,GRAPHICS={};GRAPHICS.running_left="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/running_left.gif",GRAPHICS.running_right="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/running_right.gif",GRAPHICS.jumping_left="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/jumping_left.gif",GRAPHICS.jumping_right="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/jumping_right.gif",GRAPHICS.standing_left="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/standing_left.gif",GRAPHICS.standing_right="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/standing_right.gif",GRAPHICS.ground_brick="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/ground_brick.png",GRAPHICS.ground_pipe="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/ground_pipe.gif",GRAPHICS.question_block="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/question_block.gif",GRAPHICS.block_brick="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/block_brick.gif",GRAPHICS.moving_block="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/moving_block.png",GRAPHICS.mushroom_head="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/mushroom_head.gif",GRAPHICS.coin="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/coin.gif",GRAPHICS.small_brick="http://hazedvision.com/dev/Mario%20game%20%28in%20pure%20JavaScript%29/small_brick.png";var bOnSurface=!1,bCanJump=!0,GROUNDED_TIMER=500,BOUNCE_FACTOR=2,elevators=[],coins=[],hitables=[],coinboxes=[],mushroomboxes=[],warppipes=[],mushrooms=[],bAttemptingToWarp=!1,theta=0,MOTION_LEFT=0,MOTION_RIGHT=1,hoizontal_motion_direction=MOTION_RIGHT,debug,collideCount,fpsCount;$(document).ready(function(){debug=document.getElementById("debug"),loadGraphics(function(){renderWorld(),stage.scrollLeft=0}),setTimeout(function(){update_interval=setInterval(function(){update()},15),gravity_interval=setInterval(function(){gravity()},30),setInterval(function(){debugUpdate()},1e3),$(this).keydown(onkeyDown),$(this).keyup(onKeyUp)},500)}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment