Skip to content
This repository
Browse code

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
Joris van de Donk jorisvddonk authored
187 src/parallax.js 100755 → 100644
... ... @@ -1,87 +1,100 @@
1   -var jaws = (function(jaws) {
2   -
3   -/**
4   -* @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
5   -* @constructor
6   -*
7   -* @property scale number, scale factor for all layers (2 will double everything and so on)
8   -* @property repeat_x true|false, repeat all parallax layers horizontally
9   -* @property repeat_y true|false, repeat all parallax layers vertically
10   -* @property camera_x number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
11   -* @property camera_y number, y-position of "camera". defaults to 0
12   -*
13   -* @example
14   -* parallax = new jaws.Parallax({repeat_x: true})
15   -* parallax.addLayer({image: "parallax_1.png", damping: 100})
16   -* parallax.addLayer({image: "parallax_2.png", damping: 6})
17   -* parallax.camera_x += 1 // scroll layers horizontally
18   -* parallax.draw()
19   -*
20   -*/
21   -jaws.Parallax = function Parallax(options) {
22   - if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
23   -
24   - this.scale = options.scale || 1
25   - this.repeat_x = options.repeat_x
26   - this.repeat_y = options.repeat_y
27   - this.camera_x = options.camera_x || 0
28   - this.camera_y = options.camera_y || 0
29   - this.layers = []
30   -}
31   -
32   -/** Draw all layers in parallax scroller */
33   -jaws.Parallax.prototype.draw = function(options) {
34   - //TODO: work correctly with repeat_x, repeat_y and scale
35   - var layer, numx, numy, initx;
36   -
37   - for(var i=0; i < this.layers.length; i++) {
38   - layer = this.layers[i]
39   -
40   - numx = (jaws.width / layer.width)+1;
41   - numy = (jaws.height / layer.height)+1;
42   -
43   - initx = -((this.camera_x / layer.damping) % layer.width);
44   - layer.x = initx;
45   - layer.y = -((this.camera_y / layer.damping) % layer.height);
46   -
47   - for (var y = 0; y < numy; y++) {
48   - for (var x = 0; x < numx; x++) {
49   - layer.draw();
50   - layer.x = layer.x + layer.width;
51   - }
52   - layer.y = layer.y + layer.height;
53   - layer.x = initx;
54   - }
55   - }
56   -}
57   -/** Add a new layer to the parallax scroller */
58   -jaws.Parallax.prototype.addLayer = function(options) {
59   - var layer = new jaws.ParallaxLayer(options)
60   - layer.scale(this.scale)
61   - this.layers.push(layer)
62   -}
63   -/** Debugstring for Parallax() */
64   -jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
65   -
66   -/**
67   - * @class A single layer that's contained by Parallax()
68   - *
69   - * @property damping number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
70   - * @constructor
71   - * @extends jaws.Sprite
72   - */
73   -jaws.ParallaxLayer = function ParallaxLayer(options) {
74   - if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
75   -
76   - this.damping = options.damping || 0
77   - jaws.Sprite.call(this, options)
78   -}
79   -jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
80   -
81   -/** Debugstring for ParallaxLayer() */
82   -// This overwrites Sprites toString, find another sollution.
83   -// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
84   -
85   -return jaws;
86   -})(jaws || {});
87   -
  1 +var jaws = (function(jaws) {
  2 +
  3 +/**
  4 +* @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
  5 +* @constructor
  6 +*
  7 +* @property scale number, scale factor for all layers (2 will double everything and so on)
  8 +* @property repeat_x true|false, repeat all parallax layers horizontally
  9 +* @property repeat_y true|false, repeat all parallax layers vertically
  10 +* @property camera_x number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
  11 +* @property camera_y number, y-position of "camera". defaults to 0
  12 +*
  13 +* @example
  14 +* parallax = new jaws.Parallax({repeat_x: true})
  15 +* parallax.addLayer({image: "parallax_1.png", damping: 100})
  16 +* parallax.addLayer({image: "parallax_2.png", damping: 6})
  17 +* parallax.camera_x += 1 // scroll layers horizontally
  18 +* parallax.draw()
  19 +*
  20 +*/
  21 +jaws.Parallax = function Parallax(options) {
  22 + if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
  23 +
  24 + this.scale = options.scale || 1
  25 + this.repeat_x = options.repeat_x
  26 + this.repeat_y = options.repeat_y
  27 + this.camera_x = options.camera_x || 0
  28 + this.camera_y = options.camera_y || 0
  29 + this.layers = []
  30 +}
  31 +
  32 +/** Draw all layers in parallax scroller */
  33 +jaws.Parallax.prototype.draw = function(options) {
  34 + var layer, numx, numy, initx;
  35 +
  36 + for(var i=0; i < this.layers.length; i++) {
  37 + layer = this.layers[i]
  38 +
  39 + if (this.repeat_x) {
  40 + initx = -((this.camera_x / layer.damping) % layer.width);
  41 + } else {
  42 + initx = -(this.camera_x / layer.damping)
  43 + }
  44 +
  45 + if (this.repeat_y) {
  46 + layer.y = -((this.camera_y / layer.damping) % layer.height);
  47 + } else {
  48 + layer.y = -(this.camera_y / layer.damping);
  49 + }
  50 +
  51 + layer.x = initx;
  52 + while (layer.y < jaws.height) {
  53 + while (layer.x < jaws.width) {
  54 + if (layer.x + layer.width >= 0 && layer.y + layer.height >= 0) { //Make sure it's on screen
  55 + layer.draw(); //Draw only if actually on screen, for performance reasons
  56 + }
  57 + layer.x = layer.x + layer.width;
  58 + if (!this.repeat_x) {
  59 + break;
  60 + }
  61 + }
  62 + layer.y = layer.y + layer.height;
  63 + layer.x = initx;
  64 + if (!this.repeat_y) {
  65 + break;
  66 + }
  67 + }
  68 + }
  69 +}
  70 +/** Add a new layer to the parallax scroller */
  71 +jaws.Parallax.prototype.addLayer = function(options) {
  72 + var layer = new jaws.ParallaxLayer(options)
  73 + layer.scale(this.scale)
  74 + this.layers.push(layer)
  75 +}
  76 +/** Debugstring for Parallax() */
  77 +jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
  78 +
  79 +/**
  80 + * @class A single layer that's contained by Parallax()
  81 + *
  82 + * @property damping number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
  83 + * @constructor
  84 + * @extends jaws.Sprite
  85 + */
  86 +jaws.ParallaxLayer = function ParallaxLayer(options) {
  87 + if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
  88 +
  89 + this.damping = options.damping || 0
  90 + jaws.Sprite.call(this, options)
  91 +}
  92 +jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
  93 +
  94 +/** Debugstring for ParallaxLayer() */
  95 +// This overwrites Sprites toString, find another sollution.
  96 +// jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
  97 +
  98 +return jaws;
  99 +})(jaws || {});
  100 +
