Browse Source

Feature: adding wave 3 and 4 + new moves : half_part_rotation & rotation

Fabrice Ecaille 12 years ago
parent
commit
be0c33a034
3 changed files with 172 additions and 30 deletions
  1. 6 6
      js/spaceinvaders-core.js
  2. 121 24
      js/spaceinvaders-models.js
  3. 45 0
      js/tools.js

+ 6 - 6
js/spaceinvaders-core.js

@@ -30,10 +30,11 @@ Game = {
 		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) {
-			var aliensRow = wave[row], type = aliensRow[0], offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length)
-					* ALIENS_WIDTH) / 2;
+			var 	aliensRow = wave[row], 
+				offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length) * ALIENS_WIDTH) / 2;
+				
 			for (col = 0; col < aliensRow.length; col = col + 1) {
 			for (col = 0; col < aliensRow.length; col = col + 1) {
-				Game.setAlien(col, row, offset, aliensRow[col], Game.wave.move);
+				Game.setAlien(col, row, col * wave.length + row, offset, aliensRow[col], Game.wave.move);
 			}
 			}
 		}
 		}
 
 
@@ -99,12 +100,11 @@ Game = {
 		}
 		}
 	},
 	},
 	
 	
-	setAlien : function(x, y, offset, type, move) {
+	setAlien : function(x, y, id, offset, type, move) {
 		"use strict";
 		"use strict";
 		if( typeof type == "undefined" ) {
 		if( typeof type == "undefined" ) {
 			return;
 			return;
 		}
 		}
-		var id = x * ROWS + y;
 		var alien = new type("alien" + id, {
 		var alien = new type("alien" + id, {
 			x : offset + x * ALIENS_WIDTH * 1.5,
 			x : offset + x * ALIENS_WIDTH * 1.5,
 			y : START_Y + (y * 1.25 * ALIENS_HEIGHT)
 			y : START_Y + (y * 1.25 * ALIENS_HEIGHT)
@@ -187,7 +187,7 @@ Game = {
 				return false;
 				return false;
 			}
 			}
 			if( alien.health > 0 && Math.random() < alien.aggression ) {
 			if( alien.health > 0 && Math.random() < alien.aggression ) {
-				//alien.fire($("#aliensShots"), "alienShot");
+				alien.fire($("#aliensShots"), "alienShot");
 			}
 			}
 		});
 		});
 	},
 	},

+ 121 - 24
js/spaceinvaders-models.js

@@ -8,6 +8,19 @@
  * 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.
 */
 */
 
 
+function getAliensMidHeight() {
+	var higherAlien = Math.max.apply( null, 
+		$(".alien").map(function() {
+			return $(this).y();
+		}).get() ),
+		lowerAlien = Math.min.apply( null, 
+		$(".alien").map(function() {
+			return $(this).y();
+		}).get() );
+		
+	return (higherAlien + lowerAlien) / 2;
+}
+
 WORLD.farm.bonus = [
 WORLD.farm.bonus = [
 	{
 	{
 		type: "weapon",
 		type: "weapon",
@@ -34,7 +47,6 @@ var MOVE = {
 				this.directionX *= -1;
 				this.directionX *= -1;
 				this.y = (this.y + this.height / 4);
 				this.y = (this.y + this.height / 4);
 			}
 			}
-		
 		},
 		},
 	},
 	},
 	mirror : {
 	mirror : {
@@ -50,20 +62,89 @@ var MOVE = {
 				this.directionX *= -1;
 				this.directionX *= -1;
 				this.y = (this.y + this.height / 4);
 				this.y = (this.y + this.height / 4);
 			}
 			}
-		
 		},
 		},
 	},
 	},
