瀏覽代碼

Feature: add shotcorn

Fabrice Ecaille 12 年之前
父節點
當前提交
586dcbc0f6
共有 3 個文件被更改,包括 28 次插入3 次删除
  1. 4 0
      css/spaceinvaders.css
  2. 4 2
      js/spaceinvaders-core.js
  3. 20 1
      js/spaceinvaders-models.js

+ 4 - 0
css/spaceinvaders.css

@@ -23,6 +23,10 @@
 	background-color: orange;
 }
 
+.shipShot.shotCorn {
+	background-color: white;
+}
+
 .alienShot {
 	background-color: red;
 }

+ 4 - 2
js/spaceinvaders-core.js

@@ -183,7 +183,7 @@ Game = {
 		$.each(Game.aliens, function(index, alien ) {
 			alien.move();
 			if( alien.health > 0 && Math.random() < alien.aggression ) {
-				alien.fire($("#aliensShots"), "alienShot");
+				//alien.fire($("#aliensShots"), "alienShot");
 			}
 		});
 	},
@@ -200,8 +200,10 @@ Game = {
 		
 		shots.each(function(i,e) {
 			var posy = $(this).y(),
+				posx = $(this).x(),
 				weapon = $(this)[0].weapon;
-			if( posy < -$(this).height() ) {
+				
+			if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) {
 				this.remove();
 				return;
 			}

+ 20 - 1
js/spaceinvaders-models.js

@@ -97,7 +97,7 @@ heriter(CarotWeapon.prototype, Weapon.prototype);
 function CornWeapon() {
 	"use strict";
 	this.directionY = -1;
-	this.stock = 10;
+	this.stock = 3;
 	this.clazz = "corn";
 	this.load = 1;
 	this.max_load = 1;
@@ -114,6 +114,25 @@ function CornWeapon() {
 		
 		if( shot.y() < mediumAlien ) {
 			shot.remove();
+			var shipShots = $("#shipShots");
+			for( var i = 0; i < 8; i++) {
+				var cos = Math.cos( (Math.PI / 4) * i ),
+					sin = Math.sin( (Math.PI / 4) * i);
+				if( Math.abs(cos) < 0.01 ) {
+					cos = 0;
+				}
+				if( Math.abs(sin) < 0.01) {
+					sin = 0;
+				}
+				shipShots.addSprite( "shotCorn" + i, { posx : shot.x() + 5 * cos, posy : shot.y() + 5 * sin, width: 2, height: 2});
+				var shotCorn = $("#shotCorn" + i);
+				shotCorn.addClass("shipShot").addClass("shotCorn");
+				$("#shotCorn" + i)[0].weapon = $.extend({
+						directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0,
+						directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0,
+						speed : 1
+					}, shot.weapon); 
+			}
 		}
 	}
 }