Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

jaws.gameloop is now jaws.game_loop

  • Loading branch information...
commit b1401a3c47294aa112958d91d7495c0ebefce2a0 1 parent 096f810
@ippa authored
View
4 benchmarks/benchmark1.html
@@ -29,7 +29,7 @@
this.draw = function() {
for(var i=0; i < loop; i++) { jaws.context.drawImage(image, 0, 0) }
- fps.innerHTML = jaws.gameloop.fps + ". " + "context.drawImage(image)"
+ fps.innerHTML = jaws.game_loop.fps + ". " + "context.drawImage(image)"
}
}
@@ -46,7 +46,7 @@
this.draw = function() {
for(var i=0; i < loop; i++) { jaws.context.drawImage(canvas, 0, 0) }
- fps.innerHTML = jaws.gameloop.fps + ". " + "context.drawImage(context)"
+ fps.innerHTML = jaws.game_loop.fps + ". " + "context.drawImage(context)"
}
}
View
6 benchmarks/benchmark2.html
@@ -46,7 +46,7 @@
sprite.draw();
}
sprite.rotate(1)
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
}
@@ -66,7 +66,7 @@
jaws.context.restore()
}
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
rotation += 1
}
}
@@ -74,7 +74,7 @@
jaws.onload = function() {
jaws.assets.add("plane.png")
jaws.on_keydown("space", function() { jaws.switchGameState(BenchCanvas) })
- jaws.on_keydown("esc", function() { jaws.gameloop.stop() })
+ jaws.on_keydown("esc", function() { jaws.game_loop.stop() })
jaws.start(BenchSprite, 200) // Try impossible framerate
}
</script>
View
6 benchmarks/benchmark3.html
@@ -43,7 +43,7 @@
jaws.clear()
sprite.draw()
}
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
}
@@ -60,14 +60,14 @@
jaws.clear()
sprite.draw()
}
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
}
jaws.onload = function() {
jaws.assets.add("plane.png")
jaws.on_keydown("space", function() { jaws.switchGameState(BenchCanvas) })
- jaws.on_keydown("esc", function() { jaws.gameloop.stop() })
+ jaws.on_keydown("esc", function() { jaws.game_loop.stop() })
jaws.start(BenchSprite, 200) // Try impossible framerate
}
</script>
View
4 examples/example1.html
@@ -64,8 +64,8 @@
player.draw()
bullets.draw() // will call draw() on all items in the list
- info_tag.innerHTML = "FPS: " + jaws.gameloop.fps + " Player position: " + player.x + "/" + player.y + ". W/H: " + canvas.width + "/" + canvas.height
- fps.innerHTML = jaws.gameloop.fps
+ info_tag.innerHTML = "FPS: " + jaws.game_loop.fps + " Player position: " + player.x + "/" + player.y + ". W/H: " + canvas.width + "/" + canvas.height
+ fps.innerHTML = jaws.game_loop.fps
}
/* 2 functions that will help us remove bullets outside the canvas + keep the plane within the canvas. */
View
4 examples/example11.html
@@ -42,8 +42,8 @@
draw: function() {
jaws.clear()
player.draw()
- info_tag.innerHTML = "FPS: " + jaws.gameloop.fps + " Player position: " + player.x + "/" + player.y
- fps.innerHTML = jaws.gameloop.fps
+ info_tag.innerHTML = "FPS: " + jaws.game_loop.fps + " Player position: " + player.x + "/" + player.y
+ fps.innerHTML = jaws.game_loop.fps
}
}
View
2  examples/example2.html
@@ -60,7 +60,7 @@
forceInsideCanvas(player)
bullets.deleteIf(isOutsideCanvas) // delete items for which isOutsideCanvas(item) is true
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
this.draw = function() {
View
2  examples/example3.html
@@ -65,7 +65,7 @@
if(jaws.pressed("up")) { player.y -= speed; player.image = player.anim_up.next() }
if(jaws.pressed("down")) { player.y += speed; player.image = player.anim_down.next() }
//forceInsideCanvas(player)
- fps.innerHTML = jaws.gameloop.fps + ". player: " + player.x + "/" + player.y
+ fps.innerHTML = jaws.game_loop.fps + ". player: " + player.x + "/" + player.y
}
/* Directly after each update draw() will be called. Put all your on-screen operations here. */
View
2  examples/example4.html
@@ -62,7 +62,7 @@
}
});
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
/* Directly after each update draw() will be called. Put all your on-screen operations here. */
View
2  examples/example5.html
@@ -55,7 +55,7 @@
sprite.rotate(1)
});
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
/* Directly after each update draw() will be called. Put all your on-screen operations here. */
View
2  examples/example6.html
@@ -37,7 +37,7 @@
if(jaws.pressed("left")) { parallax.camera_x -= 10 }
if(jaws.pressed("right")) { parallax.camera_x += 10 }
else { parallax.camera_x += 2 }
- fps.innerHTML = jaws.gameloop.fps
+ fps.innerHTML = jaws.game_loop.fps
}
this.draw = function() {
View
4 examples/example7.html
@@ -73,11 +73,11 @@
player.vy /= 1.10
bullets.update()
- // jaws.log("bullets: " + bullets.length + " fps: " + jaws.gameloop.fps)
+ // jaws.log("bullets: " + bullets.length + " fps: " + jaws.game_loop.fps)
}
this.draw = function() {
- //fps.innerHTML = jaws.gameloop.fps
+ //fps.innerHTML = jaws.game_loop.fps
player.draw()
bullets.draw()
}
View
2  examples/example8.html
@@ -87,7 +87,7 @@
if(jaws.pressed("right")) { player.move(2,0); player.setImage(player.anim_right.next()) }
if(jaws.pressed("up")) { player.move(0, -2); player.setImage(player.anim_up.next()) }
if(jaws.pressed("down")) { player.move(0, 2); player.setImage(player.anim_down.next()) }
- fps.innerHTML = jaws.gameloop.fps + ". player: " + player.x + "/" + player.y
+ fps.innerHTML = jaws.game_loop.fps + ". player: " + player.x + "/" + player.y
}
/* Directly after each update draw() will be called. Put all your on-screen operations here. */
View
2  examples/example9.html
@@ -124,7 +124,7 @@
// It won't go outside of 0 or outside of our previously specified max_x, max_y values.
viewport.centerAround(player)
- live_info.innerHTML = jaws.gameloop.fps + " fps. Player: " + parseInt(player.x) + "/" + parseInt(player.y) + ". "
+ live_info.innerHTML = jaws.game_loop.fps + " fps. Player: " + parseInt(player.x) + "/" + parseInt(player.y) + ". "
live_info.innerHTML += "Viewport: " + parseInt(viewport.x) + "/" + parseInt(viewport.y) + "."
}
View
25 jaws-min.js
@@ -1,11 +1,11 @@
var jaws=function(c){var b,d;c.title=function(c){return c?b.innerHTML=c:b.innerHTML};c.unpack=function(){"Sprite,SpriteList,Animation,Viewport,SpriteSheet,Parallax,TileMap,Rect,pressed".split(",").forEach(function(b){window[b]?c.log(b+"already exists in global namespace"):window[b]=c[b]})};c.log=function(b,c){if(d)b+="<br />",d.innerHTML=c?d.innerHTML.toString()+b:b};c.init=function(){b=document.getElementsByTagName("title")[0];c.url_parameters=c.getUrlParameters();d=document.getElementById("jaws-log");
if(c.url_parameters.debug&&!d)d=document.createElement("div"),d.style.cssText="overflow: auto; color: #aaaaaa; width: 300px; height: 150px; margin: 40px auto 0px auto; padding: 5px; border: #444444 1px solid; clear: both; font: 10px verdana; text-align: left;",document.body.appendChild(d);c.canvas=document.getElementsByTagName("canvas")[0];c.canvas?c.context=c.canvas.getContext("2d"):(c.dom=document.getElementById("canvas"),c.dom.style.position="relative");c.width=c.canvas?c.canvas.width:c.dom.offsetWidth;
c.height=c.canvas?c.canvas.height:c.dom.offsetHeight};c.start=function(b,d){function g(b){if(c.context)c.context.save(),c.context.fillStyle="black",c.context.fillRect(0,0,c.width,c.height),c.context.textAlign="center",c.context.fillStyle="white",c.context.font="15px terminal",c.context.fillText("Loading",c.width/2,c.height/2-30),c.context.font="bold 30px terminal",c.context.fillText(b+"%",c.width/2,c.height/2),c.context.restore()}function h(b,d){c.log(d+"%: "+b,!0);g(d)}function l(b){c.log("Error loading: "+
-b,!0)}function k(){c.log("all assets loaded",!0);b&&c.isFunction(b)&&(b=new b);b||(b=window);c.gameloop=new c.GameLoop(b.setup,b.update,b.draw,m);c.game_state=b;c.gameloop.start()}var m=d&&d.fps||60;c.init();g(0);c.log("setupInput()",!0);c.setupInput();c.log("assets.loadAll()",!0);c.assets.length()>0?c.assets.loadAll({onload:h,onerror:l,onfinish:k}):k()};c.switchGameState=function(b){c.gameloop.stop();c.clearKeyCallbacks();c.isFunction(b)&&(b=new b);c.previous_game_state=c.game_state;c.game_state=
-b;c.gameloop=new c.GameLoop(b.setup,b.update,b.draw,c.gameloop.fps);c.gameloop.start()};c.imageToCanvas=function(b){var c=document.createElement("canvas");c.src=b.src;c.width=b.width;c.height=b.height;c.getContext("2d").drawImage(b,0,0,b.width,b.height);return c};c.forceArray=function(b){return Array.isArray(b)?b:[b]};c.clear=function(){c.context.clearRect(0,0,c.width,c.height)};c.isImage=function(b){return Object.prototype.toString.call(b)==="[object HTMLImageElement]"};c.isCanvas=function(b){return Object.prototype.toString.call(b)===
-"[object HTMLCanvasElement]"};c.isDrawable=function(b){return c.isImage(b)||c.isCanvas(b)};c.isString=function(b){return typeof b=="string"};c.isArray=function(b){return b.constructor.toString().indexOf("Array")!=-1};c.isFunction=function(b){return Object.prototype.toString.call(b)==="[object Function]"};c.isOutsideCanvas=function(b){return b.x<0||b.y<0||b.x>c.width||b.y>c.height};c.forceInsideCanvas=function(b){if(b.x<0)b.x=0;if(b.x>c.width)b.x=c.width;if(b.y<0)b.y=0;if(b.y>c.height)b.y=c.height};
-c.getUrlParameters=function(){for(var b=[],c,d=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),h=0;h<d.length;h++)c=d[h].split("="),b.push(c[0]),b[c[0]]=c[1];return b};return c}(jaws||{}),jaws=function(c){var b={},d=[],e=[],f=[];c.setupInput=function(){var c=[];c[8]="backspace";c[9]="tab";c[13]="enter";c[16]="shift";c[17]="ctrl";c[18]="alt";c[19]="pause";c[20]="capslock";c[27]="esc";c[32]="space";c[33]="pageup";c[34]="pagedown";c[35]="end";c[36]="home";c[37]="left";c[38]=
-"up";c[39]="right";c[40]="down";c[45]="insert";c[46]="delete";c[91]="leftwindowkey";c[92]="rightwindowkey";c[93]="selectkey";c[106]="multiply";c[107]="add";c[109]="subtract";c[110]="decimalpoint";c[111]="divide";c[144]="numlock";c[145]="scrollock";c[186]="semicolon";c[187]="equalsign";c[188]="comma";c[189]="dash";c[190]="period";c[191]="forwardslash";c[192]="graveaccent";c[219]="openbracket";c[220]="backslash";c[221]="closebracket";c[222]="singlequote";for(var l="numpad1,numpad2,numpad3,numpad4,numpad5,numpad6,numpad7,numpad8,numpad9".split(","),
+b,!0)}function k(){c.log("all assets loaded",!0);b&&c.isFunction(b)&&(b=new b);b||(b=window);c.game_loop=new c.GameLoop(b.setup,b.update,b.draw,m);c.game_state=b;c.game_loop.start()}var m=d&&d.fps||60;c.init();g(0);c.log("setupInput()",!0);c.setupInput();c.log("assets.loadAll()",!0);c.assets.length()>0?c.assets.loadAll({onload:h,onerror:l,onfinish:k}):k()};c.switchGameState=function(b){c.game_loop.stop();c.clearKeyCallbacks();c.isFunction(b)&&(b=new b);c.previous_game_state=c.game_state;c.game_state=
+b;c.game_loop=new c.GameLoop(b.setup,b.update,b.draw,c.game_loop.fps);c.game_loop.start()};c.imageToCanvas=function(b){var c=document.createElement("canvas");c.src=b.src;c.width=b.width;c.height=b.height;c.getContext("2d").drawImage(b,0,0,b.width,b.height);return c};c.forceArray=function(b){return Array.isArray(b)?b:[b]};c.clear=function(){c.context.clearRect(0,0,c.width,c.height)};c.isImage=function(b){return Object.prototype.toString.call(b)==="[object HTMLImageElement]"};c.isCanvas=function(b){return Object.prototype.toString.call(b)===
+"[object HTMLCanvasElement]"};c.isDrawable=function(b){return c.isImage(b)||c.isCanvas(b)};c.isString=function(b){return typeof b=="string"};c.isArray=function(b){return b===void 0?!1:b.constructor.toString().indexOf("Array")!=-1};c.isFunction=function(b){return Object.prototype.toString.call(b)==="[object Function]"};c.isOutsideCanvas=function(b){return b.x<0||b.y<0||b.x>c.width||b.y>c.height};c.forceInsideCanvas=function(b){if(b.x<0)b.x=0;if(b.x>c.width)b.x=c.width;if(b.y<0)b.y=0;if(b.y>c.height)b.y=
+c.height};c.getUrlParameters=function(){for(var b=[],c,d=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),h=0;h<d.length;h++)c=d[h].split("="),b.push(c[0]),b[c[0]]=c[1];return b};return c}(jaws||{}),jaws=function(c){var b={},d=[],e=[],f=[];c.setupInput=function(){var c=[];c[8]="backspace";c[9]="tab";c[13]="enter";c[16]="shift";c[17]="ctrl";c[18]="alt";c[19]="pause";c[20]="capslock";c[27]="esc";c[32]="space";c[33]="pageup";c[34]="pagedown";c[35]="end";c[36]="home";c[37]="left";
+c[38]="up";c[39]="right";c[40]="down";c[45]="insert";c[46]="delete";c[91]="leftwindowkey";c[92]="rightwindowkey";c[93]="selectkey";c[106]="multiply";c[107]="add";c[109]="subtract";c[110]="decimalpoint";c[111]="divide";c[144]="numlock";c[145]="scrollock";c[186]="semicolon";c[187]="equalsign";c[188]="comma";c[189]="dash";c[190]="period";c[191]="forwardslash";c[192]="graveaccent";c[219]="openbracket";c[220]="backslash";c[221]="closebracket";c[222]="singlequote";for(var l="numpad1,numpad2,numpad3,numpad4,numpad5,numpad6,numpad7,numpad8,numpad9".split(","),
k="f1,f2,f3,f4,f5,f6,f7,f8,f9".split(","),m="0,1,2,3,4,5,6,7,8,9".split(","),n="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(","),j=0;m[j];j++)c[48+j]=m[j];for(j=0;n[j];j++)c[65+j]=n[j];for(j=0;l[j];j++)c[96+j]=l[j];for(j=0;k[j];j++)c[112+j]=k[j];d=c;window.onkeydown=function(c){event=c?c:window.event;var f=d[event.keyCode];b[f]=!0;e[f]&&(e[f](),c.preventDefault());g[f]&&c.preventDefault()};window.onkeyup=function(c){event=c?c:window.event;var e=d[event.keyCode];b[e]=!1;f[e]&&(f[e](),
c.preventDefault());g[e]&&c.preventDefault()};window.onkeypress=function(){}};var g=[];c.preventDefaultKeys=function(b){b.forEach(function(b){g[b]=!0})};c.pressed=function(c){return b[c]};c.on_keydown=function(b,d){if(c.isArray(b))for(var f=0;b[f];f++)e[b[f]]=d;else e[b]=d};c.on_keyup=function(b,d){if(c.isArray(b))for(var e=0;b[e];e++)f[b[e]]=d;else f[b]=d};c.clearKeyCallbacks=function(){f=[];e=[]};return c}(jaws||{}),jaws=function(c){c.Assets=function(){this.loaded=[];this.loading=[];this.src_list=
[];this.data=[];this.fuchia_to_transparent=this.image_to_canvas=!0;this.root="";this.file_type={};this.file_type.json="json";this.file_type.wav="audio";this.file_type.mp3="audio";this.file_type.ogg="audio";this.file_type.png="image";this.file_type.jpg="image";this.file_type.jpeg="image";this.file_type.gif="image";this.file_type.bmp="image";this.file_type.tiff="image";var b=this;this.length=function(){return this.src_list.length};this.get=function(d){if(c.isArray(d))return d.map(function(c){return b.data[c]});
@@ -15,8 +15,8 @@ g.image.src=b;break;case "audio":b=g.src+"?"+parseInt(Math.random()*1E7);g.audio
b.loading[e]=!1;if(f=="json"){if(this.readyState!=4)return;b.data[d.src]=JSON.parse(this.responseText)}else if(f=="image"){e=b.image_to_canvas?c.imageToCanvas(d.image):d.image;if(b.fuchia_to_transparent&&b.getPostfix(d.src)=="bmp"){canvas=c.isImage(e)?c.imageToCanvas(e):e;for(var e=canvas.getContext("2d"),f=e.getImageData(0,0,canvas.width,canvas.height),g=f.data,h=0;h<g.length;h+=4)g[h]==255&&g[h+1]==0&&g[h+2]==255&&(g[h+3]=0);e.putImageData(f,0,0);e=canvas}b.data[d.src]=e}else if(f=="audio")d.audio.removeEventListener("canplay",
b.assetLoaded,!1),b.data[d.src]=d.audio;b.load_count++;b.processCallbacks(d,!0)};this.assetError=function(){var c=this.asset;b.error_count++;b.processCallbacks(c,!1)};this.processCallbacks=function(c,e){var f=parseInt((b.load_count+b.error_count)/b.src_list.length*100);if(e){if(b.onload)b.onload(c.src,f);if(c.onload)c.onload()}else{if(b.onerror)b.onerror(c.src,f);if(c.onerror)c.onerror(c)}if(f==100){if(b.onfinish)b.onfinish();b.onload=null;b.onerror=null;b.onfinish=null}}};c.assets=new c.Assets;return c}(jaws||
{}),jaws=function(c){function b(b){this.size=b;this.values=Array(this.size);this.add=function(b){if(this.values.length>this.size){this.values.splice(0,1);for(var c=this.value=0;this.values[c];c++)this.value+=this.values[c];this.value/=this.size}this.values.push(b);return this};this.get=function(){return parseInt(this.value)}}window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||
-function(b){window.setTimeout(b,16.666)}}();c.GameLoop=function(d,e,f,g){this.fps=this.tick_duration=this.ticks=0;var h=!1,l=!1,k=this,m=new b(20);this.start=function(){c.log("gameloop start",!0);this.current_tick=(new Date).getTime();this.last_tick=(new Date).getTime();d&&d();step_delay=1E3/g;requestAnimFrame(this.loop);c.log("gameloop loop",!0)};this.loop=function(){k.current_tick=(new Date).getTime();k.tick_duration=k.current_tick-k.last_tick;k.fps=m.add(1E3/k.tick_duration).get();h||(e&&e(),f&&
-f(),k.ticks++,l||requestAnimFrame(k.loop));k.last_tick=k.current_tick};this.pause=function(){h=!0};this.unpause=function(){h=!1};this.stop=function(){l=!0}};return c}(jaws||{}),jaws=function(c){c.Rect=function(b,c,e,f){this.x=b;this.y=c;this.width=e;this.height=f;this.right=b+e;this.bottom=c+f};c.Rect.prototype.getPosition=function(){return[this.x,this.y]};c.Rect.prototype.move=function(b,c){this.x+=b;this.y+=c;this.right+=b;this.bottom+=c;return this};c.Rect.prototype.moveTo=function(b,c){this.x=
+function(b){window.setTimeout(b,16.666)}}();c.GameLoop=function(d,e,f,g){this.fps=this.tick_duration=this.ticks=0;var h=!1,l=!1,k=this,m=new b(20);this.start=function(){c.log("game loop start",!0);this.current_tick=(new Date).getTime();this.last_tick=(new Date).getTime();d&&d();step_delay=1E3/g;requestAnimFrame(this.loop);c.log("game loop loop",!0)};this.loop=function(){k.current_tick=(new Date).getTime();k.tick_duration=k.current_tick-k.last_tick;k.fps=m.add(1E3/k.tick_duration).get();h||(e&&e(),
+f&&f(),k.ticks++,l||requestAnimFrame(k.loop));k.last_tick=k.current_tick};this.pause=function(){h=!0};this.unpause=function(){h=!1};this.stop=function(){l=!0}};return c}(jaws||{}),jaws=function(c){c.Rect=function(b,c,e,f){this.x=b;this.y=c;this.width=e;this.height=f;this.right=b+e;this.bottom=c+f};c.Rect.prototype.getPosition=function(){return[this.x,this.y]};c.Rect.prototype.move=function(b,c){this.x+=b;this.y+=c;this.right+=b;this.bottom+=c;return this};c.Rect.prototype.moveTo=function(b,c){this.x=
b;this.y=c;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};c.Rect.prototype.resize=function(b,c){this.width+=b;this.height+=c;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};c.Rect.prototype.resizeTo=function(b,c){this.width=b;this.height=c;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};c.Rect.prototype.draw=function(){c.context.strokeStyle="red";c.context.strokeRect(this.x,this.y,this.width,this.height);return this};c.Rect.prototype.collidePoint=
function(b,c){return b>=this.x&&b<=this.right&&c>=this.y&&c<=this.bottom};c.Rect.prototype.collideRect=function(b){return(this.x>=b.x&&this.x<=b.right||b.x>=this.x&&b.x<=this.right)&&(this.y>=b.y&&this.y<=b.bottom||b.y>=this.y&&b.y<=this.bottom)};c.Rect.prototype.toString=function(){return"[Rect "+this.x+", "+this.y+", "+this.width+", "+this.height+"]"};return c}(jaws||{});if(typeof module!=="undefined"&&"exports"in module)module.exports=jaws.Rect;
jaws=function(c){c.Sprite=function(b){this.options=b;this.set(b);(this.context=b.context||c.context)||this.createDiv()};c.Sprite.prototype.set=function(b){this.scale_factor_x=this.scale_factor_y=b.scale||1;this.x=b.x||0;this.y=b.y||0;this.alpha=b.alpha===void 0?1:b.alpha;this.angle=b.angle||0;this.flipped=b.flipped||!1;this.anchor(b.anchor||"top_left");if(!b.anchor_x==void 0)this.anchor_x=b.anchor_x;if(!b.anchor_y==void 0)this.anchor_y=b.anchor_y;b.image&&this.setImage(b.image);b.scale_image&&this.scaleImage(b.scale_image);
@@ -28,22 +28,21 @@ b;return this.cacheOffsets()};c.Sprite.prototype.scaleHeight=function(b){this.sc
this.anchor_x),this.bottom_offset=this.height*(1-this.anchor_y),this.cached_rect&&this.cached_rect.resizeTo(this.width,this.height),this};c.Sprite.prototype.rect=function(){if(!this.cached_rect)this.cached_rect=new c.Rect(this.x,this.top,this.width,this.height);this.cached_rect.moveTo(this.x-this.left_offset,this.y-this.top_offset);return this.cached_rect};c.Sprite.prototype.createDiv=function(){this.div=document.createElement("div");this.div.style.position="absolute";if(this.image)this.div.style.width=
this.image.width+"px",this.div.style.height=this.image.height+"px",this.div.style.backgroundImage=this.image.toDataURL?"url("+this.image.toDataURL()+")":"url("+this.image.src+")";c.dom&&c.dom.appendChild(this.div);this.updateDiv()};c.Sprite.prototype.updateDiv=function(){this.div.style.left=this.x+"px";this.div.style.top=this.y+"px";var b="";b+="rotate("+this.angle+"deg) ";b+=this.flipped?"scale(-"+this.scale_factor_x+","+this.scale_factor_y+")":"scale("+this.scale_factor_x+","+this.scale_factor_y+
")";this.div.style.MozTransform=b;this.div.style.WebkitTransform=b;this.div.style.OTransform=b;this.div.style.msTransform=b;this.div.style.transform=b;return this};c.Sprite.prototype.draw=function(){if(!this.image)return this;if(c.dom)return this.updateDiv();this.context.save();this.context.translate(this.x,this.y);this.angle!=0&&c.context.rotate(this.angle*Math.PI/180);this.flipped&&this.context.scale(-1,1);this.context.globalAlpha=this.alpha;this.context.translate(-this.left_offset,-this.top_offset);
-this.context.drawImage(this.image,0,0,this.width,this.height);this.context.restore();return this};c.Sprite.prototype.scaleImage=function(b){if(this.image)return this.setImage(c.gfx.retroScaleImage(this.image,b)),this};c.Sprite.prototype.asCanvasContext=function(){var b=document.createElement("canvas");b.width=this.width;b.height=this.height;b=b.getContext("2d");b.mozImageSmoothingEnabled=c.context.mozImageSmoothingEnabled;b.drawImage(this.image,0,0,this.width,this.height);return b};c.Sprite.prototype.toString=
-function(){return"[Sprite "+this.x+", "+this.y+","+this.width+","+this.height+"]"};return c}(jaws||{});
+this.context.drawImage(this.image,0,0,this.width,this.height);this.context.restore();return this};c.Sprite.prototype.scaleImage=function(b){if(this.image)return this.setImage(c.gfx.retroScaleImage(this.image,b)),this};c.Sprite.prototype.asCanvasContext=function(){var b=document.createElement("canvas");b.width=this.width;b.height=this.height;b=b.getContext("2d");b.mozImageSmoothingEnabled=c.context.mozImageSmoothingEnabled;b.drawImage(this.image,0,0,this.width,this.height);return b};c.Sprite.prototype.asCanvas=
+function(){var b=document.createElement("canvas");b.width=this.width;b.height=this.height;var d=b.getContext("2d");d.mozImageSmoothingEnabled=c.context.mozImageSmoothingEnabled;d.drawImage(this.image,0,0,this.width,this.height);return b};c.Sprite.prototype.toString=function(){return"[Sprite "+this.x+", "+this.y+","+this.width+","+this.height+"]"};return c}(jaws||{});
jaws=function(c){c.SpriteList=function(){};c.SpriteList.prototype=[];c.SpriteList.prototype.remove=function(b){b=this.indexOf(b);b>-1&&this.splice(b,1)};c.SpriteList.prototype.draw=function(){for(i=0;this[i];i++)this[i].draw()};c.SpriteList.prototype.drawIf=function(b){for(i=0;this[i];i++)b(this[i])&&this[i].draw()};c.SpriteList.prototype.update=function(){for(i=0;this[i];i++)this[i].update()};c.SpriteList.prototype.updateIf=function(b){for(i=0;this[i];i++)b(this[i])&&this[i].update()};c.SpriteList.prototype.deleteIf=
function(b){for(var c=0;this[c];c++)b(this[c])&&this.splice(c,1)};c.SpriteList.prototype.removeIf=function(b){for(var c=0;this[c];c++)b(this[c])&&this.splice(c,1)};c.SpriteList.prototype.toString=function(){return"[SpriteList "+this.length+" sprites]"};return c}(jaws||{});
jaws=function(c){function b(b,c,f,g,h){var l=document.createElement("canvas");l.width=g;l.height=h;l.getContext("2d").drawImage(b,c,f,g,h,0,0,l.width,l.height);return l}c.SpriteSheet=function(d){this.image=c.isDrawable(d.image)?d.image:c.assets.data[d.image];this.orientation=d.orientation||"right";this.frame_size=d.frame_size||[32,32];this.frames=[];for(d=0;d<this.image.width;d+=this.frame_size[0])for(var e=0;e<this.image.height;e+=this.frame_size[1])this.frames.push(b(this.image,d,e,this.frame_size[0],
this.frame_size[1]))};c.SpriteSheet.prototype.toString=function(){return"[SpriteSheet "+this.frames.length+" frames]"};return c}(jaws||{});
jaws=function(c){c.Parallax=function(b){this.scale=b.scale||1;this.repeat_x=b.repeat_x;this.repeat_y=b.repeat_y;this.camera_x=b.camera_x||0;this.camera_y=b.camera_y||0;this.layers=[]};c.Parallax.prototype.draw=function(){for(var b,d,e,f=0;f<this.layers.length;f++){b=this.layers[f];d=b.x;e=b.y;b.x=-(this.camera_x/b.damping);for(b.y=-(this.camera_y/b.damping);this.repeat_x&&b.x>0;)b.x-=b.width;for(;this.repeat_y&&b.y>0;)b.y-=b.width;for(;this.repeat_x&&b.x<c.width;){for(;this.repeat_y&&b.y<c.height;)b.draw(),
-b.y+=b.height;b.y=e;b.draw();b.x+=b.width-1}for(;b.repeat_y&&!b.repeat_x&&b.y<c.height;)b.draw(),b.y+=b.height;b.x=d}};c.Parallax.prototype.addLayer=function(b){b=new c.ParallaxLayer(b);b.scale(this.scale);this.layers.push(b)};c.Parallax.prototype.toString=function(){return"[Parallax "+this.x+", "+this.y+". "+this.layers.length+" layers]"};c.ParallaxLayer=function(b){this.damping=b.damping||0;c.Sprite.call(this,b)};c.ParallaxLayer.prototype=c.Sprite.prototype;c.ParallaxLayer.prototype.toString=function(){return"[ParallaxLayer "+
-this.x+", "+this.y+"]"};return c}(jaws||{});
+b.y+=b.height;b.y=e;b.draw();b.x+=b.width-1}for(;b.repeat_y&&!b.repeat_x&&b.y<c.height;)b.draw(),b.y+=b.height;b.x=d}};c.Parallax.prototype.addLayer=function(b){b=new c.ParallaxLayer(b);b.scale(this.scale);this.layers.push(b)};c.Parallax.prototype.toString=function(){return"[Parallax "+this.x+", "+this.y+". "+this.layers.length+" layers]"};c.ParallaxLayer=function(b){this.damping=b.damping||0;c.Sprite.call(this,b)};c.ParallaxLayer.prototype=c.Sprite.prototype;return c}(jaws||{});
jaws=function(c){c.Animation=function(b){this.options=b;this.frames=b.frames||[];this.frame_duration=b.frame_duration||100;this.index=b.index||0;this.loop=b.loop==void 0?1:b.loop;this.bounce=b.bounce||0;this.frame_direction=1;this.frame_size=b.frame_size;if(b.scale_image){var d=c.isDrawable(b.sprite_sheet)?b.sprite_sheet:c.assets.get(b.sprite_sheet);this.frame_size[0]*=b.scale_image;this.frame_size[1]*=b.scale_image;b.sprite_sheet=c.gfx.retroScaleImage(d,b.scale_image)}if(b.sprite_sheet)d=c.isDrawable(b.sprite_sheet)?
b.sprite_sheet:c.assets.get(b.sprite_sheet),this.frames=(new c.SpriteSheet({image:d,frame_size:this.frame_size})).frames;this.current_tick=(new Date).getTime();this.last_tick=(new Date).getTime();this.sum_tick=0};c.Animation.prototype.update=function(){this.current_tick=(new Date).getTime();this.sum_tick+=this.current_tick-this.last_tick;this.last_tick=this.current_tick;if(this.sum_tick>this.frame_duration)this.index+=this.frame_direction,this.sum_tick=0;if(this.index>=this.frames.length||this.index<=
0)if(this.bounce)this.frame_direction=-this.frame_direction,this.index+=this.frame_direction*2;else if(this.loop)this.index=0;return this};c.Animation.prototype.slice=function(b,d){var e={};e.frame_duration=this.frame_duration;e.loop=this.loop;e.bounce=this.bounce;e.frame_direction=this.frame_direction;e.frames=this.frames.slice().slice(b,d);return new c.Animation(e)};c.Animation.prototype.next=function(){this.update();return this.frames[this.index]};c.Animation.prototype.currentFrame=function(){return this.frames[this.index]};
c.Animation.prototype.toString=function(){return"[Animation, "+this.frames.length+" frames]"};return c}(jaws||{});
jaws=function(c){c.Viewport=function(b){this.options=b;this.context=b.context||c.context;this.width=b.width||c.width;this.height=b.height||c.height;this.max_x=b.max_x||c.width;this.max_y=b.max_y||c.height;var d=this;this.move=function(b,c){b&&(this.x+=b);c&&(this.y+=c);this.verifyPosition()};this.moveTo=function(b,c){if(b!=void 0)this.x=b;if(c!=void 0)this.y=c;this.verifyPosition()};this.isOutside=function(b){return!d.isInside(b)};this.isInside=function(b){return b.x>=d.x&&b.x<=d.x+d.width&&b.y>=
-d.y&&b.y<=d.y+d.height};this.centerAround=function(b){this.x=b.x-this.width/2;this.y=b.y-this.height/2;this.verifyPosition()};this.forceInsideVisibleArea=function(b,d){if(b.x<this.x+d)b.x=this.x+d;if(b.x>this.x+c.width-d)b.x=this.x+c.width-d;if(b.y<this.y+d)b.y=this.y+d;if(b.y>this.y+c.height-d)b.y=this.y+c.height-d};this.apply=function(b){this.context.save();this.context.translate(-this.x,-this.y);b();this.context.restore()};this.verifyPosition=function(){var b=this.max_x-this.width;if(this.x<0)this.x=
-0;if(this.x>b)this.x=b;b=this.max_y-this.height;if(this.y<0)this.y=0;if(this.y>b)this.y=b};this.moveTo(b.x||0,b.y||0)};c.Viewport.prototype.toString=function(){return"[Viewport "+this.x+", "+this.y+","+this.width+","+this.height+"]"};return c}(jaws||{});
+d.y&&b.y<=d.y+d.height};this.centerAround=function(b){this.x=b.x-this.width/2;this.y=b.y-this.height/2;this.verifyPosition()};this.forceInsideVisibleArea=function(b,d){if(b.x<this.x+d)b.x=this.x+d;if(b.x>this.x+c.width-d)b.x=this.x+c.width-d;if(b.y<this.y+d)b.y=this.y+d;if(b.y>this.y+c.height-d)b.y=this.y+c.height-d};this.forceInside=function(b,c){if(b.x<c)b.x=c;if(b.x>this.max_x-c)b.x=this.max_x-c;if(b.y<c)b.y=c;if(b.y>this.max_y-c)b.y=this.max_y-c};this.apply=function(b){this.context.save();this.context.translate(-this.x,
+-this.y);b();this.context.restore()};this.verifyPosition=function(){var b=this.max_x-this.width;if(this.x<0)this.x=0;if(this.x>b)this.x=b;b=this.max_y-this.height;if(this.y<0)this.y=0;if(this.y>b)this.y=b};this.moveTo(b.x||0,b.y||0)};c.Viewport.prototype.toString=function(){return"[Viewport "+this.x+", "+this.y+","+this.width+","+this.height+"]"};return c}(jaws||{});
jaws=function(c){c.TileMap=function(b){this.cell_size=b.cell_size||[32,32];this.size=b.size||[100,100];this.cells=Array(this.size[0]);this.sortFunction=void 0;for(b=0;b<this.size[0];b++){this.cells[b]=Array(this.size[1]);for(var c=0;c<this.size[1];c++)this.cells[b][c]=[]}};c.TileMap.prototype.clear=function(){for(var b=0;b<this.size[0];b++)for(var c=0;c<this.size[1];c++)this.cells[b][c]=[]};c.TileMap.prototype.sortCells=function(b){for(var c=0;c<this.size[0];c++)for(var e=0;e<this.size[1];e++)this.cells[c][e].sort(b)};
c.TileMap.prototype.push=function(b){if(b.length){for(var c=0;c<b.length;c++)this.push(b[c]);return b}if(b.rect)return this.pushAsRect(b,b.rect());else{var c=parseInt(b.x/this.cell_size[0]),e=parseInt(b.y/this.cell_size[1]);return this.pushToCell(c,e,b)}};c.TileMap.prototype.pushAsPoint=function(b){if(Array.isArray(b)){for(var c=0;c<b.length;c++)this.pushAsPoint(b[c]);return b}else{var c=parseInt(b.x/this.cell_size[0]),e=parseInt(b.y/this.cell_size[1]);return this.pushToCell(c,e,b)}};c.TileMap.prototype.pushAsRect=
function(b,c){for(var e=parseInt(c.x/this.cell_size[0]),f=parseInt((c.right-1)/this.cell_size[0]);e<=f;e++)for(var g=parseInt(c.y/this.cell_size[1]),h=parseInt((c.bottom-1)/this.cell_size[1]);g<=h;g++)this.pushToCell(e,g,b);return b};c.TileMap.prototype.pushToCell=function(b,c,e){this.cells[b][c].push(e);this.sortFunction&&this.cells[b][c].sort(this.sortFunction);return this};c.TileMap.prototype.at=function(b,c){var e=parseInt(b/this.cell_size[0]),f=parseInt(c/this.cell_size[1]);return this.cells[e][f]};
View
67 jaws.js
@@ -125,7 +125,7 @@ function findOrCreateCanvas() {
}
/**
- * Quick and easy startup of a jaws gameloop
+ * Quick and easy startup of a jaws game loop
*
* @example
* jaws.start(MyGame) // Start game state Game() with default options
@@ -177,9 +177,9 @@ jaws.start = function(game_state, options) {
if( game_state && jaws.isFunction(game_state) ) { game_state = new game_state }
if(!game_state) { game_state = window }
- jaws.gameloop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, wanted_fps)
+ jaws.game_loop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, wanted_fps)
jaws.game_state = game_state
- jaws.gameloop.start()
+ jaws.game_loop.start()
}
jaws.log("assets.loadAll()", true)
@@ -211,7 +211,7 @@ jaws.start = function(game_state, options) {
*
*/
jaws.switchGameState = function(game_state) {
- jaws.gameloop.stop()
+ jaws.game_loop.stop()
jaws.clearKeyCallbacks() // clear out all keyboard callbacks
@@ -219,8 +219,8 @@ jaws.switchGameState = function(game_state) {
jaws.previous_game_state = jaws.game_state
jaws.game_state = game_state
- jaws.gameloop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, jaws.gameloop.fps)
- jaws.gameloop.start()
+ jaws.game_loop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, jaws.game_loop.fps)
+ jaws.game_loop.start()
}
/**
@@ -271,6 +271,7 @@ jaws.isString = function(obj) {
/** returns true if obj is an Array */
jaws.isArray = function(obj) {
+ if(obj === undefined) return false;
return !(obj.constructor.toString().indexOf("Array") == -1)
}
@@ -740,7 +741,7 @@ window.requestAnimFrame = (function(){
})();
/**
- * @class A classic gameloop forever looping calls to update() / draw() with given framerate
+ * @class A classic game loop forever looping calls to update() / draw() with given framerate
*
* @example
*
@@ -748,8 +749,8 @@ window.requestAnimFrame = (function(){
* ... your stuff executed every 30 FPS ...
* }
*
- * gameloop = new jaws.GameLoop(setup, update, draw, 30)
- * gameloop.start()
+ * game_loop = new jaws.GameLoop(setup, update, draw, 30)
+ * game_loop.start()
*
* // You can also use the shortcut jaws.start(), it will:
* // 1) Load all assets with jaws.assets.loadAll()
@@ -768,9 +769,9 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
var that = this
var mean_value = new MeanValue(20) // let's have a smooth, non-jittery FPS-value
- /** Start the gameloop by calling setup() once and then loop update()/draw() forever with given FPS */
+ /** Start the game loop by calling setup() once and then loop update()/draw() forever with given FPS */
this.start = function() {
- jaws.log("gameloop start", true)
+ jaws.log("game loop start", true)
this.current_tick = (new Date()).getTime();
this.last_tick = (new Date()).getTime();
if(setup) { setup() }
@@ -779,10 +780,10 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
// update_id = setInterval(this.loop, step_delay);
requestAnimFrame(this.loop)
- jaws.log("gameloop loop", true)
+ jaws.log("game loop loop", true)
}
- /** The core of the gameloop. Calculate a mean FPS and call update()/draw() if gameloop is not paused */
+ /** The core of the game loop. Calculate a mean FPS and call update()/draw() if game loop is not paused */
this.loop = function() {
that.current_tick = (new Date()).getTime();
that.tick_duration = that.current_tick - that.last_tick
@@ -797,13 +798,13 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
that.last_tick = that.current_tick;
}
- /** Pause the gameloop. loop() will still get called but not update() / draw() */
+ /** Pause the game loop. loop() will still get called but not update() / draw() */
this.pause = function() { paused = true }
- /** unpause the gameloop */
+ /** unpause the game loop */
this.unpause = function() { paused = false }
- /** Stop the gameloop */
+ /** Stop the game loop */
this.stop = function() {
if(update_id) clearInterval(update_id);
stopped = true;
@@ -1212,6 +1213,21 @@ jaws.Sprite.prototype.asCanvasContext = function() {
return context
}
+/**
+ * Returns sprite as a canvas
+ */
+jaws.Sprite.prototype.asCanvas = function() {
+ var canvas = document.createElement("canvas")
+ canvas.width = this.width
+ canvas.height = this.height
+
+ var context = canvas.getContext("2d")
+ context.mozImageSmoothingEnabled = jaws.context.mozImageSmoothingEnabled
+
+ context.drawImage(this.image, 0, 0, this.width, this.height)
+ return canvas
+}
+
jaws.Sprite.prototype.toString = function() { return "[Sprite " + this.x + ", " + this.y + "," + this.width + "," + this.height + "]" }
return jaws;
@@ -1421,8 +1437,10 @@ jaws.ParallaxLayer = function(options) {
jaws.Sprite.call(this, options)
}
jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
+
/** Debugstring for ParallaxLayer() */
-jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
+// This overwrites Sprites toString, find another sollution.
+// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
return jaws;
})(jaws || {});
@@ -1642,6 +1660,21 @@ jaws.Viewport = function(options) {
if(item.y < this.y+buffer) { item.y = this.y+buffer }
if(item.y > this.y+jaws.height-buffer) { item.y = this.y+jaws.height-buffer }
}
+
+ /**
+ * force 'item' inside the limits of the viewport
+ * using 'buffer' as indicator how close to the 'item' is allowed to go
+ *
+ * @example
+ * viewport.forceInside(player, 10)
+ */
+ this.forceInside = function(item, buffer) {
+ if(item.x < buffer) { item.x = buffer }
+ if(item.x > this.max_x-buffer) { item.x = this.max_x-buffer }
+ if(item.y < buffer) { item.y = buffer }
+ if(item.y > this.max_y-buffer) { item.y = this.max_y-buffer }
+ }
+
/**
* executes given draw-callback with a translated canvas which will draw items relative to the viewport
View
12 src/core.js
@@ -125,7 +125,7 @@ function findOrCreateCanvas() {
}
/**
- * Quick and easy startup of a jaws gameloop
+ * Quick and easy startup of a jaws game loop
*
* @example
* jaws.start(MyGame) // Start game state Game() with default options
@@ -177,9 +177,9 @@ jaws.start = function(game_state, options) {
if( game_state && jaws.isFunction(game_state) ) { game_state = new game_state }
if(!game_state) { game_state = window }
- jaws.gameloop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, wanted_fps)
+ jaws.game_loop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, wanted_fps)
jaws.game_state = game_state
- jaws.gameloop.start()
+ jaws.game_loop.start()
}
jaws.log("assets.loadAll()", true)
@@ -211,7 +211,7 @@ jaws.start = function(game_state, options) {
*
*/
jaws.switchGameState = function(game_state) {
- jaws.gameloop.stop()
+ jaws.game_loop.stop()
jaws.clearKeyCallbacks() // clear out all keyboard callbacks
@@ -219,8 +219,8 @@ jaws.switchGameState = function(game_state) {
jaws.previous_game_state = jaws.game_state
jaws.game_state = game_state
- jaws.gameloop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, jaws.gameloop.fps)
- jaws.gameloop.start()
+ jaws.game_loop = new jaws.GameLoop(game_state.setup, game_state.update, game_state.draw, jaws.game_loop.fps)
+ jaws.game_loop.start()
}
/**
View
20 src/game_loop.js
@@ -13,7 +13,7 @@ window.requestAnimFrame = (function(){
})();
/**
- * @class A classic gameloop forever looping calls to update() / draw() with given framerate
+ * @class A classic game loop forever looping calls to update() / draw() with given framerate
*
* @example
*
@@ -21,8 +21,8 @@ window.requestAnimFrame = (function(){
* ... your stuff executed every 30 FPS ...
* }
*
- * gameloop = new jaws.GameLoop(setup, update, draw, 30)
- * gameloop.start()
+ * game_loop = new jaws.GameLoop(setup, update, draw, 30)
+ * game_loop.start()
*
* // You can also use the shortcut jaws.start(), it will:
* // 1) Load all assets with jaws.assets.loadAll()
@@ -41,9 +41,9 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
var that = this
var mean_value = new MeanValue(20) // let's have a smooth, non-jittery FPS-value
- /** Start the gameloop by calling setup() once and then loop update()/draw() forever with given FPS */
+ /** Start the game loop by calling setup() once and then loop update()/draw() forever with given FPS */
this.start = function() {
- jaws.log("gameloop start", true)
+ jaws.log("game loop start", true)
this.current_tick = (new Date()).getTime();
this.last_tick = (new Date()).getTime();
if(setup) { setup() }
@@ -52,10 +52,10 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
// update_id = setInterval(this.loop, step_delay);
requestAnimFrame(this.loop)
- jaws.log("gameloop loop", true)
+ jaws.log("game loop loop", true)
}
- /** The core of the gameloop. Calculate a mean FPS and call update()/draw() if gameloop is not paused */
+ /** The core of the game loop. Calculate a mean FPS and call update()/draw() if game loop is not paused */
this.loop = function() {
that.current_tick = (new Date()).getTime();
that.tick_duration = that.current_tick - that.last_tick
@@ -70,13 +70,13 @@ jaws.GameLoop = function(setup, update, draw, wanted_fps) {
that.last_tick = that.current_tick;
}
- /** Pause the gameloop. loop() will still get called but not update() / draw() */
+ /** Pause the game loop. loop() will still get called but not update() / draw() */
this.pause = function() { paused = true }
- /** unpause the gameloop */
+ /** unpause the game loop */
this.unpause = function() { paused = false }
- /** Stop the gameloop */
+ /** Stop the game loop */
this.stop = function() {
if(update_id) clearInterval(update_id);
stopped = true;
Please sign in to comment.
Something went wrong with that request. Please try again.