Spaceship.prototype = Object.create(Phaser.Sprite.prototype);
Spaceship.constructor = Spaceship;

function Spaceship() {
	this.isActivated = false;
	this.speed = Spaceship.DEFAULT_SPEED;

	Phaser.Sprite.call(this, game, game.world.centerX, game.world.centerY, 'spaceship');
	this.frame = Spaceship.FRAME_NONE;
	game.add.existing(this);
	game.physics.arcade.enable(this);

	this.scale.set(1);
	this.anchor.set(0.5);
	this.halfWidth = this.width / 2;

	// this.setActive(true);
}



Spaceship.prototype.update = function() {
	if(this.isActivated) {
		game.physics.arcade.moveToPointer(this, this.speed);

		if(Phaser.Rectangle.contains(this.body, game.input.x, game.input.y)) {
			this.stop();
			this.setDirection(Spaceship.DIR_MIDDLE);
			return;
		}

		if(this.x - this.halfWidth < game.input.x && game.input.x < this.x + this.halfWidth) {
			this.setDirection(Spaceship.DIR_MIDDLE);
		} else if(this.x > game.input.x) {
			this.setDirection(Spaceship.DIR_LEFT);
		} else {
			this.setDirection(Spaceship.DIR_RIGHT);
		}
	} else {
		this.stop();
	}
}

Spaceship.prototype.setDirection = function(dir) {
	switch(dir) {
		case Spaceship.DIR_LEFT:
		this.frame = Spaceship.FRAME_LEFT;
		break;

		case Spaceship.DIR_MIDDLE:
		this.frame = Spaceship.FRAME_NONE;
		break;

		case Spaceship.DIR_RIGHT:
		this.frame = Spaceship.FRAME_RIGHT;
		break;
	}
}

Spaceship.prototype.stop = function() {
	this.body.velocity.setTo(0, 0);
}

Spaceship.prototype.setActive = function(isActivated) {
	this.isActivated = isActivated;
}

Spaceship.prototype.goRandomPosition = function() {
	// set random position
	var SPAWN_BOX_LEFT		= 80;
	var SPAWN_BOX_WIDTH		= game.world.width - 160;
	var SPAWN_BOX_TOP		= 280;
	var SPAWN_BOX_HEIGHT	= game.world.height - 400;

	this.x = SPAWN_BOX_LEFT + Math.random() * SPAWN_BOX_WIDTH;
	this.y = SPAWN_BOX_TOP + Math.random() * SPAWN_BOX_HEIGHT;

	// if(isDebugMode()) {
	// 	this.game.add.graphics()
	// 		.beginFill(0xffffff, 0.1)
	// 		.drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT);
	// }

}

Spaceship.loadResources = function() {
	game.load.spritesheet('spaceship', '../../../resources/image/character/spaceship/spaceship.png', 32, 32);
}


Spaceship.DEFAULT_SPEED		= 400;

Spaceship.DIR_LEFT			= 0;
Spaceship.DIR_MIDDLE		= 1;
Spaceship.DIR_RIGHT			= 2;

Spaceship.FRAME_LEFT		= 1;
Spaceship.FRAME_NONE		= 2;
Spaceship.FRAME_RIGHT		= 3;