12-00-spaceShooter/demo05/demo05.js
/* global Phaser */

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  */
/* GLOBAL CONSTANTS */
const SHOWDEBUG = true;

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  */
/* GLOBAL VARIABLES */
var game = new Phaser.Game(960, 640, Phaser.AUTO, "", { preload: preload, create: create, update: update, render: render });
var playerShip;
var earth;
var moon;
var spaceDust;
var cursorKeys;

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  */
/* PHASER STATE FUNCTIONS */
function preload() 
{
    game.load.spritesheet("ship", "../assets/spaceship.png", 94, 100, -1, -1, 1);
    game.load.image("background", "../assets/background_02_parallax_01.jpg");
    game.load.image("spaceDust", "../assets/background_02_parallax_02.png");
    game.load.image("earth", "../assets/background_02_parallax_03.png");
    game.load.image("moon", "../assets/background_02_parallax_04.png");
}

function create() 
{
    
    /* enable ARCADE physics */
    game.physics.startSystem(Phaser.Physics.ARCADE);    
    
    /* background image */
    game.add.image(0, 0, "background");

	/* create the parallax background elements */
	earth = game.add.image(0, 0, "earth");
	earth.x = game.rnd.integerInRange(0, game.width-earth.width);
	moon = game.add.image(0, 0, "moon");
	moon.x = game.rnd.integerInRange(0, game.width-moon.width);
	spaceDust = game.add.tileSprite(0,0,960,640,"spaceDust");
    
    /* player ship SPRITE with Physics enabled */
    playerShip = game.add.sprite(game.width/2,game.height-100, "ship");
    game.physics.arcade.enable(playerShip);
    playerShip.anchor.setTo(0.5);
	playerShip.body.drag.x = 1000;
	playerShip.body.maxVelocity.x = 600;
    /* add animations to the ship */	
    playerShip.animations.add("straight", [0, 1, 2, 1, 2, 3, 2, 1, 2, 1], 16, true);
    playerShip.animations.add("left", [4,5,6,7], 16, true);
    playerShip.animations.add("hardleft", [8,9,10,11], 16, true);
    playerShip.animations.add("right", [12,13,14,15], 16, true);
    playerShip.animations.add("hardright", [16,17,18,19], 16, true);


    /* add the cursor keys - access to the left,right,up,down arrow keys */
    cursorKeys = game.input.keyboard.createCursorKeys();

}

function update() 
{

    /* update the earth, moon, and dust */
    spaceDust.tilePosition.y += 8;
    
    earth.y += 0.1;
    if (earth.y > game.height)
    {
        earth.y = -earth.height;
	    earth.x = game.rnd.integerInRange(0, game.width-earth.width);
    }
    
    moon.y += 0.5;
    if (moon.y > game.height)
    {
        moon.y = -moon.height;
	    moon.x = game.rnd.integerInRange(0, game.width-moon.width);
    }
    
    /* move the playerShip */
    if (cursorKeys.left.isDown) {
        playerShip.body.acceleration.x = -1000;
    } else if (cursorKeys.right.isDown) {
        playerShip.body.acceleration.x = 1000;
    } else {
        playerShip.body.acceleration.x = 0;
    }


    if (playerShip.body.velocity.x < -200) {
        playerShip.animations.play("hardleft");
    } else if (playerShip.body.velocity.x < -20) {
        playerShip.animations.play("left");
    } else if (playerShip.body.velocity.x > 200) {
        playerShip.animations.play("hardright");
    } else if (playerShip.body.velocity.x > 20) {
        playerShip.animations.play("right");
    } else {
        playerShip.animations.play("straight");
    }

    /* wrap the playerShip around the game world */
    game.world.wrap(playerShip);
    
}

function render() 
{
    if (SHOWDEBUG)
    {
    }
}