"use strict";

/*
Developer storytime!
Skydragon's core mechanic is adapted from an innovative CAPCOM
NES title which happened to be a Disney license, and which,
as with all CAPCOM titles of the era, would have been a solid
and in this case innovative title independent of theme.
Back in my NES ROMhacking days (my earliest ones, at least),
I tore into this game's level data and worked out how the
background blocks and foreground objects were formatted.
This gave me free reign to modify the objects placed in a
level, not only changing their locations but changing the object's 
numeric ID/type, which determined what graphics, A.I. and other
behavior were connected to it.  The first few IDs were used
in the level I was looking at, the next few I assume were used 
in later levels since their behavior was consistent with 
enemies/objects from those levels but the graphics were garbage
(meaning the numeric index of the graphics tiles used by them
were used for something else by the current level).
Then I got out into weird and uncharted territory, where a
surprising number of the objects used the protagonist's sprite
(surprising because the protagonist was comprised of multiple
hardware sprites on multiple palettes arranged just-so, and
consequently nothing that wasn't explicitly written to use
them would have managed to look correct).  Some had behavior
of randomly swimming around in the background, others just
sat there frozen; the creepiest ones sat still in one position
but mirrored exactly what the player was doing cosmetically.
Oh, and some of them were benign while others inflicted damage,
and there was little rhyme or reason one way or the other.
So.
When I have a ghost level and need ghosts, what's creepier
than something which mimics the player visually even to the
point of contradicting its own direction of movement, and
which moves in a way that is not easy to pin down, but is
irrefutably player-seeking?  And which, even notwithstanding 
my own intent, only damages the player in certain deterministic
but obscure edge cases?
*/

var EN_Ghost = function(){
	this.sprite = new Sprite(EN_Ghost.spriteSheet,"Ghost");
	this.sprite.game_object = this; // no longer a generic GO

	this._basicInit(EN_Ghost.params);

	//-----

	this.sprite.collision_flags |= 0;//(DEFINE_SPRITE_COLLISION_FLAGS.GROUND | DEFINE_SPRITE_COLLISION_FLAGS.AIR);

	this.direction = 1;

	this.meli = null;
	this.meliFind=false; // she probably won't be in the map when we init; hopefully when we update

	this.accel_timer=0;
	this.accel_target = {x:-1, y:-1};
	this.damage_recoil=0;

	this.frame_timer=0;
};
EN_Ghost.spriteImg = SDS_Load_Image("art/sheets/sprite/protagonist.png");
EN_Ghost.spriteSheet = new TileSheet(4, 4, 3, 3, EN_Ghost.spriteImg);
EN_Ghost.params = {
	name: "Ghost",
	box: [2,0,13,15], // left, top, right, bottom
	max_step: 2,
	hp: 3,
	infection: 3,
};

EN_Ghost.prototype = new GO_Enemy();
	EN_Ghost.prototype.accel = [
		0,0,0,0,0,
		1,0,0,1,0,0,1,0,0,1,0,0,
		1,0,1,0,1,0,1,0,1,0,1,0,
		1,1,1,1,1,1,1,
		2,1,2,1,2,1,2,1,2,1,
		2,2,2,2,2,2,2,
		3,2,3,2,3,2,3,2,3,2,
		3];

	EN_Ghost.prototype.ai = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}
		if (this.meli){
			this.sprite.frame = this.meli.sprite.frame;
			if (this.meli.controls.held.left
				|| this.meli.controls.held.right
				|| this.meli.controls.held.up
				|| this.meli.controls.held.down){
				this.sprite.visible = !(this.frame_timer&1);
				if (this.accel_target.x<0 || this.accel_target.y<0){
					this.accel_target.x=(this.meli.sprite.x + this.sprite.x)>>1;
					this.accel_target.y=(this.meli.sprite.y + this.sprite.y)>>1;
				}
			}
			else{
				this.sprite.visible = !(this.frame_timer&31);
			}
		}
		if (this.damage_recoil!=0){
			this.sprite.visible = !(this.frame_timer&1);
			this.accel_timer=0;
			this.sprite.frame=27;
			if (this.damage_recoil>0) this.sprite.frame+=32;
			this.sprite.move(this.damage_recoil, 0);
			this.damage_recoil=(this.damage_recoil>0) ? this.damage_recoil-1 : this.damage_recoil+1;
		}
		else{
			if (this.accel_target.x>0 && this.accel_target.y>0){
				this.accel_timer=Math.min(this.accel_timer+1,this.accel.length-1);
				this._heap.diffx=this.accel_target.x-this.sprite.x;
				this._heap.diffy=this.accel_target.y-this.sprite.y;
				this._heap.absdiffx=Math.abs(this._heap.diffx);
				this._heap.absdiffy=Math.abs(this._heap.diffy);

				this._heap.movex=0;
				this._heap.movey=0;
				if (this._heap.absdiffx>this._heap.absdiffy){
					this._heap.movex = Math.min(this._heap.absdiffx,this.accel[this.accel_timer]);
					this._heap.movey = this._heap.absdiffy ? Math.floor(this._heap.movex * this._heap.absdiffy/this._heap.absdiffx) : 0;
				}
				else{
					this._heap.movey = Math.min(this._heap.absdiffy,this.accel[this.accel_timer]);
					this._heap.movex = this._heap.absdiffx ? Math.floor(this._heap.movey * this._heap.absdiffx/this._heap.absdiffy) : 0;
				}
				this._heap.movex *= (this._heap.diffx<0) ? -1 : 1;
				this._heap.movey *= (this._heap.diffy<0) ? -1 : 1;
				this.sprite.move(this._heap.movex, this._heap.movey);

				if (this._heap.absdiffx<=3 && this._heap.absdiffy<=3){
					this.accel_target.x=-1;
					this.accel_target.y=-1;
					//this.accel_timer=0;
				}
			}
			else{
				this.accel_timer=Math.max(this.accel_timer-1,0);
			}
		}

		if (!this.infection){
			this.sprite.visible=false;
		}

		

		this.toggleInteractionsStd();
		if (this.accel_target.x>=0 && this.accel_target.y>=0){
			this.interactions &= !DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE;
		}
		if (this.damage_recoil!=0){
			this.interactions &= !DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;
		}

		this.frame_timer++;
		this.frame_timer&=255;
		
	};
	EN_Ghost.prototype.damage = function(source,points){
		this.damage_recoil=8;
		if (source.sprite && source.sprite.x>this.sprite.x) this.damage_recoil*=-1;

		if (points>0){
			this.hp-=points;
			return (this.hp<=0);
		}
		else{
			this._heap.cough = new GO_Smoke();
			this._heap.cough.sprite.x = this.sprite.x;
			this._heap.cough.sprite.y = this.sprite.y;
			this._heap.cough.launch_dir.x=0;
			this._heap.cough.launch_dir.y=0;
			this._heap.cough.host_obj = this;

			this.sprite.map_room.addSprite(this._heap.cough.sprite);
			this.manager.add(this._heap.cough);

			this._heap.cough = null;

			this.infection--;
			return false;
		}
	};
	EN_Ghost.prototype._inflictVal = {hp:1, chi:16};
	EN_Ghost.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Ghost.prototype.drop = function(){
		//var dropRand = Math.random();
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.25){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


