changeset 6:8f7ea4b92180

Add touch support
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 30 Oct 2012 19:31:09 +0100
parents d05bcb43302d
children 4f17fcc3b4ba
files mhrok.js
diffstat 1 files changed, 67 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mhrok.js	Tue Oct 30 16:26:49 2012 +0100
+++ b/mhrok.js	Tue Oct 30 19:31:09 2012 +0100
@@ -199,7 +199,6 @@
     };
     Game.prototype = {};
     Game.prototype.run = function () {
-        var that = this;
         this.frame_number = 0;
         this.mobs = [];
         this.keys = {};
@@ -231,17 +230,80 @@
         this.hero = new Hero(this);
         this.mobs.push(this.hero);
 
+        var that = this;
         this.animation = window.setInterval(function () {
             that.frame();
         }, 50, this);
         window.addEventListener('keydown', function (e) {
-            that.last_key = e.keyCode;
-            that.keys[e.keyCode] = true;
+            that.keydown(e);
         });
         window.addEventListener('keyup', function (e) {
-            delete that.keys[e.keyCode];
+            that.keyup(e);
+        });
+        ['mousedown', 'touchstart', 'touchmove', 'gesturestart',
+         'gesturemove'].forEach(function (eventName) {
+            window.addEventListener(eventName, function (e) {
+                that.mousedown(e);
+            });
+        });
+        ['mouseup', 'touchend', 'touchcancel', 'gestureend',
+         'gesturecancel'].forEach(function (eventName) {
+            window.addEventListener(eventName, function (e) {
+                that.mouseup(e);
+            });
         });
     };
+    Game.prototype.key_from_click = function (clickX, clickY) {
+        var x = clickX / window.innerWidth - 0.5;
+        var y = clickY / window.innerHeight - 0.5;
+        var key = null;
+        if (Math.abs(x) > Math.abs(y)) {
+            if (x > 0) {
+                key = 39;
+            } else {
+                key = 37;
+            };
+        } else {
+            if (y > 0) {
+                key = 40;
+            } else {
+                key = 38;
+            };
+        };
+        return key;
+    };
+    Game.prototype.keydown = function (e) {
+        e = e || window.event;
+        e.preventDefault && e.preventDefault();
+        e.stopPropagation && e.stopPropagation();
+        e.cancelBubble = true;
+        e.returnValue = false;
+        this.last_key = e.keyCode;
+        this.keys[e.keyCode] = true;
+        return false;
+    };
+    Game.prototype.keyup = function (e) {
+        e = e || window.event;
+        delete this.keys[e.keyCode];
+    };
+    Game.prototype.mouseup = function (e) {
+        this.keys = {};
+        this.last_key = null;
+        return false;
+    };
+    Game.prototype.mousedown = function (e) {
+        e = e || window.event;
+        e.preventDefault && e.preventDefault();
+        e.stopPropagation && e.stopPropagation();
+        e.cancelBubble = true;
+        e.returnValue = false;
+        if (e.touches) {
+            e = e.touches[0];
+        };
+        this.last_key = this.key_from_click(e.clientX, e.clientY);
+        this.keys[this.last_key] = true;
+        return false;
+    };
     Game.prototype.frame = function () {
         if (this.hero.animations[this.hero.animation].frames.length === 1) {
             // Treat the player character specially
@@ -255,11 +317,9 @@
                 mob.update(this);
             }, this);
         };
+        var html = '';
         this.mobs.forEach(function (mob) {
             mob.animate(this.frame_number);
-        }, this);
-        var html = '';
-        this.mobs.forEach(function (mob) {
             html += mob.html();
         }, this);
         this.mobs_element.innerHTML = html;