diff --git a/jaws-min.js b/jaws-min.js index fc9fb2c..be7d92b 100644 --- a/jaws-min.js +++ b/jaws-min.js @@ -1,13 +1,13 @@ -/* Built at 2013-09-17 22:31:23 +0200 */ +/* Built at 2013-09-28 21:53:59 +0200 */ var jaws=function(b){function c(c){b.mouse_x=c.pageX||c.clientX;b.mouse_y=c.pageY||c.clientY;c=b.canvas?b.canvas:b.dom;b.mouse_x-=c.offsetLeft;b.mouse_y-=c.offsetTop}var d,e;b.Parallax=function(){b.log.error("To use jaws.Parallax() you need to include src/extras/parallax.js")};b.SpriteList=function(){b.log.error("To use SpriteList() you need to include src/extras/sprite_list.js")};b.TileMap=function(){b.log.error("To use TileMap() you need to include src/extras/tile_map.js")};b.PixelMap=function(){b.log.error("To use PixelMap() you need to include src/extras/pixel_map.js")}; b.QuadTree=function(){b.log.error("To use QuadTree() you need to include src/extras/quadtree.js")};b.Audio=function(){b.log.error("To use jaws.Audio() you need to include src/extras/audio.js")};b.title=function(c){if(b.isString(c))return c?d.innerHTML=c:d.innerHTML;b.log.error("jaws.title: Passed in value is not a String.")};b.unpack=function(){"Sprite SpriteList Animation Viewport SpriteSheet Parallax TileMap pressed QuadTree".split(" ").forEach(function(c){window[c]?b.log.warn("jaws.unpack: "+c+ " already exists in global namespace."):window[c]=b[c]})};b.log=function(c,f){b.isString(c)||(c=JSON.stringify(c));b.log.on&&(e&&b.log.use_log_element&&(e.innerHTML=f?e.innerHTML+(c+"
"):c),console.log&&b.log.use_console&&console.log("JawsJS: ",c))};b.log.on=!0;b.log.use_console=!1;b.log.use_log_element=!0;b.log.warn=function(c){console.warn&&b.log.use_console&&b.log.on?console.warn(c):b.log("[WARNING]: "+JSON.stringify(c),!0)};b.log.error=function(c){console.error&&b.log.use_console&&b.log.on? console.error(c):b.log("[ERROR]: "+JSON.stringify(c),!0)};b.log.info=function(c){console.info&&b.log.use_console&&b.log.on?console.info(c):b.log("[INFO]: "+JSON.stringify(c),!0)};b.log.debug=function(c){console.debug&&b.log.use_console&&b.log.on?console.debug(c):b.log("[DEBUG]: "+JSON.stringify(c),!0)};b.log.clear=function(){e&&(e.innerHTML="");console.clear&&console.clear()};b.init=function(k){d=document.getElementsByTagName("title")[0];b.url_parameters=b.getUrlParameters();b.canvas=document.getElementsByTagName("canvas")[0]; b.canvas||(b.dom=document.getElementById("canvas"));b.canvas?b.context=b.canvas.getContext("2d"):b.dom?b.dom.style.position="relative":(b.canvas=document.createElement("canvas"),b.canvas.width=k.width,b.canvas.height=k.height,b.context=b.canvas.getContext("2d"),document.body.appendChild(b.canvas));e=document.getElementById("jaws-log");b.url_parameters.debug&&!e&&(e=document.createElement("div"),e.id="jaws-log",e.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(e));b.url_parameters.bust_cache&&(b.log.info("Busting cache when loading assets"),b.assets.bust_cache=!0);b.context&&b.useCrispScaling();b.width=b.canvas?b.canvas.width:b.dom.offsetWidth;b.height=b.canvas?b.canvas.height:b.dom.offsetHeight;b.mouse_x=0;b.mouse_y=0;window.addEventListener("mousemove",c)};b.useCrispScaling=function(){b.context.imageSmoothingEnabled=!1;b.context.webkitImageSmoothingEnabled=!1;b.context.mozImageSmoothingEnabled=!1};b.useSmoothScaling=function(){b.context.imageSmoothingEnabled= -!0;b.context.webkitImageSmoothingEnabled=!0;b.context.mozImageSmoothingEnabled=!0};b.start=function(c,f,d){function e(c,k){b.log.info(k+"%: "+c,!0);f.loading_screen&&b.assets.displayProgress(k)}function h(c,k){b.log.info(k+"%: Error loading asset "+c,!0)}function p(){b.log.info("all assets loaded",!0);b.switchGameState(c||window,{fps:v},d)}if(b.isFunction(c))if(b.isObject(d)||void 0===d){f||(f={});var v=f.fps||60;void 0===f.loading_screen&&(f.loading_screen=!0);f.width||(f.width=500);f.height||(f.height= -300);b.init(f);f.loading_screen&&b.assets.displayProgress(0);b.log.info("setupInput()",!0);b.setupInput();b.log.info("assets.loadAll()",!0);0c.x||0>c.y||c.x>b.width||c.y>b.height};b.forceInsideCanvas=function(c){c.x&&c.y&&(0>c.x&&(c.x=0),c.x>b.width&&(c.x=b.width),0>c.y&&(c.y=0),c.y>b.height&&(c.y=b.height))};b.getUrlParameters=function(){for(var b=[],c,d=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"), @@ -36,10 +36,10 @@ b.context&&(b.context.save(),b.context.fillStyle="black",b.context.fillRect(0,0, b.width/2,b.height-1),b.context.restore())}};b.assets=new b.Assets;return b}(jaws||{}),jaws=function(b){function c(c){this.size=c;this.values=Array(this.size);this.value;this.add=function(c){if(this.values.length>this.size){this.values.splice(0,1);for(var b=this.value=0;this.values[b];b++)this.value+=this.values[b];this.value/=this.size}this.values.push(c);return this};this.get=function(){return parseInt(this.value)}}window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame|| window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(c,b){window.setTimeout(c,16.666)}}();b.GameLoop=function(d,e,k){if(!(this instanceof arguments.callee))return new arguments.callee(d,e);this.ticks=this.fps=this.tick_duration=0;var f,l=!1,g=!1,h=this,p=new c(20);this.runtime=function(){return this.last_tick-this.first_tick};this.start=function(){b.log.info("Game loop start",!0);this.first_tick=(new Date).getTime();this.current_tick=(new Date).getTime(); this.last_tick=(new Date).getTime();d.setup&&d.setup(k);step_delay=1E3/e.fps;60==e.fps?requestAnimFrame(this.loop):f=setInterval(this.loop,step_delay)};this.loop=function(){h.current_tick=(new Date).getTime();h.tick_duration=h.current_tick-h.last_tick;h.fps=p.add(1E3/h.tick_duration).get();g||l||(d.update&&d.update(),d.draw&&d.draw(),h.ticks++);60!=e.fps||g||requestAnimFrame(h.loop);h.last_tick=h.current_tick};this.pause=function(){l=!0};this.unpause=function(){l=!1};this.stop=function(){f&&clearInterval(f); -g=!0}};return b}(jaws||{}),jaws=function(b){b.Rect=function(c,b,e,k){if(!(this instanceof arguments.callee))return new arguments.callee(c,b,e,k);this.x=c;this.y=b;this.width=e;this.height=k;this.right=c+e;this.bottom=b+k};b.Rect.prototype.getPosition=function(){return[this.x,this.y]};b.Rect.prototype.move=function(c,b){this.x+=c;this.y+=b;this.right+=c;this.bottom+=b;return this};b.Rect.prototype.moveTo=function(c,b){this.x=c;this.y=b;this.right=this.x+this.width;this.bottom=this.y+this.height;return this}; -b.Rect.prototype.resize=function(c,b){this.width+=c;this.height+=b;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};b.Rect.prototype.clone=function(){return new b.Rect(this.x,this.y,this.width,this.height)};b.Rect.prototype.shrink=function(c,b){this.x+=c;this.y+=b;this.width-=c+c;this.height-=b+b;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};b.Rect.prototype.resizeTo=function(b,d){this.width=b;this.height=d;this.right=this.x+this.width;this.bottom= -this.y+this.height;return this};b.Rect.prototype.draw=function(){b.context.strokeStyle="red";b.context.strokeRect(this.x,this.y,this.width,this.height);return this};b.Rect.prototype.collidePoint=function(b,d){return b>=this.x&&b<=this.right&&d>=this.y&&d<=this.bottom};b.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)};b.Rect.prototype.toString=function(){return"[Rect "+this.x+ -", "+this.y+", "+this.width+", "+this.height+"]"};return b}(jaws||{});"undefined"!==typeof module&&"exports"in module&&(module.exports=jaws.Rect); +g=!0}};return b}(jaws||{}),jaws=function(b){b.Rect=function(c,b,e,k){if(!(this instanceof arguments.callee))return new arguments.callee(c,b,e,k);this.x=c;this.y=b;this.width=e;this.height=k;this.right=c+e-0;this.bottom=b+k-0};b.Rect.prototype.getPosition=function(){return[this.x,this.y]};b.Rect.prototype.move=function(c,b){this.x+=c;this.y+=b;this.right+=c;this.bottom+=b;return this};b.Rect.prototype.moveTo=function(c,b){this.x=c;this.y=b;this.right=this.x+this.width-0;this.bottom=this.y+this.height- +0;return this};b.Rect.prototype.resize=function(c,b){this.width+=c;this.height+=b;this.right=this.x+this.width-0;this.bottom=this.y+this.height-0;return this};b.Rect.prototype.clone=function(){return new b.Rect(this.x,this.y,this.width,this.height)};b.Rect.prototype.shrink=function(c,b){this.x+=c;this.y+=b;this.width-=c+c;this.height-=b+b;this.right=this.x+this.width-0;this.bottom=this.y+this.height-0;return this};b.Rect.prototype.resizeTo=function(b,d){this.width=b;this.height=d;this.right=this.x+ +this.width-0;this.bottom=this.y+this.height-0;return this};b.Rect.prototype.draw=function(){b.context.strokeStyle="red";b.context.strokeRect(this.x-0.5,this.y-0.5,this.width,this.height);return this};b.Rect.prototype.collidePoint=function(b,d){return b>=this.x&&b<=this.right&&d>=this.y&&d<=this.bottom};b.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)};b.Rect.prototype.toString= +function(){return"[Rect "+this.x+", "+this.y+", "+this.width+", "+this.height+"]"};return b}(jaws||{});"undefined"!==typeof module&&"exports"in module&&(module.exports=jaws.Rect); jaws=function(b){b.Sprite=function(c){if(!(this instanceof arguments.callee))return new arguments.callee(c);this.set(c);this.context=c.context?c.context:b.context};b.Sprite.prototype.default_options={x:0,y:0,alpha:1,angle:0,flipped:!1,anchor_x:0,anchor_y:0,image:null,image_path:null,anchor:null,scale_image:null,damping:1,scale_x:1,scale_y:1,scale:1,color:"#ddd",width:16,height:16,_constructor:null,context:null};b.Sprite.prototype.set=function(c){b.isString(this.image)&&(this.image_path=this.image); b.parseOptions(this,c,this.default_options);this.scale&&(this.scale_x=this.scale_y=this.scale);this.image&&this.setImage(this.image);this.scale_image&&this.scaleImage(this.scale_image);this.anchor&&this.setAnchor(this.anchor);if(!this.image&&this.color&&this.width&&this.height){c=document.createElement("canvas");var d=c.getContext("2d");c.width=this.width;c.height=this.height;d.fillStyle=this.color;d.fillRect(0,0,this.width,this.height);this.image=c}this.cacheOffsets();return this};b.Sprite.prototype.clone= function(b){b=new (this._constructor?eval(this._constructor):this.constructor)(this.attributes());b._constructor=this._constructor||this.constructor.name;return b};b.Sprite.prototype.setImage=function(c){var d=this;if(b.isDrawable(c))return this.image=c,this.cacheOffsets();b.assets.isLoaded(c)?(this.image=b.assets.get(c),this.cacheOffsets()):(b.log.warn("Image '"+c+"' not preloaded with jaws.assets.add(). Image and a working sprite.rect() will be delayed."),b.assets.load(c,{onload:function(){d.image= @@ -71,12 +71,13 @@ jaws=function(b){function c(b,c){for(var k=function(c){return void 0!==b.isSprit 0;l=c.x&&b.x<=c.right||c.x>=b.x&&c.x<=b.right)&&(b.y>=c.y&&b.y<=c.bottom||c.y>=b.y&&c.y<=b.bottom)};b.distanceBetween=function(b,c){return Math.sqrt(Math.pow(b.x-c.x,2)+Math.pow(b.y-c.y,2))};b.collide=function(c,e,k){if(c.rect&&e.forEach)return 0b)b=0;if(void 0===d||0>d)d=0;if(void 0===e||e>this.width)e=this.width;if(void 0===k||k>this.height)k=this.height;if(0==arguments.length)this.data=this.context.getImageData(b,d,e,k).data;else for(var f=this.context.getImageData(b,d,e,k).data,l=0,g=4*this.width,h=4*d*this.width+4*b,p=4*e,v=0;vb.x;e--)if(this.namedColorAt(e,k)==d||void 0===d)return this.namedColorAt(e,k);for(;k>b.y;k--)if(this.namedColorAt(e,k)==d||void 0===d)return this.namedColorAt(e,k);return!1};b.PixelMap.prototype.at=function(b,d){b=parseInt(b); -d=parseInt(d);0>d&&(d=0);var e=4*d*this.width+4*b;return[this.data[e],this.data[e+1],this.data[e+2],this.data[e+3]]};b.PixelMap.prototype.namedColorAt=function(b,d){for(var e=this.at(b,d),k=0;kb)b=0;if(void 0===d||0>d)d=0;if(void 0===e||e>this.width)e=this.width;if(void 0===k||k>this.height)k=this.height;if(0==arguments.length)this.data=this.context.getImageData(b,d,e,k).data;else for(var f=this.context.getImageData(b,d,e,k).data,l=0,g=4*this.width,h=4*d*this.width+4*b,p=4*e,v=0;vb.x;e--)if(this.namedColorAt(e,k)==d||void 0===d)return this.namedColorAt(e,k);for(;k>b.y;k--)if(this.namedColorAt(e,k)==d||void 0===d)return this.namedColorAt(e, +k);return!1};b.PixelMap.prototype.at=function(b,d){b=parseInt(b);d=parseInt(d);0>d&&(d=0);var e=4*d*this.width+4*b;return[this.data[e],this.data[e+1],this.data[e+2],this.data[e+3]]};b.PixelMap.prototype.namedColorAt=function(b,d){for(var e=this.at(b,d),k=0;kk;b.xe&&(f?d=0:k&&(d=3));return d};b.QuadTree.prototype.insert=function(b){if(b.hasOwnProperty("x")|| diff --git a/jaws.js b/jaws.js index 30683cb..4934a1c 100644 --- a/jaws.js +++ b/jaws.js @@ -1,4 +1,4 @@ -/* Built at 2013-09-17 22:31:22 +0200 */ +/* Built at 2013-09-28 21:53:58 +0200 */ /** * @namespace JawsJS core functions. * @@ -303,9 +303,18 @@ var jaws = (function(jaws) { * */ jaws.start = function(game_state, options, game_state_setup_options) { + if (!options) options = {}; - if (!jaws.isFunction(game_state)) { - jaws.log.error("jaws.start: Passed in GameState is not a function."); + var fps = options.fps || 60; + if (options.loading_screen === undefined) options.loading_screen = true; + if (!options.width) options.width = 500; + if (!options.height) options.height = 300; + + /* Takes care of finding/creating canvas-element and debug-div */ + jaws.init(options); + + if (!jaws.isFunction(game_state) && !jaws.isObject(game_state)) { + jaws.log.error("jaws.start: Passed in GameState is niether function or object"); return; } if (!jaws.isObject(game_state_setup_options) && game_state_setup_options !== undefined) { @@ -313,18 +322,6 @@ var jaws = (function(jaws) { return; } - if (!options) - options = {}; - var fps = options.fps || 60; - if (options.loading_screen === undefined) - options.loading_screen = true; - if (!options.width) - options.width = 500; - if (!options.height) - options.height = 300; - - jaws.init(options); - if (options.loading_screen) { jaws.assets.displayProgress(0); } @@ -630,7 +627,7 @@ var jaws = (function(jaws) { } } for (var option in defaults) { - if( jaws.isFunction(defaults[option]) ) defaults[option] = defaults[option](); + if( jaws.isFunction(defaults[option]) ) defaults[option] = defaults[option](); object[option] = (options[option] !== undefined) ? options[option] : jaws.clone(defaults[option]); } }; @@ -1659,6 +1656,19 @@ return jaws; var jaws = (function(jaws) { +/* +* 2013-09-28: +* +* For a 10x10 sprite in the topleft corner, should sprite.rect().bottom be 9 or 10? +* There's no right or wrong answer. In some cases 9 makes sense (if checking directly for pixel-values for example). +* In other cases 10 makes sense (bottom = x + height). +* +* The important part is beeing consistent across the lib/game. +* Jaws started out with bottom = x + height so we'll continue with that way until good reasons to change come up. +* Therefore correction = 0 for now. +*/ +var correction = 0; + /** @class A Basic rectangle. @example @@ -1678,8 +1688,8 @@ jaws.Rect = function Rect(x, y, width, height) { this.y = y this.width = width this.height = height - this.right = x + width - this.bottom = y + height + this.right = x + width - correction + this.bottom = y + height - correction } /** Return position as [x,y] */ @@ -1700,16 +1710,16 @@ jaws.Rect.prototype.move = function(x, y) { jaws.Rect.prototype.moveTo = function(x, y) { this.x = x this.y = y - this.right = this.x + this.width - this.bottom = this.y + this.height + this.right = this.x + this.width - correction + this.bottom = this.y + this.height - correction return this } /** Modify width and height */ jaws.Rect.prototype.resize = function(width, height) { this.width += width this.height += height - this.right = this.x + this.width - this.bottom = this.y + this.height + this.right = this.x + this.width - correction + this.bottom = this.y + this.height - correction return this } @@ -1724,8 +1734,8 @@ jaws.Rect.prototype.shrink = function(x, y) { this.y += y this.width -= (x+x) this.height -= (y+y) - this.right = this.x + this.width - this.bottom = this.y + this.height + this.right = this.x + this.width - correction + this.bottom = this.y + this.height - correction return this } @@ -1733,15 +1743,15 @@ jaws.Rect.prototype.shrink = function(x, y) { jaws.Rect.prototype.resizeTo = function(width, height) { this.width = width this.height = height - this.right = this.x + this.width - this.bottom = this.y + this.height + this.right = this.x + this.width - correction + this.bottom = this.y + this.height - correction return this } /** Draw rect in color red, useful for debugging */ jaws.Rect.prototype.draw = function() { jaws.context.strokeStyle = "red" - jaws.context.strokeRect(this.x, this.y, this.width, this.height) + jaws.context.strokeRect(this.x-0.5, this.y-0.5, this.width, this.height) return this } @@ -2905,6 +2915,9 @@ jaws.PixelMap = function PixelMap(options) { this.options = options this.scale = options.scale || 1 + this.x = options.x || 0 + this.y = options.y || 0 + if(options.image) { this.setContext(options.image); @@ -2966,14 +2979,14 @@ jaws.PixelMap.prototype.update = function(x, y, width, height) { * Draws the pixel map on the maincanvas */ jaws.PixelMap.prototype.draw = function() { - jaws.context.drawImage(this.context.canvas, 0, 0, this.width, this.height) + jaws.context.drawImage(this.context.canvas, this.x, this.y, this.width, this.height) } /** * Trace the outline of a Rect until a named color found. * -* @param {object} Rect Instance of jaws.Rect() -* @param {string} Only look for this named color +* @param {object} Rect Instance of jaws.Rect() +* @param {string} Color_Filter Only look for this named color * * @return {string} name of found color */ @@ -2981,8 +2994,8 @@ jaws.PixelMap.prototype.namedColorAtRect = function(rect, color) { var x = rect.x var y = rect.y - for(; x < rect.right; x++) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); - for(; y < rect.bottom; y++) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); + for(; x < rect.right-1; x++) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); + for(; y < rect.bottom-1; y++) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); for(; x > rect.x; x--) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); for(; y > rect.y; y--) if(this.namedColorAt(x, y) == color || color===undefined) return this.namedColorAt(x,y); @@ -3058,15 +3071,13 @@ var jaws = (function(jaws) { } jaws.Parallax.prototype.default_options = { - width: function() { jaws.width }, - height: function() { jaws.height }, + width: function() { return jaws.width }, + height: function() { return jaws.height }, scale: 1, repeat_x: null, repeat_y: null, camera_x: 0, camera_y: 0, - width: null, - height: null, layers: [] }