/*
* A solid platform-box which responds to Signals from Up/Down/Left/Right-named GOs
*/
"use strict";

var GO_RCPlatform = function(speed){
	speed = speed ? speed : 2;
	this.sprite = new Sprite(GO_RCPlatform.spriteSheet,"RCPlatform");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID |
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND;
	this.sprite.setBoundingBox(1,1,30,14);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.ridebox = new Sprite(null,"RCPlatform_ridebox");
	this.ridebox.setBoundingBox(1,0,30,14); // but extend when moving
	this.ridebox.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.ridebox.game_object = this;

	this._heap.riders = [];

	this.up=false;
	this.down=false;
	this.left=false;
	this.right=false;
	this.speed = speed;
};
GO_RCPlatform.spriteImg = SDS_Load_Image("art/sheets/sprite/skyshrine_platform.png");
GO_RCPlatform.spriteSheet = new TileSheet(5, 4, 0, 0, GO_RCPlatform.spriteImg);

GO_RCPlatform.prototype = new GameObject(
	new Sprite(
		GO_RCPlatform.spriteSheet,
		"RCPlatform"
	),
	"RCPlatform"
	);

	GO_RCPlatform.prototype.interactions =
		DEFINE_GAMEOBJECT_INTERACTIONS.SIGNALABLE;;

	GO_RCPlatform.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.ridebox);
		this.ridebox.x = this.sprite.x;
		this.ridebox.y = this.sprite.y;
	};

	GO_RCPlatform.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
		mapRoom.removeSprite(this.ridebox);
	};

	GO_RCPlatform.prototype.signal = function(sourceGO,direction){
		if (sourceGO.name.indexOf("Up")!==-1){
			this.up = direction>0;
		}
		if (sourceGO.name.indexOf("Down")!==-1){
			this.down = direction>0;
		}
		if (sourceGO.name.indexOf("Left")!==-1){
			this.left = direction>0;
		}
		if (sourceGO.name.indexOf("Right")!==-1){
			this.right = direction>0;
		}
		this.ridebox.setBoundingBox(
			(this.left || this.right) ? 0 : 1,
			0,
			(this.left || this.right) ? 31 : 30,
			(this.down) ? 15 : 14
			);
	};

	GO_RCPlatform.prototype.Update = function(){
		this.ridebox.getCollisions(this._heap.riders);

		this._heap.dx = 0;
		this._heap.dx += this.right ? 1 : 0;
		this._heap.dx += this.left ? -1 : 0;
		this._heap.dx *= this.speed;

		this._heap.dy = 0;
		this._heap.dy += this.down ? 1 : 0;
		this._heap.dy += this.up ? -1 : 0;
		this._heap.dy *= this.speed;

		
		for (this._heap.i=0; this._heap.i<this._heap.riders.length; ++this._heap.i){
			// don't move ourselves, other platforms, doors, or triggers.
			// could probably use a more explicit flag for mobs, but the need is not critical just yet
			if (!(this._heap.riders[this._heap.i].game_object instanceof GO_RCPlatform) && 
				!(this._heap.riders[this._heap.i].game_object.interactions & 
					(
						DEFINE_GAMEOBJECT_INTERACTIONS.TRIGGER |
						DEFINE_GAMEOBJECT_INTERACTIONS.PORT |
						DEFINE_GAMEOBJECT_INTERACTIONS.AUTOPORT
					)
				)){
				this._heap.riders[this._heap.i].move(this._heap.dx,this._heap.dy);
			}
		}
		// toggle active so we can bottom out correctly
		this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
		this.sprite.move(this._heap.dx,this._heap.dy);
		this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
		
		this.ridebox.x = this.sprite.x;
		this.ridebox.y = this.sprite.y;

	};
