diff --git a/assets/meshes/kart-driver.bcosm b/assets/meshes/kart-driver.bcosm index 01225db..dcfc87a 100644 Binary files a/assets/meshes/kart-driver.bcosm and b/assets/meshes/kart-driver.bcosm differ diff --git a/assets/meshes/kart-hulls.bcosp b/assets/meshes/kart-hulls.bcosp index a23875b..c773a18 100644 Binary files a/assets/meshes/kart-hulls.bcosp and b/assets/meshes/kart-hulls.bcosp differ diff --git a/assets/meshes/kart-vehicle.bcosm b/assets/meshes/kart-vehicle.bcosm index 62e972f..46d272b 100644 Binary files a/assets/meshes/kart-vehicle.bcosm and b/assets/meshes/kart-vehicle.bcosm differ diff --git a/src/JamSpookGame.cpp b/src/JamSpookGame.cpp index ceead3c..1b2b8bf 100644 --- a/src/JamSpookGame.cpp +++ b/src/JamSpookGame.cpp @@ -242,17 +242,17 @@ void JamSpookGame::set() // delete floorFactory; // - // Create 2 jump180s - unique_ptr jump180Factory = make_unique( - dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), - graphicsSystem, - dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), - renderLayerGame); - - jump180Factory->createJump180(vec3(0, 0, 0), vec3(0, 0, 0)); - jump180Factory->createJump180(vec3(18,-10, 12), vec3(0, 180, 0)); - - jump180Factory.reset(); +// // Create 2 jump180s +// unique_ptr jump180Factory = make_unique( +// dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), +// graphicsSystem, +// dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), +// renderLayerGame); +// +// jump180Factory->createJump180(vec3(0, 0, 0), vec3(0, 0, 0)); +// jump180Factory->createJump180(vec3(18,-10, 12), vec3(0, 180, 0)); +// +// jump180Factory.reset(); // Create the goal unique_ptr goalFactory = make_unique( @@ -261,7 +261,8 @@ void JamSpookGame::set() dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), renderLayerGame); - goalFactory->createGoal(vec3(0, -25, -10)); +// goalFactory->createGoal(vec3(0, -25, -10)); + goalFactory->createGoal(vec3(0, 0, 0)); goalFactory.reset(); @@ -285,6 +286,7 @@ void JamSpookGame::set() dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), graphicsSystem, dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), + dynamic_pointer_cast(findSystem(IDCache::get("InteractionSystem"))), renderLayerGame); kartFactory->createKart(vec3(0, 3, 0), // position diff --git a/src/entities/KartFactory.cpp b/src/entities/KartFactory.cpp index 78a419d..8d64228 100644 --- a/src/entities/KartFactory.cpp +++ b/src/entities/KartFactory.cpp @@ -9,13 +9,15 @@ namespace JamSpook { -KartFactory::KartFactory(weak_ptr assetSystem, - weak_ptr graphicsSystem, - weak_ptr physicsSystem, - weak_ptr renderLayer): +KartFactory::KartFactory(weak_ptr assetSystem, + weak_ptr graphicsSystem, + weak_ptr physicsSystem, + weak_ptr interactionSystem, + weak_ptr renderLayer): mAssetSystem(assetSystem), mGraphicsSystem(graphicsSystem), mPhysicsSystem(physicsSystem), + mInteractionSystem(interactionSystem), mRenderLayer(renderLayer) {} @@ -98,6 +100,13 @@ shared_ptr KartFactory::createKart(const vec3 position, graphicsComponent->setLightSource(lightSource); delete lightSourceFactory; + // Add interaction component + shared_ptr interactionComponent = + make_shared(transform, + entity, + getShared(mInteractionSystem)); + entity->addComponent(interactionComponent); + // Return instance return entity; } diff --git a/src/entities/KartFactory.h b/src/entities/KartFactory.h index 7888ead..9a9f2f4 100644 --- a/src/entities/KartFactory.h +++ b/src/entities/KartFactory.h @@ -55,7 +55,10 @@ #include #include +#include + #include "KartPhysicsComponent.h" +#include "KartInteractionComponent.h" namespace JamSpook { @@ -110,6 +113,8 @@ using ecos::graphics::ModelRenderable; using ecos::graphics::lighting::LightSource; using ecos::graphics::lighting::LightSourceFactory; +using ecos::interaction::InteractionSystem; + using ecos::physics::PhysicsSystem; using ecos::physics::PhysicsComponent; using ecos::physics::ColliderFactory; @@ -119,16 +124,18 @@ using ecos::physics::Collider; class KartFactory { private: - weak_ptr mAssetSystem; - weak_ptr mGraphicsSystem; - weak_ptr mPhysicsSystem; - weak_ptr mRenderLayer; + weak_ptr mAssetSystem; + weak_ptr mGraphicsSystem; + weak_ptr mPhysicsSystem; + weak_ptr mInteractionSystem; + weak_ptr mRenderLayer; public: - KartFactory(weak_ptr assetSystem, - weak_ptr graphicsSystem, - weak_ptr physicsSystem, - weak_ptr renderLayer); + KartFactory(weak_ptr assetSystem, + weak_ptr graphicsSystem, + weak_ptr physicsSystem, + weak_ptr interactionSystem, + weak_ptr renderLayer); virtual ~KartFactory(); /// Compose a kart entity diff --git a/src/entities/KartInteractionComponent.cpp b/src/entities/KartInteractionComponent.cpp new file mode 100755 index 0000000..415b78a --- /dev/null +++ b/src/entities/KartInteractionComponent.cpp @@ -0,0 +1,114 @@ +/* + * KartInteractionComponent.cpp + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#include "KartInteractionComponent.h" + +namespace JamSpook { + +KartInteractionComponent::KartInteractionComponent(mat4 transform, + shared_ptr > > entity, + weak_ptr interactionSystem): + InteractionComponent(transform, entity, interactionSystem), + mOldDirection(vec3(0)), + mIsOnGround(false) +{} + +KartInteractionComponent::~KartInteractionComponent() +{} + +void KartInteractionComponent::onInputDeviceStateChange(const InputDeviceState inputDeviceState) +{ + Log::write(LogLevel::debug, "KartInteractionComponent - onInputDeviceStateChange"); + + vec3 accumulatedDirection = vec3(0); + + // Handle controls + if (inputDeviceState.left == ButtonState::pressed || inputDeviceState.left == ButtonState::held) + { + accumulatedDirection += vec3(-1.0f, 0, 0); + } + else if (inputDeviceState.left == ButtonState::released) + { + accumulatedDirection -= vec3(-1.0f, 0, 0); + } + + if (inputDeviceState.right == ButtonState::pressed || inputDeviceState.right == ButtonState::held) + { + accumulatedDirection += vec3(1.0f, 0, 0); + } + else if (inputDeviceState.right == ButtonState::released) + { + accumulatedDirection -= vec3(1.0f, 0, 0); + } + + if (inputDeviceState.up == ButtonState::pressed || inputDeviceState.up == ButtonState::held) + { + accumulatedDirection += vec3(0, 0, 1.0f); + } + else if (inputDeviceState.up == ButtonState::released) + { + accumulatedDirection -= vec3(0, 0, 1.0f); + } + + if (inputDeviceState.down == ButtonState::pressed || inputDeviceState.down == ButtonState::held) + { + accumulatedDirection += vec3(0, 0, -1.0f); + } + else if (inputDeviceState.down == ButtonState::released) + { + accumulatedDirection -= vec3(0, 0, -1.0f); + } + + + if (inputDeviceState.action == ButtonState::pressed) + { + // if on ground (can jump) + if (mIsOnGround) + { + // Send jump message, containing current position + ICCBroadcast(dynamic_pointer_cast( + make_shared(getTranslation(mTransform)))); + } + } + + if (inputDeviceState.up == ButtonState::neutral && + inputDeviceState.down == ButtonState::neutral && + inputDeviceState.left == ButtonState::neutral && + inputDeviceState.right == ButtonState::neutral) + { + accumulatedDirection = vec3(0); + } + + if (accumulatedDirection != mOldDirection) + { + ICCBroadcast(dynamic_pointer_cast( + make_shared( + getTranslation(mTransform), + mOldDirection, + accumulatedDirection))); + } + mOldDirection = accumulatedDirection; +} + +void KartInteractionComponent::onICCMessage(shared_ptr message) +{ + InteractionComponent::onICCMessage(message); + + // Jump message, make the kart jump + if (message->getMessageType() == IDCache::get("GroundCollisionMessage")) + { + Log::write(LogLevel::debug, "KartInteractionComponent - onICCMessage - GroundCollisionMessage"); + mIsOnGround = true; + } + if (message->getMessageType() == IDCache::get("GroundSeparationMessage")) + { + Log::write(LogLevel::debug, "KartInteractionComponent - onICCMessage - GroundSeparationMessage"); + mIsOnGround = false; + } +} + +} // namespace JamSpook diff --git a/src/entities/KartInteractionComponent.h b/src/entities/KartInteractionComponent.h new file mode 100755 index 0000000..1969e27 --- /dev/null +++ b/src/entities/KartInteractionComponent.h @@ -0,0 +1,87 @@ +/* + * KartInteractionComponent.h + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#ifndef KARTINTERACTIONCOMPONENT_H_ +#define KARTINTERACTIONCOMPONENT_H_ + +#include +#include +#include + +#define GLM_FORCE_RADIANS +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include "../messages/DirectionChangeMessage.h" +#include "../messages/JumpMessage.h" + +namespace JamSpook { + +using std::chrono::milliseconds; +using std::shared_ptr; +using std::make_shared; +using std::weak_ptr; +using std::dynamic_pointer_cast; + +using glm::vec3; +using glm::mat4; +using glm::translate; +using glm::rotate; +using glm::radians; + +using ecos::utility::getTranslation; + +using ecos::core::IDCache; +using ecos::core::logging::Log; +using ecos::core::logging::LogLevel; +using ecos::core::BroadcastObservable; +using ecos::core::Message; + +using ecos::physics::TransformChangeMessage; + +using ecos::interaction::InteractionSystem; +using ecos::interaction::InputDeviceState; +using ecos::interaction::ButtonState; +using ecos::interaction::InteractionComponent; + +using messages::DirectionChangeMessage; +using messages::JumpMessage; + +/// Kart interaction component +/// entity specific interaction system code +class KartInteractionComponent : + public InteractionComponent +{ +private: + vec3 mOldDirection; ///< Used to construct the DirectionChangeMessaage + bool mIsOnGround; + +public: + KartInteractionComponent(mat4 transform, + shared_ptr > > entity, + weak_ptr wInteractionSystem); + virtual ~KartInteractionComponent(); + + virtual void onInputDeviceStateChange(const InputDeviceState inputDeviceState); + virtual void onICCMessage(shared_ptr message); +}; + +} // namespace JamSpook + +#endif // KARTINTERACTIONCOMPONENT_H_ diff --git a/src/entities/KartPhysicsComponent.cpp b/src/entities/KartPhysicsComponent.cpp index aef2ca9..0ec58c6 100644 --- a/src/entities/KartPhysicsComponent.cpp +++ b/src/entities/KartPhysicsComponent.cpp @@ -37,20 +37,56 @@ void KartPhysicsComponent::update(const milliseconds dtms) 10, /// above the ball getFloatInRange(-10.0f, 10.0f))); } + + 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 + + } } 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 == "ground-plane") + if (tag == "goal") { - vec3 position = getPosition(); - setPosition(vec3(position.x, 10.0f, position.z)); - ICCBroadcast(make_shared(IDCache::get("GroundContactMessage"))); + ICCBroadcast(make_shared(IDCache::get("GroundCollisionMessage"))); + } +} + +void KartPhysicsComponent::onSeparation(const string& tag) +{ + if (tag == "goal") + { + ICCBroadcast(make_shared(IDCache::get("GroundSeparationMessage"))); } } diff --git a/src/entities/KartPhysicsComponent.h b/src/entities/KartPhysicsComponent.h index e45469c..4dd6eac 100644 --- a/src/entities/KartPhysicsComponent.h +++ b/src/entities/KartPhysicsComponent.h @@ -15,7 +15,9 @@ #include #include +#include +#include #include #include @@ -30,6 +32,9 @@ #include #include +#include "../messages/DirectionChangeMessage.h" +#include "../messages/JumpMessage.h" + namespace JamSpook { using std::chrono::milliseconds; @@ -40,10 +45,14 @@ using std::dynamic_pointer_cast; using std::string; using glm::vec3; +using glm::vec4; +using glm::mat3; using glm::mat4; using glm::translate; using glm::scale; +using glm::rotate; +using ecos::utility::getDirection; using ecos::utility::getFloatInRange; using ecos::core::IDCache; @@ -59,10 +68,16 @@ using ecos::physics::TransformChangeMessage; using ecos::physics::CollisionStateChangeMessage; using ecos::physics::ColliderQueryMessage; +using messages::DirectionChangeMessage; +using messages::JumpMessage; + /// class KartPhysicsComponent: public ecos::physics::PhysicsComponent { +private: + vec3 mDirection; + public: KartPhysicsComponent(mat4 transform, shared_ptr > > entity, @@ -75,6 +90,7 @@ public: virtual void update(const milliseconds dtms); virtual void onICCMessage(shared_ptr message); virtual void onCollision(const string& tag); + virtual void onSeparation(const string& tag); }; } // namespace JamSpook diff --git a/src/messages/JumpMessage.cpp b/src/messages/JumpMessage.cpp new file mode 100755 index 0000000..8986008 --- /dev/null +++ b/src/messages/JumpMessage.cpp @@ -0,0 +1,28 @@ +/* + * JumpMessage.cpp + * + * Created on: Oct 20, 2018 + * Author: fredrick + */ + +#include "JumpMessage.h" + +namespace JamSpook { +namespace messages { + + +JumpMessage::JumpMessage(const vec3 position): + Message(IDCache::add("JumpMessage")), + mPosition(position) +{} + +JumpMessage::~JumpMessage() +{} + +const vec3 JumpMessage::getPosition() const +{ + return mPosition; +} + +} // namespace messages +} // namespace JamSpook diff --git a/src/messages/JumpMessage.h b/src/messages/JumpMessage.h new file mode 100755 index 0000000..f14d237 --- /dev/null +++ b/src/messages/JumpMessage.h @@ -0,0 +1,41 @@ +/* + * JumpMessage.h + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#ifndef JUMPMESSAGE_H_ +#define JUMPMESSAGE_H_ + +#include + +#include +#include + +namespace JamSpook { +namespace messages { + +using glm::vec3; +using ecos::core::IDCache; +using ecos::core::Message; + +/// Jump message +/// Used to inform systems about a jump action being initiated +class JumpMessage : + public Message +{ +private: + vec3 mPosition; ///< Where the jump occured + +public: + JumpMessage(const vec3 position); + virtual ~JumpMessage(); + + const vec3 getPosition() const; +}; + +} // namespace messages +} // namespace JamSpook + +#endif // JUMPMESSAGE_H_