void
ColliderEntity::update(float d)
{
	if (!m_init) {
		Game::SharedColliderComponent l_collider_component =
		    new Game::ColliderComponent("static", *this);
		l_collider_component->active() = false;
		pushComponent(l_collider_component.staticCast<Game::IComponent>());
		m_init = true;
	}

	Game::EntityBase::update(d);
}
void
Box2DColliderEntity::update(TIME d)
{
    if (!m_init) {
        Game::SharedSizeComponent l_size_component =
            getComponentType(Game::SizeComponent::Type()).staticCast<Game::SizeComponent>();
        if (!l_size_component) {
            MMERROR1("Collider entity requires a size component to be present");
            return;
        }

        Game::SharedBox2DComponent l_box2d_component =
            new Game::Box2DComponent("box2d", *this);
        l_box2d_component->bodyType() = b2_staticBody;
        l_box2d_component->size() = l_size_component->size();
        pushComponent(l_box2d_component.staticCast<Game::IComponent>());
        m_init = true;
    }

    Game::EntityBase::update(d);
}
void
PlayerEntity::update(float d)
{
	if (!m_init) {
		Game::SharedSizeComponent l_size_component =
		    getComponentType(Game::SizeComponent::Type()).staticCast<Game::SizeComponent>();
		if (!l_size_component) {
			MMERROR("Player entity requires a size component to be present");
			return;
		}

		m_animation_component =
		    new Game::AnimationComponent("animation", *this);

		m_animation_component->pushFrame("stand-left",  115, 12);
		m_animation_component->pushFrame("stand-left",  116, 4);
		m_animation_component->pushFrame("stand-right", 111, 12);
		m_animation_component->pushFrame("stand-right", 112, 4);

		m_animation_component->pushFrame("walk-left", 117, 1);
		m_animation_component->pushFrame("walk-left", 118, 1);
		m_animation_component->setFrameRate("walk-left", 16);
		m_animation_component->pushFrame("walk-right", 113, 1);
		m_animation_component->pushFrame("walk-right", 114, 1);
		m_animation_component->setFrameRate("walk-right", 16);

		m_animation_component->pushFrame("jump-left", 118, 1);
		m_animation_component->setFrameRate("jump-left", 1);
		m_animation_component->pushFrame("jump-right", 113, 1);
		m_animation_component->setFrameRate("jump-right", 1);

		m_animation_component->pushFrame("dying", 126, 1);
		m_animation_component->pushFrame("dying", 127, 1);

		pushComponent(m_animation_component.staticCast<Game::IComponent>());

		/* movement component */
		m_movement_component =
		    new Game::MovementComponent("movement", *this);
		pushComponent(m_movement_component.staticCast<Game::IComponent>());

		/* collider component */
		m_collider_component =
		    new ActorColliderComponent("collider", *this);
		pushComponent(m_collider_component.staticCast<Game::IComponent>());

		/* input component */
		m_input_component = new InputComponent("input", *this);
		pushComponent(m_input_component.staticCast<Game::IComponent>());

		m_direction = -1;

		m_init = true;
	}

	/*
	 * XXX(gamaral): Most of this needs to be moved elsewhere.
	 */
	else if (m_input_component->isEnabled()) {
		/* make camera follow player */
		Game::SharedPositionComponent l_pos_component =
		    getComponentType(Game::PositionComponent::Type()).staticCast<Game::PositionComponent>();
		if (l_pos_component) {
			Math::Point2 l_pos = l_pos_component->position();
			const Math::Size2f &l_zoom = Graphics::Camera::Zoom();

			if (m_input_component->inMotion()) {
				const float l_lratio = m_input_component->linearRatio();
				m_animation_component->setPlaybackRatio(l_lratio < 0 ? -l_lratio : l_lratio);
			}
			else m_animation_component->setPlaybackRatio(1.f);

			void setFrameRate(const Core::Identifier &animation, float fps);
			/* camara snap - calculate using map */

			float l_limit;
			Game::SharedTilemapSceneLayer l_platform_layer =
			    Game::Engine::Instance()->sceneManager()->activeScene()->getLayer("platform").staticCast<Game::TilemapSceneLayer>();
			const Math::Size2f &l_hrsize = l_platform_layer->virtualHalfSize();

			l_limit = l_hrsize.width - (Graphics::Viewport::Size().width / (2.f * l_zoom.width));
			if (l_pos.x < -l_limit) l_pos.x = -l_limit;
			else if (l_pos.x > l_limit) l_pos.x = l_limit;
			l_limit = l_hrsize.height - (Graphics::Viewport::Size().height / (2.f * l_zoom.height));
			if (l_pos.y < -l_limit) l_pos.y = -l_limit;
			else if (l_pos.y > l_limit) l_pos.y = l_limit;

			Graphics::Camera::SetPosition(l_pos);

			/* translate background layers (parallax) */

			Game::SharedTilemapSceneLayer l_clouds =
			    Game::Engine::Instance()->sceneManager()->activeScene()->
			        getLayer("clouds").staticCast<Game::TilemapSceneLayer>();
			if (l_clouds) {
				if ((m_moving_sky += 8.f * d) > l_clouds->virtualSize().area())
					m_moving_sky = 8.f * d;
				l_clouds->setTranslation(Math::Vector2(m_moving_sky + (l_pos.x * .15f), m_moving_sky));
			}

			Game::SharedTilemapSceneLayer l_cloudbg =
			    Game::Engine::Instance()->sceneManager()->activeScene()->
			        getLayer("cloudbg").staticCast<Game::TilemapSceneLayer>();
			if (l_cloudbg) {
				if ((m_moving_sky_bg += 2.f * d) > l_cloudbg->virtualSize().area())
					m_moving_sky_bg = 2.f * d;
				l_cloudbg->setTranslation(Math::Vector2(m_moving_sky_bg + (l_pos.x * .05f), m_moving_sky_bg));
			}

		}

		/* update animation */
		switch(m_input_component->direction()) {

		case InputComponent::ICDLeft:
			if (m_direction == InputComponent::ICDLeft
			 && m_in_motion == m_input_component->inMotion()
			 && m_on_platform == m_collider_component->onPlatform())
				break;

			m_direction = InputComponent::ICDLeft;
			m_in_motion = m_input_component->inMotion();
			m_on_platform = m_collider_component->onPlatform();

			if (m_on_platform) {
				if (m_in_motion)
					m_animation_component->play("walk-left", true);
				else
					m_animation_component->play("stand-left", true);
			} else m_animation_component->play("jump-left", true);
			break;

		case InputComponent::ICDRight:
			if (m_direction == InputComponent::ICDRight
			 && m_in_motion == m_input_component->inMotion()
			 && m_on_platform == m_collider_component->onPlatform())
				break;

			m_direction = InputComponent::ICDRight;
			m_in_motion = m_input_component->inMotion();
			m_on_platform = m_collider_component->onPlatform();

			if (m_on_platform) {
				if (m_in_motion)
					m_animation_component->play("walk-right", true);
				else
					m_animation_component->play("stand-right", true);
			} else m_animation_component->play("jump-right", true);
			break;

		default: break;
		}
	}

	Game::EntityBase::update(d);
}