"use strict";

var GO_BattleTrident = function(vertical, target, owner){
	this.sprite = new Sprite(GO_BattleTrident.spriteSheet,"BattleTrident");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.sprite.setBoundingBox(0,0,15,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.target_name = target;
	this.target = null;
	this.owner_name = owner;
	this.owner = null;

	this.vertical = vertical;

	this.timer=-1;

	this.phase = GO_KickWave.PHASE.SHOOT;
};
GO_BattleTrident.spriteImg = SDS_Load_Image("art/sheets/sprite/battletrident.png");
GO_BattleTrident.spriteSheet = new TileSheet(4, 4, 2, 1, GO_BattleTrident.spriteImg);

GO_KickWave.PHASE = {
	"SHOOT" : 0,
	"TUMBLE" : 1,
	"RETURN" : 2
}

GO_BattleTrident.prototype = new GameObject(
	new Sprite(
		GO_BattleTrident.spriteSheet,
		"BattleTrident"
	),
	"BattleTrident"
	);

	GO_BattleTrident.prototype.interactions = 0;

	GO_BattleTrident.prototype._collidingS = (
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_SH | 
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_SV |
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.COLLIDING_S);

	GO_BattleTrident.prototype.Update = function(){
		if (this.target_name && !this.target) this.target = this.manager.getNamedObject(this.target_name);
		if (this.owner_name && !this.owner) this.owner = this.manager.getNamedObject(this.owner_name);
		this._heap.dx = this._heap.dy = 0;

		++this.timer;

		switch(this.phase){
			case GO_KickWave.PHASE.SHOOT:
				this._heap.dx = this.vertical ? 0 : -6;
				this._heap.dy = this.vertical ? -6 : 0;

				this.sprite.frame = this.vertical ? 6 : 4;
				this.sprite.frame += (this.timer&1);
			break;
			case GO_KickWave.PHASE.TUMBLE:
				if (!(this.timer&3)) this.sprite.frame += this.vertical ? 1 : 3; // 3==-1
				this.sprite.frame = this.sprite.frame & 3;
			break;
			case GO_KickWave.PHASE.RETURN:
				if (this.owner){
					this._heap.dx = this.owner.sprite.x + this.owner.sprite.center.x - this.sprite.x - this.sprite.center.x;
					this._heap.dy = this.owner.sprite.y + this.owner.sprite.center.y - this.sprite.y - this.sprite.center.y;
				}
				this._heap.dx = Math.min(2,Math.max(this._heap.dx,-2));
				this._heap.dy = Math.min(2,Math.max(this._heap.dy,-2));
				this.sprite.frame = 0;
			break;
		}

		
		this.sprite.move(this._heap.dx,this._heap.dy,  this._heap.spriteMoveRes);


		switch(this.phase){
			case GO_KickWave.PHASE.SHOOT:
				if (this._heap.spriteMoveRes.flags & this._collidingS){
					for (this._heap.i=0; this._heap.i<this._heap.spriteMoveRes.collisions.length; this._heap.i++){
						if (this._heap.spriteMoveRes.collisions[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE){
							this._heap.spriteMoveRes.collisions[this._heap.i].game_object.damage(this,9999); // justification of damage cap is left as an exercise to the reader
							
							if (this._heap.spriteMoveRes.collisions[this._heap.i].game_object == this.target){
								this.phase = GO_KickWave.PHASE.TUMBLE;
								this.sprite.frame = this.vertical ? 1 : 3;
								this.timer = 0;
							}
						}
					}
				}
			break;
			case GO_KickWave.PHASE.TUMBLE:
				if (this.sprite.frame == 0 && this.owner){
					this.phase = GO_KickWave.PHASE.RETURN;
				}
			break;
			case GO_KickWave.PHASE.RETURN:
				/*
				if (this._heap.spriteMoveRes.flags & this._collidingS){
					for (this._heap.i=0; this._heap.i<this._heap.spriteMoveRes.collisions.length; this._heap.i++){
						if (this._heap.spriteMoveRes.collisions[this._heap.i].game_object == this.owner){
							this.manager.remove(this);
							this.sprite.map_room.removeSprite(this.sprite);
							break;
						}
					}
				}
				*/

				// faster & easier & will get it all the way "onto" the owner.
				if (this._heap.dx==0 && this._heap.dy==0){
					this.manager.remove(this);
					this.sprite.map_room.removeSprite(this.sprite);
				}
			break;
		}

	}