BIN  test/assets/rect19.png
63 test/index.html
... ... @@ -1,31 +1,32 @@
1   -<!DOCTYPE html>
2   -<html>
3   -<head>
4   - <script src="../jaws-dynamic.js" type="text/javascript"></script>
5   - <link href="qunit.css" rel="stylesheet" type="text/css" />
6   -
7   - <script src="qunit.js" type="text/javascript"></script>
8   - <script src="core.js" type="text/javascript"></script>
9   - <script src="assets.js" type="text/javascript"></script>
10   - <script src="input.js" type="text/javascript"></script>
11   - <script src="rect.js" type="text/javascript"></script>
12   - <script src="animation.js" type="text/javascript"></script>
13   - <script src="tile_map.js" type="text/javascript"></script>
14   - <script src="viewport.js" type="text/javascript"></script>
15   - <script src="sprite.js" type="text/javascript"></script>
16   - <script src="sprite_list.js" type="text/javascript"></script>
17   - <script src="sprite_sheet.js" type="text/javascript"></script>
18   - <script src="collision_detection.js" type="text/javascript"></script>
19   -
20   -</head>
21   -
22   -<body>
23   - <h1 id="qunit-header">Jaws test-suite</h1>
24   - <h2 id="qunit-banner"></h2>
25   - <div id="qunit-testrunner-toolbar"></div>
26   - <h2 id="qunit-userAgent"></h2>
27   - <ol id="qunit-tests"></ol>
28   - <div id="qunit-fixture"></div>
29   - <canvas width=100 height=100></canvas>
30   -</body>
31   -</html>
  1 +<!DOCTYPE html>
  2 +<html>
  3 +<head>
  4 + <script src="../jaws-dynamic.js" type="text/javascript"></script>
  5 + <link href="qunit.css" rel="stylesheet" type="text/css" />
  6 +
  7 + <script src="qunit.js" type="text/javascript"></script>
  8 + <script src="core.js" type="text/javascript"></script>
  9 + <script src="assets.js" type="text/javascript"></script>
  10 + <script src="input.js" type="text/javascript"></script>
  11 + <script src="rect.js" type="text/javascript"></script>
  12 + <script src="animation.js" type="text/javascript"></script>
  13 + <script src="tile_map.js" type="text/javascript"></script>
  14 + <script src="viewport.js" type="text/javascript"></script>
  15 + <script src="sprite.js" type="text/javascript"></script>
  16 + <script src="sprite_list.js" type="text/javascript"></script>
  17 + <script src="sprite_sheet.js" type="text/javascript"></script>
  18 + <script src="collision_detection.js" type="text/javascript"></script>
  19 + <script src="parallax.js" type="text/javascript"></script>
  20 +
  21 +</head>
  22 +
  23 +<body>
  24 + <h1 id="qunit-header">Jaws test-suite</h1>
  25 + <h2 id="qunit-banner"></h2>
  26 + <div id="qunit-testrunner-toolbar"></div>
  27 + <h2 id="qunit-userAgent"></h2>
  28 + <ol id="qunit-tests"></ol>
  29 + <div id="qunit-fixture"></div>
  30 + <canvas width=100 height=100></canvas>
  31 +</body>
  32 +</html>
889 test/parallax.js
... ... @@ -0,0 +1,889 @@
  1 +module("Parallax")
  2 +
  3 +test ("Performance test: repeat_x=false, repeat_y=false", function() {
  4 + stop();
  5 + jaws.init()
  6 + jaws.assets.root = "assets/";
  7 + jaws.assets.getOrLoad("rect.png", function(){
  8 +
  9 + parallax = new jaws.Parallax({
  10 + repeat_x: false,
  11 + repeat_y: false
  12 + });
  13 + parallax.addLayer({
  14 + image: "rect.png",
  15 + damping: 1
  16 + });
  17 +
  18 + parallax.layers[0]
  19 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  20 + parallax.layers[0].draw = function() {
  21 + this._old_draw();
  22 + number_of_calls = number_of_calls + 1;
  23 + }
  24 +
  25 + parallax.camera_x = 0;
  26 + parallax.camera_y = 0;
  27 + number_of_calls = 0;
  28 + parallax.draw();
  29 + same(number_of_calls, 1, "A total of 1 Sprite.draw() calls are required for a parallax draw at 0,0");
  30 +
  31 + parallax.camera_x = 10000;
  32 + parallax.camera_y = 0;
  33 + number_of_calls = 0;
  34 + parallax.draw();
  35 + same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 10000,0");
  36 +
  37 + parallax.camera_x = 0;
  38 + parallax.camera_y = 10000;
  39 + number_of_calls = 0;
  40 + parallax.draw();
  41 + same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 0,10000");
  42 +
  43 + jaws.clear(); start();
  44 + }, function(){
  45 + same(true,false," Rectangle asset couldn't be loaded"); start();
  46 + });
  47 +});
  48 +
  49 +test ("Performance test: repeat_x=true, repeat_y=false", function() {
  50 + stop();
  51 + jaws.init()
  52 + jaws.assets.root = "assets/";
  53 + jaws.assets.getOrLoad("rect.png", function(){
  54 +
  55 + parallax = new jaws.Parallax({
  56 + repeat_x: true,
  57 + repeat_y: false
  58 + });
  59 + parallax.addLayer({
  60 + image: "rect.png",
  61 + damping: 1
  62 + });
  63 +
  64 + parallax.layers[0]
  65 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  66 + parallax.layers[0].draw = function() {
  67 + this._old_draw();
  68 + number_of_calls = number_of_calls + 1;
  69 + }
  70 +
  71 + parallax.camera_x = 0;
  72 + parallax.camera_y = 0;
  73 + number_of_calls = 0;
  74 + parallax.draw();
  75 + 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)");
  76 +
  77 + parallax.camera_x = 10000;
  78 + parallax.camera_y = 0;
  79 + number_of_calls = 0;
  80 + parallax.draw();
  81 + 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)");
  82 +
  83 + parallax.camera_x = 0;
  84 + parallax.camera_y = 10000;
  85 + number_of_calls = 0;
  86 + parallax.draw();
  87 + same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 0,10000 (no rectangles)");
  88 +
  89 + jaws.clear(); start();
  90 + }, function(){
  91 + same(true,false," Rectangle asset couldn't be loaded"); start();
  92 + });
  93 +});
  94 +
  95 +test ("Performance test: repeat_x=false, repeat_y=true", function() {
  96 + stop();
  97 + jaws.init()
  98 + jaws.assets.root = "assets/";
  99 + jaws.assets.getOrLoad("rect.png", function(){
  100 +
  101 + parallax = new jaws.Parallax({
  102 + repeat_x: false,
  103 + repeat_y: true
  104 + });
  105 + parallax.addLayer({
  106 + image: "rect.png",
  107 + damping: 1
  108 + });
  109 +
  110 + parallax.layers[0]
  111 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  112 + parallax.layers[0].draw = function() {
  113 + this._old_draw();
  114 + number_of_calls = number_of_calls + 1;
  115 + }
  116 +
  117 + parallax.camera_x = 0;
  118 + parallax.camera_y = 0;
  119 + number_of_calls = 0;
  120 + parallax.draw();
  121 + 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)");
  122 +
  123 + parallax.camera_x = 0;
  124 + parallax.camera_y = 10000;
  125 + number_of_calls = 0;
  126 + parallax.draw();
  127 + 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)");
  128 +
  129 + parallax.camera_x = 10000;
  130 + parallax.camera_y = 0;
  131 + number_of_calls = 0;
  132 + parallax.draw();
  133 + same(number_of_calls, 0, "A total of 0 Sprite.draw() calls are required for a parallax draw at 10000,0 (no rectangles)");
  134 +
  135 + jaws.clear(); start();
  136 + }, function(){
  137 + same(true,false," Rectangle asset couldn't be loaded"); start();
  138 + });
  139 +});
  140 +
  141 +test("Performance test: repeat_x=true, repeat_y=true", function() {
  142 + stop();
  143 + jaws.init()
  144 + jaws.assets.root = "assets/";
  145 + jaws.assets.getOrLoad("rect.png", function(){
  146 +
  147 + parallax = new jaws.Parallax({
  148 + repeat_x: true,
  149 + repeat_y: true
  150 + });
  151 + parallax.addLayer({
  152 + image: "rect.png",
  153 + damping: 1
  154 + });
  155 + parallax.layers[0].setImage("rect.png");
  156 + var number_of_calls = 0;
  157 +
  158 +
  159 + parallax.layers[0]
  160 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  161 + parallax.layers[0].draw = function() {
  162 + this._old_draw();
  163 + number_of_calls = number_of_calls + 1;
  164 + }
  165 +
  166 + parallax.camera_x = 0;
  167 + parallax.camera_y = 0;
  168 + number_of_calls = 0;
  169 + parallax.draw();
  170 + same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at 0,0");
  171 +
  172 + parallax.camera_x = -1000;
  173 + parallax.camera_y = 0;
  174 + number_of_calls = 0;
  175 + parallax.draw();
  176 + same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at -1000,0");
  177 +
  178 + parallax.camera_x = 5;
  179 + parallax.camera_y = 5;
  180 + number_of_calls = 0;
  181 + parallax.draw();
  182 + same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 5,5");
  183 +
  184 + number_of_calls = 0;
  185 + parallax.camera_x = 3000;
  186 + parallax.camera_y = 3000;
  187 + parallax.draw();
  188 + same(number_of_calls, 25, "A total of 25 Sprite.draw() calls are required for a parallax draw at 3000,3000");
  189 +
  190 + number_of_calls = 0;
  191 + parallax.camera_x = 3005;
  192 + parallax.camera_y = 3005;
  193 + parallax.draw();
  194 + same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 3005,3005");
  195 +
  196 + jaws.clear(); start();
  197 + }, function(){
  198 + same(true,false," Rectangle asset couldn't be loaded"); start();
  199 + });
  200 +
  201 +});
  202 +
  203 +
  204 +test("Performance test: repeat_x=true, repeat_y=true, scale=2x)", function() {
  205 + stop();
  206 + jaws.init()
  207 + jaws.assets.root = "assets/";
  208 + jaws.assets.getOrLoad("rect.png", function(){
  209 +
  210 + parallax = new jaws.Parallax({
  211 + repeat_x: true,
  212 + repeat_y: true
  213 + });
  214 + parallax.addLayer({
  215 + image: "rect.png",
  216 + damping: 1,
  217 + scale: 2
  218 + });
  219 + parallax.layers[0].setImage("rect.png");
  220 + var number_of_calls = 0;
  221 +
  222 +
  223 + parallax.layers[0]
  224 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  225 + parallax.layers[0].draw = function() {
  226 + this._old_draw();
  227 + number_of_calls = number_of_calls + 1;
  228 + }
  229 +
  230 + parallax.camera_x = 0;
  231 + parallax.camera_y = 0;
  232 + number_of_calls = 0;
  233 + parallax.draw();
  234 + same(number_of_calls, 9, "A total of 9 Sprite.draw() calls are required for a parallax draw at 0,0");
  235 +
  236 + parallax.camera_x = 30;
  237 + parallax.camera_y = 30;
  238 + number_of_calls = 0;
  239 + parallax.draw();
  240 + same(number_of_calls, 16, "A total of 16 Sprite.draw() calls are required for a parallax draw at 30,30");
  241 +
  242 + jaws.clear(); start();
  243 + }, function(){
  244 + same(true,false," Rectangle asset couldn't be loaded"); start();
  245 + });
  246 +
  247 +});
  248 +
  249 +
  250 +
  251 +
  252 +
  253 +test("Performance test: repeat_x=true, repeat_y=true, rectangle=19x19", function() {
  254 + stop();
  255 + jaws.init()
  256 + jaws.assets.root = "assets/";
  257 + jaws.assets.getOrLoad("rect19.png", function(){
  258 +
  259 + parallax = new jaws.Parallax({
  260 + repeat_x: true,
  261 + repeat_y: true
  262 + });
  263 + parallax.addLayer({
  264 + image: "rect19.png",
  265 + damping: 1
  266 + });
  267 + parallax.layers[0].setImage("rect19.png");
  268 + var number_of_calls = 0;
  269 +
  270 +
  271 + parallax.layers[0]
  272 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  273 + parallax.layers[0].draw = function() {
  274 + this._old_draw();
  275 + number_of_calls = number_of_calls + 1;
  276 + }
  277 +
  278 + parallax.camera_x = 0;
  279 + parallax.camera_y = 0;
  280 + number_of_calls = 0;
  281 + parallax.draw();
  282 + same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 0,0");
  283 +
  284 + parallax.camera_x = 18;
  285 + parallax.camera_y = 18;
  286 + number_of_calls = 0;
  287 + parallax.draw();
  288 + same(number_of_calls, 49, "A total of 49 Sprite.draw() calls are required for a parallax draw at 18,18");
  289 +
  290 + number_of_calls = 0;
  291 + parallax.camera_x = 3800;
  292 + parallax.camera_y = 3800;
  293 + parallax.draw();
  294 + same(number_of_calls, 36, "A total of 36 Sprite.draw() calls are required for a parallax draw at 3800,3800");
  295 +
  296 + number_of_calls = 0;
  297 + parallax.camera_x = 3818;
  298 + parallax.camera_y = 3818;
  299 + parallax.draw();
  300 + same(number_of_calls, 49, "A total of 49 Sprite.draw() calls are required for a parallax draw at 3818,3818");
  301 +
  302 + jaws.clear(); start();
  303 + }, function(){
  304 + same(true,false," Rectangle asset couldn't be loaded"); start();
  305 + });
  306 +
  307 +});
  308 +
  309 +
  310 +
  311 +
  312 +test ("Accuracy test: repeat_x=true, repeat_y=false", function() {
  313 + stop();
  314 + jaws.init()
  315 + jaws.assets.root = "assets/";
  316 + jaws.assets.getOrLoad("rect.png", function(){
  317 +
  318 + parallax = new jaws.Parallax({
  319 + repeat_x: true,
  320 + repeat_y: false
  321 + });
  322 + parallax.addLayer({
  323 + image: "rect.png",
  324 + damping: 1
  325 + });
  326 +
  327 + var draw_calls = [];
  328 + var subtest1, subtest2;
  329 +
  330 + parallax.layers[0]
  331 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  332 + parallax.layers[0].draw = function() {
  333 + this._old_draw();
  334 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  335 + }
  336 +
  337 + parallax.camera_x = 0;
  338 + parallax.camera_y = 0;
  339 + draw_calls = [];
  340 + parallax.draw();
  341 + subtest1 = true;
  342 + subtest2 = true;
  343 + for (var i in draw_calls) {
  344 + if (draw_calls[i].y != 0) {
  345 + subtest1 = false;
  346 + }
  347 + if (draw_calls[i].x != i*20) {
  348 + subtest2 = false;
  349 + }
  350 + }
  351 + ok(subtest2, "All sprites' X positions are a multiple of 20 (camera_x=0, camera_y=0)");
  352 + ok(subtest1, "All sprites' Y positions are 0 (camera_x=0, camera_y=0)");
  353 +
  354 + parallax.camera_x = 10000;
  355 + parallax.camera_y = 0;
  356 + draw_calls = [];
  357 + parallax.draw();
  358 + subtest1 = true;
  359 + subtest2 = true;
  360 + for (var i in draw_calls) {
  361 + if (draw_calls[i].y != 0) {
  362 + subtest1 = false;
  363 + }
  364 + if (draw_calls[i].x != i*20) {
  365 + subtest2 = false;
  366 + }
  367 + }
  368 + ok(subtest2, "All sprites' X positions are a multiple of 20 (camera_x=10000, camera_y=0)");
  369 + ok(subtest1, "All sprites' Y positions are 0 (camera_x=10000, camera_y=0)");
  370 +
  371 + parallax.camera_x = 10;
  372 + parallax.camera_y = 10;
  373 + draw_calls = [];
  374 + parallax.draw();
  375 + subtest1 = true;
  376 + subtest2 = true;
  377 + for (var i in draw_calls) {
  378 + if (draw_calls[i].y != -10) {
  379 + subtest1 = false;
  380 + }
  381 + if (draw_calls[i].x != i*20 - 10) {
  382 + subtest2 = false;
  383 + }
  384 + }
  385 + ok(subtest2, "All sprites' X positions are a multiple of 20 (- 10) (camera_x=10, camera_y=10)");
  386 + ok(subtest1, "All sprites' Y positions are 0 (camera_x=10, camera_y=10)");
  387 +
  388 + jaws.clear(); start();
  389 + }, function(){
  390 + same(true,false," Rectangle asset couldn't be loaded"); start();
  391 + });
  392 +});
  393 +
  394 +
  395 +test ("Accuracy test: repeat_x=false, repeat_y=true", function() {
  396 + stop();
  397 + jaws.init()
  398 + jaws.assets.root = "assets/";
  399 + jaws.assets.getOrLoad("rect.png", function(){
  400 +
  401 + parallax = new jaws.Parallax({
  402 + repeat_x: false,
  403 + repeat_y: true
  404 + });
  405 + parallax.addLayer({
  406 + image: "rect.png",
  407 + damping: 1
  408 + });
  409 +
  410 + var draw_calls = [];
  411 + var subtest1, subtest2;
  412 +
  413 + parallax.layers[0]
  414 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  415 + parallax.layers[0].draw = function() {
  416 + this._old_draw();
  417 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  418 + }
  419 +
  420 + parallax.camera_x = 0;
  421 + parallax.camera_y = 0;
  422 + draw_calls = [];
  423 + parallax.draw();
  424 + subtest1 = true;
  425 + subtest2 = true;
  426 + for (var i in draw_calls) {
  427 + if (draw_calls[i].x != 0) {
  428 + subtest1 = false;
  429 + }
  430 + if (draw_calls[i].y != i * 20) {
  431 + subtest2 = false;
  432 + }
  433 + }
  434 + ok(subtest1, "All sprites' X positions on screen are 0 (camera_x=0, camera_y=0)");
  435 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
  436 +
  437 + parallax.camera_x = 0;
  438 + parallax.camera_y = 10000;
  439 + draw_calls = [];
  440 + parallax.draw();
  441 + subtest1 = true;
  442 + subtest2 = true;
  443 + for (var i in draw_calls) {
  444 + if (draw_calls[i].x != 0) {
  445 + subtest1 = false;
  446 + }
  447 + if (draw_calls[i].y != i * 20) {
  448 + subtest2 = false;
  449 + }
  450 + }
  451 + ok(subtest1, "All sprites' X positions on screen are 0 (camera_x=0, camera_y=10000)");
  452 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=10000)");
  453 +
  454 + parallax.camera_x = 10;
  455 + parallax.camera_y = 10;
  456 + draw_calls = [];
  457 + parallax.draw();
  458 + subtest1 = true;
  459 + subtest2 = true;
  460 + for (var i in draw_calls) {
  461 + if (draw_calls[i].x != -10) {
  462 + subtest1 = false;
  463 + }
  464 + if (draw_calls[i].y != i * 20 - 10) {
  465 + subtest2 = false;
  466 + }
  467 + }
  468 + ok(subtest1, "All sprites' X positions on screen are -10 (camera_x=10, camera_y=10)");
  469 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
  470 +
  471 + jaws.clear(); start();
  472 + }, function(){
  473 + same(true,false," Rectangle asset couldn't be loaded"); start();
  474 + });
  475 +});
  476 +
  477 +test ("Accuracy test: repeat_x=true, repeat_y=true", function() {
  478 + stop();
  479 + jaws.init()
  480 + jaws.assets.root = "assets/";
  481 + jaws.assets.getOrLoad("rect.png", function(){
  482 +
  483 + parallax = new jaws.Parallax({
  484 + repeat_x: true,
  485 + repeat_y: true
  486 + });
  487 + parallax.addLayer({
  488 + image: "rect.png",
  489 + damping: 1
  490 + });
  491 +
  492 + var draw_calls = [];
  493 + var subtest1, subtest2;
  494 +
  495 + parallax.layers[0]
  496 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  497 + parallax.layers[0].draw = function() {
  498 + this._old_draw();
  499 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  500 + }
  501 +
  502 + parallax.camera_x = 0;
  503 + parallax.camera_y = 0;
  504 + draw_calls = [];
  505 + parallax.draw();
  506 + subtest1 = true;
  507 + subtest2 = true;
  508 + for (var i in draw_calls) {
  509 + if (draw_calls[i].x % 20 != 0) {
  510 + subtest1 = false;
  511 + }
  512 + if (draw_calls[i].y % 20 != 0) {
  513 + subtest2 = false;
  514 + }
  515 + }
  516 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
  517 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
  518 +
  519 + parallax.camera_x = 0;
  520 + parallax.camera_y = 10000;
  521 + draw_calls = [];
  522 + parallax.draw();
  523 + subtest1 = true;
  524 + subtest2 = true;
  525 + for (var i in draw_calls) {
  526 + if (draw_calls[i].x % 20 != 0) {
  527 + subtest1 = false;
  528 + }
  529 + if (draw_calls[i].y % 20 != 0) {
  530 + subtest2 = false;
  531 + }
  532 + }
  533 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=10000, camera_y=10000)");
  534 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=10000, camera_y=10000)");
  535 +
  536 + parallax.camera_x = 10;
  537 + parallax.camera_y = 10;
  538 + draw_calls = [];
  539 + parallax.draw();
  540 + subtest1 = true;
  541 + subtest2 = true;
  542 + for (var i in draw_calls) {
  543 + if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
  544 + subtest1 = false;
  545 + }
  546 + if (Math.abs((draw_calls[i].y + 10)) % 20 != 0) {
  547 + subtest2 = false;
  548 + }
  549 + }
  550 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
  551 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-10) (camera_x=10, camera_y=10)");
  552 +
  553 + parallax.camera_x = -1000;
  554 + parallax.camera_y = -1000;
  555 + draw_calls = [];
  556 + parallax.draw();
  557 + subtest1 = true;
  558 + subtest2 = true;
  559 + for (var i in draw_calls) {
  560 + if (draw_calls[i].x % 20 != 0) {
  561 + subtest1 = false;
  562 + }
  563 + if (draw_calls[i].y % 20 != 0) {
  564 + subtest2 = false;
  565 + }
  566 + }
  567 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
  568 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
  569 +
  570 + parallax.camera_x = 0;
  571 + parallax.camera_y = 0;
  572 + draw_calls = [];
  573 + parallax.draw();
  574 + subtest1 = false;
  575 + for (var i in draw_calls) {
  576 + if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
  577 + subtest1 = true;
  578 + }
  579 + }
  580 + ok(subtest1, "A sprite is drawn at 60,60 (camera_x=0, camera_y=0)");
  581 +
  582 + parallax.camera_x = 1000;
  583 + parallax.camera_y = 1000;
  584 + draw_calls = [];
  585 + parallax.draw();
  586 + subtest1 = false;
  587 + for (var i in draw_calls) {
  588 + if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
  589 + subtest1 = true;
  590 + }
  591 + }
  592 + ok(subtest1, "A sprite is drawn at 60,60 (camera_x=1000, camera_y=1000)");
  593 +
  594 + jaws.clear(); start();
  595 + }, function(){
  596 + same(true,false," Rectangle asset couldn't be loaded"); start();
  597 + });
  598 +});
  599 +
  600 +test ("Accuracy test: repeat_x=true, repeat_y=true, damping=2", function() {
  601 + stop();
  602 + jaws.init()
  603 + jaws.assets.root = "assets/";
  604 + jaws.assets.getOrLoad("rect.png", function(){
  605 +
  606 + parallax = new jaws.Parallax({
  607 + repeat_x: true,
  608 + repeat_y: true
  609 + });
  610 + parallax.addLayer({
  611 + image: "rect.png",
  612 + damping: 2,
  613 + });
  614 +
  615 + var draw_calls = [];
  616 + var subtest1, subtest2;
  617 +
  618 + parallax.layers[0]
  619 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  620 + parallax.layers[0].draw = function() {
  621 + this._old_draw();
  622 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  623 + }
  624 +
  625 + parallax.camera_x = 0;
  626 + parallax.camera_y = 0;
  627 + draw_calls = [];
  628 + parallax.draw();
  629 + subtest1 = true;
  630 + subtest2 = true;
  631 + for (var i in draw_calls) {
  632 + if (draw_calls[i].x % 20 != 0) {
  633 + subtest1 = false;
  634 + }
  635 + if (draw_calls[i].y % 20 != 0) {
  636 + subtest2 = false;
  637 + }
  638 + }
  639 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
  640 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=0)");
  641 +
  642 + parallax.camera_x = 0;
  643 + parallax.camera_y = 20000;
  644 + draw_calls = [];
  645 + parallax.draw();
  646 + subtest1 = true;
  647 + subtest2 = true;
  648 + for (var i in draw_calls) {
  649 + if (draw_calls[i].x % 20 != 0) {
  650 + subtest1 = false;
  651 + }
  652 + if (draw_calls[i].y % 20 != 0) {
  653 + subtest2 = false;
  654 + }
  655 + }
  656 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=0, camera_y=20000)");
  657 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=0, camera_y=20000)");
  658 +
  659 + parallax.camera_x = 10;
  660 + parallax.camera_y = 10;
  661 + draw_calls = [];
  662 + parallax.draw();
  663 + subtest1 = true;
  664 + subtest2 = true;
  665 + for (var i in draw_calls) {
  666 + if (Math.abs((draw_calls[i].x + 5)) % 20 != 0) {
  667 + subtest1 = false;
  668 + }
  669 + if (Math.abs((draw_calls[i].y + 5)) % 20 != 0) {
  670 + subtest2 = false;
  671 + }
  672 + }
  673 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (-5) (camera_x=10, camera_y=10)");
  674 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (-5) (camera_x=10, camera_y=10)");
  675 +
  676 + parallax.camera_x = -1000;
  677 + parallax.camera_y = -1000;
  678 + draw_calls = [];
  679 + parallax.draw();
  680 + subtest1 = true;
  681 + subtest2 = true;
  682 + for (var i in draw_calls) {
  683 + if (draw_calls[i].x % 20 != 0) {
  684 + subtest1 = false;
  685 + }
  686 + if (draw_calls[i].y % 20 != 0) {
  687 + subtest2 = false;
  688 + }
  689 + }
  690 + ok(subtest1, "All sprites' X positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
  691 + ok(subtest2, "All sprites' Y positions on screen are a multiple of 20 (camera_x=-1000, camera_y=-1000)");
  692 +
  693 + parallax.camera_x = 0;
  694 + parallax.camera_y = 0;
  695 + draw_calls = [];
  696 + parallax.draw();
  697 + subtest1 = false;
  698 + for (var i in draw_calls) {
  699 + if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
  700 + subtest1 = true;
  701 + }
  702 + }
  703 + ok(subtest1, "A sprite is drawn at 60,60 (camera_x=0, camera_y=0)");
  704 +
  705 + parallax.camera_x = 1000;
  706 + parallax.camera_y = 1000;
  707 + draw_calls = [];
  708 + parallax.draw();
  709 + subtest1 = false;
  710 + for (var i in draw_calls) {
  711 + if (draw_calls[i].x == 60 && draw_calls[i].x == 60) {
  712 + subtest1 = true;
  713 + }
  714 + }
  715 + ok(subtest1, "A sprite is drawn at 60,60 (camera_x=1000, camera_y=1000)");
  716 +
  717 +
  718 +
  719 + jaws.clear(); start();
  720 + }, function(){
  721 + same(true,false," Rectangle asset couldn't be loaded"); start();
  722 + });
  723 +});
  724 +
  725 +test ("Accuracy test: repeat_x=true, repeat_y=false, scale=2x (layer level)", function() {
  726 + stop();
  727 + jaws.init()
  728 + jaws.assets.root = "assets/";
  729 + jaws.assets.getOrLoad("rect.png", function(){
  730 +
  731 + parallax = new jaws.Parallax({
  732 + repeat_x: true,
  733 + repeat_y: false,
  734 + });
  735 + parallax.addLayer({
  736 + image: "rect.png",
  737 + damping: 1,
  738 + scale: 2
  739 + });
  740 +
  741 + var draw_calls = [];
  742 + var subtest1, subtest2;
  743 +
  744 + parallax.layers[0]
  745 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  746 + parallax.layers[0].draw = function() {
  747 + this._old_draw();
  748 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  749 + }
  750 +
  751 + parallax.camera_x = 0;
  752 + parallax.camera_y = 0;
  753 + draw_calls = [];
  754 + parallax.draw();
  755 + subtest1 = true;
  756 + subtest2 = true;
  757 + for (var i in draw_calls) {
  758 + if (draw_calls[i].x % 40 != 0) {
  759 + subtest1 = false;
  760 + }
  761 + if (draw_calls[i].y != 0) {
  762 + subtest2 = false;
  763 + }
  764 + }
  765 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=0, camera_y=0)");
  766 + ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=0, camera_y=0)");
  767 +
  768 + parallax.camera_x = 10000;
  769 + parallax.camera_y = 0;
  770 + draw_calls = [];
  771 + parallax.draw();
  772 + subtest1 = true;
  773 + subtest2 = true;
  774 + for (var i in draw_calls) {
  775 + if (draw_calls[i].x % 40 != 0) {
  776 + subtest1 = false;
  777 + }
  778 + if (draw_calls[i].y != 0) {
  779 + subtest2 = false;
  780 + }
  781 + }
  782 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=10000, camera_y=0)");
  783 + ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=10000, camera_y=0)");
  784 +
  785 + parallax.camera_x = 10;
  786 + parallax.camera_y = 10;
  787 + draw_calls = [];
  788 + parallax.draw();
  789 + subtest1 = true;
  790 + subtest2 = true;
  791 + for (var i in draw_calls) {
  792 + if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
  793 + subtest1 = false;
  794 + }
  795 + if (draw_calls[i].y != -10) {
  796 + subtest2 = false;
  797 + }
  798 + }
  799 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (-10) (camera_x=10, camera_y=10)");
  800 + ok(subtest2, "All sprites' Y positions on screen are -10 (camera_x=10, camera_y=10)");
  801 +
  802 + jaws.clear(); start();
  803 + }, function(){
  804 + same(true,false," Rectangle asset couldn't be loaded"); start();
  805 + });
  806 +});
  807 +
  808 +test ("Accuracy test: repeat_x=true, repeat_y=false, scale=2x (parallax level)", function() {
  809 + stop();
  810 + jaws.init()
  811 + jaws.assets.root = "assets/";
  812 + jaws.assets.getOrLoad("rect.png", function(){
  813 +
  814 + parallax = new jaws.Parallax({
  815 + repeat_x: true,
  816 + repeat_y: false,
  817 + scale: 2
  818 + });
  819 + parallax.addLayer({
  820 + image: "rect.png",
  821 + damping: 1,
  822 + });
  823 +
  824 + var draw_calls = [];
  825 + var subtest1, subtest2;
  826 +
  827 + parallax.layers[0]
  828 + parallax.layers[0]._old_draw = parallax.layers[0].draw;
  829 + parallax.layers[0].draw = function() {
  830 + this._old_draw();
  831 + draw_calls.push({"width": this.width, "height": this.height, "x": this.x, "y": this.y});
  832 + }
  833 +
  834 + parallax.camera_x = 0;
  835 + parallax.camera_y = 0;
  836 + draw_calls = [];
  837 + parallax.draw();
  838 + subtest1 = true;
  839 + subtest2 = true;
  840 + for (var i in draw_calls) {
  841 + if (draw_calls[i].x % 40 != 0) {
  842 + subtest1 = false;
  843 + }
  844 + if (draw_calls[i].y != 0) {
  845 + subtest2 = false;
  846 + }
  847 + }
  848 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=0, camera_y=0)");
  849 + ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=0, camera_y=0)");
  850 +
  851 + parallax.camera_x = 10000;
  852 + parallax.camera_y = 0;
  853 + draw_calls = [];
  854 + parallax.draw();
  855 + subtest1 = true;
  856 + subtest2 = true;
  857 + for (var i in draw_calls) {
  858 + if (draw_calls[i].x % 40 != 0) {
  859 + subtest1 = false;
  860 + }
  861 + if (draw_calls[i].y != 0) {
  862 + subtest2 = false;
  863 + }
  864 + }
  865 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (camera_x=10000, camera_y=0)");
  866 + ok(subtest2, "All sprites' Y positions on screen are 0 (camera_x=10000, camera_y=0)");
  867 +
  868 + parallax.camera_x = 10;
  869 + parallax.camera_y = 10;
  870 + draw_calls = [];
  871 + parallax.draw();
  872 + subtest1 = true;
  873 + subtest2 = true;
  874 + for (var i in draw_calls) {
  875 + if (Math.abs((draw_calls[i].x + 10)) % 20 != 0) {
  876 + subtest1 = false;
  877 + }
  878 + if (draw_calls[i].y != -10) {
  879 + subtest2 = false;
  880 + }
  881 + }
  882 + ok(subtest1, "All sprites' X positions on screen are a multiple of 40 (-10) (camera_x=10, camera_y=10)");
  883 + ok(subtest2, "All sprites' Y positions on screen are -10 (camera_x=10, camera_y=10)");
  884 +
  885 + jaws.clear(); start();
  886 + }, function(){
  887 + same(true,false," Rectangle asset couldn't be loaded"); start();
  888 + });
  889 +});

0 comments on commit b675568

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