/*
Dark drops serving as the walls of the Nautilus maze
*/

"use strict";

var GO_DarkDrop = function(){
	this.sprite = new Sprite(GO_DarkDrop.spriteSheet,"DarkDrop");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(4,2,10,13);
	this.sprite.game_object = this; // no longer a generic GO

	this.meli = null;
	this.base_x=null;
	this.base_y=null;
	this.seek_timer=0;
	this.seek_pow=11;
	this.seektimes = 2<<(this.seek_pow-1);
		
	this.frame_timer=0;
};
GO_DarkDrop.spriteImg = SDS_Load_Image("art/sheets/sprite/teartrap.png");
GO_DarkDrop.spriteSheet = new TileSheet(4, 4, 2, 2, GO_DarkDrop.spriteImg);

GO_DarkDrop.prototype = new GameObject(
	new Sprite(
		GO_DarkDrop.spriteSheet,
		"DarkDrop"
	),
	"DarkDrop"
	);
	
	GO_DarkDrop.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
	
	GO_DarkDrop.prototype._inflictVal = {hp:2, chi:64};
	GO_DarkDrop.prototype.inflict = function(){
		// clear out all dark tears and the gate
		this._heap.gate = this.manager.getNamedObject("GhostVortex");
		while (this._heap.gate){
			this._heap.gate.sprite.map_room.removeSprite(this._heap.gate.sprite);
			this._heap.gate.manager.remove(this._heap.gate);
			this._heap.gate = this.manager.getNamedObject("GhostVortex");
		}

		this._heap.tear = this.manager.getNamedObject("DarkDrop");
		while (this._heap.tear){
			this._heap.tear.sprite.map_room.removeSprite(this._heap.tear.sprite);
			this._heap.tear.manager.remove(this._heap.tear);
			this._heap.tear = this.manager.getNamedObject("DarkDrop");
		}

		return this._inflictVal;
	};

	GO_DarkDrop.prototype.Update = function(){
		if (!this.meli) this.meli=this.manager.getNamedObject("Meli");
		if (this.base_x==null) this.base_x=this.sprite.x;
		if (this.base_y==null) this.base_y=this.sprite.y;

		if (this.meli && this.base_x!=null && this.base_y!=null){
			if (this.seek_timer<this.seektimes-1) this.seek_timer++;

			//this._heap.dx = this.meli.sprite.x - this.base_x;
			//this._heap.dy = this.meli.sprite.y - this.base_y;
			//if (this._heap.dx*this._heap.dx + this._heap.dy*this._heap.dy < 32*32){
			//	this.sprite.x=this.base_x;
			//	this.sprite.y=this.base_y;
			//}
			//else{
				this._heap.seekx = this.seek_timer*this.meli.sprite.x + (this.seektimes-this.seek_timer)*this.base_x;
				this._heap.seeky = this.seek_timer*this.meli.sprite.y + (this.seektimes-this.seek_timer)*this.base_y;
				this._heap.seekx = this._heap.seekx>>this.seek_pow;
				this._heap.seeky = this._heap.seeky>>this.seek_pow;
				this.sprite.x=this._heap.seekx;
				this.sprite.y=this._heap.seeky;
			//}
		}

		this.frame_timer++;
		this.frame_timer&=15;
		this.sprite.frame = this.frame_timer>>2;
		
		//this.sprite.frame = this.frame_timer&1 ? 15 : this.sprite.frame + 4;
		this.sprite.frame+=4;
		this.sprite.visible = !(this.frame_timer&1);
	};
