"use strict";

// itinerary is a [] array of {x: int, y: int} coordinates.
// they will be traversed on the first 45
var GO_Bladespinner = function(itinerary,speed){
	this.sprite = new Sprite(GO_Bladespinner.spriteSheet,"Bladespinner");
	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_Bladespinner.spriteImg = SDS_Load_Image("art/sheets/sprite/bladespinner.png");
GO_Bladespinner.spriteSheet = new TileSheet(4, 4, 2, 1, GO_Bladespinner.spriteImg);

GO_Bladespinner.prototype = new GameObject(
	new Sprite(
		GO_Bladespinner.spriteSheet,
		"Bladespinner"
	),
	"Bladespinner"
	);

	GO_Bladespinner.prototype.bbs = [
		[5,0,10,10],
		[5,1,14,10],
		[5,5,15,10],
		[5,5,14,14],
		[5,5,10,15],
		[1,5,10,14],
		[0,5,10,10],
		[1,1,10,10],
	];

	GO_Bladespinner.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;

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

	GO_Bladespinner.prototype.Update = function(){
		this._heap.x = this.sprite.x+8;
		this._heap.y = this.sprite.y+8;

		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]);
	};

