Browse Source

* Optim: better way to manage waves
* Feature: add mirror move and a beginnig of sinusoid move

Fabrice Ecaille 12 năm trước cách đây
mục cha
commit
d588759e90
3 tập tin đã thay đổi với 91 bổ sung40 xóa
  1. 9 36
      js/spaceinvaders-core.js
  2. 78 0
      js/spaceinvaders-models.js
  3. 4 4
      js/spaceinvaders-ui.js

+ 9 - 36
js/spaceinvaders-core.js

@@ -8,39 +8,6 @@
  * 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.
 */
 
-var WAVES = [
-		{
-			wave : [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
-			move : function() {
-				var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
-				if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
-					this.directionX *= -1;
-					this.y = (this.y + this.height / 4);
-				}
-			},
-			bonus : [40, 20]
-		},
-		{
-			wave : [ [ 0, 0, 0, 0, 0, 0, 0 ], 
-				 [ 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
-				 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
-			move : function() {
-				var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
-				if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
-					this.directionX *= -1;
-					this.y = (this.y + this.height / 4);
-				}
-			},
-			bonus : [30, 15]
-		} 
-	];
-
 Game = {
 	running : false,
 	wave_index : -1,
@@ -66,7 +33,7 @@ Game = {
 			var aliensRow = wave[row], type = aliensRow[0], offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length)
 					* ALIENS_WIDTH) / 2;
 			for (col = 0; col < aliensRow.length; col = col + 1) {
-				Game.setAlien(col, row, offset, type, Game.wave.move);
+				Game.setAlien(col, row, offset, aliensRow[col], Game.wave.move);
 			}
 		}
 
@@ -134,11 +101,17 @@ Game = {
 	
 	setAlien : function(x, y, offset, type, move) {
 		"use strict";
+		if( typeof type == "undefined" ) {
+			return;
+		}
 		var id = x * ROWS + y;
-		var alien = new Alien("alien" + id, {
+		var alien = new type("alien" + id, {
 			x : offset + x * ALIENS_WIDTH * 1.5,
 			y : START_Y + (y * 1.25 * ALIENS_HEIGHT)
-		}, move);
+		}, move.move);
+		var directions = move.init( alien.x, alien.y );
+		alien.directionX = directions.directionX;
+		alien.directionY = directions.directionY;
 		$("#actors").addSprite("alien" + id, $.extend({posx : alien.x, posy : alien.y}, animations.alien));
 		alien.node = $("#alien" + id);
 		alien.node.addClass("alien");

+ 78 - 0
js/spaceinvaders-models.js

@@ -21,6 +21,84 @@ WORLD.farm.bonus = [
 	}
 ];
 
+/*** Move ***/
+
+var MOVE = {
+	translation : {
+		init : function (x, y) {
+			return {directionX : 1, directionY : 0};
+		},
+		move : function() {
+			var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
+			if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
+				this.directionX *= -1;
+				this.y = (this.y + this.height / 4);
+			}
+		
+		},
+	},
+	mirror : {
+		init : function(x, y) {
+			if( x < PLAYGROUND_WIDTH / 2 ) {
+				return {directionX: -1, directionY: 0};
+			}
+			return {directionX: 1, directionY: 0};
+		},
+		move : function() {
+			var offset =  this.width / 2;
+			if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
+				this.directionX *= -1;
+				this.y = (this.y + this.height / 4);
+			}
+		
+		},
+	},
+	sinusoid : {
+		init : function (x, y) {
+			return {directionX : 1, directionY : 0};
+		},
+		move : function () {
+			var offsetX = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
+			if (Math.abs((this.getOriginX() - this.getX())) >= offsetX) {
+				this.directionX *= -1;
+			}
+			var offsetY = 5 * ALIENS_HEIGHT;
+			if (Math.abs((this.getOriginY() - this.getY())) >= offsetY) {
+				this.directionY *= -1;
+			}
+		}
+	}
+};
+
+/*** Move - end ***/
+
+/*** Waves ***/
+
+var WAVES = [
+		{
+			wave : [ [ 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,
+			bonus : [40, 20]
+		},
+		{
+			wave : [ [ 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, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ] ],
+			move : MOVE.mirror,
+			bonus : [30, 15]
+		} 
+	];
+
+
+/*** Waves - end ***/
+
+
 /*** Weapons ***/
 
 function Weapon() {

+ 4 - 4
js/spaceinvaders-ui.js

@@ -46,15 +46,15 @@ function displayModal(data) {
 	
 	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})
+			.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})
 			;
+		$("#totalLbl").append("Wave").lettering();
+		$("#totalNbr").append(data.wave_score).lettering();
 		$("#bonusLbl").append("Bonus").lettering();
 		$("#bonusNbr").append(data.bonus).lettering();
-		$("#totalLbl").append("Wave score").lettering();
-		$("#totalNbr").append(data.wave_score).lettering();
 	}
 	
 	$("#modal_pane").addClass( "modal" );