Commits

kiswa committed 044f60c Merge

Merging dev branch into default.

Comments (0)

Files changed (8)

-kilo
-----------
  .sh for Linux/Mac) to generate kilo output files.
  minified and is meant for production use.
---------
-------
----------
+kilo
-----------
  .sh for Linux/Mac) to generate kilo output files.
  minified and is meant for production use.
---------
-------
-----
----------

demo/tiledbg.html

 
                         // Adjust the backgrounds.
                         demo.bg0.setOffsetX(this.pos.x / 2);
-                        if (this.pos.x < 380) {
-                            demo.bg1.setOffsetX(this.pos.x);
-                        }
+                        demo.bg1.setOffsetX(this.pos.x);
                     }
                 };
             });
                 background-color: #333;
             }
             h1 {
-                font-size: 2em;
+                font-size: 3em;
                 text-align: center;
                 border-bottom: 1px solid #333;
+                padding-bottom: .5em;
                 margin-bottom: 1em;
+                text-shadow: 1px 1px 3px #fff;
             }
             p {
                 margin: 1em 0;
                 padding: 1em;
                 text-align: left;
                 background-color: #ccc;
+                box-shadow: 0px 0px 5px #ccc;
             }
             .demo {
                 margin: 2em 0;
             <p id="warn">JavaScript is not enabled!</p>
             <p>This page itself is a demo of sorts. It demonstrates checking for
             support of elements kilo requires, as well as whether JavaScript is
-            enabled. If there are any problems, a warning is displayed above.</p>
+            enabled. If there are any problems, a warning is displayed above
+            this paragraph.</p>
+            <p>Don't forget to build kilo before attempting to run any of these
+            demos!</p>
             <div class="demo">
                 <h3><a href="demo/sprite.html">Sprite</a></h3>
                 <p>Demonstrates using the kilo.Sprite object to create static
 
 (function (kilo) {
 
+    // A simple wrapper around a hash object to prevent overwriting existing
+    // keys (properties) in the hash.
     kilo.Cache = kilo.Class.create(function () {
         var hash = {};
 
             // Update an existing object (overwrites existing objects)
             set: function (key, obj) {
                 hash[key] = obj;
+
+                return this;
             },
 
             // Add a new object - won't overwrite existing object at provided key
                 if (hash[key] === undefined) {
                     hash[key] = obj;
                 }
+
+                return this;
             },
 
             // Remove a cached object (by setting to undefined
                 if (hash[key] !== undefined) {
                     delete hash[key];
                 }
+
+                return this;
             }
         };
     });
 
 var kilo = (function () {
     var kilo = {
-        version: "0.6",
+        version: "0.7",
 
         // http://mir.aculo.us/2010/06/04/making-an-ipad-html5-app-making-it-really-fast/
         supportsTouch: Object.prototype.hasOwnProperty.call(document, 'createTouch')

src/gamecanvas.js

             quadTree,
             sprites = [],
             backgrounds = [],
+            foregrounds = [],
 
             updateOffset = function () {
                 var x = 0, y = 0,
                 quadTree.insert(sprites);
             },
 
+            // Draws the backgrounds, sprites, foregrounds and quadtree in this
+            // order. Also draws items in each array in the order they were
+            // added so overlaps are consistent and controllable.
             draw = function () {
-                // Using for loop because order is important here.
-                for (var i = 0, l = backgrounds.length; i < l; i++) {
+                var i, l;
+
+                for (i = 0, l = backgrounds.length; i < l; i++) {
                     backgrounds[i].draw();
                 }
 
-                l = sprites.length;
-                while (l--) {
-                    sprites[l].draw();
+                for (i = 0, l = sprites.length; i < l; i++) {
+                    sprites[i].draw();
+                }
+
+                for (i = 0, l = foregrounds.length; i < l; i++) {
+                    foregrounds[i].draw();
                 }
 
                 if (kilo.gameCanvas.showTree) {
                             this.removeBackground(background[l]);
                         }
                     }
+                },
+
+                addForeground: function (foreground) {
+                    if (foreground instanceof kilo.TiledBG) {
+                        foregrounds.push(foreground);
+                    } else if (foreground instanceof Array) {
+                        var l = foreground.length;
+                        while (l--) {
+                            this.addForeground(foreground[l]);
+                        }
+                    }
+                },
+
+                removeForeground: function (foreground) {
+                    if (foreground instanceof kilo.TiledBG) {
+                        var index = foregrounds.indexOf(foreground);
+                        if (index !== -1) {
+                            foregrounds.splice(index, 1);
+                        }
+                    } else if (foreground instanceof Array) {
+                        var l = foreground.length;
+                        while (l--) {
+                            this.removeForeground(foreground[l]);
+                        }
+                    }
                 }
             };
 
 
             gameLoop = {
                 MaxFPS: 60,
-                MaxUPS: 30,
+                MaxUPS: 40,
                 MaxFrameSkip: 15,
                 FPS: 0,
                 UPS: 0,
                     if (kilo.gameLoop.MaxFPS > 0) {
                         frameStep = 1000 / kilo.gameLoop.MaxFPS;
                     }
-                
+
                     if (elapsed >= 1000) {
                         elapsed = elapsed % 1000;
                         kilo.gameLoop.FPS = frameCount;
 
             setOffset: function (xOffset, yOffset) {
                 if (isNaN(parseInt(xOffset, 10)) || isNaN(parseInt(yOffset, 10))) {
-                    return;
+                    return this;
                 }
 
-                xO = xOffset;
-                yO = yOffset;
+                if (xOffset + kilo.gameCanvas.width <= width) {
+                    xO = xOffset;
+                }
+                if (yOffset + kilo.gameCanvas.height <= height) {
+                    yO = yOffset;
+                }
 
                 return this;
             },
 
             setOffsetX: function (xOffset) {
                 if (isNaN(parseInt(xOffset, 10))) {
-                    return;
+                    return this;
                 }
 
-                xO = xOffset;
+                if (xOffset + kilo.gameCanvas.width <= width) {
+                    xO = xOffset;
+                }
 
                 return this;
             },
 
             setOffsetY: function (yOffset) {
                 if (isNaN(parseInt(yOffset, 10))) {
-                    return;
+                    return this;
                 }
 
-                yO = yOffset;
+                if (yOffset + kilo.gameCanvas.height <= height) {
+                    yO = yOffset;
+                }
 
                 return this;
             },