#include "Salamander.h"
#include "Obstacle.h"

#include "CameraEvent.h"

Obstacle::Obstacle(std::string filename) : Platform(filename)
{
	// for wrapping
	events().receive<Obstacle, CameraEvent>(this, &Obstacle::onCameraEvent);
	
	// aaaaaaargh falling!
	speed_ = rand() % 300 + 200.0;
}

Obstacle::~Obstacle()
{
	
}

void Obstacle::frame()
{
	Platform::frame();

	velocity_.y_ = -speed_;
}

void Obstacle::onCameraEvent(const CameraEvent& event)
{
	// if we're outside bounds, wrap
	Vec2 translation = getTranslation();
	Vec2 lowerbound = event.getTranslation() - screen().getResolution() * 0.6;
	Vec2 upperbound = event.getTranslation() + screen().getResolution() * 0.6;

	if(translation.x_ < lowerbound.x_) translation.x_ = upperbound.x_;
	if(translation.x_ > upperbound.x_) translation.x_ = lowerbound.x_;
	if(translation.y_ < lowerbound.y_) translation.y_ = upperbound.y_;
	if(translation.y_ > upperbound.y_) translation.y_ = lowerbound.y_;

	setTranslation(translation);
}
