/* * KartPhysicsComponent.cpp * * Created on: Aug 19, 2020 * Author: fredrick */ #include "KartPhysicsComponent.h" namespace JamSpook { KartPhysicsComponent::KartPhysicsComponent(mat4 transform, shared_ptr > > entity, weak_ptr > physicsSystem, weak_ptr > > physicsCollisionSubSystem, weak_ptr > > physicsColliderQuerySubSystem, shared_ptr collider): PhysicsComponent(transform, entity, physicsSystem, physicsCollisionSubSystem, physicsColliderQuerySubSystem, collider), mInitialTransform(transform) {} KartPhysicsComponent::~KartPhysicsComponent() {} void KartPhysicsComponent::update(const milliseconds dtms) { // Log::write(LogLevel::trace, "CollectablePhysicsComponent - update"); PhysicsComponent::update(dtms); if (getPosition().y < -100.0f) { setPosition(vec3(0, 0, 0), true); setTransform(mInitialTransform); } if (glm::length(mDirection) > 0) { // Forward or backwards force mCollider->applyForce(mat3(mTransform) * vec3(0, 0, mDirection.z * 500), vec3(0, 0, 0)); // Turning torque mCollider->applyTorque(mat3(mTransform) * vec3(0, mDirection.x * 500, 0)); } } void KartPhysicsComponent::onICCMessage(shared_ptr message) { PhysicsComponent::onICCMessage(message); // DirectionChangeMessage if (message->getMessageType() == IDCache::get("DirectionChangeMessage")) { Log::write(LogLevel::debug, "KartPhysicsComponent - onICCMessage - DirectionChangeMessage - getNewDirection: " + glm::to_string(mDirection)); shared_ptr msg = dynamic_pointer_cast(message); mDirection = msg->getNewDirection(); } // Jump message, make the kart jump if (message->getMessageType() == IDCache::get("JumpMessage")) { Log::write(LogLevel::debug, "KartPhysicsComponent - onICCMessage - JumpMessage"); // shared_ptr msg = dynamic_pointer_cast(message); // Does not use any data in message, skip casting, but leave code as reference mCollider->applyImpulse(vec3(0, 1000, 0), vec3(0, 0, 0)); } } void KartPhysicsComponent::onCollision(const string& tag) { if (tag == "goal" || tag == "jump180") { ICCBroadcast(make_shared(IDCache::get("GroundCollisionMessage"))); } if (tag == "reset-trigger") { // ICCBroadcast(make_shared(IDCache::get("GroundCollisionMessage"))); Log::write(LogLevel::debug, "KartPhysicsComponent - onCollision: reset-trigger"); setPosition(vec3(0, 3, 0), true); setOrientationEulerDegreesXYZ(vec3(0, 180, 0), true); } } void KartPhysicsComponent::onSeparation(const string& tag) { if (tag == "goal" || tag == "jump180") { ICCBroadcast(make_shared(IDCache::get("GroundSeparationMessage"))); } } } // namespace JamSpook