Refactoring: better way to manage weapons

This commit is contained in:
Fabrice Ecaille
2013-11-28 18:23:24 +01:00
parent 9500c64e20
commit 045ae06241
5 changed files with 107 additions and 507 deletions

View File

@@ -30,7 +30,7 @@
<script src="js/spaceinvaders-ui.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-core.js" type="text/javascript" ></script>
<script src="js/spaceinvaders-utils.js" type="text/javascript" ></script> <script src="js/models/weapons.js" type="text/javascript" ></script>
<script src="js/models/models.js" type="text/javascript" ></script> <script src="js/models/models.js" type="text/javascript" ></script>
<script src="js/models/aliens.js" type="text/javascript" ></script> <script src="js/models/aliens.js" type="text/javascript" ></script>
<script src="js/models/waves.js" type="text/javascript" ></script> <script src="js/models/waves.js" type="text/javascript" ></script>

View File

@@ -10,25 +10,25 @@
var ALIENS = { var ALIENS = {
alien1 : { alien1 : {
health : 1, health : 1,
weapon : AlienWeapon, weapon : WEAPONS.ALIEN,
score : 5, score : 5,
aggression : 0.0005, aggression : 0.0005,
animation : ALIENS_TYPE[0] animation : ALIENS_TYPE[0]
}, },
alien2 : { alien2 : {
health : 1, health : 1,
weapon : AlienWeapon, weapon : WEAPONS.ALIEN,
score : 10, score : 10,
aggression : 0.001, aggression : 0.001,
animation : ALIENS_TYPE[1] animation : ALIENS_TYPE[1]
}, },
alien3 : { alien3 : {
health : 1, health : 1,
weapon : AlienWeapon, weapon : WEAPONS.ALIEN,
score : 20, score : 20,
aggression : 0.0015, aggression : 0.0015,
animation : ALIENS_TYPE[2] animation : ALIENS_TYPE[2]
} }
} }
@@ -42,7 +42,7 @@ function Alien(id, start, move, type) {
this.y = start.y; this.y = start.y;
this.moveFct = move; this.moveFct = move;
this.weapon = new type.weapon(); this.weapon = new Weapon(type.weapon);
this.fireDirectionY = 1; this.fireDirectionY = 1;
this.originX = this.x; this.originX = this.x;

View File

@@ -24,355 +24,16 @@ function getAliensMidHeight() {
WORLD.farm.bonus = [ WORLD.farm.bonus = [
{ {
type: "weapon", type: "weapon",
clazz: CarotWeapon, clazz: WEAPONS.CAROT,
animation: WORLD.farm.weapons.carot animation: WORLD.farm.weapons.carot
}, },
{ {
type: "weapon", type: "weapon",
clazz: CornWeapon, clazz: WEAPONS.CORN,
animation: WORLD.farm.weapons.corn animation: WORLD.farm.weapons.corn
} }
]; ];
/*** 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);
}
},
},
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 : {
init : function (x, y) {
return {directionX : 1, directionY : 1};
},
move : function () {
var offset = this.width / 2;
if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
this.directionX *= -1;
}
if( Math.abs(this.getOriginY() - this.getY()) >= 3 * this.height ) {
this.directionY *= -1;
}
}
}
};
/*** Move - end ***/
var ALIENS = {
alien1 : {
health : 1,
weapon : AlienWeapon,
score : 5,
aggression : 0.0005,
animation : ALIENS_TYPE[0]
},
alien2 : {
health : 1,
weapon : AlienWeapon,
score : 10,
aggression : 0.001,
animation : ALIENS_TYPE[1]
},
alien3 : {
health : 1,
weapon : AlienWeapon,
score : 20,
aggression : 0.0015,
animation : ALIENS_TYPE[2]
}
}
/*** Waves ***/
var WAVES = [
{
wave : [
[ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
[ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
[ ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3 ],
[ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ],
[ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ]
],
move : MOVE.translation,
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]
},
{
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]
}
];
/*** Waves - end ***/
/*** Weapons ***/
function Weapon() {
"use strict";
}
Weapon.prototype = {
speed : 5,
strength : 10,
stock: Infinity,
rof : 300,
ror : 1500,
load : 1,
max_load : 1,
width : 5,
height : 5,
shot_timer : false,
reload_timer : false,
directionX : 0,
directionY : 1,
animation : null,
clazz : "default",
callback : undefined,
fire : function() {
if (this.shot_timer || this.load <= 0) {
return false;
}
var _this = this;
this.load = Math.max(0,this.load - 1);
this.shot_timer = setInterval(function() {
if (_this.load > 0) {
clearTimeout(_this.shot_timer);
_this.shot_timer = false;
}
}, this.rof);
if( !this.reload_timer) {
this.reload_timer = setInterval( function() {
_this.load = Math.min(_this.load + 1, Math.min(_this.max_load, _this.stock));
if( _this.load == _this.max_load ) {
clearInterval(_this.reload_timer);
_this.reload_timer = false;
}
}, this.ror);
}
return true;
}
}
function ShotgunWeapon() {
"use strict";
this.directionY = -1;
this.rof = 200;
this.ror = 1500;
this.load = 2;
this.max_load = 2;
this.width = 3;
this.height = 3;
this.clazz = "Shotgun"
}
ShotgunWeapon.prototype = {
}
heriter(ShotgunWeapon.prototype, Weapon.prototype);
function CarotWeapon() {
"use strict";
this.directionY = -1;
this.stock = 10;
this.clazz = "carot";
this.load = 5;
this.max_load = 5;
this.width = 5;
this.height = 10;
}
CarotWeapon.prototype = {
}
heriter(CarotWeapon.prototype, Weapon.prototype);
function CornWeapon() {
"use strict";
this.directionY = -1;
this.stock = 3;
this.clazz = "corn";
this.load = 1;
this.max_load = 1;
this.callback = function(shot) {
var mediumAlien = getAliensMidHeight();
if( shot.y() < mediumAlien ) {
var x = shot.x(),
y = shot.y(),
explosion = EXPLOSIONS.SMALL[0];
$("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2});
explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()});
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 : x + 5 * cos, posy : 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);
}
}
}
}
CornWeapon.prototype = {
}
heriter(CornWeapon.prototype, Weapon.prototype);
function AlienWeapon() {
"use strict";
this.directionY = 1;
this.width = 5;
this.height = 10;
}
AlienWeapon.prototype = {
}
heriter(AlienWeapon.prototype, Weapon.prototype);
/*** Weapons -END ***/
/*** Actors ***/ /*** Actors ***/
function Actor() { function Actor() {
"use strict"; "use strict";
@@ -469,62 +130,6 @@ Actor.prototype = {
} }
}; };
/*** Actors - Aliens ***/
function Alien(id, start, move, type) {
"use strict";
this.id = id;
this.x = start.x;
this.y = start.y;
this.moveFct = move;
this.weapon = new type.weapon();
this.fireDirectionY = 1;
this.originX = this.x;
this.originY = this.y;
this.directionX = -1;
this.speed = 0.5;
this.animation = type.animation;
this.width = type.animation.width;
this.height = type.animation.height;
this.health = type.health;
this.aggression = type.aggression;
this.score = type.score;
}
Alien.prototype = {
moveFct : null,
width : 0,
height : 0,
aggression : 0,
animation : null,
score : 0,
init : function() {
"use strict";
this.speed = 0;
this.node.x(this.x);
this.node.y(this.y);
},
move : function() {
"use strict";
this._super("move", arguments);
if (typeof this.moveFct !== undefined) {
this.moveFct();
}
},
destroy : function() {
this._super("destroy", arguments);
Game.addToScore( this.score );
}
};
heriter(Alien.prototype, Actor.prototype);
/*** Actors - Aliens - END ***/
/*** Actors - Heroes - END ***/ /*** Actors - Heroes - END ***/
function Ship(id, start, speed, animation) { function Ship(id, start, speed, animation) {
@@ -533,7 +138,7 @@ function Ship(id, start, speed, animation) {
this.x = start.x; this.x = start.x;
this.y = start.y; this.y = start.y;
this.weapon = new ShotgunWeapon(); this.weapon = new Weapon(WEAPONS.SHOTGUN);
this.fireDirectionY = -1; this.fireDirectionY = -1;
this.originX = this.x; this.originX = this.x;
@@ -615,7 +220,7 @@ Ship.prototype = {
Game.shots.total = Game.shots.total + 1; 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 Weapon(WEAPONS.SHOTGUN);
$("#current_weapon").setAnimation(this.weapon.animation); $("#current_weapon").setAnimation(this.weapon.animation);
} }
} }

