Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/space-invaders/assets/javascript/module/Aliens.js
1520 views
1
/**
2
* Created by stryker on 2014.03.05..
3
*/
4
define(['module/HUD'],function(HUD){
5
6
//Private variables
7
var _game = null;
8
//var _alienGroups = [];
9
var _playerShip = null;
10
11
//Private class
12
//This is a wrapper for the aliengroup
13
//Instead of extending Phaser group, i wrap it up in a class
14
var _Aliens = function(configuration){
15
var _alienGroup = _game.add.group(),
16
_cols = configuration.cols,
17
_rows = configuration.rows,
18
_scoreValue = configuration.scoreValue,
19
_firingTime = configuration.firingTime,
20
_bulletSpeed = configuration.bulletSpeed,
21
_health = configuration.health,
22
_easing = configuration.easing,
23
_alien = null,
24
_tween = null,
25
_bulletGroup = null,
26
_bullet = null,
27
_explosionGroup = null,
28
_explosion = null,
29
_livingAlien = [],
30
_randomAlienIndex = null,
31
_shooter = null,
32
_shootingEvent = null;
33
34
_alienGroup.enableBody = true;
35
_alienGroup.physicsBodyType = Phaser.Physics.ARCADE;
36
_createAllienGroup();
37
38
function _createAllienGroup(){
39
//making aliens
40
for(var i=0;i < _cols;i++){
41
for(var j=0; j < _rows;j++){
42
_alien = _alienGroup.create(i * 48, j * 50, 'invader');
43
44
//custome properties
45
_alien.health = _health;
46
_alien.myScore = _scoreValue;
47
48
_alien.anchor.setTo(0.5, 0.5);
49
_alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
50
_alien.play('fly');
51
_alien.body.moves = false;
52
}
53
}
54
55
56
//setting aliens postition
57
_alienGroup.x = 100;
58
_alienGroup.y = 50;
59
60
// All this does is basically start the invaders moving.
61
_tween = _game.add.tween(_alienGroup).to( { x: 200 }, 2000, _easing, true, 0, 1000, true);
62
63
}
64
65
66
67
var _fireBullet = function(){
68
_bullet = _bulletGroup.getFirstExists(false);
69
70
_livingAlien = [];
71
72
_alienGroup.forEachAlive(function(alien){
73
_livingAlien.push(alien);
74
});
75
76
if(_bullet && _livingAlien.length > 0){
77
78
//_bullet.lifespan = _game.height / (_bulletSpeed/1000);
79
_bullet.checkWorldBounds = true;
80
81
_randomAlienIndex = _game.rnd.integerInRange(0,_livingAlien.length);
82
83
_shooter = _livingAlien[_randomAlienIndex];
84
85
if(_shooter){
86
_bullet.reset(_shooter.body.x,_shooter.body.y);
87
88
_game.physics.arcade.moveToObject(_bullet,_playerShip,_bulletSpeed);
89
}
90
//all alien died
91
}else if(_livingAlien.length == 0){
92
_game.state.start('End');
93
}
94
95
};
96
97
var _collisionHandler = function(bullet, alien){
98
99
alien.damage(bullet.bulletDamage);
100
bullet.kill();
101
102
if(alien.health == 0){
103
_explosion = _explosionGroup.getFirstExists(false);
104
_explosion.reset(alien.body.x,alien.body.y);
105
_explosion.play('kaboom',30,false,true);
106
}
107
108
HUD.updateScoreText(alien.myScore);
109
};
110
111
//Public functions
112
return{
113
setBulletGroup: function(bullets){
114
_bulletGroup = bullets.getBulletGroup();
115
},
116
getBulletGroup: function(){
117
return _bulletGroup;
118
},
119
setExplosionGroup: function(explosions){
120
_explosionGroup = explosions.getExplosionGroup();
121
},
122
startShooting: function(){
123
_shootingEvent = _game.time.events.loop(_firingTime,_fireBullet,this);
124
},
125
stopShooting: function(){
126
_game.time.events.remove(_shootingEvent);
127
},
128
createOverLap: function(bulletGroup){
129
_game.physics.arcade.overlap(bulletGroup, _alienGroup, _collisionHandler, null, this);
130
},
131
getAlienGroup: function(){
132
return _alienGroup;
133
}
134
135
}
136
137
};//End of _Aliens
138
139
//Public functions
140
return{
141
init: function(game){
142
_game = game;
143
},
144
preload: function(){
145
_game.load.spritesheet('invader', 'assets/img/invader32x32x4.png', 32, 32);
146
},
147
create: function(configuration){
148
return( new _Aliens(configuration) );
149
},
150
setPlayerShip: function(playerShip){
151
_playerShip = playerShip;
152
}
153
}
154
});
155