Cogs.Core
LocationComponent.cpp
1#include "LocationComponent.h"
2#include "Context.h"
3#include "EntityStore.h"
4#include "../Utils.h"
5
6#include "Components/Core/MeshComponent.h"
7#include "Components/Core/TransformComponent.h"
8#include "Components/Appearance/MaterialComponent.h"
9
10#include "Resources/MeshManager.h"
11
12#include "../Components/DataSetComponent.h"
13#include "../Components/DataRefComponent.h"
14
15#include "Foundation/Logging/Logger.h"
16
17namespace {
18 Cogs::Logging::Log logger = Cogs::Logging::getLogger("LocationComponent");
19}
20
21using glm::make_quat;
22using glm::value_ptr;
23
24using namespace Cogs::Core;
25using namespace Cogs::Reflection;
26using namespace Cogs::Core::EchoSounder;
27
28void LocationComponent::registerType()
29{
30
31 Field fields[] = {
32 Field(Name("time"), &LocationComponent::time),
33 Field(Name("updateTransform"), &LocationComponent::updateTransform)
34 };
35 Method methods[] = {
36 Method(Name("initialize"), &LocationComponent::initialize),
37 Method(Name("update"), &LocationComponent::update),
38 };
39 DynamicComponent::registerDerivedType<LocationComponent>()
40 .setFields(fields)
41 .setMethods(methods);
42}
43
44void LocationComponent::initialize(Context * context)
45{
46 this->context = context;
47 this->dataSystem = ExtensionRegistry::getExtensionSystem<DataSetSystem>(context);
48}
49
50void LocationComponent::update()
51{
52 if (!this->dataSystem) return;
53
54 auto dataRefComp = getComponent<DataRefComponent>();
55 if (dataRefComp->data) {
56 auto * dataComp = dataRefComp->data->getComponent<DataSetComponent>();
57 auto & dataData = dataSystem->getData(dataComp);
58
59 if (hasFieldChanged(&LocationComponent::time) ||
60 dataRefComp->hasChanged() ||
61 dataComp->hasChanged() ||
62 dataGen != dataData.dataGen)
63 {
64 const auto & config = dataData.persistent->config;
65 const auto & buffer = dataData.persistent->buffer;
66
67 dataGen = dataData.dataGen;
68 if (buffer.sliceCount) {
69 try {
70 index = locateContainingInterval((int)buffer.sliceBegin,
71 (int)(buffer.sliceBegin + buffer.sliceCount) - 1,
72 config.capacity, buffer.timestamps, time);
73 }
74 catch (std::runtime_error e) {
75 index = buffer.sliceBegin;
76 }
77 index = index % config.capacity;
78
79 if (updateTransform) {
80 auto * trComp = getComponent<TransformComponent>();
81 trComp->position = buffer.metaPings[index].vesselPositionGlobal;
82 trComp->rotation = buffer.metaPings[index].vesselOrientationGlobal;
83 trComp->setChanged();
84 }
85 }
86 }
87 }
88}
bool hasFieldChanged(const Reflection::FieldId fieldId) const
Gets if the field with the given id on this component instance has changed.
Definition: Component.h:272
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Log implementation class.
Definition: LogManager.h:139
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Contains reflection support.
Definition: Component.h:11
Represents an unique name.
Definition: Name.h:70