View File

@@ -8,9 +8,91 @@
* 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 Weapon() { var WEAPONS = {
"use strict"; SHOTGUN : {
directionY : -1,
rof : 200,
ror : 1500,
load : 2,
max_load : 2,
width : 3,
height : 3,
clazz : "Shotgun"
},
CAROT : {
directionY : -1,
stock : 10,
clazz : "carot",
load : 5,
max_load : 5,
width : 5,
height : 10
},
CORN : {
directionY : -1,
stock : 3,
clazz : "corn",
load : 1,
max_load : 1,
callback : function(shot) {
var mediumAlien = getAliensMidHeight();
if( shot.y() < mediumAlien ) {
var x = shot.x(),
y = shot.y(),
explosion = EXPLOSIONS.SMALL[0];
$("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2});
explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()});
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 : x + 5 * cos, posy : 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);
}
}
}
},
ALIEN : {
directionY : 1,
width : 5,
height : 10
}
} }
function Weapon(type) {
"use strict";
this.directionY = type.directionY || 1;
this.directionX = type.directionX || 0;
this.width = type.width;
this.height = type.height;
this.speed = type.speed || 5;
this.strenght = type.strength || 10;
this.stock = type.stock || Infinity;
this.rof = type.rof || 300;
this.ror = type.ror || 1500;
this.load = type.load || 1;
this.max_load = type.max_load || 1;
this.animation = type.animation;
this.clazz = type.clazz || "default";
this.callback = type.callback;
}
Weapon.prototype = { Weapon.prototype = {
speed : 5, speed : 5,
strength : 10, strength : 10,
@@ -55,91 +137,4 @@ Weapon.prototype = {
return true; return true;
} }
} }
function ShotgunWeapon() {
"use strict";
this.directionY = -1;
this.rof = 200;
this.ror = 1500;
this.load = 2;
this.max_load = 2;
this.width = 3;
this.height = 3;
this.clazz = "Shotgun"
}
ShotgunWeapon.prototype = {
}
heriter(ShotgunWeapon.prototype, Weapon.prototype);
function CarotWeapon() {
"use strict";
this.directionY = -1;
this.stock = 10;
this.clazz = "carot";
this.load = 5;
this.max_load = 5;
this.width = 5;
this.height = 10;
}
CarotWeapon.prototype = {
}
heriter(CarotWeapon.prototype, Weapon.prototype);
function CornWeapon() {
"use strict";
this.directionY = -1;
this.stock = 3;
this.clazz = "corn";
this.load = 1;
this.max_load = 1;
this.callback = function(shot) {
var mediumAlien = getAliensMidHeight();
if( shot.y() < mediumAlien ) {
var x = shot.x(),
y = shot.y(),
explosion = EXPLOSIONS.SMALL[0];
$("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2});
explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()});
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 : x + 5 * cos, posy : 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);
}
}
}
}
CornWeapon.prototype = {
}
heriter(CornWeapon.prototype, Weapon.prototype);
function AlienWeapon() {
"use strict";
this.directionY = 1;
this.width = 5;
this.height = 10;
}
AlienWeapon.prototype = {
}
heriter(AlienWeapon.prototype, Weapon.prototype);

View File

@@ -256,7 +256,7 @@ Game = {
var bonus = $(this)[0].bonus; var bonus = $(this)[0].bonus;
if( collisions.length > 0 ) { if( collisions.length > 0 ) {
if( bonus.type === "weapon" ) { if( bonus.type === "weapon" ) {
Game.ship.weapon = new bonus.clazz; Game.ship.weapon = new Weapon(bonus.clazz);
$("#current_weapon").setAnimation(bonus.animation.animation); $("#current_weapon").setAnimation(bonus.animation.animation);
this.remove(); this.remove();
} }