Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Parallax support for repeat_x and repeat_y

Parallax drawing now supports repeat_x and repeat_y.
Also, unit tests have been added for parallax drawing.
Both parallax performance (number of draw calls) and accuracy
(positioning of sprites) is tested.
  • Loading branch information...
commit b675568dabd8036f1ee7a381dd771476f4295518 1 parent 0be1590
@jorisvddonk jorisvddonk authored
View
187 src/parallax.js 100755 → 100644
@@ -1,87 +1,100 @@
-var jaws = (function(jaws) {
-
-/**
-* @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
-* @constructor
-*
-* @property scale number, scale factor for all layers (2 will double everything and so on)
-* @property repeat_x true|false, repeat all parallax layers horizontally
-* @property repeat_y true|false, repeat all parallax layers vertically
-* @property camera_x number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
-* @property camera_y number, y-position of "camera". defaults to 0
-*
-* @example
-* parallax = new jaws.Parallax({repeat_x: true})
-* parallax.addLayer({image: "parallax_1.png", damping: 100})
-* parallax.addLayer({image: "parallax_2.png", damping: 6})
-* parallax.camera_x += 1 // scroll layers horizontally
-* parallax.draw()
-*
-*/
-jaws.Parallax = function Parallax(options) {
- if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
-
- this.scale = options.scale || 1
- this.repeat_x = options.repeat_x
- this.repeat_y = options.repeat_y
- this.camera_x = options.camera_x || 0
- this.camera_y = options.camera_y || 0
- this.layers = []
-}
-
-/** Draw all layers in parallax scroller */
-jaws.Parallax.prototype.draw = function(options) {
- //TODO: work correctly with repeat_x, repeat_y and scale
- var layer, numx, numy, initx;
-
- for(var i=0; i < this.layers.length; i++) {
- layer = this.layers[i]
-
- numx = (jaws.width / layer.width)+1;
- numy = (jaws.height / layer.height)+1;
-
- initx = -((this.camera_x / layer.damping) % layer.width);
- layer.x = initx;
- layer.y = -((this.camera_y / layer.damping) % layer.height);
-
- for (var y = 0; y < numy; y++) {
- for (var x = 0; x < numx; x++) {
- layer.draw();
- layer.x = layer.x + layer.width;
- }
- layer.y = layer.y + layer.height;
- layer.x = initx;
- }
- }
-}
-/** Add a new layer to the parallax scroller */
-jaws.Parallax.prototype.addLayer = function(options) {
- var layer = new jaws.ParallaxLayer(options)
- layer.scale(this.scale)
- this.layers.push(layer)
-}
-/** Debugstring for Parallax() */
-jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
-
-/**
- * @class A single layer that's contained by Parallax()
- *
- * @property damping number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
- * @constructor
- * @extends jaws.Sprite
- */
-jaws.ParallaxLayer = function ParallaxLayer(options) {
- if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
-
- this.damping = options.damping || 0
- jaws.Sprite.call(this, options)
-}
-jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
-
-/** Debugstring for ParallaxLayer() */
-// This overwrites Sprites toString, find another sollution.
-// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
-
-return jaws;
-})(jaws || {});
-
+var jaws = (function(jaws) {
+
+/**
+* @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
+* @constructor
+*
+* @property scale number, scale factor for all layers (2 will double everything and so on)
+* @property repeat_x true|false, repeat all parallax layers horizontally
+* @property repeat_y true|false, repeat all parallax layers vertically
+* @property camera_x number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
+* @property camera_y number, y-position of "camera". defaults to 0
+*
+* @example
+* parallax = new jaws.Parallax({repeat_x: true})
+* parallax.addLayer({image: "parallax_1.png", damping: 100})
+* parallax.addLayer({image: "parallax_2.png", damping: 6})
+* parallax.camera_x += 1 // scroll layers horizontally
+* parallax.draw()
+*
+*/
+jaws.Parallax = function Parallax(options) {
+ if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
+
+ this.scale = options.scale || 1
+ this.repeat_x = options.repeat_x
+ this.repeat_y = options.repeat_y
+ this.camera_x = options.camera_x || 0
+ this.camera_y = options.camera_y || 0
+ this.layers = []
+}
+
+/** Draw all layers in parallax scroller */
+jaws.Parallax.prototype.draw = function(options) {
+ var layer, numx, numy, initx;
+
+ for(var i=0; i < this.layers.length; i++) {
+ layer = this.layers[i]
+
+ if (this.repeat_x) {
+ initx = -((this.camera_x / layer.damping) % layer.width);
+ } else {
+ initx = -(this.camera_x / layer.damping)
+ }
+
+ if (this.repeat_y) {
+ layer.y = -((this.camera_y / layer.damping) % layer.height);
+ } else {
+ layer.y = -(this.camera_y / layer.damping);
+ }
+
+ layer.x = initx;
+ while (layer.y < jaws.height) {
+ while (layer.x < jaws.width) {
+ if (layer.x + layer.width >= 0 && layer.y + layer.height >= 0) { //Make sure it's on screen
+ layer.draw(); //Draw only if actually on screen, for performance reasons
+ }
+ layer.x = layer.x + layer.width;
+ if (!this.repeat_x) {
+ break;
+ }
+ }
+ layer.y = layer.y + layer.height;
+ layer.x = initx;
+ if (!this.repeat_y) {
+ break;
+ }
+ }
+ }
+}
+/** Add a new layer to the parallax scroller */
+jaws.Parallax.prototype.addLayer = function(options) {
+ var layer = new jaws.ParallaxLayer(options)
+ layer.scale(this.scale)
+ this.layers.push(layer)
+}
+/** Debugstring for Parallax() */
+jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
+
+/**
+ * @class A single layer that's contained by Parallax()
+ *
+ * @property damping number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
+ * @constructor
+ * @extends jaws.Sprite
+ */
+jaws.ParallaxLayer = function ParallaxLayer(options) {
+ if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
+
+ this.damping = options.damping || 0
+ jaws.Sprite.call(this, options)
+}
+jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
+
+/** Debugstring for ParallaxLayer() */
+// This overwrites Sprites toString, find another sollution.
+// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
+
+return jaws;
+})(jaws || {});
+
View
BIN  test/assets/rect19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
63 test/index.html
@@ -1,31 +1,32 @@
-<!DOCTYPE html>
-<html>
-<head>
- <script src="../jaws-dynamic.js" type="text/javascript"></script>
- <link href="qunit.css" rel="stylesheet" type="text/css" />
-
- <script src="qunit.js" type="text/javascript"></script>
- <script src="core.js" type="text/javascript"></script>
- <script src="assets.js" type="text/javascript"></script>
- <script src="input.js" type="text/javascript"></script>
- <script src="rect.js" type="text/javascript"></script>
- <script src="animation.js" type="text/javascript"></script>
- <script src="tile_map.js" type="text/javascript"></script>
- <script src="viewport.js" type="text/javascript"></script>
- <script src="sprite.js" type="text/javascript"></script>
- <script src="sprite_list.js" type="text/javascript"></script>
- <script src="sprite_sheet.js" type="text/javascript"></script>
- <script src="collision_detection.js" type="text/javascript"></script>
-
-</head>
-
-<body>
- <h1 id="qunit-header">Jaws test-suite</h1>
- <h2 id="qunit-banner"></h2>
- <div id="qunit-testrunner-toolbar"></div>
- <h2 id="qunit-userAgent"></h2>
- <ol id="qunit-tests"></ol>
- <div id="qunit-fixture"></div>
- <canvas width=100 height=100></canvas>
-</body>
-</html>
+<!DOCTYPE html>
+<html>
+<head>
+ <script src="../jaws-dynamic.js" type="text/javascript"></script>
+ <link href="qunit.css" rel="stylesheet" type="text/css" />
+
+ <script src="qunit.js" type="text/javascript"></script>
+ <script src="core.js" type="text/javascript"></script>
+ <script src="assets.js" type="text/javascript"></script>
+ <script src="input.js" type="text/javascript"></script>
+ <script src="rect.js" type="text/javascript"></script>
+ <script src="animation.js" type="text/javascript"></script>
+ <script src="tile_map.js" type="text/javascript"></script>
+ <script src="viewport.js" type="text/javascript"></script>
+ <script src="sprite.js" type="text/javascript"></script>
+ <script src="sprite_list.js" type="text/javascript"></script>
+ <script src="sprite_sheet.js" type="text/javascript"></script>
+ <script src="collision_detection.js" type="text/javascript"></script>
+ <script src="parallax.js" type="text/javascript"></script>
+
+</head>
+
+<body>
+ <h1 id="qunit-header">Jaws test-suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture"></div>
+ <canvas width=100 height=100></canvas>
+</body>
+</html>
View
889 test/parallax.js
@@ -0,0 +1,889 @@
+module("Parallax")
+
+test ("Performance test: repeat_x=false, repeat_y=false", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: false,
+ repeat_y: false
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 1, "A total of 1 Sprite.draw() calls are required for a parallax draw at 0,0");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 10000,0");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 10000;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 0,10000");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Performance test: repeat_x=true, repeat_y=false", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: false
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 5, "A total of 5 Sprite.draw() calls are required for a parallax draw at 0,0 (5 horizontal rectangles with width=20px)");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 5, "A total of 5 Sprite.draw() calls are required for a parallax draw at 10000,0 (5 horizontal rectangles with width=20px)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 10000;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 0,10000 (no rectangles)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Performance test: repeat_x=false, repeat_y=true", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: false,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 5, "A total of 5 Sprite.draw() calls are required for a parallax draw at 0,0 (5 vertical rectangles with height=20px)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 10000;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 5, "A total of 5 Sprite.draw() calls are required for a parallax draw at 0,10000 (5 vertical rectangles with height=20px)");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 10000,0 (no rectangles)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test("Performance test: repeat_x=true, repeat_y=true", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+ parallax.layers[0].setImage("rect.png");
+ var number_of_calls = 0;
+
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at 0,0");
+
+ parallax.camera_x = -1000;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at -1000,0");
+
+ parallax.camera_x = 5;
+ parallax.camera_y = 5;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 5,5");
+
+ number_of_calls = 0;
+ parallax.camera_x = 3000;
+ parallax.camera_y = 3000;
+ parallax.draw();
+ same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at 3000,3000");
+
+ number_of_calls = 0;
+ parallax.camera_x = 3005;
+ parallax.camera_y = 3005;
+ parallax.draw();
+ same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 3005,3005");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+
+});
+
+
+test("Performance test: repeat_x=true, repeat_y=true, scale=2x)", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1,
+ scale: 2
+ });
+ parallax.layers[0].setImage("rect.png");
+ var number_of_calls = 0;
+
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 9, "A total of 9 Sprite.draw() calls are required for a parallax draw at 0,0");
+
+ parallax.camera_x = 30;
+ parallax.camera_y = 30;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 16, "A total of 16 Sprite.draw() calls are required for a parallax draw at 30,30");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+
+});
+
+
+
+
+
+test("Performance test: repeat_x=true, repeat_y=true, rectangle=19x19", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect19.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect19.png",
+ damping: 1
+ });
+ parallax.layers[0].setImage("rect19.png");
+ var number_of_calls = 0;
+
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ number_of_calls = number_of_calls + 1;
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 0,0");
+
+ parallax.camera_x = 18;
+ parallax.camera_y = 18;
+ number_of_calls = 0;
+ parallax.draw();
+ same(number_of_calls, 49, "A total of 49 Sprite.draw() calls are required for a parallax draw at 18,18");
+
+ number_of_calls = 0;
+ parallax.camera_x = 3800;
+ parallax.camera_y = 3800;
+ parallax.draw();
+ same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 3800,3800");
+
+ number_of_calls = 0;
+ parallax.camera_x = 3818;
+ parallax.camera_y = 3818;
+ parallax.draw();
+ same(number_of_calls, 49, "A total of 49 Sprite.draw() calls are required for a parallax draw at 3818,3818");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+
+});
+
+
+
+
+test ("Accuracy test: repeat_x=true, repeat_y=false", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: false
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].y != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].x != i*20) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest2, "All sprites' X positions are a multiple of 20 (camera_x=0, camera_y=0)");
+ ok(subtest1, "All sprites' Y positions are 0 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].y != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].x != i*20) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest2, "All sprites' X positions are a multiple of 20 (camera_x=10000, camera_y=0)");
+ ok(subtest1, "All sprites' Y positions are 0 (camera_x=10000, camera_y=0)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].y != -10) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].x != i*20 - 10) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest2, "All sprites' X positions are a multiple of 20 (- 10) (camera_x=10, camera_y=10)");
+ ok(subtest1, "All sprites' Y positions are 0 (camera_x=10, camera_y=10)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+
+test ("Accuracy test: repeat_x=false, repeat_y=true", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: false,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != i * 20) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are 0 (camera_x=0, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 10000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != i * 20) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are 0 (camera_x=0, camera_y=10000)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=10000)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x != -10) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != i * 20 - 10) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are -10 (camera_x=10, camera_y=10)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Accuracy test: repeat_x=true, repeat_y=true", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 10000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=10000, camera_y=10000)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=10000, camera_y=10000)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
+ subtest1 = false;
+ }
+ if (Math.abs((draw_calls[i].y + 10)) % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
+
+ parallax.camera_x = -1000;
+ parallax.camera_y = -1000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = false;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
+ subtest1 = true;
+ }
+ }
+ ok(subtest1, "A sprite is drawn at 60,60 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 1000;
+ parallax.camera_y = 1000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = false;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
+ subtest1 = true;
+ }
+ }
+ ok(subtest1, "A sprite is drawn at 60,60 (camera_x=1000, camera_y=1000)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Accuracy test: repeat_x=true, repeat_y=true, damping=2", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: true
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 2,
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 20000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=20000)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=20000)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (Math.abs((draw_calls[i].x + 5)) % 20 != 0) {
+ subtest1 = false;
+ }
+ if (Math.abs((draw_calls[i].y + 5)) % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (-5) (camera_x=10, camera_y=10)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-5) (camera_x=10, camera_y=10)");
+
+ parallax.camera_x = -1000;
+ parallax.camera_y = -1000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y % 20 != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
+ ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = false;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
+ subtest1 = true;
+ }
+ }
+ ok(subtest1, "A sprite is drawn at 60,60 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 1000;
+ parallax.camera_y = 1000;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = false;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
+ subtest1 = true;
+ }
+ }
+ ok(subtest1, "A sprite is drawn at 60,60 (camera_x=1000, camera_y=1000)");
+
+
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Accuracy test: repeat_x=true, repeat_y=false, scale=2x (layer level)", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: false,
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1,
+ scale: 2
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 40 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=0, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 40 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=10000, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=10000, camera_y=0)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != -10) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (-10) (camera_x=10, camera_y=10)");
+ ok(subtest2, "All sprites' Y positions on screen are -10 (camera_x=10, camera_y=10)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});
+
+test ("Accuracy test: repeat_x=true, repeat_y=false, scale=2x (parallax level)", function() {
+ stop();
+ jaws.init()
+ jaws.assets.root = "assets/";
+ jaws.assets.getOrLoad("rect.png", function(){
+
+ parallax = new jaws.Parallax({
+ repeat_x: true,
+ repeat_y: false,
+ scale: 2
+ });
+ parallax.addLayer({
+ image: "rect.png",
+ damping: 1,
+ });
+
+ var draw_calls = [];
+ var subtest1, subtest2;
+
+ parallax.layers[0]
+ parallax.layers[0]._old_draw = parallax.layers[0].draw;
+ parallax.layers[0].draw = function() {
+ this._old_draw();
+ draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
+ }
+
+ parallax.camera_x = 0;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 40 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=0, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=0, camera_y=0)");
+
+ parallax.camera_x = 10000;
+ parallax.camera_y = 0;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (draw_calls[i].x % 40 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != 0) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=10000, camera_y=0)");
+ ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=10000, camera_y=0)");
+
+ parallax.camera_x = 10;
+ parallax.camera_y = 10;
+ draw_calls = [];
+ parallax.draw();
+ subtest1 = true;
+ subtest2 = true;
+ for (var i in draw_calls) {
+ if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
+ subtest1 = false;
+ }
+ if (draw_calls[i].y != -10) {
+ subtest2 = false;
+ }
+ }
+ ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (-10) (camera_x=10, camera_y=10)");
+ ok(subtest2, "All sprites' Y positions on screen are -10 (camera_x=10, camera_y=10)");
+
+ jaws.clear(); start();
+ }, function(){
+ same(true,false," Rectangle asset couldn't be loaded"); start();
+ });
+});

0 comments on commit b675568

Please sign in to comment.
Something went wrong with that request. Please try again.