"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_SinOrbit = function(target,dx,dy,speed,img){
	/*
	img = img ? img : 0;

	//this.sprite = new Sprite(GO_SinOrbit.spriteSheet,"SinOrbit");
	this.sprite = new Sprite(GO_SinOrbit.spriteSheets[img],"SinOrbit");
	this.sprite.collision_flags = 0;
	//this.sprite.setBoundingBox(
	//	0,
	//	0,
	//	1,
	//	4);
	// bounds will init automatically to tile size

	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	// timestep counts
	this.tx = -dx;
	this.ty = -dy;

	// deltas for each step
	this.dx = dx;
	this.dy = dy;

	this.speed = speed; // how much to lshift .t* before indexing

	this.target = target;
	// orbit span
	// these are in log2, but that's what we want
	this.halfwidth = target.sprite.tile_sheet.tile_width;//-1;
	this.halfheight = target.sprite.tile_sheet.tile_height;//-1;

	this.targetoffx = target.sprite.tile_sheet.tile_pixel_width>>1;
	this.targetoffy = target.sprite.tile_sheet.tile_pixel_height>>1;

	// own sprite size
	this.halftilew = (this.sprite.tile_sheet.tile_pixel_width>>1);
	this.halftileh = (this.sprite.tile_sheet.tile_pixel_height>>1);
	*/

	this.initOrbit(target,dx,dy,speed,img);
};

//GO_SinOrbit.spriteImg = SDS_Load_Image("art/sheets/sprite/taintspark.png");
//GO_SinOrbit.spriteSheet = new TileSheet(2, 2, 2, 1, GO_SinOrbit.spriteImg);
//GO_SinOrbit.spriteImg = SDS_Load_Image("art/sheets/sprite/linedot.png");
//GO_SinOrbit.spriteSheet = new TileSheet(0, 2, 3, 0, GO_SinOrbit.spriteImg);
GO_SinOrbit.spriteImg = SDS_Load_Image("art/sheets/sprite/killdot.png");
GO_SinOrbit.spriteSheet = new TileSheet(2, 2, 3, 0, GO_SinOrbit.spriteImg);

GO_SinOrbit.spriteImgs = [
	SDS_Load_Image("art/sheets/sprite/killdot.png"),
	SDS_Load_Image("art/sheets/sprite/darkspark.png"),
	SDS_Load_Image("art/sheets/sprite/sparkle.png"),
];
GO_SinOrbit.spriteSheets = [
	new TileSheet(2, 2, 3, 0, GO_SinOrbit.spriteImgs[0]),
	new TileSheet(2, 2, 2, 1, GO_SinOrbit.spriteImgs[1]),
	new TileSheet(3, 3, 3, 0, GO_SinOrbit.spriteImgs[2]),
];

GO_SinOrbit.prototype = new GameObject(
	new Sprite(
		GO_SinOrbit.spriteSheet,
		"SinOrbit"
	),
	"SinOrbit"
	);

	GO_SinOrbit.prototype.interactions = 0;

	// breaking out the init logic so we can extend this object for Ravi's BARRIER spell
	GO_SinOrbit.prototype.initOrbit = function(target,dx,dy,speed,img){
		img = img ? img : 0;

		//this.sprite = new Sprite(GO_SinOrbit.spriteSheet,"SinOrbit");
		this.sprite = new Sprite(GO_SinOrbit.spriteSheets[img],"SinOrbit");
		this.sprite.collision_flags = 0;
		//this.sprite.setBoundingBox(
		//	0,
		//	0,
		//	1,
		//	4);
		// bounds will init automatically to tile size

		//sprite.constraints=constraints;
		this.sprite.max_step = 0;
		this.sprite.game_object = this; // no longer a generic GO

		// timestep counts
		this.tx = -dx;
		this.ty = -dy;

		// deltas for each step
		this.dx = dx;
		this.dy = dy;

		this.speed = speed; // how much to lshift .t* before indexing

		this.target = target || this; // movement will explode if targeting self, but need some default value
		// orbit span
		// these are in log2, but that's what we want
		this.halfwidth = this.target.sprite.tile_sheet.tile_width;//-1;
		this.halfheight = this.target.sprite.tile_sheet.tile_height;//-1;

		this.targetoffx = this.target.sprite.tile_sheet.tile_pixel_width>>1;
		this.targetoffy = this.target.sprite.tile_sheet.tile_pixel_height>>1;

		// own sprite size
		this.halftilew = (this.sprite.tile_sheet.tile_pixel_width>>1);
		this.halftileh = (this.sprite.tile_sheet.tile_pixel_height>>1);
	};

	GO_SinOrbit.prototype.Update = function(){
		this.tx += this.dx;
		this.ty += this.dy;

		this.tx = this.tx&255;
		this.ty = this.ty&255;

		this._heap.ix = (this.tx<<this.speed)&255;
		this._heap.iy = (this.ty<<this.speed)&255;

		this.sprite.x = SIN[this._heap.ix];
		//this.sprite.y = COS[this._heap.iy];
		this.sprite.y = (COS[this._heap.iy] * (Math.abs(COS[this._heap.ix])+1))>>7; // circular profile

		// now, these x/y vals will be in the -127 to 127 range (+/- 2^7)
		// we need them in the range +/- 2^n where n=halfwidth or halfheight
		if (this.halfwidth>7){
			this.sprite.x = this.sprite.x<<(this.halfwidth-7);
		}
		else{
			this.sprite.x = this.sprite.x>>(7-this.halfwidth);
		}
		if (this.halfheight>7){
			this.sprite.y = this.sprite.y<<(this.halfheight-7);
		}
		else{
			this.sprite.y = this.sprite.y>>(7-this.halfheight);
		}

		// now add target's offset and subtract the sprite's halfwidth
		this.sprite.x += this.target.sprite.x + this.targetoffx - this.halftilew;
		this.sprite.y += this.target.sprite.y + this.targetoffy - this.halftileh;

		//z-indexing at this point would be hell, so attempt a cheap hack
		if (this._heap.ix>64 && this._heap.ix<192 && this.sprite.isTouchingSprite(this.target.sprite)){
			this.sprite.visible = false;
		}
		else{
			this.sprite.visible = true;
		}

		//this.sprite.frame++;
		//this.sprite.frame = this.sprite.frame&7;

	};

