From a76008d05368830d56bf3d69d60a60574e2f7cd6 Mon Sep 17 00:00:00 2001 From: Cyrille Bogaert Date: Sun, 26 Feb 2012 10:08:16 +0100 Subject: [PATCH] rounds refactored --- js/_rounds.js | 297 ++++++++++++++++++++++++-------------------------- 1 file changed, 140 insertions(+), 157 deletions(-) diff --git a/js/_rounds.js b/js/_rounds.js index 6763ea0..d0c03c0 100644 --- a/js/_rounds.js +++ b/js/_rounds.js @@ -1,4 +1,4 @@ -var loadScene = function(scene) { +function loadScene(scene) { switch(scene) { case 1: resetStorage(); @@ -12,7 +12,8 @@ var loadScene = function(scene) { break; } }; -var loadLevel = function(level) { + +function loadLevel(level) { DTD.player = Crafty.e("Wolf"); DTD.skills = []; DTD.skills["throwingAxeSkill"] = throwingAxeSkill(); @@ -30,15 +31,129 @@ var loadLevel = function(level) { if(level > 3) { DTD.skills["rockSkill"] = rockSkill(); } - rounds[level](); + var round = Crafty.e("Round"); + round.create(level); } }; -var youWin = function() { + +function youWin() { $("body").append('
You WIN !
'); }; -var youLoose = function() { + +function youLoose() { $("body").append('
You LOOSE !
'); }; + +function Round() { + this.waves = []; + this.lastWaveAt = 0; +} + +Round.prototype.addWave = function(wave, at) { + this.lastWaveAt += at; + this.waves.push({ + wave : wave, + at : this.lastWaveAt + }); +}; +var rounds = []; +rounds[1] = new Round(); +rounds[1].addWave("!p!!p!", 1); +rounds[1].addWave("!!!p!!", 5); +rounds[1].addWave("!p!p!!", 3); +rounds[1].addWave("p!!!p!", 5); +rounds[1].addWave("!p!p!!", 5); +rounds[1].addWave("!!p!!!", 3); +rounds[1].addWave("!!!!!p", 3); + +rounds[2] = new Round(); +rounds[2].addWave("!!p!p!", 1); +rounds[2].addWave("!p!p!!", 4.5); +rounds[2].addWave("!p!p!p", 4.5); +rounds[2].addWave("p!!!p!", 6); +rounds[2].addWave("p!p!p!", 4.5); +rounds[2].addWave("p!!!p!", 6); +rounds[2].addWave("pp!!pp", 4.5); + +rounds[3] = new Round(); +rounds[3].addWave("!!!!!r", 1); +rounds[3].addWave("!!!!r!", 1.5); +rounds[3].addWave("!!!r!!", 1.5); +rounds[3].addWave("!!r!!!", 1.5); +rounds[3].addWave("!r!!!!", 1.5); +rounds[3].addWave("r!!!!!", 1.5); +rounds[3].addWave("p!!!!r", 1.5); +rounds[3].addWave("p!!!!r", 3.5); +rounds[3].addWave("!p!!r!", 3.5); +rounds[3].addWave("!!pr!!", 3.5); +rounds[3].addWave("prprpr", 3.5); + +rounds[4] = new Round(); +rounds[4].addWave("!!!!!r", 1); +rounds[4].addWave("p!!!!r", 3); +rounds[4].addWave("!p!!r!", 3); +rounds[4].addWave("!!pr!!", 3); +rounds[4].addWave("p!!!!r", 3.5); +rounds[4].addWave("!p!!r!", 3); +rounds[4].addWave("!!pr!!", 3); +rounds[4].addWave("prprpr", 3); +rounds[4].addWave("p!!!!r", 8); +rounds[4].addWave("!p!!r!", 3.5); +rounds[4].addWave("!!pr!!", 3); +rounds[4].addWave("p!!!!r", 3.5); +rounds[4].addWave("!p!!r!", 3); +rounds[4].addWave("!!pr!!", 3); + +rounds[5] = new Round(); +rounds[5].addWave("!!!g!!", 1); +rounds[5].addWave("!p!!p!", 7); +rounds[5].addWave("!!r!!!", 3); +rounds[5].addWave("!p!!!!", 1.5); +rounds[5].addWave("!!!!r!", 1.5); +rounds[5].addWave("!r!!!p", 4.5); +rounds[5].addWave("!!!r!p", 4.5); +rounds[5].addWave("p!!!p!", 4.5); +rounds[5].addWave("pppppp", 4); +rounds[5].addWave("prpprp", 8); +rounds[5].addWave("!!!!g!", 8); + +rounds[6] = new Round(); +rounds[6].addWave("!!g!!!", 1); +rounds[6].addWave("!r!!r!", 4); +rounds[6].addWave("p!!!!p", 3); +rounds[6].addWave("!r!r!r", 6); +rounds[6].addWave("p!p!p!", 5); +rounds[6].addWave("!r!p!r", 6); +rounds[6].addWave("!p!!!p", 5); +rounds[6].addWave("!!rp!r", 3); +rounds[6].addWave("!!rr!!", 6); +rounds[6].addWave("p!!!!p", 3); +rounds[6].addWave("!pppp!", 3); +rounds[6].addWave("!g!!g!", 6); +rounds[6].addWave("!!!r!!", 9); +rounds[6].addWave("!!g!!!", 2); +rounds[6].addWave("!!!!!p", 7); + +rounds[7] = new Round(); +rounds[7].addWave("!r!p!r", 1); +rounds[7].addWave("!p!!!p", 7); +rounds[7].addWave("!r!r!r", 3); +rounds[7].addWave("!g!!g!", 7); +rounds[7].addWave("!!g!!!", 9); +rounds[7].addWave("p!!!!p", 6); +rounds[7].addWave("!r!!r!", 3); +rounds[7].addWave("!g!p!g", 3); +rounds[7].addWave("!!rr!!", 9); +rounds[7].addWave("!!!r!!", 2); +rounds[7].addWave("!pppp!", 1); +rounds[7].addWave("!!g!!!", 7); +rounds[7].addWave("!r!r!r", 4); +rounds[7].addWave("!p!!!p", 6); +rounds[7].addWave("p!p!p!", 3); +rounds[7].addWave("!!!r!!", 7); +rounds[7].addWave("!pppp!", 3); +rounds[7].addWave("p!p!p!", 7); + var currentRound = false; Crafty.c("Round", { init : function() { @@ -94,7 +209,7 @@ Crafty.c("Round", { Crafty.scene("credits"); } else { storage.level.set(storage.level.get() + 1); - loadScene(storage.level.get()); + loadScene(storage.level.get()); } }, 2000); } @@ -102,8 +217,13 @@ Crafty.c("Round", { }; }); }, - create : function(id) { - this._roundId = id; + create : function(level) { + this._roundId = level; + var that = this; + _.each(rounds[level].waves, function(item, key) { + that.addWave(item.wave, item.at) + }); + this.play(); }, addWave : function(wave, at) { this.waves.push({ @@ -148,11 +268,11 @@ Crafty.c("Endless", { } }); }, - score: function(score) { + score : function(score) { this._score += score; $("#endlessInGameScore").html(this._score); }, - upgradeDifficulty: function() { + upgradeDifficulty : function() { if(this.pigChance >= 50) { this.pigChance--; } else { @@ -165,169 +285,32 @@ Crafty.c("Endless", { } this.realDelay(function() { this.upgradeDifficulty(); - },3000); + }, 3000); }, - generateMonster: function() { + generateMonster : function() { if(DTD.gameType == "endless") { - + if(rolling(this.pigChance)) { var newEnemy = Crafty.e("Pig"); - newEnemy.setToLine(Crafty.math.randomInt(1,6)); + newEnemy.setToLine(Crafty.math.randomInt(1, 6)); newEnemy.attachSprite(Crafty.e("PigSprite")); } else if(rolling(this.riddingChance)) { var newEnemy = Crafty.e("Ridding"); - newEnemy.setToLine(Crafty.math.randomInt(1,6)); + newEnemy.setToLine(Crafty.math.randomInt(1, 6)); newEnemy.attachSprite(Crafty.e("RiddingSprite")); } else { var newEnemy = Crafty.e("Granny"); - newEnemy.setToLine(Crafty.math.randomInt(1,6)); + newEnemy.setToLine(Crafty.math.randomInt(1, 6)); newEnemy.attachSprite(Crafty.e("GrannySprite")); } - - + this.realDelay(function() { this.generateMonster(); - },this.nextTimeout); - + }, this.nextTimeout); + } else { this.destroy(); } - + } }); - -var rounds = []; -rounds[1] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(1); - round.addWave("!p!!p!", lastWave++); - round.addWave("!!!p!!", lastWave += 5); - round.addWave("!p!p!!", lastWave += 3); - round.addWave("p!!!p!", lastWave += 5); - round.addWave("!p!p!!", lastWave += 5); - round.addWave("!!p!!!", lastWave += 3); - round.addWave("!!!!!p", lastWave += 3); - round.play(); - return round; -}; -rounds[2] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(2); - round.addWave("!!p!p!", lastWave++); - round.addWave("!p!p!!", lastWave += 4.5); - round.addWave("!p!p!p", lastWave += 4.5); - round.addWave("p!!!p!", lastWave += 6); - round.addWave("p!p!p!", lastWave += 4.5); - round.addWave("p!!!p!", lastWave += 6); - round.addWave("pp!!pp", lastWave += 4.5); - round.play(); - return round; -}; -rounds[3] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(3); - round.addWave("!!!!!r", lastWave++); - round.addWave("!!!!r!", lastWave += 1.5); - round.addWave("!!!r!!", lastWave += 1.5); - round.addWave("!!r!!!", lastWave += 1.5); - round.addWave("!r!!!!", lastWave += 1.5); - round.addWave("r!!!!!", lastWave += 1.5); - round.addWave("p!!!!r", lastWave += 1.5); - round.addWave("p!!!!r", lastWave += 3.5); - round.addWave("!p!!r!", lastWave += 3.5); - round.addWave("!!pr!!", lastWave += 3.5); - round.addWave("prprpr", lastWave += 3.5); - round.play(); - return round; -}; -rounds[4] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(4); - round.addWave("!!!!!r", lastWave++); - round.addWave("p!!!!r", lastWave += 3); - round.addWave("!p!!r!", lastWave += 3); - round.addWave("!!pr!!", lastWave += 3); - round.addWave("p!!!!r", lastWave += 3.5); - round.addWave("!p!!r!", lastWave += 3); - round.addWave("!!pr!!", lastWave += 3); - round.addWave("prprpr", lastWave += 3); - round.addWave("p!!!!r", lastWave += 8); - round.addWave("!p!!r!", lastWave += 3.5); - round.addWave("!!pr!!", lastWave += 3); - round.addWave("p!!!!r", lastWave += 3.5); - round.addWave("!p!!r!", lastWave += 3); - round.addWave("!!pr!!", lastWave += 3); - round.play(); - return round; -}; -rounds[5] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(5); - round.addWave("!!!g!!", 1); - round.addWave("!p!!p!", lastWave += 7); - round.addWave("!!r!!!", lastWave += 3); - round.addWave("!p!!!!", lastWave += 1.5); - round.addWave("!!!!r!", lastWave += 1.5); - round.addWave("!r!!!p", lastWave += 4.5); - round.addWave("!!!r!p", lastWave += 4.5); - round.addWave("p!!!p!", lastWave += 4.5); - round.addWave("pppppp", lastWave += 4); - round.addWave("prpprp", lastWave += 8); - round.addWave("!!!!g!", lastWave += 8); - round.play(); - return round; -}; - -rounds[6] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(6); - round.addWave("!!g!!!", 1); - round.addWave("!r!!r!", lastWave += 4); - round.addWave("p!!!!p", lastWave += 3); - round.addWave("!r!r!r", lastWave += 6); - round.addWave("p!p!p!", lastWave += 5); - round.addWave("!r!p!r", lastWave += 6); - round.addWave("!p!!!p", lastWave += 5); - round.addWave("!!rp!r", lastWave += 3); - round.addWave("!!rr!!", lastWave += 6); - round.addWave("p!!!!p", lastWave += 3); - round.addWave("!pppp!", lastWave += 3); - round.addWave("!g!!g!", lastWave += 6); - round.addWave("!!!r!!", lastWave += 9); - round.addWave("!!g!!!", lastWave += 2); - round.addWave("!!!!!p", lastWave += 7); - round.play(); - return round; -}; - -rounds[7] = function() { - var round = Crafty.e("Round"); - var lastWave = 0; - round.create(7); - round.addWave("!r!p!r", 1); - round.addWave("!p!!!p", lastWave += 7); - round.addWave("!r!r!r", lastWave += 3); - round.addWave("!g!!g!", lastWave += 7); - round.addWave("!!g!!!", lastWave += 9); - round.addWave("p!!!!p", lastWave += 6); - round.addWave("!r!!r!", lastWave += 3); - round.addWave("!g!p!g", lastWave += 3); - round.addWave("!!rr!!", lastWave += 9); - round.addWave("!!!r!!", lastWave += 2); - round.addWave("!pppp!", lastWave += 1); - round.addWave("!!g!!!", lastWave += 7); - round.addWave("!r!r!r", lastWave += 4); - round.addWave("!p!!!p", lastWave += 6); - round.addWave("p!p!p!", lastWave += 3); - round.addWave("!!!r!!", lastWave += 7); - round.addWave("!pppp!", lastWave += 3); - round.addWave("p!p!p!", lastWave += 7); - round.play(); - return round; -}; \ No newline at end of file