"use strict";

// target must be a GO with a .sprite
// dx and dy are how many 256ths of a circle to increment the sin curve indices by each tick
// speed is how many times to lshift the curve indices by before sampling
// e.g. dx 1 speed 0 makes 1 orbit every 256 frames
// dx 3 speed 0 makes 3 orbits every 256 frames
// dx 3 speed 1 makes 3 orbits every 128 frames
var GO_InterceptOrbit = function(target,dx,dy,speed,img){
	this.initOrbit(target,dx,dy,speed,img);

	this.sprite.collision_flags = DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.hp = 3;
	this.dead = false;
};
GO_InterceptOrbit.prototype = new GO_SinOrbit(null,1,1,0,0);
	GO_InterceptOrbit.prototype.Update = function(){
		if (!this.dead){
			GO_SinOrbit.prototype.Update.call(this);

			// SinOrbit Update doesn't actually use move code,
			// so we need to grab collisions manually
			this.sprite.getCollisions(this._heap.spriteMoveRes.collisions);

			//if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.COLLIDING_S){
			if (this._heap.spriteMoveRes.collisions.length){
				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){
						// if we hit a thing that can take damage, poke it.
						this._heap.spriteMoveRes.collisions[this._heap.i].game_object.damage(this,0);
						// would be nice to just remove ourselves, but since we know we're
						// tracked by GO_Raviene, that would be impolite and create
						// cleanup issues.
						this.dead = true;
						this.sprite.collision_flags = 0;
					}

					if (this._heap.spriteMoveRes.collisions[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT){
						// if we hit a thing that would normally cause contact damage,
						// allow it to inflict on us in hopes that will clear it
						// (bullets, etc)
						this.hp -= this._heap.spriteMoveRes.collisions[this._heap.i].game_object.inflict().hp;

						if (this.hp<=0){
							// would be nice to just remove ourselves, but since we know we're
							// tracked by GO_Raviene, that would be impolite and create
							// cleanup issues.
							this.dead = true;
							this.sprite.collision_flags = 0;
						}
					}
				}
			}
		}
		this.sprite.visible = !this.dead;
	};