+	half_part_rotation : {
+		init : function (x, y) {
+			return {directionX:0, directionY:0};
+		},
+		move : function () {
+			var 	_this = $(this)[0],
+				mid = PLAYGROUND_WIDTH / 2,
+				center = _this.center, 
+				width = _this.width;
+
+			if( this.directionX == 0 && this.directionY == 0 ) {
+				center = {x: ( this.getOriginX() < mid ? PLAYGROUND_WIDTH / 4 : 3 * PLAYGROUND_WIDTH / 4), y: getAliensMidHeight() };
+				width = distance(center, {x: this.x, y: this.y});
+				var	xAxis = {x: width, y: 0}, 
+					current = {x: center.x - this.getOriginX(), y: center.y - this.getOriginY()},
+					alpha = angle( xAxis, current );
+				this.directionX = 0.01;
+				this.directionY = alpha;
+				$(this)[0].center = center;
+				$(this)[0].width = width;
+			}
+				
+			if( this.getOriginX() < mid ) {
+				this.directionY = this.directionY + this.directionX;
+			} else {
+				this.directionY = this.directionY - this.directionX;
+			}
+			if( Math.abs(this.directionY) > 2 * Math.PI ) {
+				this.directionY = 0;
+			}
+			center.y = center.y + 0.1;
+			_this.center = center;
+			this.x = center.x + width * Math.cos(this.directionY);
+			this.y = center.y + width * Math.sin(this.directionY);
+		}
+	},
+
+	rotation : {
+		init : function (x, y) {
+			return {directionX:0, directionY:0};
+		},
+		move : function () {
+			var 	_this = $(this)[0],
+				mid = PLAYGROUND_WIDTH / 2,
+				center = _this.center, 
+				width = _this.width;
+
+			if( this.directionX == 0 && this.directionY == 0 ) {
+				center = {x: mid, y: getAliensMidHeight() };
+				width = distance(center, {x: this.x, y: this.y});
+				var	xAxis = {x: width, y: 0}, 
+					current = {x: center.x - this.getOriginX(), y: center.y - this.getOriginY()},
+					alpha = angle( xAxis, current );
+				this.directionX = 0.01;
+				this.directionY = alpha;
+				$(this)[0].center = center;
+				$(this)[0].width = width;
+			}
+				
+			this.directionY = this.directionY - this.directionX;
+			if( Math.abs(this.directionY) > 2 * Math.PI ) {
+				this.directionY = 0;
+			}
+			center.y = center.y + 0.1;
+			_this.center = center;
+			this.x = center.x + width * Math.cos(this.directionY);
+			this.y = center.y + width * Math.sin(this.directionY);
+		}
+	},
+	
 	sinusoid : {
 	sinusoid : {
 		init : function (x, y) {
 		init : function (x, y) {
-			return {directionX : 1, directionY : 0};
+			return {directionX : 1, directionY : 1};
 		},
 		},
 		move : function () {
 		move : function () {
-			var offsetX = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
-			if (Math.abs((this.getOriginX() - this.getX())) >= offsetX) {
+			var offset = this.width / 2;
+			if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
 				this.directionX *= -1;
 				this.directionX *= -1;
 			}
 			}
-			var offsetY = 5 * ALIENS_HEIGHT;
-			if (Math.abs((this.getOriginY() - this.getY())) >= offsetY) {
+			
+			if( Math.abs(this.getOriginY() - this.getY()) >= 3 * this.height ) {
 				this.directionY *= -1;
 				this.directionY *= -1;
 			}
 			}
 		}
 		}
@@ -80,8 +161,9 @@ var WAVES = [
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ],
-				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], ],
-			move : MOVE.sinusoid,
+				 [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ]
+			],
+			move : MOVE.translation,
 			bonus : [40, 20]
 			bonus : [40, 20]
 		},
 		},
 		{
 		{
@@ -89,10 +171,36 @@ var WAVES = [
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ],
 				 [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ],
-				 [ Alien, Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ] ],
+				 [ Alien, Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ] 
+			],
 			move : MOVE.mirror,
 			move : MOVE.mirror,
 			bonus : [30, 15]
 			bonus : [30, 15]
-		} 
+		}, 
+		{
+			wave : [ [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ],
+				 [ undefined, Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien, undefined ],
+				 [ Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien ],
+				 [ undefined, Alien, Alien, Alien, Alien, undefined, undefined, Alien, Alien, Alien, undefined ],
+				 [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ]
+			],
+			move : MOVE.half_part_rotation,
+			bonus : [20, 10]
+		},
+		{
+			wave : [ 
+				[ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ],
+				[ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ],
+				[ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
+				[ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
+				[ Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien ],
+				[ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
+				[ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ],
+				[ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ],
+				[ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ]
+			],
+			move : MOVE.rotation,
+			bonus : [25, 12]
+		}
 	];
 	];
 
 
 
 
@@ -188,15 +296,7 @@ function CornWeapon() {
 	this.load = 1;
 	this.load = 1;
 	this.max_load = 1;
 	this.max_load = 1;
 	this.callback = function(shot) {
 	this.callback = function(shot) {
-		var higherAlien = Math.max.apply( null, 
-			$(".alien").map(function() {
-				return $(this).y();
-			}).get() ),
-			lowerAlien = Math.min.apply( null, 
-			$(".alien").map(function() {
-				return $(this).y();
-			}).get() ),
-			mediumAlien = (higherAlien + lowerAlien) / 2;
+		var mediumAlien = getAliensMidHeight();
 		
 		
 		if( shot.y() < mediumAlien ) {
 		if( shot.y() < mediumAlien ) {
 			shot.remove();
 			shot.remove();
@@ -252,7 +352,7 @@ Actor.prototype = {
 	y : null,
 	y : null,
 	originX : 0,
 	originX : 0,
 	originY : 0,
 	originY : 0,
-	speed : null,
+	speed : 0,
 	health : 1,
 	health : 1,
 	directionX : 0,
 	directionX : 0,
 	directionY : 0,
 	directionY : 0,
@@ -356,9 +456,6 @@ function Alien(id, start, move) {
 }
 }
 
 
 Alien.prototype = {
 Alien.prototype = {
-	speed : 0,
-	directionX : 0,
-	directionY : 0,
 	moveFct : null,
 	moveFct : null,
 	width : ALIENS_WIDTH,
 	width : ALIENS_WIDTH,
 	height : ALIENS_HEIGHT,
 	height : ALIENS_HEIGHT,

+ 45 - 0
js/tools.js

@@ -22,3 +22,48 @@ function heriter(destination, source) {
         } 
         } 
     } 
     } 
 }
 }
+
+function distance(point1, point2) {
+	return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
+}
+
+function normalize(vector) {
+	var norm = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
+	if (norm == 0)
+		return {
+			x : 0,
+			y : 0
+		};
+
+	var x = vector.x / norm;
+	var y = vector.y / norm;
+	return {
+		x : x,
+		y : y
+	}
+}
+
+function angle(vector1, vector2) {
+	if ((vector1.x == 0 && vector1.y == 0)
+			&& (vector1.x == 0 && vector1.y == 0)) {
+		return 0;
+	}
+
+	if (vector1.x == 0 && vector1.y == 0) {
+		return Math.acos(vector2.x
+				/ Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y));
+	}
+	
+	if (vector2.x == 0 && vector2.y == 0) {
+		return Math.acos(vector1.x
+				/ Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y));
+	}
+	
+	var 	product = vector1.x * vector2.x + vector1.y * vector2.y,
+		alpha = Math.acos(product
+			/ (Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y) * Math
+					.sqrt(vector2.x * vector2.x + vector2.y * vector2.y))),
+		sign = normalize(vector1).y > normalize(vector2).y ? -1 : 1;
+		
+	return sign * alpha;
+}