Mercurial > mhrok3
changeset 20:8612a4068d43
make the walls block
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Wed, 31 Oct 2012 16:09:03 +0100 |
| parents | 46740b0df19c |
| children | 1e53472d9244 |
| files | mhrok.css mhrok.js |
| diffstat | 2 files changed, 68 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/mhrok.css Wed Oct 31 15:18:02 2012 +0100 +++ b/mhrok.css Wed Oct 31 16:09:03 2012 +0100 @@ -72,6 +72,6 @@ #mhrok .mhrok-map .mhrok-a { background-position: -0px -48px; } -#mhrok .mhrok-map .mhrok-b { +#mhrok .mhrok-map .mhrok-B { background-position: -32px -24px; }
--- a/mhrok.js Wed Oct 31 15:18:02 2012 +0100 +++ b/mhrok.js Wed Oct 31 16:09:03 2012 +0100 @@ -75,9 +75,17 @@ Char.prototype.update = function (game) { this.animation = 'stand'; }; - Char.prototype.walk = function (direction) { + Char.prototype.walk = function (game, direction) { + var x = Math.round(this.x / 32); + var y = Math.round(this.y /24); + var dir = this.directions[direction]; + this.direction = direction; + if (game.level.isBlocked(x + dir[0], y + dir[1])) { + this.animation = 'stand'; + return false; + }; this.animation = 'walk'; - this.direction = direction; + return true; }; @@ -97,21 +105,22 @@ Char.prototype.animate.apply(this, arguments); }; Hero.prototype.update = function (game) { - if (game.keys[37] || game.last_key === 37) { // Left arrow - this.walk('west'); - } else if (game.keys[38] || game.last_key === 38) { // Up arrow - this.walk('north'); - } else if (game.keys[39] || game.last_key === 39) { // Right arrow - this.walk('east'); - } else if (game.keys[40] || game.last_key === 40) { // Down arrow - this.walk('south'); - } else if (game.keys[32] || game.last_key === 32) { // Space + var last_key = game.last_key; + game.last_key = null; + if (game.keys[37] || last_key === 37) { // Left arrow + return this.walk(game, 'west'); + } else if (game.keys[38] || last_key === 38) { // Up arrow + return this.walk(game, 'north'); + } else if (game.keys[39] || last_key === 39) { // Right arrow + return this.walk(game, 'east'); + } else if (game.keys[40] || last_key === 40) { // Down arrow + return this.walk(game, 'south'); + } else if (game.keys[32] || last_key === 32) { // Space this.animation = 'stab'; } else { this.animation = 'stand'; return false; }; - game.last_key = null; return true; }; @@ -140,7 +149,7 @@ for (var y=0; y < 20; ++y) { for (var x=0; x < 20; ++x) { var c = this.map[y][x]; - if (c === 'b') { + if (c === 'B') { mobs.push(new Wall(x, y, 1)); }; if (c === 'X') { @@ -150,6 +159,14 @@ }; return mobs; }; + Level.prototype.isBlocked = function (x, y) { + if (x<0 || y <0 || y>=this.map.lentgh || x>=this.map[0].length) { + return false; + }; + var c = this.map[y][x]; + console.log(c); + return (c.toUpperCase() === c); + }; var Game = function (element) { @@ -294,16 +311,27 @@ Zombie.prototype.classes += ' mhrok-zombie'; Zombie.prototype.update = function (game) { if (game.hero.y > this.y) { - this.walk('south'); - } else if (game.hero.y < this.y) { - this.walk('north'); - } else if (game.hero.x > this.x) { - this.walk('east'); - } else if (game.hero.x < this.x) { - this.walk('west'); - } else { - this.animation = 'stand'; + if (this.walk(game, 'south')) { + return true; + }; + }; + if (game.hero.y < this.y) { + if (this.walk(game, 'north')) { + return true; + }; }; + if (game.hero.x > this.x) { + if (this.walk(game, 'east')) { + return true; + }; + }; + if (game.hero.x < this.x) { + if (this.walk(game, 'west')) { + return true; + }; + }; + this.animation = 'stand'; + return false; }; @@ -311,24 +339,24 @@ MhrokLib.Game.apply(this, arguments); this.level = new MhrokLib.Level([ 'XXXXXXXXXXXXXXXXXXXX', - 'XabaaaaaaaaaaaaaaaaX', - 'XaaaaaabaaaaaaaaaaaX', - 'XaaaabbbaaaaaaaaaabX', - 'XaababXbaaaaaaaaaabX', - 'XaaaabbbbaabbaaaaabX', - 'XaaaaaabbaabbaaaaaaX', - 'XaaaaaaaaaaaaaabbaaX', - 'XaaaaaaaaaaabaabbaaX', + 'XaBaaaaaaaaaaaaaaaaX', + 'XaaaaaaBaaaaaaaaaaaX', + 'XaaaaBBBaaaaaaaaaaBX', + 'XaaBaBXBaaaaaaaaaaBX', + 'XaaaaBBBBaaBBaaaaaBX', + 'XaaaaaaBBaaBBaaaaaaX', + 'XaaaaaaaaaaaaaaBBaaX', + 'XaaaaaaaaaaaBaaBBaaX', 'XaaaaaaaaaaaaaaaaaaX', - 'XaaaaaaaaaaaabbbbabX', - 'XaaaaaaaaaaaabbbbabX', - 'XaabbbbaaaaaaaabaabX', - 'XaabXXbaaaaaaaaaabbX', - 'XaabXXbaaaaaaaaaabXX', - 'XaabbbbaaaaaaaaaabXX', - 'XaaaaaaaaaabaabbbbXX', - 'XaaaaabbbabbbbbXXXXX', - 'XaaaabbXbbbXXXXXXXXX', + 'XaaaaaaaaaaaaBBBBaBX', + 'XaaaaaaaaaaaaBBBBaBX', + 'XaaBBBBaaaaaaaaBaaBX', + 'XaaBXXBaaaaaaaaaaBBX', + 'XaaBXXBaaaaaaaaaaBXX', + 'XaaBBBBaaaaaaaaaaBXX', + 'XaaaaaaaaaaBaaBBBBXX', + 'XaaaaaBBBaBBBBBXXXXX', + 'XaaaaBBXBBBXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXX', ]); this.map_element.innerHTML = this.level.html();
