"use strict";

var GO_DarkWave = function(power, direction, constraints){
	this.sprite = new Sprite(GO_DarkWave.spriteSheet,"DarkWave");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND | 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.sprite.setBoundingBox(6,5,9,10);
	this.sprite.constraints=constraints;
	this.sprite.game_object = this; // no longer a generic GO

	this.timer=-1;

	this.power=power;
	this.direction=direction;
};
GO_DarkWave.spriteImg = SDS_Load_Image("art/sheets/sprite/darkwave.png");
GO_DarkWave.spriteSheet = new TileSheet(4, 4, 3, 2, GO_DarkWave.spriteImg);

GO_DarkWave.prototype = new GameObject(
	new Sprite(
		GO_DarkWave.spriteSheet,
		"DarkWave"
	),
	"DarkWave"
	);

	GO_DarkWave.prototype.interactions = 0;
	GO_DarkWave.prototype.sizeBoxes = [
		[6,5,9,10],
		[5,2,10,13],
		[4,0,11,15]
	];
	GO_DarkWave.prototype.wave_speed = 	[
		1,1,1,1,
		2,2,2,2,
		3,3,3,3,3,
		4,4,4,4,4,
		5,5,5,5,5,5,
		6,6,6,6,6,6,6,
		7
		];
	GO_DarkWave.prototype._collidingS = (
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_SH | 
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_SV |
		DEFINE_SPRITE_MOVE_RESULT_FLAGS.COLLIDING_S);

	GO_DarkWave.prototype.Update = function(){
		++this.timer;

		if (this.power>0){
		
			this._heap.dx = this.wave_speed[Math.min(this.timer,this.wave_speed.length-1)];
			if (this.direction<0){
				this._heap.dx = -this._heap.dx;
			}
			this.sprite.move(this._heap.dx,0,  this._heap.spriteMoveRes);
			
			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,this.power);
						this.power=0;
					}
				}
			}

			if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_H){
				this.power=0;
			}
		}
		

		if (this.power<-3){
			this.manager.remove(this);
			this.sprite.map_room.removeSprite(this.sprite);
		}
		else{
			if (this.power>0){
				this._heap.size = Math.min(this.timer>>3,this.power>>4,2);
				this.sprite.setBoundingBox(
					this.sizeBoxes[this._heap.size][0],
					this.sizeBoxes[this._heap.size][1],
					this.sizeBoxes[this._heap.size][2],
					this.sizeBoxes[this._heap.size][3]
					);

				this.sprite.frame = (this._heap.size<<3) + ((this.timer>>1) & 3);
			}
			else{
				this.sprite.frame = 24 - this.power;
			}
			if (this.direction<0){
				this.sprite.frame+=4;
			}
		}

		--this.power;
	}
