/*
Death's door.  Specifically, Death's back door.  The one he chucks you out when he doesn't want you.
*/
"use strict";

var GO_DeathsDoor = function(){
	this.sprite = new Sprite(GO_DeathsDoor.spriteSheet,"Death's Door");
	
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(0,0,15,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.target_room = null;
	this.relocate_pos = null;

	this.unlocked=GLOBAL_DIARY.check(0,"MET_DEATH");

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

	// snippets adapted from Diary and Main to find the point to return to
	var currentdiary = "";
	var i=0;
	for (i=0; i<GLOBAL_DIARY.narrative.length; i++){
		if (GLOBAL_DIARY.narrative[i].chaptergory=="DIARIES"){
			currentdiary = GLOBAL_DIARY.narrative[i].eventname;
		}
	}
	// if there's no save, or we no longer can reach the last save,
	// look for the last diary touched, or that failing, 
	// look for the furthest diary we're allowed to access
	if (currentdiary=="" || !DEFINE_DIARY_EVENTS.DIARIES[currentdiary].requisites(GLOBAL_DIARY)){
		currentdiary = GLOBAL_DIARY.tagged;
	}
	if (currentdiary=="" || !DEFINE_DIARY_EVENTS.DIARIES[currentdiary].requisites(GLOBAL_DIARY)){
		//for (var d in DEFINE_DIARY_EVENTS.DIARIES){
		for (i=0; i<DEFINE_DIARY_ORDER.length; ++i){
			var d = DEFINE_DIARY_ORDER[i];
			if (DEFINE_DIARY_EVENTS.DIARIES[d].requisites(GLOBAL_DIARY)){
				currentdiary=d;
			}
		}
	}
	if (currentdiary!=""){
		this.target_room = DEFINE_DIARY_EVENTS.DIARIES[currentdiary].room;
		var room = SDS_MAP_ROOMS[this.target_room];
		var obj = null;
		for (i = 0; i<room.object_initializers.length; i++){
			obj = room.object_initializers[i](room);
			if (obj){
				if (obj.name=="Diary_"+currentdiary){
					this.relocate_pos = {x:obj.sprite.x, y:obj.sprite.y};
				}
			}
		}
		room.removeAllSprites();
	}
	if (!this.target_room || !this.relocate_pos){
		// Final fallback: respawn at home.
		// Will effectively softlock if in the middle of the ch4 final escape,
		// or ch5 mountain catacombs, so be sure those cases really properly work.
		this.target_room = "village_caves";
		this.relocate_pos = {
			x: 22<<4,
			y: 15<<4
		};
	}
	// HACK if Ravi is killed in the ending,
	// you can't get back to/from ANY diary.
	// Respawn in the fight room.
	if (GLOBAL_DIARY.check(5,"RAVI_KILL")){
		this.target_room = "underdark_seal";
		this.relocate_pos = {
			x: (18<<4)+8,
			y: 5<<4
		};
	}
};
GO_DeathsDoor.spriteImg = SDS_Load_Image("art/sheets/sprite/deathsdoor.png");
GO_DeathsDoor.spriteSheet = new TileSheet(4, 4, 2, 0, GO_DeathsDoor.spriteImg);

GO_DeathsDoor.prototype = new GameObject(
	new Sprite(
		GO_DeathsDoor.spriteSheet,
		"Death's Door"
	),
	"Death's Door"
	);

	GO_DeathsDoor.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.PORT;

	GO_DeathsDoor.prototype.getDestName = function(){
		return this.unlocked ? this.target_room : null;
	};
	GO_DeathsDoor.prototype.relocate = function(sprite){
		sprite.x = this.relocate_pos.x;
		sprite.y = this.relocate_pos.y;
		GLOBAL_DIARY.life = GLOBAL_DIARY.max_life;
	};

	GO_DeathsDoor.prototype.Update = function(){
		this.frame_timer += this.unlocked ? 1 : 0;
		this.frame_timer &= 63;
		this.sprite.frame = this.unlocked ? 0 : 3;

		if (this.unlocked && this.frame_timer&32){
			this.sprite.frame++;
			if ((this.frame_timer&16 && !(this.frame_timer&8)) || (this.frame_timer&8 && !(this.frame_timer&16))) this.sprite.frame++;
		}
	};

