Browse Source

Re-add camera component, remove the camera system, and add a constraint system

master
C. J. Howard 5 years ago
parent
commit
6028963584
Signed by: cjhoward GPG Key ID: 03E1FABA9C3EC195
9 changed files with 197 additions and 51 deletions
  1. +29
    -0
      src/entity/components/camera-component.cpp
  2. +13
    -25
      src/entity/components/camera-component.hpp
  3. +4
    -2
      src/entity/components/component-type.hpp
  4. +32
    -0
      src/entity/components/orbit-constraint-component.cpp
  5. +42
    -0
      src/entity/components/orbit-constraint-component.hpp
  6. +58
    -0
      src/entity/systems/constraint-system.cpp
  7. +13
    -16
      src/entity/systems/constraint-system.hpp
  8. +4
    -6
      src/game.cpp
  9. +2
    -2
      src/game.hpp

+ 29
- 0
src/entity/components/camera-component.cpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "camera-component.hpp"
ComponentBase* CameraComponent::clone() const
{
CameraComponent* component = new CameraComponent();
component->camera = camera;
return component;
}

src/entity/systems/camera-system.cpp → src/entity/components/camera-component.hpp View File

@ -17,34 +17,22 @@
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <emergent/emergent.hpp>
using namespace Emergent;
void CameraSystem::update(float t, float dt)
class CameraComponent: public Component<ComponentType::CAMERA>
{
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

+ 4
- 2
src/entity/components/component-type.hpp View File

@ -17,17 +17,19 @@
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
*/
#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,

+ 32
- 0
src/entity/components/orbit-constraint-component.cpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}

+ 42
- 0
src/entity/components/orbit-constraint-component.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ORBIT_CONSTRAINT_COMPONENT_HPP
#define ORBIT_CONSTRAINT_COMPONENT_HPP
#include "../component.hpp"
#include "component-type.hpp"
#include "entity/entity-id.hpp"
#include <emergent/emergent.hpp>
using namespace Emergent;
class OrbitConstraintComponent: public Component<ComponentType::ORBIT_CONSTRAINT>
{
public:
virtual ComponentBase* clone() const;
EntityID target;
float distance;
float elevation;
float azimuth;
};
#endif // ORBIT_CONSTRAINT_COMPONENT_HPP

+ 58
- 0
src/entity/systems/constraint-system.cpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<TransformComponent>(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;
}
}

src/entity/systems/camera-system.hpp → src/entity/systems/constraint-system.hpp View File

@ -17,34 +17,31 @@
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <emergent/emergent.hpp>
using namespace Emergent;
class CameraSystem:
public System,
public EventHandler<MouseMovedEvent>
typedef EntityGroup<OrbitConstraintComponent, TransformComponent> 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

+ 4
- 6
src/game.cpp View File

@ -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<MouseMovedEvent>(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.");

+ 2
- 2
src/game.hpp View File

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

Loading…
Cancel
Save