Path: blob/main/public/games/files/space-invaders/assets/javascript/module/Player.js
1520 views
/**1* Created by stryker on 2014.03.05..2* Player module3*/4define(['module/HUD'],function(HUD){56//Private Variables7var _game = null,8_health = null,9_lives = null,10_score = null,11_firingTime = null,12_ship = null,13_cursors = null,14_bulletGroup = null,15_bullet = null,16_explosionGroup = null,17_explosion = null,18_alienGroup = null,19_aliens = null,20_shootingEvent = null,21_bulletSpeed = null;2223var _fireBullet = function(){24_bullet = _bulletGroup.getFirstExists(false);2526if(_bullet){27//_bullet.lifespan = _game.height / (_bulletSpeed/1000);28_bullet.checkWorldBounds = true;29_bullet.reset(_ship.x,_ship.y+8);30_bullet.body.velocity.y = -_bulletSpeed;31}32};3334var _collisionHandler = function(ship,bullet){3536ship.damage(bullet.bulletDamage);3738bullet.kill();39HUD.updateHealthText(ship.health);4041//ship lose a life42if(ship.health == 0){43this.stopShooting();44_explosion = _explosionGroup.getFirstExists(false);45_explosion.reset(_ship.body.x,_ship.body.y);46_explosion.play('kaboom',30,false,true);4748_lives--;49HUD.updateLivesText(_lives);5051//lose life52if(_lives > 0){53ship.revive(_health);54this.startShooting();55//dead56}else{57_game.state.start('End');58}59}6061};6263return{64init: function(game){65_game = game;66},67preload: function(){68_game.load.image('ship', 'assets/img/player.png');69},70create: function(configuration){71_ship = _game.add.sprite(400,500,'ship');72_ship.anchor.setTo(0.5,0.5);73_game.physics.enable(_ship,Phaser.Physics.ARCADE);74_ship.body.collideWorldBounds = true;75_ship.health = configuration.health;76_health = configuration.health;77_lives = configuration.lives;78_score = configuration.score;79_firingTime = configuration.firingTime;80_bulletSpeed = configuration.bulletSpeed;8182_cursors = _game.input.keyboard.createCursorKeys();83},84update: function(){85_ship.body.velocity.setTo(0,0);8687if(_cursors.left.isDown){88_ship.body.velocity.x = -200;89}else if(_cursors.right.isDown){90_ship.body.velocity.x = 200;91}92},93setBulletGroup: function(bullets){94_bulletGroup = bullets.getBulletGroup();95},96getBulletGroup: function(){97return _bulletGroup;98},99setExplosionGroup: function(explosions){100_explosionGroup = explosions.getExplosionGroup();101},102startShooting: function(){103_shootingEvent = _game.time.events.loop(_firingTime,_fireBullet,this);104},105stopShooting: function(){106_game.time.events.remove(_shootingEvent);107},108getPlayerShip: function(){109return _ship;110},111createOverLap: function(bulletGroup){112_game.physics.arcade.overlap(_ship,bulletGroup,_collisionHandler,null,this);113},114setAliensAndAlienGroup: function(aliens){115_aliens = aliens;116_alienGroup=aliens.getAlienGroup();117}118}119});120121