/*
* A solid fixed platform-block with an auto-move collider on top
*/
"use strict";

var GO_Roller = function(direction,width){
	this.width = width ? width : 1;
	this.sprite = new Sprite(null,"Roller");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
	this.sprite.setBoundingBox(0,0,(this.width<<4)-1,15);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.ridebox = new Sprite(null,"Roller_ridebox");
	this.ridebox.setBoundingBox(0,-1,(this.width<<4)-1,0); // negative bounding box.  Engine should handle it...
	this.ridebox.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.ridebox.game_object = this;

	// if we have adjacent 1-block roller objects, their effects will stack.
	// so for smooth mechanics, we need multi-block roller single-objects
	this.rollers = [];
	for (this._heap.i=0; this._heap.i<this.width; ++this._heap.i){
		this.rollers.push(new Sprite(GO_Roller.spriteSheet,"RollerSegment"));
		this.rollers[this._heap.i].setBoundingBox(0,0,15,15);
		this.rollers[this._heap.i].collision_flags = 0;
		this.rollers[this._heap.i].game_object = this;
	}

	this._heap.riders = [];

	this.direction = direction ? direction : 0;
	this.frame_timer = 0;
	this.frame = 0;
};
GO_Roller.spriteImg = SDS_Load_Image("art/sheets/sprite/roller.png");
GO_Roller.spriteSheet = new TileSheet(4, 4, 2, 0, GO_Roller.spriteImg);

GO_Roller.prototype = new GameObject(
	new Sprite(
		GO_Roller.spriteSheet,
		"Roller"
	),
	"Roller"
	);

	GO_Roller.prototype.interactions = 0;

	GO_Roller.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.ridebox);
		this.ridebox.x = this.sprite.x;
		this.ridebox.y = this.sprite.y;
		for (this._heap.i=0; this._heap.i<this.width; ++this._heap.i){
			mapRoom.addSprite(this.rollers[this._heap.i]);
			this.rollers[this._heap.i].x = this.sprite.x + (this._heap.i<<4);
			this.rollers[this._heap.i].y = this.sprite.y;
		}
	}

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

		for (this._heap.i=0; this._heap.i<this._heap.riders.length; ++this._heap.i){
			// don't move ourselves, other swings, 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_Roller) && 
				!(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.direction, 0);
			}
		}

		this.frame_timer+=this.direction;
		while (this.frame_timer>=4){
			this.frame++;
			this.frame_timer-=4;
		}
		while (this.frame_timer<=-4){
			this.frame--;
			this.frame_timer+=4;
		}
		this.frame &= 3;
		for (this._heap.i=0; this._heap.i<this.width; ++this._heap.i){
			this.rollers[this._heap.i].frame = this.frame;
		}

	}



