/*
* It rolls down stairs alone or in pairs
* and over your neighbor's dog
*/
"use strict";

var GO_Log = function(damaged){
	this.sprite = new Sprite(GO_Log.spriteSheet,"Log");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND | 
		DEFINE_SPRITE_COLLISION_FLAGS.WATER |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
	this.sprite.setBoundingBox(0,5,15,15);
	//this.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=1; // no longer used for damage so we don't build new returns every call

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

	this.held = false;

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

GO_Log.prototype = new GameObject(
	new Sprite(
		GO_Log.spriteSheet,
		"Log"
	),
	"Log"
	);

	GO_Log.prototype.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.GRAB;

	GO_Log.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_Log.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_Log.prototype.weight = DEFINE_PROJECTILE_WEIGHT.LIGHT;
	GO_Log.prototype.hoistpoint = [
		{'x':13, 'y':11}, // grab point when moving direction -1
		{'x':3, 'y':11} // grab point when moving direction 1
	];

	GO_Log.prototype.Update = function(){
		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){
			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;
		}

		
		if (this.gravity_index>0 && (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.COLLIDING_S)){
			// apply damage to target
			for (this._heap.i=0; this._heap.i<this._heap.spriteMoveRes.collisions.length; this._heap.i++){
				if (this._heap.spriteMoveRes.collisions[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE){
					if (this.alreadyHit.indexOf(this._heap.spriteMoveRes.collisions[this._heap.i])==-1){
						this._heap.broken = this._heap.broken || !this._heap.spriteMoveRes.collisions[this._heap.i].game_object.damage(this,this.impact);
						this.alreadyHit.push(this._heap.spriteMoveRes.collisions[this._heap.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.sprite.frame = this.damaged ? 1 : 0;
	};

	GO_Log.prototype.IndisposedUpdate = function(){
		
	};

	GO_Log.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_Log.prototype.drop = function(){
		this.gravity_index=0;
		this.fragile=true;

		this.held = false;
	};

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