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/Player.js
1520 views
1
/**
2
* Created by stryker on 2014.03.05..
3
* Player module
4
*/
5
define(['module/HUD'],function(HUD){
6
7
//Private Variables
8
var _game = null,
9
_health = null,
10
_lives = null,
11
_score = null,
12
_firingTime = null,
13
_ship = null,
14
_cursors = null,
15
_bulletGroup = null,
16
_bullet = null,
17
_explosionGroup = null,
18
_explosion = null,
19
_alienGroup = null,
20
_aliens = null,
21
_shootingEvent = null,
22
_bulletSpeed = null;
23
24
var _fireBullet = function(){
25
_bullet = _bulletGroup.getFirstExists(false);
26
27
if(_bullet){
28
//_bullet.lifespan = _game.height / (_bulletSpeed/1000);
29
_bullet.checkWorldBounds = true;
30
_bullet.reset(_ship.x,_ship.y+8);
31
_bullet.body.velocity.y = -_bulletSpeed;
32
}
33
};
34
35
var _collisionHandler = function(ship,bullet){
36
37
ship.damage(bullet.bulletDamage);
38
39
bullet.kill();
40
HUD.updateHealthText(ship.health);
41
42
//ship lose a life
43
if(ship.health == 0){
44
this.stopShooting();
45
_explosion = _explosionGroup.getFirstExists(false);
46
_explosion.reset(_ship.body.x,_ship.body.y);
47
_explosion.play('kaboom',30,false,true);
48
49
_lives--;
50
HUD.updateLivesText(_lives);
51
52
//lose life
53
if(_lives > 0){
54
ship.revive(_health);
55
this.startShooting();
56
//dead
57
}else{
58
_game.state.start('End');
59
}
60
}
61
62
};
63
64
return{
65
init: function(game){
66
_game = game;
67
},
68
preload: function(){
69
_game.load.image('ship', 'assets/img/player.png');
70
},
71
create: function(configuration){
72
_ship = _game.add.sprite(400,500,'ship');
73
_ship.anchor.setTo(0.5,0.5);
74
_game.physics.enable(_ship,Phaser.Physics.ARCADE);
75
_ship.body.collideWorldBounds = true;
76
_ship.health = configuration.health;
77
_health = configuration.health;
78
_lives = configuration.lives;
79
_score = configuration.score;
80
_firingTime = configuration.firingTime;
81
_bulletSpeed = configuration.bulletSpeed;
82
83
_cursors = _game.input.keyboard.createCursorKeys();
84
},
85
update: function(){
86
_ship.body.velocity.setTo(0,0);
87
88
if(_cursors.left.isDown){
89
_ship.body.velocity.x = -200;
90
}else if(_cursors.right.isDown){
91
_ship.body.velocity.x = 200;
92
}
93
},
94
setBulletGroup: function(bullets){
95
_bulletGroup = bullets.getBulletGroup();
96
},
97
getBulletGroup: function(){
98
return _bulletGroup;
99
},
100
setExplosionGroup: function(explosions){
101
_explosionGroup = explosions.getExplosionGroup();
102
},
103
startShooting: function(){
104
_shootingEvent = _game.time.events.loop(_firingTime,_fireBullet,this);
105
},
106
stopShooting: function(){
107
_game.time.events.remove(_shootingEvent);
108
},
109
getPlayerShip: function(){
110
return _ship;
111
},
112
createOverLap: function(bulletGroup){
113
_game.physics.arcade.overlap(_ship,bulletGroup,_collisionHandler,null,this);
114
},
115
setAliensAndAlienGroup: function(aliens){
116
_aliens = aliens;
117
_alienGroup=aliens.getAlienGroup();
118
}
119
}
120
});
121