/*
 * BallPhysicsComponent.cpp
 *
 *  Created on: Aug 9, 2019
 *      Author: fredrick
 */

#include "BallPhysicsComponent.h"

namespace JamSpook {

BallPhysicsComponent::BallPhysicsComponent(mat4 transform,
                                           shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
                                           weak_ptr<BroadcastObservable<const milliseconds> > physicsSystem,
                                           weak_ptr<BroadcastObservable<shared_ptr<CollisionStateChangeMessage> > > physicsCollisionSubSystem,
                                           weak_ptr<BroadcastObservable<shared_ptr<ColliderQueryMessage> > > physicsColliderQuerySubSystem,
                                           shared_ptr<Collider> collider):
        PhysicsComponent(transform,
                         entity,
                         physicsSystem,
                         physicsCollisionSubSystem,
                         physicsColliderQuerySubSystem,
                         collider)
{}

BallPhysicsComponent::~BallPhysicsComponent()
{}

void BallPhysicsComponent::update(const milliseconds dtms)
{
//    Log::write(LogLevel::trace, "CollectablePhysicsComponent - update");
    PhysicsComponent::update(dtms);

    if (getPosition().y < -100.0f)
    {
    	setPosition(vec3(getFloatInRange(-10.0f, 10.0f),
                		 10, /// above the ball
						 getFloatInRange(-10.0f, 10.0f)));
    }
}

void BallPhysicsComponent::onICCMessage(shared_ptr<Message> message)
{
    PhysicsComponent::onICCMessage(message);
}

void BallPhysicsComponent::onCollision(const string& tag)
{
    if (tag == "ground-plane")
    {
        vec3 position = getPosition();
        setPosition(vec3(position.x, 10.0f, position.z));
        ICCBroadcast(make_shared<Message>(IDCache::get("GroundContactMessage")));
    }
}

} // namespace JamSpook
