From 60289635846e5c070d5da1cff3a9b2c79896eea9 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 15 Apr 2019 18:40:34 +0800 Subject: [PATCH] Re-add camera component, remove the camera system, and add a constraint system --- src/entity/components/camera-component.cpp | 29 ++++++++++ .../camera-component.hpp} | 38 +++++------- src/entity/components/component-type.hpp | 6 +- .../components/orbit-constraint-component.cpp | 32 ++++++++++ .../components/orbit-constraint-component.hpp | 42 ++++++++++++++ src/entity/systems/constraint-system.cpp | 58 +++++++++++++++++++ ...amera-system.hpp => constraint-system.hpp} | 29 +++++----- src/game.cpp | 10 ++-- src/game.hpp | 4 +- 9 files changed, 197 insertions(+), 51 deletions(-) create mode 100644 src/entity/components/camera-component.cpp rename src/entity/{systems/camera-system.cpp => components/camera-component.hpp} (61%) create mode 100644 src/entity/components/orbit-constraint-component.cpp create mode 100644 src/entity/components/orbit-constraint-component.hpp create mode 100644 src/entity/systems/constraint-system.cpp rename src/entity/systems/{camera-system.hpp => constraint-system.hpp} (66%) diff --git a/src/entity/components/camera-component.cpp b/src/entity/components/camera-component.cpp new file mode 100644 index 0000000..2f4f81f --- /dev/null +++ b/src/entity/components/camera-component.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2017-2019 Christopher J. Howard + * + * This file is part of Antkeeper Source Code. + * + * Antkeeper Source Code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper Source Code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper Source Code. If not, see . + */ + +#include "camera-component.hpp" + +ComponentBase* CameraComponent::clone() const +{ + CameraComponent* component = new CameraComponent(); + component->camera = camera; + + return component; +} + diff --git a/src/entity/systems/camera-system.cpp b/src/entity/components/camera-component.hpp similarity index 61% rename from src/entity/systems/camera-system.cpp rename to src/entity/components/camera-component.hpp index 3f1c2f6..de2f60e 100644 --- a/src/entity/systems/camera-system.cpp +++ b/src/entity/components/camera-component.hpp @@ -17,34 +17,22 @@ * along with Antkeeper Source Code. If not, see . */ -#include "camera-system.hpp" +#ifndef CAMERA_COMPONENT_HPP +#define CAMERA_COMPONENT_HPP -CameraSystem::CameraSystem(ComponentManager* componentManager): - System(componentManager), - camera(nullptr), - rig(&orbitCam) -{} +#include "../component.hpp" +#include "component-type.hpp" -CameraSystem::~CameraSystem() -{} +#include +using namespace Emergent; -void CameraSystem::update(float t, float dt) +class CameraComponent: public Component { - if (camera != nullptr) - { - cameraRig->update(dt); - } -} +public: + virtual ComponentBase* clone() const; + + Camera camera; +}; -void CameraSystem::setCamera(Camera* camera) -{ - this->camera = camera; - orbitCam.attachCamera(&camera); - freeCam.attachCamera(&camera); -} - -void CameraSystem::handleEvent(const MouseMovedEvent& event) -{ - -} +#endif // CAMERA_COMPONENT_HPP diff --git a/src/entity/components/component-type.hpp b/src/entity/components/component-type.hpp index d6e6542..9034fda 100644 --- a/src/entity/components/component-type.hpp +++ b/src/entity/components/component-type.hpp @@ -17,17 +17,19 @@ * along with Antkeeper Source Code. If not, see . */ -#ifndef COMPONENT__TYPE_HPP -#define COMPONENT__TYPE_HPP +#ifndef COMPONENT_TYPE_HPP +#define COMPONENT_TYPE_HPP enum class ComponentType { ANIMATION, ANT_HILL, BEHAVIOR, + CAMERA, COLLISION, LEGGED_LOCOMOTION, MODEL, + ORBIT_CONSTRAINT, STEERING, SOUND_SOURCE, TERRAIN_PATCH, diff --git a/src/entity/components/orbit-constraint-component.cpp b/src/entity/components/orbit-constraint-component.cpp new file mode 100644 index 0000000..dee0d24 --- /dev/null +++ b/src/entity/components/orbit-constraint-component.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017-2019 Christopher J. Howard + * + * This file is part of Antkeeper Source Code. + * + * Antkeeper Source Code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper Source Code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper Source Code. If not, see . + */ + +#include "orbit-constraint-component.hpp" + +ComponentBase* OrbitConstraintComponent::clone() const +{ + OrbitConstraintComponent* component = new OrbitConstraintComponent(); + component->target = target; + component->distance = distance; + component->elevation = elevation; + component->azimuth = azimuth; + + return component; +} + diff --git a/src/entity/components/orbit-constraint-component.hpp b/src/entity/components/orbit-constraint-component.hpp new file mode 100644 index 0000000..43ff855 --- /dev/null +++ b/src/entity/components/orbit-constraint-component.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2017-2019 Christopher J. Howard + * + * This file is part of Antkeeper Source Code. + * + * Antkeeper Source Code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper Source Code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper Source Code. If not, see . + */ + +#ifndef ORBIT_CONSTRAINT_COMPONENT_HPP +#define ORBIT_CONSTRAINT_COMPONENT_HPP + +#include "../component.hpp" +#include "component-type.hpp" +#include "entity/entity-id.hpp" + +#include +using namespace Emergent; + +class OrbitConstraintComponent: public Component +{ +public: + virtual ComponentBase* clone() const; + + EntityID target; + float distance; + float elevation; + float azimuth; +}; + +#endif // ORBIT_CONSTRAINT_COMPONENT_HPP + diff --git a/src/entity/systems/constraint-system.cpp b/src/entity/systems/constraint-system.cpp new file mode 100644 index 0000000..cf72b46 --- /dev/null +++ b/src/entity/systems/constraint-system.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017-2019 Christopher J. Howard + * + * This file is part of Antkeeper Source Code. + * + * Antkeeper Source Code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper Source Code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper Source Code. If not, see . + */ + +#include "constraint-system.hpp" + +ConstraintSystem::ConstraintSystem(ComponentManager* componentManager): + System(componentManager), + orbitConstraintGroup(componentManager) +{} + +ConstraintSystem::~ConstraintSystem() +{} + +void ConstraintSystem::update(float t, float dt) +{ + // Solve orbit constraints + for (const OrbitConstraintGroup::Member* member: *orbitConstraintGroup.getMembers()) + { + OrbitConstraintComponent* constraint = std::get<0>(member->components); + TransformComponent* transform = std::get<1>(member->components); + + // Get target transform component + TransformComponent* target = componentManager->getComponent(constraint->target); + if (!target) + { + continue; + } + + // Calculate rotation quaternion + Quaternion azimuthRotation = glm::angleAxis(constraint->azimuth, Vector3(0.0f, 1.0f, 0.0f)); + Quaternion elevationRotation = glm::angleAxis(constraint->elevation, Vector3(-1.0f, 0.0f, 0.0f)); + Quaternion rotation = azimuthRotation * elevationRotation; + + // Calculate translation vector + Vector3 translation = target->transform.translation + rotation * Vector3(0, 0, constraint->distance); + + // Update transform + transform->transform.translation = translation; + transform->transform.rotation = rotation; + } +} + diff --git a/src/entity/systems/camera-system.hpp b/src/entity/systems/constraint-system.hpp similarity index 66% rename from src/entity/systems/camera-system.hpp rename to src/entity/systems/constraint-system.hpp index c7e65b0..4dce25a 100644 --- a/src/entity/systems/camera-system.hpp +++ b/src/entity/systems/constraint-system.hpp @@ -17,34 +17,31 @@ * along with Antkeeper Source Code. If not, see . */ -#ifndef CAMERA_SYSTEM_HPP -#define CAMERA_SYSTEM_HPP +#ifndef CONSTRAINT_SYSTEM_HPP +#define CONSTRAINT_SYSTEM_HPP +#include "../entity-group.hpp" +#include "../components/transform-component.hpp" +#include "../components/orbit-constraint-component.hpp" #include "../system.hpp" -#include "game/camera-rig.hpp" #include using namespace Emergent; -class CameraSystem: - public System, - public EventHandler +typedef EntityGroup OrbitConstraintGroup; + +class ConstraintSystem: + public System { public: - CameraSystem(ComponentManager* componentManager); - virtual ~CameraSystem(); + ConstraintSystem(ComponentManager* componentManager); + virtual ~ConstraintSystem(); virtual void update(float t, float dt); - void setCamera(Camera* camera); - private: - virtual void handleEvent(const MouseMovedEvent& event); - Camera* camera; - CameraRig* rig; - OrbitCam orbitCam; - FreeCam freeCam; + OrbitConstraintGroup orbitConstraintGroup; }; -#endif // CAMERA_SYSTEM_HPP +#endif // CONSTRAINT_SYSTEM_HPP diff --git a/src/game.cpp b/src/game.cpp index 55c56ea..0ef1977 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -47,7 +47,7 @@ #include "entity/systems/sound-system.hpp" #include "entity/systems/collision-system.hpp" #include "entity/systems/render-system.hpp" -#include "entity/systems/camera-system.hpp" +#include "entity/systems/constraint-system.hpp" #include "entity/systems/tool-system.hpp" #include "entity/systems/locomotion-system.hpp" #include "entity/systems/behavior-system.hpp" @@ -508,9 +508,7 @@ void Game::setup() // Initialize systems soundSystem = new SoundSystem(componentManager); collisionSystem = new CollisionSystem(componentManager); - cameraSystem = new CameraSystem(componentManager); - cameraSystem->setCamera(&camera); - eventDispatcher.subscribe(cameraSystem); + constraintSystem = new ConstraintSystem(componentManager); renderSystem = new RenderSystem(componentManager, worldScene); toolSystem = new ToolSystem(componentManager); toolSystem->setPickingCamera(&camera); @@ -539,7 +537,7 @@ void Game::setup() systemManager->addSystem(toolSystem); systemManager->addSystem(terrainSystem); systemManager->addSystem(particleSystem); - systemManager->addSystem(cameraSystem); + systemManager->addSystem(constraintSystem); systemManager->addSystem(renderSystem); // Load navmesh @@ -916,7 +914,7 @@ void Game::setupWindow() std::string title = getString("title"); // Create window - window = windowManager->createWindow(title.c_str(), x, y, w, h, false, flags); + window = windowManager->createWindow(title.c_str(), x, y, w, h, flags); if (!window) { throw std::runtime_error("Game::Game(): Failed to create window."); diff --git a/src/game.hpp b/src/game.hpp index b0b27a9..c2163d0 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -63,7 +63,7 @@ class SystemManager; class SoundSystem; class CollisionSystem; class RenderSystem; -class CameraSystem; +class ConstraintSystem; class ToolSystem; class BehaviorSystem; class SteeringSystem; @@ -529,7 +529,7 @@ public: SystemManager* systemManager; SoundSystem* soundSystem; CollisionSystem* collisionSystem; - CameraSystem* cameraSystem; + ConstraintSystem* constraintSystem; RenderSystem* renderSystem; ToolSystem* toolSystem; BehaviorSystem* behaviorSystem;