diff --git a/assets/meshes/kart-driver.bcosm b/assets/meshes/kart-driver.bcosm index 5b96ac0..01225db 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 a3ead4d..a23875b 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 6dfabfa..62e972f 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 02020c0..ceead3c 100644 --- a/src/JamSpookGame.cpp +++ b/src/JamSpookGame.cpp @@ -265,20 +265,32 @@ void JamSpookGame::set() goalFactory.reset(); - // Create the ball - unique_ptr ballFactory = make_unique(dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), - graphicsSystem, - dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), - renderLayerGame); +// // Create the ball +// unique_ptr ballFactory = make_unique(dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), +// graphicsSystem, +// dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), +// renderLayerGame); +// +// ballFactory->createBall(vec3(0, 3, 0), // position +// 1.0f, // size +// 10.0f, // size*2 +// 0.1f, // restitution +// "base", // ballName +// vec3(1,1,1)); // lightColor +// +// ballFactory.reset(); - ballFactory->createBall(vec3(0, 3, 0), // position - 1.0f, // size - 10.0f, // size*2 - 0.1f, // restitution - "base", // ballName - vec3(1,1,1)); // lightColor + // Create the player kart + unique_ptr kartFactory = make_unique( + dynamic_pointer_cast(findSystem(IDCache::get("AssetSystem"))), + graphicsSystem, + dynamic_pointer_cast(findSystem(IDCache::get("PhysicsSystem"))), + renderLayerGame); - ballFactory.reset(); + kartFactory->createKart(vec3(0, 3, 0), // position + vec3(0, 0, 0)); // euler degrees rotation, XYZ + + kartFactory.reset(); // Set game state setGameState(ecos::core::GameState::InGame); diff --git a/src/JamSpookGame.h b/src/JamSpookGame.h index 8c76359..a018ac9 100644 --- a/src/JamSpookGame.h +++ b/src/JamSpookGame.h @@ -12,10 +12,15 @@ #include #include #include + #include #include + #include +#include +#include + #include #include #include @@ -25,14 +30,17 @@ #include #include #include -#include -#include + #include #include + #include #include + #include + #include + #include #include #include @@ -48,6 +56,7 @@ #include #include #include + #include #include @@ -56,6 +65,7 @@ #include "entities/GoalFactory.h" #include "entities/BallFactory.h" #include "entities/BoxFactory.h" +#include "entities/KartFactory.h" namespace JamSpook { @@ -69,6 +79,7 @@ using std::bind; using std::vector; using std::string; using std::to_string; + using glm::ivec2; using glm::vec2; using glm::vec3; @@ -77,6 +88,11 @@ using glm::mat4; using glm::translate; using glm::normalize; +using ecos::utility::getWorkingPath; +using ecos::utility::getRealPath; +using ecos::utility::getFloatInRange; +using ecos::utility::getIntInRange; + using ecos::core::logging::Log; using ecos::core::logging::LogLevel; using ecos::core::Game; @@ -87,19 +103,20 @@ using ecos::core::SysCall; using ecos::core::BroadcastObservable; using ecos::core::Window; using ecos::core::CycleRegulationSystem; -using ecos::utility::getWorkingPath; -using ecos::utility::getRealPath; -using ecos::utility::getFloatInRange; -using ecos::utility::getIntInRange; + using ecos::asset::AssetSystem; using ecos::asset::AssetFileSystem; +using ecos::asset::DataManagementMode; + using ecos::physics::PhysicsSystem; using ecos::physics::CollisionShapeLoader; -using ecos::graphics::TextureBlankLoader; + using ecos::animation::AnimationSystem; + using ecos::interaction::InteractionSystem; using ecos::interaction::InputDeviceState; using ecos::interaction::ButtonState; + using ecos::graphics::GraphicsSystem; using ecos::graphics::RenderLayer; using ecos::graphics::RenderLayerFactory; @@ -119,10 +136,11 @@ using ecos::graphics::TextureBlankLoader; using ecos::graphics::ShaderType; using ecos::graphics::ShaderAsset; using ecos::graphics::ShaderProgram; -using ecos::asset::DataManagementMode; + using ecos::sound::SoundSystem; using ecos::sound::SoundEffectLoader; +/// class JamSpookGame: public Game { diff --git a/src/entities/Jump180Factory.h b/src/entities/Jump180Factory.h index bfe7ed6..74265fc 100644 --- a/src/entities/Jump180Factory.h +++ b/src/entities/Jump180Factory.h @@ -72,6 +72,7 @@ using glm::eulerAngleXYZ; using glm::radians; using ecos::utility::getShared; + using ecos::core::IDCache; using ecos::core::logging::Log; using ecos::core::logging::LogLevel; @@ -80,8 +81,10 @@ using ecos::core::Message; using ecos::core::Entity; using ecos::core::SceneGraph; using ecos::core::ICCResponseComponent; + using ecos::asset::AssetSystem; using ecos::asset::DataManagementMode; + using ecos::graphics::GraphicsSystem; using ecos::graphics::GraphicsComponent; using ecos::graphics::RenderLayer; @@ -97,6 +100,7 @@ using ecos::graphics::Mesh; using ecos::graphics::MeshFactory; using ecos::graphics::ModelRenderableFactory; using ecos::graphics::ModelRenderable; + using ecos::physics::PhysicsSystem; using ecos::physics::PhysicsComponent; using ecos::physics::ColliderFactory; diff --git a/src/entities/KartFactory.cpp b/src/entities/KartFactory.cpp new file mode 100644 index 0000000..78a419d --- /dev/null +++ b/src/entities/KartFactory.cpp @@ -0,0 +1,105 @@ +/* + * KartFactory.cpp + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#include "KartFactory.h" + +namespace JamSpook { + +KartFactory::KartFactory(weak_ptr assetSystem, + weak_ptr graphicsSystem, + weak_ptr physicsSystem, + weak_ptr renderLayer): + mAssetSystem(assetSystem), + mGraphicsSystem(graphicsSystem), + mPhysicsSystem(physicsSystem), + mRenderLayer(renderLayer) +{} + +KartFactory::~KartFactory() +{} + +shared_ptr KartFactory::createKart(const vec3 position, + const vec3 eulerRotationXYZDegrees) +{ + // Create instance + shared_ptr entity = make_shared(); + entity->setEntityTag("playerkart"); + SceneGraph::addEntity(entity); + + // Create transform + mat4 rotation = eulerAngleXYZ( + radians(static_cast(eulerRotationXYZDegrees.x)), + radians(static_cast(eulerRotationXYZDegrees.y)), + radians(static_cast(eulerRotationXYZDegrees.z))); + + mat4 translation = translate(mat4(1.0f), position); + + mat4 transform = rotation * translation;// * rotateToCenter; + + // Add physics component + ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem); + shared_ptr physicsComponent = + make_shared( + transform, + entity, + getShared(mPhysicsSystem), + getShared(mPhysicsSystem), + getShared(mPhysicsSystem), + colliderFactory->createConvexHullCollider(position, + quat_cast(rotation), + "kart-hulls.bcosp", // filePath + "kart-hulls-shape", // name + 100.0f, // mass + entity, + entity->getEntityTag())); + delete colliderFactory; + // Note: Editing of rigid body params, maybe only has effect is it has been added to the world. + physicsComponent->getCollider()->getRigidBody()->setLinearFactor(btVector3(1,1,1)); // allow all positional movement movement + physicsComponent->getCollider()->getRigidBody()->setAngularFactor(btVector3(1,1,1)); // allow all rotational movement movement + physicsComponent->getCollider()->getRigidBody()->setRestitution(0.1f); + physicsComponent->getCollider()->getRigidBody()->setFriction(0.01f); +// physicsComponent->getCollider()->getRigidBody()->setGravity(btVector3(0, 0, 0)); // no gravity, length of zero + physicsComponent->getCollider()->getRigidBody()->setActivationState(DISABLE_DEACTIVATION); // RigidBody may never sleep. (Note: Or else it will not respond to setLinearVelocity when standing still for too long(a couple of seconds).) + + entity->addComponent(physicsComponent); + + // Add graphics component + shared_ptr graphicsComponent = make_shared(transform, + entity, + getShared(mGraphicsSystem), + mRenderLayer); + entity->addComponent(graphicsComponent); + + // Add the ModelRenderable of a ball + ModelRenderableFactory* modelFactory = new ModelRenderableFactory(mAssetSystem); + + shared_ptr modelRenderableKart = modelFactory->createModel("kart-vehicle.bcosm", + "kart-vehicle-diffuse.png", + "kart-vehicle-model"); + graphicsComponent->addRenderable(modelRenderableKart); + + shared_ptr modelRenderableDriver = modelFactory->createModel("kart-driver.bcosm", + "kart-driver-diffuse.png", + "kart-driver-model"); + graphicsComponent->addRenderable(modelRenderableDriver); + + delete modelFactory; + + // Add internal pointlight + LightSourceFactory* lightSourceFactory = new LightSourceFactory(mAssetSystem, mGraphicsSystem); + shared_ptr lightSource = lightSourceFactory->createPointLight(vec3(1,1,1), + 0.02f, + 0.03f, + transform); + graphicsComponent->setLightSource(lightSource); + delete lightSourceFactory; + + // Return instance + return entity; +} + +} // namespace JamSpook diff --git a/src/entities/KartFactory.h b/src/entities/KartFactory.h new file mode 100644 index 0000000..7888ead --- /dev/null +++ b/src/entities/KartFactory.h @@ -0,0 +1,141 @@ +/* + * KartFactory.h + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#ifndef KARTFACTORY_H_ +#define KARTFACTORY_H_ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "KartPhysicsComponent.h" + +namespace JamSpook { + +using std::string; +using std::shared_ptr; +using std::make_shared; +using std::weak_ptr; +using std::dynamic_pointer_cast; +using std::function; +using std::bind; +using std::vector; + +using glm::vec3; +using glm::mat4; +using glm::quat; +using glm::quat_cast; +using glm::translate; +using glm::scale; +using glm::rotate; +using glm::eulerAngleXYZ; +using glm::radians; + +using ecos::utility::getShared; + +using ecos::core::IDCache; +using ecos::core::logging::Log; +using ecos::core::logging::LogLevel; +using ecos::core::BroadcastObservable; +using ecos::core::Message; +using ecos::core::Entity; +using ecos::core::SceneGraph; +using ecos::core::ICCResponseComponent; + +using ecos::asset::AssetSystem; +using ecos::asset::DataManagementMode; + +using ecos::graphics::GraphicsSystem; +using ecos::graphics::GraphicsComponent; +using ecos::graphics::RenderLayer; +using ecos::graphics::TextureFactory; +using ecos::graphics::Texture; +using ecos::graphics::ShaderType; +using ecos::graphics::ShaderAsset; +using ecos::graphics::ShaderProgram; +using ecos::graphics::ShaderProgramFactory; +using ecos::graphics::Material; +using ecos::graphics::MaterialFactory; +using ecos::graphics::Mesh; +using ecos::graphics::MeshFactory; +using ecos::graphics::ModelRenderableFactory; +using ecos::graphics::ModelRenderable; +using ecos::graphics::lighting::LightSource; +using ecos::graphics::lighting::LightSourceFactory; + +using ecos::physics::PhysicsSystem; +using ecos::physics::PhysicsComponent; +using ecos::physics::ColliderFactory; +using ecos::physics::Collider; + +/// Factory to simplify player kart entity creation +class KartFactory +{ +private: + weak_ptr mAssetSystem; + weak_ptr mGraphicsSystem; + weak_ptr mPhysicsSystem; + weak_ptr mRenderLayer; + +public: + KartFactory(weak_ptr assetSystem, + weak_ptr graphicsSystem, + weak_ptr physicsSystem, + weak_ptr renderLayer); + virtual ~KartFactory(); + + /// Compose a kart entity + shared_ptr createKart(const vec3 position, + const vec3 eulerRotationXYZDegrees); +}; + +} // namespace JamSpook + +#endif // KARTFACTORY_H_ diff --git a/src/entities/KartPhysicsComponent.cpp b/src/entities/KartPhysicsComponent.cpp new file mode 100644 index 0000000..aef2ca9 --- /dev/null +++ b/src/entities/KartPhysicsComponent.cpp @@ -0,0 +1,57 @@ +/* + * 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) +{} + +KartPhysicsComponent::~KartPhysicsComponent() +{} + +void KartPhysicsComponent::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 KartPhysicsComponent::onICCMessage(shared_ptr message) +{ + PhysicsComponent::onICCMessage(message); +} + +void KartPhysicsComponent::onCollision(const string& tag) +{ + if (tag == "ground-plane") + { + vec3 position = getPosition(); + setPosition(vec3(position.x, 10.0f, position.z)); + ICCBroadcast(make_shared(IDCache::get("GroundContactMessage"))); + } +} + +} // namespace JamSpook diff --git a/src/entities/KartPhysicsComponent.h b/src/entities/KartPhysicsComponent.h new file mode 100644 index 0000000..e45469c --- /dev/null +++ b/src/entities/KartPhysicsComponent.h @@ -0,0 +1,82 @@ +/* + * KartPhysicsComponent.h + * + * Created on: Aug 19, 2020 + * Author: fredrick + */ + +#ifndef KARTPHYSICSCOMPONENT_H_ +#define KARTPHYSICSCOMPONENT_H_ + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace JamSpook { + +using std::chrono::milliseconds; +using std::weak_ptr; +using std::shared_ptr; +using std::make_shared; +using std::dynamic_pointer_cast; +using std::string; + +using glm::vec3; +using glm::mat4; +using glm::translate; +using glm::scale; + +using ecos::utility::getFloatInRange; + +using ecos::core::IDCache; +using ecos::core::logging::Log; +using ecos::core::logging::LogLevel; +using ecos::core::Message; +using ecos::core::BroadcastObservable; + +using ecos::physics::PhysicsSystem; +using ecos::physics::PhysicsComponent; +using ecos::physics::Collider; +using ecos::physics::TransformChangeMessage; +using ecos::physics::CollisionStateChangeMessage; +using ecos::physics::ColliderQueryMessage; + +/// +class KartPhysicsComponent: + public ecos::physics::PhysicsComponent +{ +public: + KartPhysicsComponent(mat4 transform, + shared_ptr > > entity, + weak_ptr > physicsSystem, + weak_ptr > > physicsCollisionSubSystem, + weak_ptr > > physicsColliderQuerySubSystem, + shared_ptr collider); + virtual ~KartPhysicsComponent(); + + virtual void update(const milliseconds dtms); + virtual void onICCMessage(shared_ptr message); + virtual void onCollision(const string& tag); +}; + +} // namespace JamSpook + +#endif // KARTPHYSICSCOMPONENT_H_