Commits

Lucian Brănescu-Mihăilă committed 64b89dd

Add obstacles. Fix typo. Refactor movement and player data. Disabled obstacle collision, since it's wrong.

Comments (0)

Files changed (1)

 
 function Game() {
     var keys = {
-        37: {x: -1, y: 0}, // left
-        38: {x: 0, y: -1}, // up
-        39: {x: 1, y: 0}, // right
-        40: {x: 0, y: 1}, // down
+        37: {x: -1, y:  0}, // left
+        38: {x:  0, y: -1}, // up
+        39: {x:  1, y:  0}, // right
+        40: {x:  0, y:  1}, // down
     }
 
-    var pos = {x: 50, y: 50}
-    var size = {x: 10, y: 10}
-    var speed = 5;
+    var player = {x: 50, y: 50, w: 10, h: 10}
+    var speed = 5
 
+    var obstacles = [
+        {x: 100, y: 100, w: 30, h: 20},
+        {x: 20, y: 250, w: 10, h: 20},
+    ]
 
     var canvas = document.querySelector('canvas')
     var ctx = canvas.getContext('2d')
         canvas.width = canvas.width // clear
 
         ctx.fillStyle = 'red'
-        ctx.fillRect(pos.x, pos.y, size.x, size.y)
+        ctx.fillRect(player.x, player.y, player.w, player.h)
+
+        ctx.fillStyle = 'black'
+        for (var i in obstacles) {
+            var o = obstacles[i]
+            ctx.fillRect(o.x, o.y, o.w, o.h)
+        }
     }
 
+    /*
+    less(-1, 0, 2) is the equivalent of -1 <= 0 && 0 <= 2
+    */
+    function less() {
+        for(var i=0; i<arguments.length - 1; i++) {
+            if (arguments[i] > arguments[i+1]) {
+                return false
+            }
+        }
 
-    function swap(new_pos) {
-        if ((0 <= new_pos.x && new_pos.x + size.x <= canvas.width) &&
-            (0 <= new_pos.y && new_pos.y + size.y <= canvas.height)) {
+        return true
+    }
 
-            pos = new_pos
+    function move(to) {
+        if ((0 <= to.x && to.x + player.w <= canvas.width) &&
+            (0 <= to.y && to.y + player.h <= canvas.height)) {
+            // within canvas
+
+            /*
+                0 1 2 3 4 5 6 7 8 9
+                [   { ]     }
+                [     ]  {  }
+                {   [ }     ]
+            */
+
+            /*
+            for (var i in obstacles) {
+                var o = obstacles[i]
+
+                var playerLeft = player.x
+                var playerRight = player.x + player.w
+                var playerUp = player.y
+                var playerDown = player.y + player.h
+
+                var obstacleLeft = o.x
+                var obstacleRight = o.x + o.w
+                var obstacleUp = o.y
+                var obstacleDown = o.y + o.h
+
+                if (less(obstacleLeft, playerLeft, obstacleRight) ||
+                    less(obstacleLeft, playerRight, obstacleRight) ||
+                    less(obstacleUp, playerUp, obstacleDown) ||
+                    less(obstacleUp, playerDown, obstacleDown)) {
+                    // collides with obstacle
+
+                    return false
+                }
+            }
+            */
+
+            player.x = to.x
+            player.y = to.y
+
             return true
         }
 
     }
 
     function gravity() {
-        swap({
-            x: pos.x,
-            y: pos.y + 1
+        move({
+            x: player.x,
+            y: player.y + 1
         })
     }
 
         var direction = keys[ev.keyCode]
 
         if (direction) {
-            swap({
-                x: pos.x + direction.x * speed,
-                y: pos.y + direction.y * speed,
+            move({
+                x: player.x + direction.x * speed,
+                y: player.y + direction.y * speed,
             })
 
             ev.preventDefault()
 
     }
 
-    var invervals = {}
+    var intervals = {}
 
     this.start = function() {
         window.addEventListener('keydown', handleKeys)