Просмотр исходного кода

Feature: adding bonus \nRefactoring: isolation of animations

Fabrice Ecaille 12 лет назад
Родитель
Сommit
99ef4fc3f5
7 измененных файлов с 249 добавлено и 130 удалено
  1. 4 0
      css/spaceinvaders.css
  2. 1 0
      index.html
  3. 132 0
      js/spaceinvaders-animations.js
  4. 60 13
      js/spaceinvaders-core.js
  5. 42 1
      js/spaceinvaders-models.js
  6. 9 115
      js/spaceinvaders-ui.js
  7. 1 1
      js/tools.js

+ 4 - 0
css/spaceinvaders.css

@@ -19,6 +19,10 @@
 	background-color: green;
 }
 
+.shipShot.carot {
+	background-color: orange;
+}
+
 .alienShot {
 	background-color: red;
 }

+ 1 - 0
index.html

@@ -23,6 +23,7 @@
 		<link href="css/scoreboard.css" type="text/css" rel="stylesheet"/>
 		<link href="css/countdown.css" type="text/css" rel="stylesheet"/>
 		
+		<script src="js/spaceinvaders-animations.js" type="text/javascript" ></script>
 		<script src="js/spaceinvaders-ui.js" type="text/javascript" ></script>
 		<script src="js/spaceinvaders-core.js" type="text/javascript" ></script>
 		<script src="js/spaceinvaders-models.js" type="text/javascript" ></script>

+ 132 - 0
js/spaceinvaders-animations.js

@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2013 Fabrice ECAILLE aka Febbweiss
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of 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.
+*/
+
+var ALIENS_WIDTH = 24,
+	ALIENS_HEIGHT = 17,
+	IMAGES_PREFIX = "images/";
+
+var WORLD = {
+	farm : {
+		hero : {
+			ship : {
+				animation : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png"
+				}),
+				width : 48,
+				height : 24,
+				posx : 0,
+				posy : 17
+			},
+			/*cockpit : {
+				animation : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsety : 24,
+					offsetx : 20
+				}),
+				width : 20,
+				height : 33,
+				posx : 28,
+				posy : 3
+			},*/
+			smallWheel : {
+				animation  : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsetx : 0,
+					offsety : 24
+				}),
+				posx : 4,
+				posy : 30,
+				width: 14,
+				height: 14
+			},
+			bigWheel : {
+				animation  : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsetx : 0,
+					offsety : 38
+				}),
+				width : 19,
+				height : 19,
+				posx : 25,
+				posy : 27
+			}
+		},
+		alien : {
+			animation : new $.gQ.Animation({
+				imageURL : IMAGES_PREFIX + "invader.png",
+				numberOfFrame : 2,
+				delta : ALIENS_WIDTH,
+				rate : 400,
+				type : $.gQ.ANIMATION_HORIZONTAL
+			}),
+			width : ALIENS_WIDTH,
+			height : ALIENS_HEIGHT			
+		},
+		ufo : {
+			animation: new $.gQ.Animation({imageURL: IMAGES_PREFIX + "ufo.png"}), 
+			width: 48, 
+			height: 32,
+			posy: 10
+		},
+		life : {
+			animation : new $.gameQuery.Animation({
+				imageURL : IMAGES_PREFIX + "farm.png",
+				offsetx : 14,
+				offsety : 24
+			}),
+			width: 32,
+			height: 16
+		},
+		weapons : {
+			corn : {
+				animation : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsetx : 19,
+					offsety : 37
+				}),
+				width: 19,
+				height: 19
+			},
+			carot : {
+				animation : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsetx : 38,
+					offsety : 37
+				}),
+				width: 19,
+				height: 19
+			},
+			gun : {
+				animation : new $.gameQuery.Animation({
+					imageURL : IMAGES_PREFIX + "farm.png",
+					offsetx : 57,
+					offsety : 37
+				}),
+				width: 19,
+				height: 19
+			}
+		},
+		backgrounds : {
+			farm: {
+				background1 : {
+					animation : new $.gQ.Animation({
+						imageURL : IMAGES_PREFIX + "background.png"
+					})
+				},
+				background2 : {
+					animation : new $.gQ.Animation({
+						imageURL : IMAGES_PREFIX + "background2.png"
+					})
+				}
+			}
+		}	
+	}
+};
+

+ 60 - 13
js/spaceinvaders-core.js

@@ -11,31 +11,35 @@
 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 ] ],
