Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Spaceship
  2. function Spaceship(game, x, y) {
  3.     Phaser.Sprite.call(this, game, x, y, 'spaceship');
  4.     this.anchor.set(0.5, 0.5);
  5.     this.game.physics.enable(this);
  6.     this.body.collideWorldBounds = true;
  7.  
  8.     this.animations.add('stop', [0]);
  9.     this.animations.add('fly', [1, 2], 8, true); // 8fps looped
  10.     this.animations.play('stop');
  11. }
  12.  
  13. Spaceship.prototype = Object.create(Phaser.Sprite.prototype);
  14. Spaceship.prototype.constructor = Spaceship;
  15.  
  16. // Spaceship move
  17. Spaceship.prototype.move = function (directionX, directionY) {
  18.     if (this.isFrozen) { return; }
  19.  
  20.     const SPEED = 200;
  21.     this.body.velocity.x = directionX * SPEED;
  22.     this.body.velocity.y = -directionY * SPEED;
  23.  
  24.     if (this.body.velocity.x < 0) {
  25.         this.scale.x = -1;
  26.         this.angle = 0;
  27.     }
  28.     else if (this.body.velocity.x > 0) {
  29.         this.scale.x = 1;
  30.         this.angle = 0;
  31.     }
  32.     if (this.body.velocity.y < 0) {
  33.         this.scale.x = -1;
  34.         this.angle = 90;
  35.     }
  36.     else if (this.body.velocity.y > 0) {
  37.         this.scale.x = 1;
  38.         this.angle = 90;
  39.     }
  40. };
  41.  
  42. Spaceship.prototype.freeze = function () {
  43.     this.body.enable = false;
  44.     this.isFrozen = true;
  45. };
  46. Spaceship.prototype.unfreeze = function () {
  47.     this.body.enable = true;
  48.     this.isFrozen = false;
  49. };
  50.  
  51. // Spaceship update
  52. Spaceship.prototype.update = function () {
  53.     // update sprite animation, if it needs changing
  54.     let animationName = this._getAnimationName();
  55.     if (this.animations.name !== animationName) {
  56.         this.animations.play(animationName);
  57.     }
  58. };
  59.  
  60. Spaceship.prototype._getAnimationName = function () {
  61.     let name = 'stop'; // default animation
  62.  
  63.     // frozen & not dying
  64.     if (this.isFrozen) {
  65.         name = 'stop';
  66.     }
  67.     else if (this.body.velocity.x !== 0 || this.body.velocity.y !== 0) {
  68.         name = 'fly';
  69.     }
  70.  
  71.     return name;
  72. };
  73.  
  74. // =========================================================================================
  75.  
  76. MapState = {};
  77.  
  78. MapState.preload = function () {
  79.     this.game.load.json('level:worldmap', 'data/worldmap.json');
  80.     this.game.load.image('worldmap', 'images/worldmap.png');
  81.  
  82.     // Player
  83.     this.game.load.spritesheet('spaceship', 'images/spaceship.png', 42, 42);
  84.  
  85.     // Stations
  86.     this.game.load.image('1-1', 'images/1-1.png');
  87.     this.game.load.image('1-2', 'images/1-2.png');
  88.     this.game.load.image('1-3', 'images/1-3.png');
  89. };
  90.  
  91. MapState.create = function () {
  92.     this.game.add.image(0, 0, 'worldmap');
  93.     this._loadMap(this.game.cache.getJSON('level:worldmap'));
  94. };
  95.  
  96. MapState.init = function () {
  97.     this.game.renderer.renderSession.roundPixels = true;
  98.  
  99.     this.wasd = this.game.input.keyboard.addKeys({
  100.         left: Phaser.KeyCode.A,
  101.         right: Phaser.KeyCode.D,
  102.         up: Phaser.KeyCode.W,
  103.         down: Phaser.KeyCode.S,
  104.         interact: Phaser.KeyCode.E,
  105.         tut: Phaser.KeyCode.ESC
  106.     });
  107.  
  108.     this.isTutorialOpen = false;
  109.     this.wasd.tut.onDown.add(function () {
  110.         if (this.isTutorialOpen == false) {
  111.             this.tutorial = game.add.image(0,0, 'tutorialbg');
  112.             this.spaceship.freeze();
  113.             this.isTutorialOpen = true;
  114.  
  115.  
  116.         }
  117.         else {
  118.             this.tutorial.destroy();
  119.             this.spaceship.unfreeze();
  120.             this.isTutorialOpen = false;
  121.         }
  122.  
  123.     }, this);
  124. };
  125.  
  126. MapState.update = function () {
  127.     this._handleMapCollisions();
  128.     this._handleInput();
  129.  
  130. };
  131.  
  132. MapState._handleInput = function () {
  133.     if (this.wasd.left.isDown) { // move left
  134.         this.spaceship.move(-1, 0);
  135.     }
  136.     else if (this.wasd.right.isDown) { // move right
  137.         this.spaceship.move(1, 0);
  138.     }
  139.     else if (this.wasd.up.isDown) {
  140.         this.spaceship.move(0, 1);
  141.     }
  142.     else if (this.wasd.down.isDown) {
  143.         this.spaceship.move(0, -1);
  144.     }
  145.     else { // stop
  146.         this.spaceship.move(0, 0);
  147.     }
  148. };
  149.  
  150. MapState._loadMap = function (data) {
  151.     this.stations = this.game.add.group();
  152.  
  153.     this._spawnSpaceship();
  154.     data.stations.forEach(this._spawnStation, this);
  155. };
  156.  
  157. MapState._spawnSpaceship = function () {
  158.     this.spaceship = new Spaceship(this.game, 10, 500);
  159.     this.game.add.existing(this.spaceship);
  160.  
  161. };
  162.  
  163. MapState._spawnStation = function (station) {
  164.     this.station = this.stations.create(
  165.         station.x, station.y, station.image);
  166.  
  167.     this.game.physics.enable(station);
  168.     this.station.body.allowGravity = false;
  169.     this.station.body.immovable = true;
  170.     this.station.anchor.set(0.5, 0.5);
  171.  
  172.     var ran = this.rnd.realInRange(800, 1200);
  173.     this.add.tween(station)
  174.         .to({y: '+8'}, ran, Phaser.Easing.Sinusoidal.InOut)
  175.         .yoyo(true)
  176.         .start();
  177.  
  178.  
  179. };
  180.  
  181.  
  182.  
  183. MapState._handleMapCollisions = function () {
  184.     if (game.physics.arcade.distanceBetween(this.station, this.spaceship) <= 50) {
  185.         this._showWork(stationArr[station]);
  186.     }
  187. };
  188.  
  189. MapState._onSpaceshipVsStation = function (spaceship, station) {
  190.  
  191.     if (this.isOverlapping == false) {
  192.         this.game.add.tween(station.scale)
  193.             .to({x: 0.8, y: 0.8}, 50, Phaser.Easing.Sinusoidal.InOut)
  194.             .yoyo(true)
  195.             .start();
  196.  
  197.         this.isOverlapping = true;
  198.     }
  199.  
  200. };
  201.  
  202. window.onload = function () {
  203.     game.state.add('map', MapState);
  204.     game.state.start('map');
  205. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement