JAMSPOOK-13 - Acceleration and reverse

- Can now accelerate and reverse in the direction the car is pointing in.
This commit is contained in:
Fredrick Amnehagen 2020-08-19 20:56:14 +02:00
parent 4aed7f66e1
commit d4a8e9d908
12 changed files with 368 additions and 28 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -242,17 +242,17 @@ void JamSpookGame::set()
// delete floorFactory; // delete floorFactory;
// //
// Create 2 jump180s // // Create 2 jump180s
unique_ptr<Jump180Factory> jump180Factory = make_unique<Jump180Factory>( // unique_ptr<Jump180Factory> jump180Factory = make_unique<Jump180Factory>(
dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))), // dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))),
graphicsSystem, // graphicsSystem,
dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))), // dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))),
renderLayerGame); // renderLayerGame);
//
jump180Factory->createJump180(vec3(0, 0, 0), vec3(0, 0, 0)); // jump180Factory->createJump180(vec3(0, 0, 0), vec3(0, 0, 0));
jump180Factory->createJump180(vec3(18,-10, 12), vec3(0, 180, 0)); // jump180Factory->createJump180(vec3(18,-10, 12), vec3(0, 180, 0));
//
jump180Factory.reset(); // jump180Factory.reset();
// Create the goal // Create the goal
unique_ptr<GoalFactory> goalFactory = make_unique<GoalFactory>( unique_ptr<GoalFactory> goalFactory = make_unique<GoalFactory>(
@ -261,7 +261,8 @@ void JamSpookGame::set()
dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))), dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))),
renderLayerGame); renderLayerGame);
goalFactory->createGoal(vec3(0, -25, -10)); // goalFactory->createGoal(vec3(0, -25, -10));
goalFactory->createGoal(vec3(0, 0, 0));
goalFactory.reset(); goalFactory.reset();
@ -285,6 +286,7 @@ void JamSpookGame::set()
dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))), dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))),
graphicsSystem, graphicsSystem,
dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))), dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))),
dynamic_pointer_cast<InteractionSystem>(findSystem(IDCache::get("InteractionSystem"))),
renderLayerGame); renderLayerGame);
kartFactory->createKart(vec3(0, 3, 0), // position kartFactory->createKart(vec3(0, 3, 0), // position

View File

@ -9,13 +9,15 @@
namespace JamSpook { namespace JamSpook {
KartFactory::KartFactory(weak_ptr<AssetSystem> assetSystem, KartFactory::KartFactory(weak_ptr<AssetSystem> assetSystem,
weak_ptr<GraphicsSystem> graphicsSystem, weak_ptr<GraphicsSystem> graphicsSystem,
weak_ptr<PhysicsSystem> physicsSystem, weak_ptr<PhysicsSystem> physicsSystem,
weak_ptr<RenderLayer> renderLayer): weak_ptr<InteractionSystem> interactionSystem,
weak_ptr<RenderLayer> renderLayer):
mAssetSystem(assetSystem), mAssetSystem(assetSystem),
mGraphicsSystem(graphicsSystem), mGraphicsSystem(graphicsSystem),
mPhysicsSystem(physicsSystem), mPhysicsSystem(physicsSystem),
mInteractionSystem(interactionSystem),
mRenderLayer(renderLayer) mRenderLayer(renderLayer)
{} {}
@ -98,6 +100,13 @@ shared_ptr<Entity> KartFactory::createKart(const vec3 position,
graphicsComponent->setLightSource(lightSource); graphicsComponent->setLightSource(lightSource);
delete lightSourceFactory; delete lightSourceFactory;
// Add interaction component
shared_ptr<KartInteractionComponent> interactionComponent =
make_shared<KartInteractionComponent>(transform,
entity,
getShared(mInteractionSystem));
entity->addComponent(interactionComponent);
// Return instance // Return instance
return entity; return entity;
} }

View File

@ -55,7 +55,10 @@
#include <ecos/graphics/ModelRenderableFactory.h> #include <ecos/graphics/ModelRenderableFactory.h>
#include <ecos/graphics/ShaderType.h> #include <ecos/graphics/ShaderType.h>
#include <ecos/interaction/InteractionSystem.h>
#include "KartPhysicsComponent.h" #include "KartPhysicsComponent.h"
#include "KartInteractionComponent.h"
namespace JamSpook { namespace JamSpook {
@ -110,6 +113,8 @@ using ecos::graphics::ModelRenderable;
using ecos::graphics::lighting::LightSource; using ecos::graphics::lighting::LightSource;
using ecos::graphics::lighting::LightSourceFactory; using ecos::graphics::lighting::LightSourceFactory;
using ecos::interaction::InteractionSystem;
using ecos::physics::PhysicsSystem; using ecos::physics::PhysicsSystem;
using ecos::physics::PhysicsComponent; using ecos::physics::PhysicsComponent;
using ecos::physics::ColliderFactory; using ecos::physics::ColliderFactory;
@ -119,16 +124,18 @@ using ecos::physics::Collider;
class KartFactory class KartFactory
{ {
private: private:
weak_ptr<AssetSystem> mAssetSystem; weak_ptr<AssetSystem> mAssetSystem;
weak_ptr<GraphicsSystem> mGraphicsSystem; weak_ptr<GraphicsSystem> mGraphicsSystem;
weak_ptr<PhysicsSystem> mPhysicsSystem; weak_ptr<PhysicsSystem> mPhysicsSystem;
weak_ptr<RenderLayer> mRenderLayer; weak_ptr<InteractionSystem> mInteractionSystem;
weak_ptr<RenderLayer> mRenderLayer;
public: public:
KartFactory(weak_ptr<AssetSystem> assetSystem, KartFactory(weak_ptr<AssetSystem> assetSystem,
weak_ptr<GraphicsSystem> graphicsSystem, weak_ptr<GraphicsSystem> graphicsSystem,
weak_ptr<PhysicsSystem> physicsSystem, weak_ptr<PhysicsSystem> physicsSystem,
weak_ptr<RenderLayer> renderLayer); weak_ptr<InteractionSystem> interactionSystem,
weak_ptr<RenderLayer> renderLayer);
virtual ~KartFactory(); virtual ~KartFactory();
/// Compose a kart entity /// Compose a kart entity

View File

@ -0,0 +1,114 @@
/*
* KartInteractionComponent.cpp
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#include "KartInteractionComponent.h"
namespace JamSpook {
KartInteractionComponent::KartInteractionComponent(mat4 transform,
shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
weak_ptr<InteractionSystem> 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<Message>(
make_shared<JumpMessage>(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<Message>(
make_shared<messages::DirectionChangeMessage>(
getTranslation(mTransform),
mOldDirection,
accumulatedDirection)));
}
mOldDirection = accumulatedDirection;
}
void KartInteractionComponent::onICCMessage(shared_ptr<Message> 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

View File

@ -0,0 +1,87 @@
/*
* KartInteractionComponent.h
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#ifndef KARTINTERACTIONCOMPONENT_H_
#define KARTINTERACTIONCOMPONENT_H_
#include <chrono>
#include <memory>
#include <string>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <ecos/utility/Math.h>
#include <ecos/core/IDCache.h>
#include <ecos/core/Log.h>
#include <ecos/core/BroadcastObservable.h>
#include <ecos/core/Message.h>
#include <ecos/physics/TransformChangeMessage.h>
#include <ecos/interaction/InteractionSystem.h>
#include <ecos/interaction/InteractionComponent.h>
#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<BroadcastObservable<shared_ptr<Message> > > entity,
weak_ptr<InteractionSystem> wInteractionSystem);
virtual ~KartInteractionComponent();
virtual void onInputDeviceStateChange(const InputDeviceState inputDeviceState);
virtual void onICCMessage(shared_ptr<Message> message);
};
} // namespace JamSpook
#endif // KARTINTERACTIONCOMPONENT_H_

View File

@ -37,20 +37,56 @@ void KartPhysicsComponent::update(const milliseconds dtms)
10, /// above the ball 10, /// above the ball
getFloatInRange(-10.0f, 10.0f))); 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> message) void KartPhysicsComponent::onICCMessage(shared_ptr<Message> message)
{ {
PhysicsComponent::onICCMessage(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<DirectionChangeMessage> msg = dynamic_pointer_cast<DirectionChangeMessage>(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<JumpMessage> msg = dynamic_pointer_cast<JumpMessage>(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) void KartPhysicsComponent::onCollision(const string& tag)
{ {
if (tag == "ground-plane") if (tag == "goal")
{ {
vec3 position = getPosition(); ICCBroadcast(make_shared<Message>(IDCache::get("GroundCollisionMessage")));
setPosition(vec3(position.x, 10.0f, position.z)); }
ICCBroadcast(make_shared<Message>(IDCache::get("GroundContactMessage"))); }
void KartPhysicsComponent::onSeparation(const string& tag)
{
if (tag == "goal")
{
ICCBroadcast(make_shared<Message>(IDCache::get("GroundSeparationMessage")));
} }
} }

View File

@ -15,7 +15,9 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/string_cast.hpp>
#include <ecos/utility/Math.h>
#include <ecos/utility/Random.h> #include <ecos/utility/Random.h>
#include <ecos/core/IDCache.h> #include <ecos/core/IDCache.h>
@ -30,6 +32,9 @@
#include <ecos/physics/PhysicsComponent.h> #include <ecos/physics/PhysicsComponent.h>
#include <ecos/physics/Collider.h> #include <ecos/physics/Collider.h>
#include "../messages/DirectionChangeMessage.h"
#include "../messages/JumpMessage.h"
namespace JamSpook { namespace JamSpook {
using std::chrono::milliseconds; using std::chrono::milliseconds;
@ -40,10 +45,14 @@ using std::dynamic_pointer_cast;
using std::string; using std::string;
using glm::vec3; using glm::vec3;
using glm::vec4;
using glm::mat3;
using glm::mat4; using glm::mat4;
using glm::translate; using glm::translate;
using glm::scale; using glm::scale;
using glm::rotate;
using ecos::utility::getDirection;
using ecos::utility::getFloatInRange; using ecos::utility::getFloatInRange;
using ecos::core::IDCache; using ecos::core::IDCache;
@ -59,10 +68,16 @@ using ecos::physics::TransformChangeMessage;
using ecos::physics::CollisionStateChangeMessage; using ecos::physics::CollisionStateChangeMessage;
using ecos::physics::ColliderQueryMessage; using ecos::physics::ColliderQueryMessage;
using messages::DirectionChangeMessage;
using messages::JumpMessage;
/// ///
class KartPhysicsComponent: class KartPhysicsComponent:
public ecos::physics::PhysicsComponent public ecos::physics::PhysicsComponent
{ {
private:
vec3 mDirection;
public: public:
KartPhysicsComponent(mat4 transform, KartPhysicsComponent(mat4 transform,
shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity, shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
@ -75,6 +90,7 @@ public:
virtual void update(const milliseconds dtms); virtual void update(const milliseconds dtms);
virtual void onICCMessage(shared_ptr<Message> message); virtual void onICCMessage(shared_ptr<Message> message);
virtual void onCollision(const string& tag); virtual void onCollision(const string& tag);
virtual void onSeparation(const string& tag);
}; };
} // namespace JamSpook } // namespace JamSpook

28
src/messages/JumpMessage.cpp Executable file
View File

@ -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

41
src/messages/JumpMessage.h Executable file
View File

@ -0,0 +1,41 @@
/*
* JumpMessage.h
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#ifndef JUMPMESSAGE_H_
#define JUMPMESSAGE_H_
#include <glm/glm.hpp>
#include <ecos/core/IDCache.h>
#include <ecos/core/Message.h>
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_