/*
* Special custom throwable used by Guy (and perhaps Bobo)
*/
"use strict";

var GO_GuyRock = function(damaged){
	this.sprite = new Sprite(GO_GuyRock.spriteSheet,"GuyBoulder");
	this.sprite.collision_flags = 0;/*
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND | 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;*/
	this.sprite.setBoundingBox(0,0,15,15);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.gravity_index = 0;

	this.alreadyHit = []; // things we hit while falling; don't process them again

	this.impact=2; // NOTE: not currently used directly for damage because inflict shouldn't need to build new return elements

	this.damaged = damaged ? damaged : false;
	this.fragile=false; // set to true once grabbed; allows the block to break/disappear

	this.linear_shot = true;
	this.dx = 0;
	this.dy = 16;
	this.dx_accum = 0;
	this.dy_accum = 0;

	this.destruction_timer=30;
	this.destruction_timer_reset=30;

	this.held = false;

};
GO_GuyRock.spriteImg = SDS_Load_Image("art/sheets/sprite/guy_boulder.png");
GO_GuyRock.spriteSheet = new TileSheet(4, 4, 1, 0, GO_GuyRock.spriteImg);

GO_GuyRock.prototype = new GameObject(
	new Sprite(
		GO_GuyRock.spriteSheet,
		"GuyBoulder"
	),
	"GuyBoulder"
	);

	GO_GuyRock.prototype.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
		//DEFINE_GAMEOBJECT_INTERACTIONS.GRAB;

	GO_GuyRock.prototype.gravity_air = [ 
		1, 
		2, 2, 2, 
		3, 3, 3, 
		4, 4, 4, 4, 4, 4, 
		5, 5, 5, 5, 5, 5,
		6, 6, 6, 6, 6, 6,
		7, 7, 7, 7, 7, 7, 
		8];
	GO_GuyRock.prototype.gravity_water = [ 
		1, 
		1, 1, 1, 
		2, 2, 2, 
		3, 3, 3, 3, 3, 3,
		4, 4, 4, 4, 4, 4, 
		5, 5, 5, 5, 5, 5,
		6, 6, 6, 6, 6, 6, 
		7];

	GO_GuyRock.prototype.weight = DEFINE_PROJECTILE_WEIGHT.MEDIUM;
	GO_GuyRock.prototype.hoistpoint = [
		{'x':13, 'y':8}, // grab point when moving direction -1
		{'x':3, 'y':8} // grab point when moving direction 1
	];

	GO_GuyRock.prototype.Update = function(){

		if (this.linear_shot){
			// bullet code
			this.dx_accum+=this.dx;
			this.dy_accum+=this.dy;

			this._heap.dx = this.dx_accum>>3;
			this._heap.dy = this.dy_accum>>3;

			this.dx_accum &= 7;
			this.dy_accum &= 7;

			this.sprite.move(this._heap.dx,this._heap.dy, this._heap.spriteMoveRes);


			if (this._heap.spriteMoveRes.flags & (DEFINE_SPRITE_MOVE_RESULT_EJECTED_H | DEFINE_SPRITE_MOVE_RESULT_EJECTED_V)){
				if (this.sfx) this.sfx.stop();
				this.sfx = new Chiptune("FX_fallcrash",SDS.sfxchip);
				this.sfx.play(false);

				this.linear_shot=false;
				this.sprite.frame=0;

				this.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.GRAB;
			}
			else{
				this.sprite.frame++;
				if (this.sprite.frame>1) this.sprite.frame=0;
			}
		}
		else{
			// typical projectile code
			this._heap.gravityArray = (this.sprite.isTouchingTerrain(DEFINE_MAP_TERRAIN_TYPES.WATER)) ? this.gravity_water : this.gravity_air;

			if (this._heap.gravityArray[this.gravity_index] > 3){
				this.interactions |= DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
				this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
			}
			else{
				this.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
				this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
			}

			this.sprite.move(0,this._heap.gravityArray[this.gravity_index],  this._heap.spriteMoveRes);
			this._heap.broken = false;
			if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_V){
				if (this.gravity_index>4){
					if (this.sfx) this.sfx.stop();
					this.sfx = new Chiptune("FX_fallcrash",SDS.sfxchip);
					this.sfx.play(false);
				}

				this.gravity_index=0;
				this.alreadyHit.length=0;
				this._heap.broken = this._heap.broken || this.fragile;
			}
			else{
				this.gravity_index = Math.min(this.gravity_index+1,this._heap.gravityArray.length-1);
			}

			if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_H){
				this._heap.broken = this._heap.broken || this.fragile;
			}

			// let's not let the boss suicide on falling rocks
			/*if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.COLLIDING_S){
				// apply damage to target
				for (var i=0; i<this._heap.spriteMoveRes.collisions.length; i++){
					if (this._heap.spriteMoveRes.collisions[i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE){
						if (this.alreadyHit.indexOf(this._heap.spriteMoveRes.collisions[i])==-1){
							this._heap.broken = this._heap.broken || !this._heap.spriteMoveRes.collisions[i].game_object.damage(this,this.impact);
							this.alreadyHit.push(this._heap.spriteMoveRes.collisions[i]);
						}
					}
				}
				
			}*/
			

			if (this._heap.broken){
//				this._heap.poof = new GO_Poof();
//				this._heap.poof.spawnOnSprite(this.sprite,this.manager);
//				this._heap.poof = null;
//
//				this.sprite.map_room.removeSprite(this.sprite);
//				this.manager.remove(this);
				this.destroy();
			}
			else{
				this.IndisposedUpdate();
			}
		}
	};
	GO_GuyRock.prototype.destroy = function(){
		this._heap.poof = new GO_Poof();
		this._heap.poof.spawnOnSprite(this.sprite,this.manager);
		this._heap.poof = null;

		this.sprite.map_room.removeSprite(this.sprite);
		this.manager.remove(this);
	};

	GO_GuyRock.prototype.frags = [
				{x:0,y:-8},
				{x:6,y:-6},
				{x:8,y:0},
				{x:6,y:6},
				{x:0,y:8},
				{x:-6,y:6},
				{x:-8,y:0},
				{x:-6,y:-6},
			];
	GO_GuyRock.prototype.IndisposedUpdate = function(){
		this.destruction_timer--;
		if (this.destruction_timer<0){
			this.destruction_timer_reset--;
			this.destruction_timer=this.destruction_timer_reset;
		}
		if (this.destruction_timer_reset==0){
			// explode
			this._heap.meli = this.manager.getNamedObject("Meli");
			if (this._heap.meli && this._heap.meli.holding && this._heap.meli.held_obj==this){
				this._heap.meli.holding=false;
				this._heap.meli.held_obj=null;
			}

			
			for (this._heap.f=0; this._heap.f<this.frags.length; this._heap.f++){
				this._heap.frag = new GO_Bullet(this.frags[this._heap.f].x,this.frags[this._heap.f].y);
				this._heap.frag.sprite.x = this.sprite.x+4
				this._heap.frag.sprite.y = this.sprite.y+4;
				this.sprite.map_room.addSprite(this._heap.frag.sprite);
				this.manager.add(this._heap.frag);
			}

//			this._heap.poof = new GO_Poof();
//			this._heap.poof.spawnOnSprite(this.sprite,this.manager);
//			this._heap.poof = null;
//
//			this.sprite.map_room.removeSprite(this.sprite);
//			this.manager.remove(this);
			this.destroy();
		}

		this.sprite.frame = this.destruction_timer ? 0 : 1;
	};
	GO_GuyRock.prototype.Unindispose = function(dx, dy, moveRes){
		if (moveRes.flags & (DEFINE_SPRITE_MOVE_RESULT_EJECTED_H | DEFINE_SPRITE_MOVE_RESULT_EJECTED_V)){
			if (Math.abs(dx)+Math.abs(dy) > 1){
				if (this.sfx) this.sfx.stop();
				this.sfx = new Chiptune("FX_fallcrash",SDS.sfxchip);
				this.sfx.play(false);
			}
			this.destroy();
		}
	};

	GO_GuyRock.prototype.grab = function(){
		this.gravity_index=0;
		this.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
		this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
		this.held = true;
	};
	GO_GuyRock.prototype.drop = function(){
		this.gravity_index=0;
		this.fragile=true;
		this.held = false;
	};

	GO_GuyRock.prototype._inflictVal = {hp:2, chi:0};
	GO_GuyRock.prototype.inflict = function(){
		return this._inflictVal; //{hp:this.impact, chi:0};
	};
