"use strict";

var GO_SkySeed = function(){
	this.sprite = new Sprite(GO_SkySeed.spriteSheet,"SkySeed");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(3,10,28,29);
	this.sprite.game_object = this; // no longer a generic GO

	this.sprite.frame = 0;
	this.frame_timer = 0;

	this.name = "SkySeed";

	this._makeCheckable( GLOBAL_DIARY.check(3,"DRAINED_SEED") ?
		GLOBAL_DIARY.check(3,"ACTIVATED_SEED") ?
			"SkySeed_Grow" :
			GLOBAL_DIARY.check(3,"TREASURE") ?
				"GD_SkySeed_almost" :
				"GD_SkySeed_plant" :
		"GD_SkySeed"
		);

	// a secondary sprite in the seed object, determining whether we can float upwards
	this.buoy = new Sprite(null,"SkySeed_buoy");
	this.buoy.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.AIR;
	this.buoy.setBoundingBox(3,20,28,29);
	this.buoy.game_object = this;

	this.grounded=false;
};
GO_SkySeed.spriteImg = SDS_Load_Image("art/sheets/sprite/skyseed.png");
GO_SkySeed.spriteSheet = new TileSheet(5, 5, 2, 0, GO_SkySeed.spriteImg);

GO_SkySeed.prototype = new GameObject(
	new Sprite(
		GO_SkySeed.spriteSheet,
		"SkySeed"
	),
	"SkySeed"
	);

	GO_SkySeed.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.TRIGGER;

	GO_SkySeed.prototype.triggerEvent = function(sourceGO){
		if (GLOBAL_DIARY.check(3,"DRAINED_SEED")) return null; // fixed in place once in the roots

		if (!this.grounded && sourceGO.sprite){
			if (sourceGO.sprite.x+sourceGO.sprite.center.x<this.sprite.x+this.sprite.center.x){
				this.sprite.move(1,0,this._heap.spriteMoveRes);
				this.buoy.x = this.sprite.x;
				this.frame_timer = 16;
			}
			else if (sourceGO.sprite.x+sourceGO.sprite.center.x>this.sprite.x+this.sprite.center.x){
				this.sprite.move(-1,0,this._heap.spriteMoveRes);
				this.buoy.x = this.sprite.x;
				this.frame_timer = 48;
			}

			GLOBAL_SCRATCHPAD.note("SHRINE3_SEED_X",this.sprite.x);
			GLOBAL_SCRATCHPAD.note("SHRINE3_SEED_Y",this.sprite.y);
		}
	};

	GO_SkySeed.prototype.Update = function(){
		if (GLOBAL_DIARY.check(3,"DRAINED_SEED")) return null; // fixed in place once in the roots

		if (typeof(GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_X"))!=="undefined"){
			this.sprite.x = GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_X");
		}
		if (typeof(GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_Y"))!=="undefined"){
			this.sprite.y = GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_Y");
		}

		this.buoy.x = this.sprite.x;
		this.buoy.y = this.sprite.y;
		if (!this.buoy.isTouchingTerrain(DEFINE_MAP_TERRAIN_TYPES.AIR)){
			this.grounded=false;
			this.sprite.move(0,-1,this._heap.spriteMoveRes);
			this.buoy.move(0,-1,this._heap.spriteMoveRes);
			this.sprite.y = Math.max(this.sprite.y,this.buoy.y);
		}
		else{
			this.sprite.move(0,5,this._heap.spriteMoveRes);
			this.grounded = (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_V)!=0;
			if (this.grounded) this.frame_timer=0;
		}
		this.buoy.y = this.sprite.y;

		this._checkevent = this.grounded ? "GD_SkySeed" : "GD_SkySeed_float";

		if (!this.grounded){
			++this.frame_timer;
			this.frame_timer &= 63;
		}
		this.sprite.frame = this.frame_timer>>4;

		// core puzzle element; location must persist after room exit
		GLOBAL_SCRATCHPAD.note("SHRINE3_SEED_X",this.sprite.x);
		GLOBAL_SCRATCHPAD.persist("SHRINE3_SEED_X");
		GLOBAL_SCRATCHPAD.note("SHRINE3_SEED_Y",this.sprite.y);
		GLOBAL_SCRATCHPAD.persist("SHRINE3_SEED_Y");
	};
