"use strict";

// itinerary is a [] array of {x: int, y: int} coordinates.
// they will be traversed on the first 45
var GO_Bigspinner = function(itinerary,speed){
	this.sprite = new Sprite(GO_Bigspinner.spriteSheet,"Bigspinner");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE; // so it can hit proactively when its BB changes
	this.sprite.setBoundingBox(
		this.bbs[0][0],
		this.bbs[0][1],
		this.bbs[0][2],
		this.bbs[0][3]);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.itinerary = itinerary;
	this.speed = speed;
	this.itindex = 0;
	this.frame = 0;
};
GO_Bigspinner.spriteImg = SDS_Load_Image("art/sheets/sprite/bigspinner.png");
GO_Bigspinner.spriteSheet = new TileSheet(5, 5, 2, 1, GO_Bigspinner.spriteImg);

GO_Bigspinner.prototype = new GameObject(
	new Sprite(
		GO_Bigspinner.spriteSheet,
		"Bigspinner"
	),
	"Bigspinner"
	);

	GO_Bigspinner.prototype.bbs = [
		[16,1,21,15],
		[16,10,30,15],
		[16,16,30,21],
		[16,16,21,30],
		[10,16,15,30],
		[1,16,15,21],
		[1,10,15,15],
		[10,1,15,15],
	];

	GO_Bigspinner.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;

	GO_Bigspinner.prototype.inflict = function(){
		return {hp: 2, chi:0};
	};

	GO_Bigspinner.prototype.Update = function(){
		this._heap.x = this.sprite.x+16;
		this._heap.y = this.sprite.y+16;

		if (this._heap.x>this.itinerary[this.itindex].x-this.speed &&
			this._heap.x<this.itinerary[this.itindex].x+this.speed &&
			this._heap.y>this.itinerary[this.itindex].y-this.speed &&
			this._heap.y<this.itinerary[this.itindex].y+this.speed){

			++this.itindex;
			if (this.itindex>=this.itinerary.length) this.itindex=0;
		}
		else{
			if (this._heap.x<this.itinerary[this.itindex].x) this.sprite.x+=this.speed;
			if (this._heap.x>this.itinerary[this.itindex].x) this.sprite.x-=this.speed;
			if (this._heap.y<this.itinerary[this.itindex].y) this.sprite.y+=this.speed;
			if (this._heap.y>this.itinerary[this.itindex].y) this.sprite.y-=this.speed;
		}

		this.frame = (this.frame+1)&31;
		this.sprite.frame = this.frame >> 2;

		this.sprite.setBoundingBox(
			this.bbs[this.sprite.frame][0],
			this.bbs[this.sprite.frame][1],
			this.bbs[this.sprite.frame][2],
			this.bbs[this.sprite.frame][3]);
	};