+				 [ 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 : [50, 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 ] ],
+			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,
@@ -46,6 +50,7 @@ Game = {
 	
 	init : function() {
 		"use strict";
+		animations = WORLD.farm;
 		Game.wave = Game.wave + 1;
 		var row, col;
 		var wave = WAVES[Game.wave].wave;
@@ -204,7 +209,15 @@ Game = {
 					return elementOfArray.id == alien.id;
 				}, true);
 				Game.aliens = aliensNotInArray;
-				$(this)[0].alien.hit();
+				var alienX = alien.alien.x;
+				var alienY = alien.alien.y;
+				var alienWidth = alien.alien.width;
+				var alienHeight = alien.alien.height;
+				alien.alien.hit();
+				var remainingAliens = $(".alien").length;
+				if( remainingAliens == WAVES[Game.wave].bonus[0] || remainingAliens == WAVES[Game.wave].bonus[1] ) {
+					Game.bonus(alienX, alienY, alienWidth, alienHeight);					
+				}
 			})
 			if( collisions.length > 0 ) {
 				this.remove()
@@ -216,6 +229,40 @@ Game = {
 			}
 		});
 	},
+
+	bonusCollision : function() {
+		if( !Game.running ) {
+			return false;		
+		}
+		
+		$(".bonus").each(function(i,e) {
+			var collisions = $(this).collision("#ship,."+$.gQ.groupCssClass);
+			var bonus = $(this)[0].bonus;
+			if( collisions.length > 0 ) {
+				if( bonus.type === "weapon" ) {
+					Game.ship.weapon = new bonus.clazz;
+					$("#current_weapon").setAnimation(bonus.animation.animation);
+					this.remove();
+				}
+			}
+		});
+	},
+
+
+	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)) ];
+		console.log("Bonus ", bonus, " pos ", alienX, " ", alienY, " ", alienWidth, " ", alienHeight);
+		var id = Math.round(Math.random() * 10000);
+		$("#actors").addSprite("bonus" + id, 
+			$.extend(
+				{
+					posx: alienX + (alienWidth - bonus.animation.width) / 2, 
+					posy: alienY 
+				}, bonus.animation));
+		$("#bonus" + id).addClass("bonus");
+		$("#bonus" + id)[0].bonus = bonus;
+	},
 	
 	alienShotCollision : function() {
 		if( !Game.running ) {
@@ -236,7 +283,7 @@ Game = {
 				Game.hit();
 			})
 			if( collisions.length > 0 ) {
-				this.remove()
+				this.remove();
 			}
 		});
 	}

+ 42 - 1
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.
 */
 
