1#include "TeleportNavigationComponent.h"
4#include "ViewContext.h"
5#include "Input/InputManager.h"
7#include "Components/Core/CameraComponent.h"
8#include "Components/Core/TransformComponent.h"
9#include "Components/Core/SceneComponent.h"
10#include "Components/Geometry/MeshGeneratorComponent.h"
11#include "Components/Appearance/MaterialComponent.h"
13#include "Scene/RayPick.h"
14#include "EntityStore.h"
16#include "Systems/Core/CameraSystem.h"
17#include "Systems/Core/TransformSystem.h"
18#include "Systems/Core/SceneSystem.h"
20#include "Foundation/Logging/Logger.h"
21#include "Foundation/Reflection/Method.h"
24#include <glm/gtx/quaternion.hpp>
31 glm::vec4 navEmissive(0.2f, 0.2f, 0.8f, 1);
32 glm::vec4 navDiffuse(0, 0, 0, 0.7f);
40void Cogs::Core::TeleportNavigationComponent::registerType()
43 Field(
Name(
"actionTeleport"), &TeleportNavigationComponent::actionTeleport),
44 Field(
Name(
"aimEntity"), &TeleportNavigationComponent::aimEntity),
45 Field(
Name(
"beamOffset"), &TeleportNavigationComponent::beamOffset),
46 Field(
Name(
"beamRotation"), &TeleportNavigationComponent::beamRotation),
47 Field(
Name(
"useCoordinate"), &TeleportNavigationComponent::useCoordinate),
48 Field(
Name(
"teleportOffset"), &TeleportNavigationComponent::teleportOffset),
51 Method(
Name(
"initialize"), &TeleportNavigationComponent::initialize),
52 Method(
Name(
"postUpdate"), &TeleportNavigationComponent::postUpdate),
53 Method(
Name(
"update"), &TeleportNavigationComponent::update),
56 DynamicComponent::registerDerivedType<TeleportNavigationComponent>().setMethods(methods).setFields(fields);
61 this->context = context;
72 beamMaterial->emissiveColor = navEmissive;
73 beamMaterial->diffuseColor = navDiffuse;
77 beamShape->
size = glm::vec3(0.002f, 0, 2500);
81 beamGeometryTransform->
position = glm::vec3(0, 0, -2500);
85 beamTransform->
position = beamOffset;
86 beamTransform->
rotation = beamRotation;
90 beamVisibility->
visible =
false;
103 beamCam->fieldOfView = 0.01f;
106 beamCam->viewportSize = glm::vec2(3, 3);
107 beamCam->nearPlaneLimit = 0.1f;
108 beamCam->enableClippingPlaneAdjustment =
true;
112 beamCam->setChanged();
114 destinationMarker = context->
store->
findEntity(
"destinationMarker", aimEntity.get());
115 if (!destinationMarker) {
124 destMaterial->emissiveColor = navEmissive;
125 destMaterial->diffuseColor = navDiffuse;
129 destTransform->
scale = glm::vec3(0.25f);
132 destinationVisibility = destinationMarker->getComponentHandle<
SceneComponent>();
135 destVis->visible =
false;
136 destVis->setChanged();
139void logVector(
const Cogs::Logging::Log& logger_,
const char * name, glm::dvec3 vector)
141 LOG_DEBUG(logger_,
"%s: (%f,%f,%f)", name, vector.x, vector.y, vector.z);
144void Cogs::Core::TeleportNavigationComponent::postUpdate()
148void Cogs::Core::TeleportNavigationComponent::update()
153 if (!navigationTransform) {
154 navigationTransform = getComponent<TransformComponent>()->getComponentHandle<
TransformComponent>();
157 auto destVis = destinationVisibility.resolveComponent<
SceneComponent>();
160 bool pressed = input->getActionState(actionTeleport);
165 context->transformSystem->
update(context);
166 context->cameraSystem->update(context);
169 std::vector<RayPicking::RayPickHit> hits;
175 destinationTransform->position = hits.front().position;
176 destinationTransform->coordinates = context->transformSystem->
getOrigin();
180 if (!destVis->visible) {
181 destVis->visible =
true;
182 destVis->setChanged();
183 context->sceneSystem->updateState(*destVis);
188 if (destVis->visible) {
189 destVis->visible =
false;
190 destVis->setChanged();
191 context->sceneSystem->updateState(*destVis);
196 if (pressed != wasPressed) {
199 beamVis->setChanged();
202 if (destVis->visible) {
204 destVis->visible =
false;
205 destVis->setChanged();
207 auto pickedCoord = destinationTransform->position;
210 if (!useCoordinate) {
211 navTransform->
position = pickedCoord;
214 auto origin = context->transformSystem->
getOrigin();
215 auto globalPickedPos = origin + glm::dvec3(pickedCoord);
217 glm::dvec3 newOrigin(globalPickedPos.x, globalPickedPos.y, 0);
218 glm::dvec3 newPosition = globalPickedPos - newOrigin;
220 logVector(logger,
"origin", origin);
221 logVector(logger,
"pickedCoord", pickedCoord);
222 logVector(logger,
"globalPickedPos", globalPickedPos);
223 logVector(logger,
"newOrigin", newOrigin);
224 logVector(logger,
"newPosition", newPosition);
226 context->transformSystem->
setOrigin(newOrigin);
227 navTransform->coordinates = newOrigin;
228 navTransform->position = glm::vec3(newPosition) + teleportOffset;
230 navTransform->setChanged();
235 wasPressed = pressed;
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
ComponentHandle getComponentHandle() const
glm::vec2 viewportSize
Size of the viewport covered by this instance, given in pixels.
ProjectionMode projectionMode
The projection mode to use for the camera.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class EntityStore * store
Entity store.
std::unique_ptr< class RayPicking > rayPicking
RayPicking service instance.
ComponentModel::Component * addComponent(ComponentModel::Entity *entity, Reflection::TypeId typeId)
Add a component of the given type to the entity.
EntityPtr createChildEntity(const StringView &type, ComponentModel::Entity *parent, const StringView &name=StringView())
Create a new Entity, parenting it to the given parent.
EntityPtr findEntity(const StringView &name, const ComponentModel::Entity *root=nullptr, EntityFind findOptions=EntityFind::Default) const
Finds an entity with the given name.
Contains information on how the entity behaves in the scene.
bool visible
If the entity this component is a member of should be visible.
bool pickable
If the entity this component is a member of should be pickable.
std::unique_ptr< class InputManager > inputManager
Log implementation class.
Field definition describing a single data member of a data structure.
Simple method definition.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
@ Closest
Return just the closest hit.
@ EnablePicking
Supports picking.
@ None
No flags specified,.
@ Perspective
Perspective projection.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains reflection support.
Exposes material properties for legacy entities and code.
Generates mesh data and assigns the generated mesh to a MeshComponent on the same entity.
glm::vec3 size
Size parameters.
Represents an unique name.