"use strict";

// Gabe's lightning, later borrowed by Ravi
var GO_Lightning = function(dx,dy){
	//dx = dx<0 ? -1 : 1;
	//dy = dy<0 ? -1 : 1;
	this.sprite = new Sprite(GO_Lightning.spriteSheet,"Lightning");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE | // so it can hit solid e.g. carried blocks
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND;
	if (Math.abs(dx) >= Math.abs(dy)){
		this.sprite.setBoundingBox(0,2,7,5);
	}
	else{
		this.sprite.setBoundingBox(2,0,5,7);
	}
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO
	this.sprite.frame = -1;

	this.shadow1 = new Sprite(GO_Lightning.spriteSheet,"Lightning_b");
	this.shadow1.collision_flags = 0;
	this.shadow1.game_object = this; // no longer a generic GO
	this.shadow1.frame = -1;

	this.shadow2 = new Sprite(GO_Lightning.spriteSheet,"Lightning_y");
	this.shadow2.collision_flags = 0;
	this.shadow2.game_object = this; // no longer a generic GO
	this.shadow2.frame = -1;

	++GO_Lightning.INIT_FRAME;
	this.frame = (GO_Lightning.INIT_FRAME %= 4);

	this.dx = dx;
	this.dy = dy;

};
GO_Lightning.spriteImg = SDS_Load_Image("art/sheets/sprite/lightning.png");
GO_Lightning.spriteSheet = new TileSheet(3, 3, 2, 3, GO_Lightning.spriteImg);

GO_Lightning.prototype = new GameObject(
	new Sprite(
		GO_Lightning.spriteSheet,
		"Lightning"
	),
	"Lightning"
	);

	GO_Lightning.INIT_FRAME = 0;

	GO_Lightning.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;

	GO_Lightning.prototype.inflict = function(){
		this.clearFromRoom(this.sprite.map_room);
		this.manager.remove(this);

		return {hp: 2, chi:-8};
	};

	GO_Lightning.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.shadow2);
		mapRoom.addSprite(this.shadow1);
		mapRoom.addSprite(this.sprite);
	}
	GO_Lightning.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
		mapRoom.removeSprite(this.shadow1);
		mapRoom.removeSprite(this.shadow2);
	}

	GO_Lightning.prototype.Update = function(){
		if (this.sprite.collision_flags){
			this.sprite.move(this.dx,this.dy,  this._heap.spriteMoveRes);
		}
		else{
			// just so we maintain correct data in the spriteMoveRes object.
			// We don't want a repeat of Spiders...
			this.sprite.move(0,0,  this._heap.spriteMoveRes);
		}

		this._heap.hit = this._heap.spriteMoveRes.flags & (DEFINE_SPRITE_MOVE_RESULT_EJECTED_H | DEFINE_SPRITE_MOVE_RESULT_EJECTED_V);

		if (this._heap.hit){
			// use existing state as a de-facto flag for is-ded
			this.sprite.collision_flags = 0;

			// if we clean up and spriteswap now,
			// we'll leave a gap where this segment would have been this frame;
			// drop cleanup logic to the bottom of the frame logic instead
			//if (this.dx!=0){
			//	this.sprite.x += (this.dx>0) ? 4 : -4;
			//}
			//if (this.dy!=0){
			//	this.sprite.y += (this.dy>0) ? 4 : -4;
			//}
			//this.dx = 0;
			//this.dy = 0;
			//this.frame = -1;
			//this.shadow1.visible = false;
			//this.shadow2.visible = false;
		}

		++this.frame;
		if (this._heap.hit || this.sprite.collision_flags!=0){
			this.frame %= 4;
			this.sprite.frame = this.frame;
			this.shadow1.frame = ((this.frame+1)%4)+8;
			this.shadow2.frame = ((this.frame+2)%4)+16;
			if (Math.abs(this.dy) > Math.abs(this.dx)){
				this.sprite.frame += 4;
				this.shadow1.frame += 4;
				this.shadow2.frame += 4;
			}
			//this.shadow1.x = this.sprite.x - this.dx;
			//this.shadow1.y = this.sprite.y - this.dy;
			//this.shadow2.x = this.sprite.x - (this.dx<<1);
			//this.shadow2.y = this.sprite.y - (this.dy<<1);
			this.shadow1.x = this.sprite.x;
			this.shadow1.y = this.sprite.y;
			this.shadow2.x = this.sprite.x;
			this.shadow2.y = this.sprite.y;
			if (this.dx != 0){
				this.shadow1.x -= (this.dx>0 ? 2 : -2);
				this.shadow2.x -= (this.dx>0 ? 4 : -4);
			}
			if (this.dy != 0){
				this.shadow1.y -= (this.dy>0 ? 2 : -2);
				this.shadow2.y -= (this.dy>0 ? 4 : -4);
			}
			if (this._heap.hit){
				// reset frame index for spark
				// now that we're done with the lightning frame
				this.frame = -1;
			}
		}
		else if (this.frame >= 8){
			this.clearFromRoom(this.sprite.map_room);
			this.manager.remove(this);
		}
		else{
			this.sprite.frame = this.frame+24;
			this.shadow1.visible = false;
			this.shadow2.visible = false;

			// if we have NOT yet aligned the spark
			// to the collision point (a one-time
			// operation), do so.
			if (this.dx!=0){
				this.sprite.x += (this.dx>0) ? 4 : -4;
			}
			if (this.dy!=0){
				this.sprite.y += (this.dy>0) ? 4 : -4;
			}
			this.dx = 0;
			this.dy = 0;
		}
	};

