Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Spaceship
- function Spaceship(game, x, y) {
- Phaser.Sprite.call(this, game, x, y, 'spaceship');
- this.anchor.set(0.5, 0.5);
- this.game.physics.enable(this);
- this.body.collideWorldBounds = true;
- this.animations.add('stop', [0]);
- this.animations.add('fly', [1, 2], 8, true); // 8fps looped
- this.animations.play('stop');
- }
- Spaceship.prototype = Object.create(Phaser.Sprite.prototype);
- Spaceship.prototype.constructor = Spaceship;
- // Spaceship move
- Spaceship.prototype.move = function (directionX, directionY) {
- if (this.isFrozen) { return; }
- const SPEED = 200;
- this.body.velocity.x = directionX * SPEED;
- this.body.velocity.y = -directionY * SPEED;
- if (this.body.velocity.x < 0) {
- this.scale.x = -1;
- this.angle = 0;
- }
- else if (this.body.velocity.x > 0) {
- this.scale.x = 1;
- this.angle = 0;
- }
- if (this.body.velocity.y < 0) {
- this.scale.x = -1;
- this.angle = 90;
- }
- else if (this.body.velocity.y > 0) {
- this.scale.x = 1;
- this.angle = 90;
- }
- };
- Spaceship.prototype.freeze = function () {
- this.body.enable = false;
- this.isFrozen = true;
- };
- Spaceship.prototype.unfreeze = function () {
- this.body.enable = true;
- this.isFrozen = false;
- };
- // Spaceship update
- Spaceship.prototype.update = function () {
- // update sprite animation, if it needs changing
- let animationName = this._getAnimationName();
- if (this.animations.name !== animationName) {
- this.animations.play(animationName);
- }
- };
- Spaceship.prototype._getAnimationName = function () {
- let name = 'stop'; // default animation
- // frozen & not dying
- if (this.isFrozen) {
- name = 'stop';
- }
- else if (this.body.velocity.x !== 0 || this.body.velocity.y !== 0) {
- name = 'fly';
- }
- return name;
- };
- // =========================================================================================
- MapState = {};
- MapState.preload = function () {
- this.game.load.json('level:worldmap', 'data/worldmap.json');
- this.game.load.image('worldmap', 'images/worldmap.png');
- // Player
- this.game.load.spritesheet('spaceship', 'images/spaceship.png', 42, 42);
- // Stations
- this.game.load.image('1-1', 'images/1-1.png');
- this.game.load.image('1-2', 'images/1-2.png');
- this.game.load.image('1-3', 'images/1-3.png');
- };
- MapState.create = function () {
- this.game.add.image(0, 0, 'worldmap');
- this._loadMap(this.game.cache.getJSON('level:worldmap'));
- };
- MapState.init = function () {
- this.game.renderer.renderSession.roundPixels = true;
- this.wasd = this.game.input.keyboard.addKeys({
- left: Phaser.KeyCode.A,
- right: Phaser.KeyCode.D,
- up: Phaser.KeyCode.W,
- down: Phaser.KeyCode.S,
- interact: Phaser.KeyCode.E,
- tut: Phaser.KeyCode.ESC
- });
- this.isTutorialOpen = false;
- this.wasd.tut.onDown.add(function () {
- if (this.isTutorialOpen == false) {
- this.tutorial = game.add.image(0,0, 'tutorialbg');
- this.spaceship.freeze();
- this.isTutorialOpen = true;
- }
- else {
- this.tutorial.destroy();
- this.spaceship.unfreeze();
- this.isTutorialOpen = false;
- }
- }, this);
- };
- MapState.update = function () {
- this._handleMapCollisions();
- this._handleInput();
- };
- MapState._handleInput = function () {
- if (this.wasd.left.isDown) { // move left
- this.spaceship.move(-1, 0);
- }
- else if (this.wasd.right.isDown) { // move right
- this.spaceship.move(1, 0);
- }
- else if (this.wasd.up.isDown) {
- this.spaceship.move(0, 1);
- }
- else if (this.wasd.down.isDown) {
- this.spaceship.move(0, -1);
- }
- else { // stop
- this.spaceship.move(0, 0);
- }
- };
- MapState._loadMap = function (data) {
- this.stations = this.game.add.group();
- this._spawnSpaceship();
- data.stations.forEach(this._spawnStation, this);
- };
- MapState._spawnSpaceship = function () {
- this.spaceship = new Spaceship(this.game, 10, 500);
- this.game.add.existing(this.spaceship);
- };
- MapState._spawnStation = function (station) {
- this.station = this.stations.create(
- station.x, station.y, station.image);
- this.game.physics.enable(station);
- this.station.body.allowGravity = false;
- this.station.body.immovable = true;
- this.station.anchor.set(0.5, 0.5);
- var ran = this.rnd.realInRange(800, 1200);
- this.add.tween(station)
- .to({y: '+8'}, ran, Phaser.Easing.Sinusoidal.InOut)
- .yoyo(true)
- .start();
- };
- MapState._handleMapCollisions = function () {
- if (game.physics.arcade.distanceBetween(this.station, this.spaceship) <= 50) {
- this._showWork(stationArr[station]);
- }
- };
- MapState._onSpaceshipVsStation = function (spaceship, station) {
- if (this.isOverlapping == false) {
- this.game.add.tween(station.scale)
- .to({x: 0.8, y: 0.8}, 50, Phaser.Easing.Sinusoidal.InOut)
- .yoyo(true)
- .start();
- this.isOverlapping = true;
- }
- };
- window.onload = function () {
- game.state.add('map', MapState);
- game.state.start('map');
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement