diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2014-10-10 10:03:06 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2014-10-10 10:03:06 -0400 |
commit | 533da4ea00703f4ad6d5518e1ce81d20261c40c0 (patch) | |
tree | 91353158f611e6434e3309423cf8487c62b1826d | |
parent | f339f441f930de624413968270ea4c164437daec (diff) | |
parent | 994a3006b61e0a729569770de51d5c13af6a23ed (diff) | |
download | libgit2-533da4ea00703f4ad6d5518e1ce81d20261c40c0.tar.gz |
Merge pull request #2473 from arthurschreiber/arthur/new-javascript-test-files
New test files for the javascript diff driver.
15 files changed, 328 insertions, 354 deletions
diff --git a/src/userdiff.h b/src/userdiff.h index 523f2f8d4..91c1f42dc 100644 --- a/src/userdiff.h +++ b/src/userdiff.h @@ -191,9 +191,9 @@ PATTERNS("php", "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), PATTERNS("javascript", - "^[ \t]*(function[ \t][a-zA-Z_][^\\{]*)\n" - "^[ \t]*(var[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*function[ \t\\(][^\\{]*)\n" - "^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*[ \t]*:[ \t]*function[ \t\\(][^\\{]*)", + "([a-zA-Z_$][a-zA-Z0-9_$]*(\\.[a-zA-Z0-9_$]+)*[ \t]*=[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n" + "([a-zA-Z_$][a-zA-Z0-9_$]*[ \t]*:[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n" + "[^a-zA-Z0-9_\\$](function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)", /* -- */ "[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?" diff --git a/tests/resources/userdiff/.gitted/index b/tests/resources/userdiff/.gitted/index Binary files differindex b69d5cfb1..548dc7bfd 100644 --- a/tests/resources/userdiff/.gitted/index +++ b/tests/resources/userdiff/.gitted/index diff --git a/tests/resources/userdiff/.gitted/objects/09/65b377c214bbe5e0d18fcdaf556df7fa7ed7c8 b/tests/resources/userdiff/.gitted/objects/09/65b377c214bbe5e0d18fcdaf556df7fa7ed7c8 Binary files differnew file mode 100644 index 000000000..cbaf4c1bc --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/09/65b377c214bbe5e0d18fcdaf556df7fa7ed7c8 diff --git a/tests/resources/userdiff/.gitted/objects/0c/20ef1409ae1df4d5a76cdbd98d5c33ccdb6bcc b/tests/resources/userdiff/.gitted/objects/0c/20ef1409ae1df4d5a76cdbd98d5c33ccdb6bcc Binary files differnew file mode 100644 index 000000000..fa149da98 --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/0c/20ef1409ae1df4d5a76cdbd98d5c33ccdb6bcc diff --git a/tests/resources/userdiff/.gitted/objects/39/ea75107a09091ba54ff86fcc780b59477e42cd b/tests/resources/userdiff/.gitted/objects/39/ea75107a09091ba54ff86fcc780b59477e42cd Binary files differnew file mode 100644 index 000000000..748373621 --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/39/ea75107a09091ba54ff86fcc780b59477e42cd diff --git a/tests/resources/userdiff/.gitted/objects/3c/c08384deae5957247bc36776ab626cc9e0582b b/tests/resources/userdiff/.gitted/objects/3c/c08384deae5957247bc36776ab626cc9e0582b Binary files differnew file mode 100644 index 000000000..29b72fbbc --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/3c/c08384deae5957247bc36776ab626cc9e0582b diff --git a/tests/resources/userdiff/.gitted/objects/46/8d6f2afc940e14c76347fa9af26e429a3c9044 b/tests/resources/userdiff/.gitted/objects/46/8d6f2afc940e14c76347fa9af26e429a3c9044 Binary files differnew file mode 100644 index 000000000..5fc839170 --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/46/8d6f2afc940e14c76347fa9af26e429a3c9044 diff --git a/tests/resources/userdiff/.gitted/objects/53/917973acfe0111f93c2cfaacf854be245880e8 b/tests/resources/userdiff/.gitted/objects/53/917973acfe0111f93c2cfaacf854be245880e8 Binary files differnew file mode 100644 index 000000000..debf7e40f --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/53/917973acfe0111f93c2cfaacf854be245880e8 diff --git a/tests/resources/userdiff/.gitted/objects/63/1d44e0c72e8cd1b594fa11d7d1ee8a6d67ff67 b/tests/resources/userdiff/.gitted/objects/63/1d44e0c72e8cd1b594fa11d7d1ee8a6d67ff67 Binary files differnew file mode 100644 index 000000000..e8b884cd5 --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/63/1d44e0c72e8cd1b594fa11d7d1ee8a6d67ff67 diff --git a/tests/resources/userdiff/.gitted/objects/f3/be389d351e4bcc6dcc4b5fe22134ef0f63f8bd b/tests/resources/userdiff/.gitted/objects/f3/be389d351e4bcc6dcc4b5fe22134ef0f63f8bd Binary files differnew file mode 100644 index 000000000..cfbef992d --- /dev/null +++ b/tests/resources/userdiff/.gitted/objects/f3/be389d351e4bcc6dcc4b5fe22134ef0f63f8bd diff --git a/tests/resources/userdiff/after/file.javascript b/tests/resources/userdiff/after/file.javascript index 7cd3c5a8a..53917973a 100644 --- a/tests/resources/userdiff/after/file.javascript +++ b/tests/resources/userdiff/after/file.javascript @@ -1,108 +1,108 @@ -/* - Some code extracted from https://github.com/julianlloyd/scrollReveal.js - which happens to be a trending Javascript repo with an MIT license at - the time I was working on Javascript userdiff support in libgit2 +define(function(require, exports, module) { + module.exports = Player; - I extracted just some of the code, so I suspect this is no longer valid - Javascript code, but it contains enough example patterns to work. -*/ -;(function (window) { + var Key = require("./key") + , Direction = require("./direction"); - 'use strict'; + function Player(game) { + this.game = game; - var docElem = window.document.documentElement; + this.image = new Image("./assets/fighter.png"); + this.game.resources.add(this.image); - function getViewportH () { - var client = docElem['clientHeight'], - inner = window['innerHeight'], - sample = window['otherProperty']; + this.x = 0; + this.y = 0; - return (client < inner) ? inner : client; - } - - function getOffset (el) { - var offsetTop = 0, - offsetLeft = 0; + this.pixelX = 10; + this.pixelY = 10; - do { - if (!isNaN(el.offsetTop)) { - offsetTop += el.offsetTop + 1; - } - if (!isNaN(el.offsetLeft)) { - offsetLeft += el.offsetLeft; - } - } while (el = el.offsetParent) - - return { - top: offsetTop, - left: offsetLeft - } + this.animationStep = 0; } - function isElementInViewport (el, h) { - var scrolled = window.pageYOffset, - viewed = scrolled + getViewportH(), - elTop = getOffset(el).top, - elBottom = elTop + el.offsetHeight, - h = h || 0; - - return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled; - } - - scrollReveal.prototype = { - - _init: function () { - - var self = this; - - this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]')); - this.scrolled = false; + Player.prototype.update = function() { + if (!this.isWalking()) { + this.handleInput(); + } - this.elems.forEach(function (el, i) { - self.animate(el); - }); + if (this.isWalking()) { + // Increase the animation step. + this.animationStep = ++this.animationStep % 60; - var scrollHandler = function () { - if (!self.scrolled) { - self.scrolled = true; - setTimeout(function () { - self._scrollPage(); - }, 61); - } - }; + if (this.x * 32 > this.pixelX) { + this.pixelX++; + } else if (this.x * 32 < this.pixelX) { + this.pixelX--; + } - var resizeHandler = function () { - function delayed() { - self._scrollPage(); - self.resizeTimeout = null; - } - if (self.resizeTimeout) { - clearTimeout(self.resizeTimeout); + if (this.y * 32 > this.pixelY) { + this.pixelY++; + } else if (this.y * 32 < this.pixelY) { + this.pixelY--; + } + } else { + // Reset the animation step. + this.animationStep = 0; + } + }; + + Player.prototype.handleInput = function() { + var keyboard = this.game.keyboard, finalAction, action, inputs = { + 'moveDown': keyboard.isDown(Key.DOWN), + 'moveUp': keyboard.isDown(Key.UP), + 'moveLeft': keyboard.isDown(Key.LEFT), + 'moveRight': keyboard.isDown(Key.RIGHT) + }; + + for (action in inputs) { + if (inputs[action]) { + if (!finalAction || inputs[finalAction] < inputs[action]) { + finalAction = action; } - self.resizeTimeout = setTimeout(delayed, 200); - }; - - window.addEventListener('scroll', scrollHandler, false); - window.addEventListener('resize', resizeHandler, false); - }, - - /*=============================================================================*/ - - _scrollPage: function () { - var self = this; - - this.elems.forEach(function (el, i) { - if (isElementInViewport(el, self.options.viewportFactor)) { - self.animate(el); - } - }); - this.scrolled = false; - this.tested = true; - }, - }; // end scrollReveal.prototype + } + } - document.addEventListener("DOMContentLoaded", function (evt) { - window.scrollReveal = new scrollReveal(); - }); + this[finalAction] && this[finalAction](); + }; + + Player.prototype.isWalking = function() { + return this.x * 32 != this.pixelX || this.y * 32 != this.pixelY; + }; + + Player.prototype.moveDown = function() { + this.y += 1; + this.direction = Direction.DOWN; + }; + + Player.prototype.moveUp = function() { + this.y -= 1; + this.direction = Direction.UP; + }; + + Player.prototype.moveLeft = function() { + this.x -= 5; + this.direction = Direction.LEFT; + }; + + Player.prototype.moveRight = function() { + this.x += 1; + this.direction = Direction.RIGHT; + }; + + Player.prototype.draw = function(context) { + var offsetX = Math.floor(this.animationStep / 15) * 32, offsetY = 0; + + switch(this.direction) { + case Direction.UP: + offsetY = 48 * 3; + break; + case Direction.RIGHT: + offsetY = 48 * 2; + break; + case Direction.LEFT: + offsetY = 48; + break; + } -})(window); + context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY, 32, 48); + }; +}); diff --git a/tests/resources/userdiff/before/file.javascript b/tests/resources/userdiff/before/file.javascript index b9f1286e5..0965b377c 100644 --- a/tests/resources/userdiff/before/file.javascript +++ b/tests/resources/userdiff/before/file.javascript @@ -1,109 +1,109 @@ -/* - Some code extracted from https://github.com/julianlloyd/scrollReveal.js - which happens to be a trending Javascript repo with an MIT license at - the time I was working on Javascript userdiff support in libgit2 +define(function(require, exports, module) { + module.exports = Player; - I extracted just some of the code, so I suspect this is no longer valid - Javascript code, but it contains enough example patterns to work. -*/ -;(function (window) { + var Key = require("./key") + , Direction = require("./direction") + , Image = require("./image"); - 'use strict'; + function Player(game) { + this.game = game; - var docElem = window.document.documentElement; + this.image = new Image("./assets/fighter.png"); + this.game.resources.add(this.image); - function getViewportH () { - var client = docElem['clientHeight'], - inner = window['innerHeight']; + this.x = 0; + this.y = 0; - return (client < inner) ? inner : client; - } - - function getOffset (el) { - var offsetTop = 0, - offsetLeft = 0; + this.pixelX = 0; + this.pixelY = 0; - do { - if (!isNaN(el.offsetTop)) { - offsetTop += el.offsetTop; - } - if (!isNaN(el.offsetLeft)) { - offsetLeft += el.offsetLeft; - } - } while (el = el.offsetParent) - - return { - top: offsetTop, - left: offsetLeft - } + this.animationStep = 0; } - function isElementInViewport (el, h) { - var scrolled = window.pageYOffset, - viewed = scrolled + getViewportH(), - elH = el.offsetHeight, - elTop = getOffset(el).top, - elBottom = elTop + elH, - h = h || 0; - - return (elTop + elH * h) <= viewed && (elBottom) >= scrolled; - } - - scrollReveal.prototype = { - - _init: function () { - - var self = this; - - this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]')); - this.scrolled = false; + Player.prototype.update = function() { + if (!this.isWalking()) { + this.handleInput(); + } - // Initialize all scrollreveals, triggering all - // reveals on visible elements. - this.elems.forEach(function (el, i) { - self.animate(el); - }); + if (this.isWalking()) { + // Increase the animation step. + this.animationStep = ++this.animationStep % 60; - var scrollHandler = function () { - if (!self.scrolled) { - self.scrolled = true; - setTimeout(function () { - self._scrollPage(); - }, 60); - } - }; + if (this.x * 32 > this.pixelX) { + this.pixelX++; + } else if (this.x * 32 < this.pixelX) { + this.pixelX--; + } - var resizeHandler = function () { - function delayed() { - self._scrollPage(); - self.resizeTimeout = null; - } - if (self.resizeTimeout) { - clearTimeout(self.resizeTimeout); + if (this.y * 32 > this.pixelY) { + this.pixelY++; + } else if (this.y * 32 < this.pixelY) { + this.pixelY--; + } + } else { + // Reset the animation step. + this.animationStep = 0; + } + }; + + Player.prototype.handleInput = function() { + var keyboard = this.game.keyboard, finalAction, action, inputs = { + 'moveDown': keyboard.isDown(Key.DOWN), + 'moveUp': keyboard.isDown(Key.UP), + 'moveLeft': keyboard.isDown(Key.LEFT), + 'moveRight': keyboard.isDown(Key.RIGHT) + }; + + for (action in inputs) { + if (inputs[action]) { + if (!finalAction || inputs[finalAction] < inputs[action]) { + finalAction = action; } - self.resizeTimeout = setTimeout(delayed, 200); - }; - - window.addEventListener('scroll', scrollHandler, false); - window.addEventListener('resize', resizeHandler, false); - }, - - /*=============================================================================*/ - - _scrollPage: function () { - var self = this; - - this.elems.forEach(function (el, i) { - if (isElementInViewport(el, self.options.viewportFactor)) { - self.animate(el); - } - }); - this.scrolled = false; - }, - }; // end scrollReveal.prototype + } + } - document.addEventListener("DOMContentLoaded", function (evt) { - window.scrollReveal = new scrollReveal(); - }); + this[finalAction] && this[finalAction](); + }; + + Player.prototype.isWalking = function() { + return this.x * 32 != this.pixelX || this.y * 32 != this.pixelY; + }; + + Player.prototype.moveDown = function() { + this.y += 1; + this.direction = Direction.DOWN; + }; + + Player.prototype.moveUp = function() { + this.y -= 1; + this.direction = Direction.UP; + }; + + Player.prototype.moveLeft = function() { + this.x -= 1; + this.direction = Direction.LEFT; + }; + + Player.prototype.moveRight = function() { + this.x += 1; + this.direction = Direction.RIGHT; + }; + + Player.prototype.draw = function(context) { + var offsetX = Math.floor(this.animationStep / 15) * 32, offsetY = 0; + + switch(this.direction) { + case Direction.UP: + offsetY = 48 * 3; + break; + case Direction.RIGHT: + offsetY = 48 * 2; + break; + case Direction.LEFT: + offsetY = 48; + break; + } -})(window); + context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY - 16, 32, 48); + }; +}); diff --git a/tests/resources/userdiff/expected/driver/diff.javascript b/tests/resources/userdiff/expected/driver/diff.javascript index 4e65d0746..4cefe5cff 100644 --- a/tests/resources/userdiff/expected/driver/diff.javascript +++ b/tests/resources/userdiff/expected/driver/diff.javascript @@ -1,40 +1,27 @@ diff --git a/files/file.javascript b/files/file.javascript -index b9f1286..7cd3c5a 100644 +index 0965b37..5391797 100644 --- a/files/file.javascript +++ b/files/file.javascript -@@ -16,3 +16,4 @@ function getViewportH () - var client = docElem['clientHeight'], -- inner = window['innerHeight']; -+ inner = window['innerHeight'], -+ sample = window['otherProperty']; +@@ -4,4 +4,3 @@ function(require, exports, module) + var Key = require("./key") +- , Direction = require("./direction") +- , Image = require("./image"); ++ , Direction = require("./direction"); -@@ -27,3 +28,3 @@ function getOffset (el) - if (!isNaN(el.offsetTop)) { -- offsetTop += el.offsetTop; -+ offsetTop += el.offsetTop + 1; - } -@@ -43,8 +44,7 @@ function isElementInViewport (el, h) - viewed = scrolled + getViewportH(), -- elH = el.offsetHeight, - elTop = getOffset(el).top, -- elBottom = elTop + elH, -+ elBottom = elTop + el.offsetHeight, - h = h || 0; +@@ -16,4 +15,4 @@ function Player(game) -- return (elTop + elH * h) <= viewed && (elBottom) >= scrolled; -+ return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled; - } -@@ -60,4 +60,2 @@ _init: function () +- this.pixelX = 0; +- this.pixelY = 0; ++ this.pixelX = 10; ++ this.pixelY = 10; -- // Initialize all scrollreveals, triggering all -- // reveals on visible elements. - this.elems.forEach(function (el, i) { -@@ -71,3 +69,3 @@ var scrollHandler = function () - self._scrollPage(); -- }, 60); -+ }, 61); - } -@@ -101,2 +99,3 @@ _scrollPage: function () - this.scrolled = false; -+ this.tested = true; - }, +@@ -82,3 +81,3 @@ Player.prototype.moveUp = function() + Player.prototype.moveLeft = function() { +- this.x -= 1; ++ this.x -= 5; + this.direction = Direction.LEFT; +@@ -106,3 +105,3 @@ Player.prototype.draw = function(context) + +- context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY - 16, 32, 48); ++ context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY, 32, 48); + }; diff --git a/tests/resources/userdiff/expected/nodriver/diff.javascript b/tests/resources/userdiff/expected/nodriver/diff.javascript index 69afe4fd8..4bbd54764 100644 --- a/tests/resources/userdiff/expected/nodriver/diff.javascript +++ b/tests/resources/userdiff/expected/nodriver/diff.javascript @@ -1,40 +1,27 @@ diff --git a/files/file.javascript b/files/file.javascript -index b9f1286..7cd3c5a 100644 +index 0965b37..5391797 100644 --- a/files/file.javascript +++ b/files/file.javascript -@@ -16,3 +16,4 @@ - var client = docElem['clientHeight'], -- inner = window['innerHeight']; -+ inner = window['innerHeight'], -+ sample = window['otherProperty']; +@@ -4,4 +4,3 @@ define(function(require, exports, module) { + var Key = require("./key") +- , Direction = require("./direction") +- , Image = require("./image"); ++ , Direction = require("./direction"); -@@ -27,3 +28,3 @@ - if (!isNaN(el.offsetTop)) { -- offsetTop += el.offsetTop; -+ offsetTop += el.offsetTop + 1; - } -@@ -43,8 +44,7 @@ - viewed = scrolled + getViewportH(), -- elH = el.offsetHeight, - elTop = getOffset(el).top, -- elBottom = elTop + elH, -+ elBottom = elTop + el.offsetHeight, - h = h || 0; +@@ -16,4 +15,4 @@ define(function(require, exports, module) { -- return (elTop + elH * h) <= viewed && (elBottom) >= scrolled; -+ return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled; - } -@@ -60,4 +60,2 @@ +- this.pixelX = 0; +- this.pixelY = 0; ++ this.pixelX = 10; ++ this.pixelY = 10; -- // Initialize all scrollreveals, triggering all -- // reveals on visible elements. - this.elems.forEach(function (el, i) { -@@ -71,3 +69,3 @@ - self._scrollPage(); -- }, 60); -+ }, 61); - } -@@ -101,2 +99,3 @@ - this.scrolled = false; -+ this.tested = true; - }, +@@ -82,3 +81,3 @@ define(function(require, exports, module) { + Player.prototype.moveLeft = function() { +- this.x -= 1; ++ this.x -= 5; + this.direction = Direction.LEFT; +@@ -106,3 +105,3 @@ define(function(require, exports, module) { + +- context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY - 16, 32, 48); ++ context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY, 32, 48); + }; diff --git a/tests/resources/userdiff/files/file.javascript b/tests/resources/userdiff/files/file.javascript index 7cd3c5a8a..53917973a 100644 --- a/tests/resources/userdiff/files/file.javascript +++ b/tests/resources/userdiff/files/file.javascript @@ -1,108 +1,108 @@ -/* - Some code extracted from https://github.com/julianlloyd/scrollReveal.js - which happens to be a trending Javascript repo with an MIT license at - the time I was working on Javascript userdiff support in libgit2 +define(function(require, exports, module) { + module.exports = Player; - I extracted just some of the code, so I suspect this is no longer valid - Javascript code, but it contains enough example patterns to work. -*/ -;(function (window) { + var Key = require("./key") + , Direction = require("./direction"); - 'use strict'; + function Player(game) { + this.game = game; - var docElem = window.document.documentElement; + this.image = new Image("./assets/fighter.png"); + this.game.resources.add(this.image); - function getViewportH () { - var client = docElem['clientHeight'], - inner = window['innerHeight'], - sample = window['otherProperty']; + this.x = 0; + this.y = 0; - return (client < inner) ? inner : client; - } - - function getOffset (el) { - var offsetTop = 0, - offsetLeft = 0; + this.pixelX = 10; + this.pixelY = 10; - do { - if (!isNaN(el.offsetTop)) { - offsetTop += el.offsetTop + 1; - } - if (!isNaN(el.offsetLeft)) { - offsetLeft += el.offsetLeft; - } - } while (el = el.offsetParent) - - return { - top: offsetTop, - left: offsetLeft - } + this.animationStep = 0; } - function isElementInViewport (el, h) { - var scrolled = window.pageYOffset, - viewed = scrolled + getViewportH(), - elTop = getOffset(el).top, - elBottom = elTop + el.offsetHeight, - h = h || 0; - - return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled; - } - - scrollReveal.prototype = { - - _init: function () { - - var self = this; - - this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]')); - this.scrolled = false; + Player.prototype.update = function() { + if (!this.isWalking()) { + this.handleInput(); + } - this.elems.forEach(function (el, i) { - self.animate(el); - }); + if (this.isWalking()) { + // Increase the animation step. + this.animationStep = ++this.animationStep % 60; - var scrollHandler = function () { - if (!self.scrolled) { - self.scrolled = true; - setTimeout(function () { - self._scrollPage(); - }, 61); - } - }; + if (this.x * 32 > this.pixelX) { + this.pixelX++; + } else if (this.x * 32 < this.pixelX) { + this.pixelX--; + } - var resizeHandler = function () { - function delayed() { - self._scrollPage(); - self.resizeTimeout = null; - } - if (self.resizeTimeout) { - clearTimeout(self.resizeTimeout); + if (this.y * 32 > this.pixelY) { + this.pixelY++; + } else if (this.y * 32 < this.pixelY) { + this.pixelY--; + } + } else { + // Reset the animation step. + this.animationStep = 0; + } + }; + + Player.prototype.handleInput = function() { + var keyboard = this.game.keyboard, finalAction, action, inputs = { + 'moveDown': keyboard.isDown(Key.DOWN), + 'moveUp': keyboard.isDown(Key.UP), + 'moveLeft': keyboard.isDown(Key.LEFT), + 'moveRight': keyboard.isDown(Key.RIGHT) + }; + + for (action in inputs) { + if (inputs[action]) { + if (!finalAction || inputs[finalAction] < inputs[action]) { + finalAction = action; } - self.resizeTimeout = setTimeout(delayed, 200); - }; - - window.addEventListener('scroll', scrollHandler, false); - window.addEventListener('resize', resizeHandler, false); - }, - - /*=============================================================================*/ - - _scrollPage: function () { - var self = this; - - this.elems.forEach(function (el, i) { - if (isElementInViewport(el, self.options.viewportFactor)) { - self.animate(el); - } - }); - this.scrolled = false; - this.tested = true; - }, - }; // end scrollReveal.prototype + } + } - document.addEventListener("DOMContentLoaded", function (evt) { - window.scrollReveal = new scrollReveal(); - }); + this[finalAction] && this[finalAction](); + }; + + Player.prototype.isWalking = function() { + return this.x * 32 != this.pixelX || this.y * 32 != this.pixelY; + }; + + Player.prototype.moveDown = function() { + this.y += 1; + this.direction = Direction.DOWN; + }; + + Player.prototype.moveUp = function() { + this.y -= 1; + this.direction = Direction.UP; + }; + + Player.prototype.moveLeft = function() { + this.x -= 5; + this.direction = Direction.LEFT; + }; + + Player.prototype.moveRight = function() { + this.x += 1; + this.direction = Direction.RIGHT; + }; + + Player.prototype.draw = function(context) { + var offsetX = Math.floor(this.animationStep / 15) * 32, offsetY = 0; + + switch(this.direction) { + case Direction.UP: + offsetY = 48 * 3; + break; + case Direction.RIGHT: + offsetY = 48 * 2; + break; + case Direction.LEFT: + offsetY = 48; + break; + } -})(window); + context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY, 32, 48); + }; +}); |