+WORLD.farm.bonus = [
+	{
+		type: "weapon",
+		clazz: CarotWeapon,
+		animation: WORLD.farm.weapons.carot
+	},
+	{
+		type: "weapon",
+		clazz: CarotWeapon,
+		animation: WORLD.farm.weapons.corn
+	}
+];
+
 /*** Weapons ***/
 
 function Weapon() {
@@ -16,6 +29,7 @@ function Weapon() {
 Weapon.prototype = {
 	speed : 5,
 	strength : 10,
+	stock: Infinity,
 	rof : 300,
 	ror : 1500,
 	load : 3,
@@ -27,6 +41,7 @@ Weapon.prototype = {
 	directionX : 0,
 	directionY : 1,
 	animation : null,
+	clazz : "default",
 
 	fire : function() {
 		if (this.shot_timer || this.load <= 0) {
@@ -65,6 +80,20 @@ HeroWeapon.prototype = {
 }
 heriter(HeroWeapon.prototype, Weapon.prototype);
 
+function CarotWeapon() {
+	"use strict";
+	this.directionY = -1;
+	this.stock = 10;
+	this.clazz = "carot";
+	this.load = 1;
+	this.max_load = 1;
+}
+CarotWeapon.prototype = {
+		
+}
+heriter(CarotWeapon.prototype, Weapon.prototype);
+
+
 function AlienWeapon() {
 	"use strict";
 	this.directionY = 1;
@@ -153,9 +182,11 @@ Actor.prototype = {
 		var name = "shot" + Math.ceil(Math.random() * 1000);
 		if (this.weapon.fire(layout)) {
 			layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
-			$("#" + name).addClass(clazz)
+			$("#" + name).addClass(clazz).addClass(this.weapon.clazz);
 			$("#" + name)[0].weapon = this.weapon;
+			return true;
 		}
+		return false;
 	},
 	
 	hit : function() {
@@ -312,6 +343,16 @@ Ship.prototype = {
 			$("#hero").children().show();
 			_this.health = 1;
 		}, 2000);
+	}, 
+
+	fire : function() {
+		if(this._super("fire", arguments)) {
+			this.weapon.stock--;
+			if( this.weapon.stock == 0 ) {
+				this.weapon = new HeroWeapon();
+				$("#current_weapon").setAnimation(bonus.animation);
+			} 
+		}
 	}
 
 };

+ 9 - 115
js/spaceinvaders-ui.js

@@ -30,121 +30,7 @@ var SHIPS = {
   	cruser: {width: 30, height: 25}
 };
 
-var animations = {
-		hero : {
-			ship : {
-				animation : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png"
-				}),
-				width : 48,
-				height : 24,
-				posx : 0,
-				posy : 17
-			},
-			/*cockpit : {
-				animation : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsety : 24,
-					offsetx : 20
-				}),
-				width : 20,
-				height : 33,
-				posx : 28,
-				posy : 3
-			},*/
-			smallWheel : {
-				animation  : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsetx : 0,
-					offsety : 24
-				}),
-				posx : 4,
-				posy : 30,
-				width: 14,
-				height: 14
-			},
-			bigWheel : {
-				animation  : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsetx : 0,
-					offsety : 38
-				}),
-				width : 19,
-				height : 19,
-				posx : 25,
-				posy : 27
-			}
-		},
-		alien : {
-			animation : new $.gQ.Animation({
-				imageURL : IMAGES_PREFIX + "invader.png",
-				numberOfFrame : 2,
-				delta : ALIENS_WIDTH,
-				rate : 400,
-				type : $.gQ.ANIMATION_HORIZONTAL
-			}),
-			width : ALIENS_WIDTH,
-			height : ALIENS_HEIGHT			
-		},
-		ufo : {
-			animation: new $.gQ.Animation({imageURL: IMAGES_PREFIX + "ufo.png"}), 
-			width: 48, 
-			height: 32,
-			posy: 10
-		},
-		life : {
-			animation : new $.gameQuery.Animation({
-				imageURL : IMAGES_PREFIX + "farm.png",
-				offsetx : 14,
-				offsety : 24
-			}),
-			width: 32,
-			height: 16
-		},
-		weapons : {
-			carot : {
-				animation : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsetx : 19,
-					offsety : 37
-				}),
-				width: 19,
-				height: 19
-			},
-			corn : {
-				animation : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsetx : 38,
-					offsety : 37
-				}),
-				width: 19,
-				height: 19
-			},
-			gun : {
-				animation : new $.gameQuery.Animation({
-					imageURL : IMAGES_PREFIX + "farm.png",
-					offsetx : 57,
-					offsety : 37
-				}),
-				width: 19,
-				height: 19
-			}
-		},
-		backgrounds : {
-			farm: {
-				background1 : {
-					animation : new $.gQ.Animation({
-						imageURL : IMAGES_PREFIX + "background.png"
-					})
-				},
-				background2 : {
-					animation : new $.gQ.Animation({
-						imageURL : IMAGES_PREFIX + "background2.png"
-					})
-				}
-			}
-		}
-};
+var animations = WORLD.farm;
 
 function updateWeaponLoad( load ) {
 	"use strict";
@@ -259,6 +145,7 @@ $(function(){
 	
 	// Collisions management
 	$.playground().registerCallback(Game.heroShotCollision, REFRESH_RATE);
+	$.playground().registerCallback(Game.bonusCollision, REFRESH_RATE);
 	$.playground().registerCallback(Game.alienShotCollision, REFRESH_RATE);
 	
 	// Refresh playground
@@ -266,6 +153,13 @@ $(function(){
 		updateWeaponLoad(Game.ship.weapon.load / Game.ship.weapon.max_load);
 		if( Game.running ) {
 			Game.ship.move();
+			$(".bonus").each( function(index, bonus) {
+				var posy = $(this).y();
+				if( posy < Game.ship.y + 10 ) {
+					$(this).y(3, true);
+					return;
+				}			
+			});
 		}
 	}, REFRESH_RATE);
 });

+ 1 - 1
js/tools.js

@@ -4,7 +4,7 @@ function heriter(destination, source) {
             obj["_super"] = function() { 
                 var methodName = arguments[0]; 
                 var parameters = arguments[1]; 
-                this["__parent_methods"][methodName].apply(this, parameters); 
+                return this["__parent_methods"][methodName].apply(this, parameters); 
             } 
         }