Bläddra i källkod

Feature: adding wave bonus + modal pane

Fabrice Ecaille 12 år sedan
förälder
incheckning
97dee3deb0
5 ändrade filer med 85 tillägg och 18 borttagningar
  1. 5 0
      css/spaceinvaders.css
  2. 5 5
      js/lettering.js
  3. 31 9
      js/spaceinvaders-core.js
  4. 6 0
      js/spaceinvaders-models.js
  5. 38 4
      js/spaceinvaders-ui.js

+ 5 - 0
css/spaceinvaders.css

@@ -122,3 +122,8 @@
 }
 }
 /** Scoreboard end **/
 /** Scoreboard end **/
 
 
+
+/** Modal **/
+.modal {
+	background-color: yellow;
+/** Modal - end **/

+ 5 - 5
js/lettering.js

@@ -9,7 +9,7 @@
 */
 */
 
 
 (function($) {
 (function($) {
-	function injector(t, splitter, klass, after) {
+	function injector(t, splitter, forceSmallParam) {
 		var a = t.text().split(splitter);
 		var a = t.text().split(splitter);
 		var html = "",
 		var html = "",
 		clazz = "clock",
 		clazz = "clock",
@@ -20,7 +20,7 @@
 		lineSize,
 		lineSize,
 		letter, iLetter,
 		letter, iLetter,
 		i, x, y
 		i, x, y
-		forceSmall = false;
+		forceSmall = false;//forceSmallParam ? forceSmallParam : false;
 		if( typeof customClazz != "undefined" ) {
 		if( typeof customClazz != "undefined" ) {
 			clazz = " clock " + customClazz;
 			clazz = " clock " + customClazz;
 		}
 		}
@@ -33,14 +33,14 @@
 						width += 16;
 						width += 16;
 						count = count + 1;
 						count = count + 1;
 					} else {
 					} else {
-						if( letter.charCodeAt(0) > 47 && letter.charCodeAt(0) < 58 ) {
+						if( letter.charCodeAt(0) > 47 && letter.charCodeAt(0) < 58 ) { // Numbers
 							letterSize = 32;
 							letterSize = 32;
 							if( forceSmall ) {
 							if( forceSmall ) {
 								letterSize = 16;
 								letterSize = 16;
 							}
 							}
-							html += "<span class='" + clazz + (forceSmall ? "small" : "") + "' style='top: -50%;background-position: -" + ( parseInt( letter, null ) * letterSize) + "px -" + (forceSmall > -1 ? 128 : 0) +"px'></span>";
+							html += "<span class='" + clazz + (forceSmall ? "small" : "") + "' style='top: -50%;background-position: -" + ( parseInt( letter, null ) * letterSize) + "px -" + (forceSmall ? 128 : 0) +"px'></span>";
 							count = count + 1;
 							count = count + 1;
-						} else {
+						} else { // Letters
 							if( ( letter.charCodeAt(0) >= 'a'.charCodeAt(0) && letter.charCodeAt(0) <= 'z'.charCodeAt(0)) ) {
 							if( ( letter.charCodeAt(0) >= 'a'.charCodeAt(0) && letter.charCodeAt(0) <= 'z'.charCodeAt(0)) ) {
 								if( height < 16 ) {
 								if( height < 16 ) {
 									height = 16;
 									height = 16;

+ 31 - 9
js/spaceinvaders-core.js

@@ -48,13 +48,18 @@ Game = {
 	aliens : [],
 	aliens : [],
 	ship : null,
 	ship : null,
 	score : 0,
 	score : 0,
+	shots : {
+		total : 0,
+		lost : 0
+	},
 	
 	
 	init : function() {
 	init : function() {
 		"use strict";
 		"use strict";
 		animations = WORLD.farm;
 		animations = WORLD.farm;
 		Game.wave_index = Game.wave_index + 1;
 		Game.wave_index = Game.wave_index + 1;
 		Game.wave = WAVES[Math.min(Game.wave_index, WAVES.length - 1)];
 		Game.wave = WAVES[Math.min(Game.wave_index, WAVES.length - 1)];
-		console.log(Game.wave_index, Game.wave);
+		Game.wave.score = 0;
+		
 		var row, col, wave = Game.wave.wave;
 		var row, col, wave = Game.wave.wave;
 		
 		
 		for (row = 0; row < wave.length; row = row + 1) {
 		for (row = 0; row < wave.length; row = row + 1) {
@@ -68,18 +73,36 @@ Game = {
 		SCOREBOARD.init();
 		SCOREBOARD.init();
 		SCOREBOARD.set_score( Game.score );
 		SCOREBOARD.set_score( Game.score );
 		
 		
+		$("#level").append(Game.wave_index + 1).lettering();
+		
 		Game.setShip();
 		Game.setShip();
 		
 		
+		hideModal();
 		Game.running = true;
 		Game.running = true;
 	},
 	},
 
 
+	game_over : function() {
+		displayModal( {
+			end: "Loose",
+			score: Game.score
+		});
+	},
+	
 	levelComplete : function() {
 	levelComplete : function() {
 		"use strict";
 		"use strict";
 		Game.running = false;
 		Game.running = false;
+		var bonus = Math.round(((Game.shots.total - Game.shots.lost) / Game.shots.total) * Game.wave.score);
+		Game.addToScore(bonus);
+		
+		displayModal( {
+			bonus: bonus,
+			wave_score: Game.wave.score,
+			score: Game.score,
+		});
 
 
 		setTimeout(function() {
 		setTimeout(function() {
 			Game.init();
 			Game.init();
-		}, 3000);
+		}, 5000);
 	},
 	},
 	
 	
 	hit : function() {
 	hit : function() {
@@ -105,8 +128,7 @@ Game = {
 			}, 2000);
 			}, 2000);
 		}
 		}
 		else {
 		else {
-			GUI.drawText( $("#message"), game_over, true );
-			Game.show_game_over();
+			Game.game_over();
 		}
 		}
 	},
 	},
 	
 	
@@ -140,6 +162,7 @@ Game = {
 	
 	
 	addToScore : function( toAdd ) {
 	addToScore : function( toAdd ) {
 		Game.score = Game.score + toAdd;
 		Game.score = Game.score + toAdd;
+		Game.wave.score = Game.wave.score + toAdd;
 		SCOREBOARD.add( toAdd );
 		SCOREBOARD.add( toAdd );
 	},
 	},
 	
 	
@@ -207,6 +230,7 @@ Game = {
 				weapon = $(this)[0].weapon;
 				weapon = $(this)[0].weapon;
 				
 				
 			if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) {
 			if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) {
+				Game.shots.lost = Game.shots.lost + 1;
 				this.remove();
 				this.remove();
 				return;
 				return;
 			}
 			}
@@ -265,9 +289,7 @@ Game = {
 
 
 
 
 	bonus : function(alienX, alienY, alienWidth, alienHeight) {
 	bonus : function(alienX, alienY, alienWidth, alienHeight) {
-		console.log( animations.bonus, animations.bonus.length - 1);
 		var bonus = animations.bonus[Math.round(Math.random() * (animations.bonus.length - 1)) ];
 		var bonus = animations.bonus[Math.round(Math.random() * (animations.bonus.length - 1)) ];
-		console.log("Bonus ", bonus, " pos ", alienX, " ", alienY, " ", alienWidth, " ", alienHeight);
 		var id = Math.round(Math.random() * 10000);
 		var id = Math.round(Math.random() * 10000);
 		$("#actors").addSprite("bonus" + id, 
 		$("#actors").addSprite("bonus" + id, 
 			$.extend(
 			$.extend(
@@ -294,10 +316,10 @@ Game = {
 			$(this).y(weapon.directionY * weapon.speed, true);
 			$(this).y(weapon.directionY * weapon.speed, true);
 			$(this).x(weapon.directionX * weapon.speed, true);
 			$(this).x(weapon.directionX * weapon.speed, true);
 			var collisions = $(this).collision("#ship,."+$.gQ.groupCssClass);
 			var collisions = $(this).collision("#ship,."+$.gQ.groupCssClass);
-			collisions.each( function() {
-				Game.hit();
-			})
 			if( collisions.length > 0 ) {
 			if( collisions.length > 0 ) {
+				collisions.each( function() {
+					Game.hit();
+				});
 				this.remove();
 				this.remove();
 			}
 			}
 		});
 		});

+ 6 - 0
js/spaceinvaders-models.js

@@ -394,6 +394,11 @@ Ship.prototype = {
 		$("#life" + this.lives).remove();
 		$("#life" + this.lives).remove();
 		this.lives = this.lives - 1;
 		this.lives = this.lives - 1;
 		$("#hero").children().hide();
 		$("#hero").children().hide();
+		if( this.lives == 0 ) {
+			Game.game_over();
+			return true;
+		}
+		
 		var _this = this;
 		var _this = this;
 		setTimeout(function() {
 		setTimeout(function() {
 			$("#hero").children().show();
 			$("#hero").children().show();
@@ -403,6 +408,7 @@ Ship.prototype = {
 
 
 	fire : function() {
 	fire : function() {
 		if(this._super("fire", arguments)) {
 		if(this._super("fire", arguments)) {
+			Game.shots.total = Game.shots.total + 1;
 			this.weapon.stock--;
 			this.weapon.stock--;
 			if( this.weapon.stock == 0 ) {
 			if( this.weapon.stock == 0 ) {
 				this.weapon = new ShotgunWeapon();
 				this.weapon = new ShotgunWeapon();

+ 38 - 4
js/spaceinvaders-ui.js

@@ -8,10 +8,11 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 */
 
 
-/*global jQuery, console */
+/*global jQuery */
 
 
 var PLAYGROUND_WIDTH = 448,
 var PLAYGROUND_WIDTH = 448,
 	PLAYGROUND_HEIGHT = 544,
 	PLAYGROUND_HEIGHT = 544,
+	MODAL_WIDTH = 300,
 	REFRESH_RATE = 15,
 	REFRESH_RATE = 15,
 	HUD_HEIGHT = 70,
 	HUD_HEIGHT = 70,
 
 
@@ -31,6 +32,40 @@ var SHIPS = {
 
 
 var animations = WORLD.farm;
 var animations = WORLD.farm;
 
 
+function displayModal(data) {
+	$.playground()
+		.addGroup( "modal_pane", {width: MODAL_WIDTH, height: data.end ? 190 : 140, posx: (PLAYGROUND_WIDTH - MODAL_WIDTH ) /2, posy: (PLAYGROUND_HEIGHT - (140 ))/2})
+			.addSprite("scoreLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 50 : 10})
+			.addSprite("scoreNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 50 : 10})
+		.end();
+	if( data.end ) {
+		var width = (data.end.length - 1 ) * 16 + 32;
+		$("#modal_pane").addSprite("gameSt", {width: width, height: 32, posx: (MODAL_WIDTH - width) / 2, posy: 10});
+		$("#gameSt").append(data.end).lettering();
+	}
+	
+	if( data.bonus ) {
+		$("#modal_pane")
+			.addSprite("bonusLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 90 : 50})
+			.addSprite("bonusNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 90 : 50})
+			.addSprite("totalLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 130 : 90})
+			.addSprite("totalNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 130 : 90})
+			;
+		$("#bonusLbl").append("Bonus").lettering();
+		$("#bonusNbr").append(data.bonus).lettering();
+		$("#totalLbl").append("Wave score").lettering();
+		$("#totalNbr").append(data.wave_score).lettering();
+	}
+	
+	$("#modal_pane").addClass( "modal" );
+	$("#scoreLbl").append("Score").lettering();
+	$("#scoreNbr").append(data.score).lettering();
+}
+
+function hideModal() {
+	$("#modal_pane").remove();
+}
+
 function updateWeaponLoad( load ) {
 function updateWeaponLoad( load ) {
 	"use strict";
 	"use strict";
 	var HTMLDiv = $("#weapon_load");
 	var HTMLDiv = $("#weapon_load");
@@ -107,7 +142,7 @@ $(function(){
 				.addSprite("life1", $.extend({posx: 64}, animations.life))
 				.addSprite("life1", $.extend({posx: 64}, animations.life))
 			.end()
 			.end()
 			.addGroup("levelGrp", {
 			.addGroup("levelGrp", {
-				posx: PLAYGROUND_WIDTH - 6 * 32 + 16,
+				posx: PLAYGROUND_WIDTH - (6 * 32 + 16),
 				posy: 2,
 				posy: 2,
 				width: 32 + 5 * 16,
 				width: 32 + 5 * 16,
 				height: 16
 				height: 16
@@ -118,8 +153,7 @@ $(function(){
 				.addSprite("level", {
 				.addSprite("level", {
 					width : 64,
 					width : 64,
 					posx: 32 + 7 * 16,
 					posx: 32 + 7 * 16,
-					posy: 16,
-					height: 16
+					height: 32
 				})
 				})
 			.end()
 			.end()
 		.end()
 		.end()