Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add isolated and simplified path to add components #1204

Merged
merged 16 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 95 additions & 144 deletions dGame/Entity.cpp

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion dGame/Entity.h
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ class Entity {

eGameMasterLevel GetGMLevel() const { return m_GMLevel; }

uint8_t GetCollectibleID() const { return uint8_t(m_CollectibleID); }
uint8_t GetCollectibleID() const;

Entity* GetParentEntity() const { return m_ParentEntity; }

@@ -274,6 +274,9 @@ class Entity {
template<typename T>
T GetVarAs(const std::u16string& name) const;

template<typename ComponentType, typename... VaArgs>
ComponentType* AddComponent(VaArgs... args);

/**
* Get the LDF data.
*/
@@ -501,3 +504,36 @@ T Entity::GetNetworkVar(const std::u16string& name) {

return LDFData<T>::Default;
}

/**
* @brief Adds a component of type ComponentType to this entity and forwards the arguments to the constructor.
*
* @tparam ComponentType The component class type to add. Must derive from Component.
* @tparam VaArgs The argument types to forward to the constructor.
* @param args The arguments to forward to the constructor. The first argument passed to the ComponentType constructor will be this entity.
* @return ComponentType* The added component. Will never return null.
*/
template<typename ComponentType, typename... VaArgs>
inline ComponentType* Entity::AddComponent(VaArgs... args) {
static_assert(std::is_base_of_v<Component, ComponentType>, "ComponentType must be a Component");

// Get the component if it already exists, or default construct a nullptr
auto*& componentToReturn = m_Components[ComponentType::ComponentType];

// If it doesn't exist, create it and forward the arguments to the constructor
if (!componentToReturn) {
componentToReturn = new ComponentType(this, std::forward<VaArgs>(args)...);
} else {
// In this case the block is already allocated and ready for use
// so we use a placement new to construct the component again as was requested by the caller.
// Placement new means we already have memory allocated for the object, so this just calls its constructor again.
// This is useful for when we want to create a new object in the same memory location as an old one.
componentToReturn->~Component();
new(componentToReturn) ComponentType(this, std::forward<VaArgs>(args)...);
}

// Finally return the created or already existing component.
// Because of the assert above, this should always be a ComponentType* but I need a way to guarantee the map cannot be modifed outside this function
// To allow a static cast here instead of a dynamic one.
return dynamic_cast<ComponentType*>(componentToReturn);
}
2 changes: 1 addition & 1 deletion dGame/dComponents/BaseCombatAIComponent.h
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ struct AiSkillEntry
*/
class BaseCombatAIComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::BASE_COMBAT_AI;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BASE_COMBAT_AI;

BaseCombatAIComponent(Entity* parentEntity, uint32_t id);
~BaseCombatAIComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/BouncerComponent.h
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
*/
class BouncerComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::BOUNCER;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BOUNCER;

BouncerComponent(Entity* parentEntity);
~BouncerComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/BuffComponent.h
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ struct Buff
*/
class BuffComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::BUFF;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BUFF;

explicit BuffComponent(Entity* parent);

2 changes: 1 addition & 1 deletion dGame/dComponents/BuildBorderComponent.h
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
*/
class BuildBorderComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::BUILD_BORDER;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BUILD_BORDER;

BuildBorderComponent(Entity* parent);
~BuildBorderComponent() override;
7 changes: 6 additions & 1 deletion dGame/dComponents/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -3,11 +3,13 @@ set(DGAME_DCOMPONENTS_SOURCES "BaseCombatAIComponent.cpp"
"BuffComponent.cpp"
"BuildBorderComponent.cpp"
"CharacterComponent.cpp"
"CollectibleComponent.cpp"
"Component.cpp"
"ControllablePhysicsComponent.cpp"
"DestroyableComponent.cpp"
"DonationVendorComponent.cpp"
"InventoryComponent.cpp"
"ItemComponent.cpp"
"LevelProgressionComponent.cpp"
"LUPExhibitComponent.cpp"
"MissionComponent.cpp"
@@ -42,4 +44,7 @@ set(DGAME_DCOMPONENTS_SOURCES "BaseCombatAIComponent.cpp"
"SwitchComponent.cpp"
"TriggerComponent.cpp"
"VehiclePhysicsComponent.cpp"
"VendorComponent.cpp" PARENT_SCOPE)
"VendorComponent.cpp"
"ZoneControlComponent.cpp"
PARENT_SCOPE
)
2 changes: 1 addition & 1 deletion dGame/dComponents/CharacterComponent.h
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ enum StatisticID {
*/
class CharacterComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::CHARACTER;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::CHARACTER;

CharacterComponent(Entity* parent, Character* character);
~CharacterComponent() override;
5 changes: 5 additions & 0 deletions dGame/dComponents/CollectibleComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "CollectibleComponent.h"

void CollectibleComponent::Serialize(RakNet::BitStream* outBitStream, bool isConstruction) {
outBitStream->Write(GetCollectibleId());
}
18 changes: 18 additions & 0 deletions dGame/dComponents/CollectibleComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __COLLECTIBLECOMPONENT__H__
#define __COLLECTIBLECOMPONENT__H__

#include "Component.h"
#include "eReplicaComponentType.h"

class CollectibleComponent : public Component {
public:
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::COLLECTIBLE;
CollectibleComponent(Entity* parentEntity, int32_t collectibleId) : Component(parentEntity), m_CollectibleId(collectibleId) {}

int16_t GetCollectibleId() const { return m_CollectibleId; }
void Serialize(RakNet::BitStream* outBitStream, bool isConstruction) override;
private:
int16_t m_CollectibleId = 0;
};

#endif //!__COLLECTIBLECOMPONENT__H__
2 changes: 1 addition & 1 deletion dGame/dComponents/ControllablePhysicsComponent.h
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ enum class eStateChangeType : uint32_t;
*/
class ControllablePhysicsComponent : public PhysicsComponent {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::CONTROLLABLE_PHYSICS;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::CONTROLLABLE_PHYSICS;

ControllablePhysicsComponent(Entity* entity);
~ControllablePhysicsComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/DestroyableComponent.h
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ enum class eStateChangeType : uint32_t;
*/
class DestroyableComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::DESTROYABLE;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::DESTROYABLE;

DestroyableComponent(Entity* parentEntity);
~DestroyableComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/InventoryComponent.h
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ enum class eItemType : int32_t;
class InventoryComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::INVENTORY;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::INVENTORY;
explicit InventoryComponent(Entity* parent, tinyxml2::XMLDocument* document = nullptr);

void Update(float deltaTime) override;
5 changes: 5 additions & 0 deletions dGame/dComponents/ItemComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "ItemComponent.h"

void ItemComponent::Serialize(RakNet::BitStream* outBitStream, bool isConstruction) {
outBitStream->Write0();
}
16 changes: 16 additions & 0 deletions dGame/dComponents/ItemComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __ITEMCOMPONENT__H__
#define __ITEMCOMPONENT__H__

#include "Component.h"
#include "eReplicaComponentType.h"

class ItemComponent : public Component {
public:
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::ITEM;

ItemComponent(Entity* entity) : Component(entity) {}

void Serialize(RakNet::BitStream* bitStream, bool isConstruction) override;
};

#endif //!__ITEMCOMPONENT__H__
2 changes: 1 addition & 1 deletion dGame/dComponents/LUPExhibitComponent.h
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
class LUPExhibitComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::EXHIBIT;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::EXHIBIT;

LUPExhibitComponent(Entity* parent);
~LUPExhibitComponent();
2 changes: 1 addition & 1 deletion dGame/dComponents/LevelProgressionComponent.h
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@

class LevelProgressionComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::LEVEL_PROGRESSION;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::LEVEL_PROGRESSION;

/**
* Constructor for this component
2 changes: 1 addition & 1 deletion dGame/dComponents/MissionComponent.h
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ class AchievementCacheKey;
class MissionComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION;

explicit MissionComponent(Entity* parent);
~MissionComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/MissionOfferComponent.h
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ struct OfferedMission {
*/
class MissionOfferComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION_OFFER;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION_OFFER;

MissionOfferComponent(Entity* parent, LOT parentLot);
~MissionOfferComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/ModelComponent.h
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ class Entity;
*/
class ModelComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MODEL;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MODEL;

ModelComponent(Entity* parent);

2 changes: 1 addition & 1 deletion dGame/dComponents/ModuleAssemblyComponent.h
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
*/
class ModuleAssemblyComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MODULE_ASSEMBLY;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MODULE_ASSEMBLY;

ModuleAssemblyComponent(Entity* parent);
~ModuleAssemblyComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/MovementAIComponent.h
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ struct MovementAIInfo {
*/
class MovementAIComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVEMENT_AI;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVEMENT_AI;

MovementAIComponent(Entity* parentEntity, MovementAIInfo info);

2 changes: 1 addition & 1 deletion dGame/dComponents/MovingPlatformComponent.h
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ class MoverSubComponent {
*/
class MovingPlatformComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVING_PLATFORM;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MOVING_PLATFORM;

MovingPlatformComponent(Entity* parent, const std::string& pathName);
~MovingPlatformComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/MultiZoneEntranceComponent.h
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
*/
class MultiZoneEntranceComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::MULTI_ZONE_ENTRANCE;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MULTI_ZONE_ENTRANCE;

/**
* Constructor for this component, builds the m_LUPWorlds vector
2 changes: 1 addition & 1 deletion dGame/dComponents/PetComponent.h
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ enum class PetAbilityType
class PetComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PET;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PET;

explicit PetComponent(Entity* parentEntity, uint32_t componentId);
~PetComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/PhantomPhysicsComponent.h
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ enum class ePhysicsEffectType : uint32_t ;
*/
class PhantomPhysicsComponent : public PhysicsComponent {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PHANTOM_PHYSICS;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PHANTOM_PHYSICS;

PhantomPhysicsComponent(Entity* parent);
~PhantomPhysicsComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/PlayerForcedMovementComponent.h
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
*/
class PlayerForcedMovementComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PLAYER_FORCED_MOVEMENT;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PLAYER_FORCED_MOVEMENT;

/**
* Constructor for this component
2 changes: 1 addition & 1 deletion dGame/dComponents/PossessableComponent.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
*/
class PossessableComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSABLE;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSABLE;

PossessableComponent(Entity* parentEntity, uint32_t componentId);

2 changes: 1 addition & 1 deletion dGame/dComponents/PossessorComponent.h
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ enum class ePossessionType : uint8_t {
*/
class PossessorComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSOR;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::POSSESSOR;

PossessorComponent(Entity* parent);
~PossessorComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/PropertyComponent.h
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ struct PropertyState {
*/
class PropertyComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY;
explicit PropertyComponent(Entity* parentEntity);
~PropertyComponent() override;
[[nodiscard]] PropertyState* GetPropertyState() const { return m_PropertyState; };
2 changes: 1 addition & 1 deletion dGame/dComponents/PropertyEntranceComponent.cpp
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
#include "eObjectBits.h"
#include "eGameMasterLevel.h"

PropertyEntranceComponent::PropertyEntranceComponent(uint32_t componentID, Entity* parent) : Component(parent) {
PropertyEntranceComponent::PropertyEntranceComponent(Entity* parent, uint32_t componentID) : Component(parent) {
this->propertyQueries = {};

auto table = CDClientManager::Instance().GetTable<CDPropertyEntranceComponentTable>();
4 changes: 2 additions & 2 deletions dGame/dComponents/PropertyEntranceComponent.h
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@
*/
class PropertyEntranceComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_ENTRANCE;
explicit PropertyEntranceComponent(uint32_t componentID, Entity* parent);
explicit PropertyEntranceComponent(Entity* parent, uint32_t componentID);
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_ENTRANCE;

/**
* Handles an OnUse request for some other entity, rendering the property browse menu
2 changes: 1 addition & 1 deletion dGame/dComponents/PropertyManagementComponent.h
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ enum class PropertyPrivacyOption
class PropertyManagementComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_MANAGEMENT;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_MANAGEMENT;
PropertyManagementComponent(Entity* parent);
static PropertyManagementComponent* Instance();

2 changes: 1 addition & 1 deletion dGame/dComponents/PropertyVendorComponent.h
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
class PropertyVendorComponent : public Component
{
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_VENDOR;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY_VENDOR;
explicit PropertyVendorComponent(Entity* parent);

/**
2 changes: 1 addition & 1 deletion dGame/dComponents/ProximityMonitorComponent.h
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
*/
class ProximityMonitorComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::PROXIMITY_MONITOR;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROXIMITY_MONITOR;

ProximityMonitorComponent(Entity* parentEntity, int smallRadius = -1, int largeRadius = -1);
~ProximityMonitorComponent() override;
2 changes: 1 addition & 1 deletion dGame/dComponents/RacingControlComponent.h
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ struct RacingPlayerInfo {
*/
class RacingControlComponent : public Component {
public:
static const eReplicaComponentType ComponentType = eReplicaComponentType::RACING_CONTROL;
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RACING_CONTROL;

RacingControlComponent(Entity* parentEntity);
~RacingControlComponent();
Loading

Unchanged files with check annotations Beta