Mercurial > mhrok3
changeset 21:1e53472d9244
throwable razor blades
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Wed, 31 Oct 2012 18:19:45 +0100 |
| parents | 8612a4068d43 |
| children | b688f8cce051 |
| files | effects.png mhrok.css mhrok.js zombie.png |
| diffstat | 4 files changed, 89 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/mhrok.css Wed Oct 31 16:09:03 2012 +0100 +++ b/mhrok.css Wed Oct 31 18:19:45 2012 +0100 @@ -28,6 +28,9 @@ #mhrok .mhrok-zombie .mhrok-sprite { background-image: url('zombie.png'); } +#mhrok .mhrok-razor .mhrok-sprite { + background-image: url('effects.png'); +} #mhrok .mhrok-shadow { width: 48px;
--- a/mhrok.js Wed Oct 31 16:09:03 2012 +0100 +++ b/mhrok.js Wed Oct 31 18:19:45 2012 +0100 @@ -5,12 +5,21 @@ this.x = 0; this.frameX = 0; this.frameY = 0; + this.dead = false; + this.frame = 0; }; Mob.prototype = {}; Mob.prototype.classes = 'mhrok-mob'; + Mob.prototype.directions = { + 'west': [-1, 0, 1], + 'north': [0, -1, 2], + 'east': [1, 0, 3], + 'south': [0, 1, 0], + }; Mob.prototype.animate = function (frame) { + this.frame = (this.frame + 1) % 8; this.frameX = 0; - this.frameY = Math.round(frame / 2); + this.frameY = Math.round(this.frame / 2); }; Mob.prototype.update = function (game) {}; Mob.prototype.html = function () { @@ -52,21 +61,17 @@ this.y = 24; }; Char.prototype = Object.create(Mob.prototype); - Char.prototype.directions = { - 'west': [-1, 0, 1], - 'north': [0, -1, 2], - 'east': [1, 0, 3], - 'south': [0, 1, 0], - }; Char.prototype.animate = function (frame) { var dir = this.directions[this.direction]; if (this.animation == 'stand') { + this.frame = 0; this.frameX = dir[2] + 4; this.frameY = 0; }; if (this.animation == 'walk') { + this.frame = (this.frame + 1) % 8; this.frameX = dir[2] + 4; - this.frameY = Math.round(frame / 2); + this.frameY = Math.round(this.frame / 2); this.x += dir[0] * 4; this.y += dir[1] * 3; }; @@ -89,42 +94,6 @@ }; - var Hero = function () { - Char.apply(this, arguments); - this.waiting = true; - }; - Hero.prototype = Object.create(Char.prototype); - Hero.prototype.classes += ' mhrok-hero'; - Hero.prototype.animate = function (frame) { - var dir = this.directions[this.direction]; - this.waiting = (this.animation === 'stand'); - if (this.animation == 'stab') { - this.frameX = dir[2] + 8; - this.frameY = Math.round(frame / 2); - }; - Char.prototype.animate.apply(this, arguments); - }; - Hero.prototype.update = function (game) { - 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; - }; - return true; - }; - - var Level = function (map) { this.map = map; }; @@ -164,7 +133,6 @@ return false; }; var c = this.map[y][x]; - console.log(c); return (c.toUpperCase() === c); }; @@ -282,9 +250,13 @@ }; }; var html = ''; - this.mobs.forEach(function (mob) { - mob.animate(this.frame_number); - html += mob.html(); + this.mobs.forEach(function (mob, i) { + if (!mob.dead) { + mob.animate(this.frame_number); + html += mob.html(); + } else { + delete this.mobs[i]; + }; }, this); this.mobs_element.innerHTML = html; }; @@ -293,7 +265,6 @@ Game: Game, Mob: Mob, Char: Char, - Hero: Hero, Wall: Wall, Level: Level, } @@ -334,6 +305,70 @@ return false; }; + var Razor = function (x, y, direction) { + MhrokLib.Mob.apply(this, arguments); + this.x = x * 32; + this.y = y * 24; + this.z = y; + this.direction = direction; + this.ttl = 100; + }; + Razor.prototype = Object.create(MhrokLib.Mob.prototype); + Razor.prototype.classes += ' mhrok-razor'; + Razor.prototype.animate = function (frame) { + var dir = this.directions[this.direction]; + this.frame = (this.frame + 1) % 8; + this.frameX = 0; + this.frameY = this.frame; + this.x += dir[0] * 8; + this.y += dir[1] * 6; + this.ttl -= 1; + if (this.ttl <= 0) { + this.dead = true; + }; + }; + + + var Hero = function () { + MhrokLib.Char.apply(this, arguments); + this.waiting = true; + }; + Hero.prototype = Object.create(MhrokLib.Char.prototype); + Hero.prototype.classes += ' mhrok-hero'; + Hero.prototype.animate = function (frame) { + var dir = this.directions[this.direction]; + this.waiting = (this.animation === 'stand'); + if (this.animation == 'stab') { + this.frameX = dir[2] + 8; + this.frameY = Math.round(frame / 2); + }; + MhrokLib.Char.prototype.animate.apply(this, arguments); + }; + Hero.prototype.update = function (game) { + 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 + return this.stab(game); + } else { + this.animation = 'stand'; + return false; + }; + return true; + }; + Hero.prototype.stab = function (game) { + this.animation = 'stab'; + game.mobs.push(new Razor(this.x/32, this.y/24, this.direction)); + return true; + }; + var Game = function () { MhrokLib.Game.apply(this, arguments); @@ -360,9 +395,9 @@ 'XXXXXXXXXXXXXXXXXXXX', ]); this.map_element.innerHTML = this.level.html(); - this.hero = new MhrokLib.Hero(); + this.mobs.push(new Zombie(10, 8)); + this.hero = new Hero(); this.mobs.push(this.hero); - this.mobs.push(new Zombie(10, 8)); }; Game.prototype = Object.create(MhrokLib.Game.prototype);
