diff --git a/include/server/game/objectmanager.hpp b/include/server/game/objectmanager.hpp index 8716629a..19ccfa3c 100644 --- a/include/server/game/objectmanager.hpp +++ b/include/server/game/objectmanager.hpp @@ -4,6 +4,7 @@ #include "server/game/object.hpp" #include "server/game/item.hpp" +#include "server/game/solidsurface.hpp" #include "shared/utilities/smartvector.hpp" @@ -58,6 +59,14 @@ class ObjectManager { */ SmartVector getItems(); + /** + * @brief Get a list of all SolidSurfaces in this game instance at the + * current timestep. + * @return SmartVector of SolidSurface pointers of all SolidSurface objects + * in the game instance. + */ + SmartVector getSolidSurfaces(); + /* SharedGameState generation */ /** @@ -115,7 +124,12 @@ class ObjectManager { SmartVector base_objects; /** - * @brief SmartVector of Item pointers to all items ObjectType::Item. + * @brief SmartVector of Item pointers to all Item objects. + */ + SmartVector items; + + /** + * @brief SmartVector of SolidSurface pointers to all SolidSurface objects. */ - SmartVector base_items; + SmartVector solid_surfaces; }; \ No newline at end of file diff --git a/include/server/game/solidsurface.hpp b/include/server/game/solidsurface.hpp new file mode 100644 index 00000000..abc99ecf --- /dev/null +++ b/include/server/game/solidsurface.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "server/game/object.hpp" +#include "shared/game/sharedobject.hpp" + +class SolidSurface : public Object { +public: + SolidSurface(); + + ~SolidSurface(); + + SharedSolidSurface shared{}; + + virtual SharedObject toShared() override; +}; \ No newline at end of file diff --git a/include/shared/game/sharedobject.hpp b/include/shared/game/sharedobject.hpp index e1138888..d4abffb2 100644 --- a/include/shared/game/sharedobject.hpp +++ b/include/shared/game/sharedobject.hpp @@ -13,7 +13,9 @@ * class names in the inheritance tree in which Object is the root. */ enum class ObjectType { - Object // Generic object type (base class) + Object, // Generic object type (base class) + Item, + SolidSurface }; /** @@ -26,6 +28,10 @@ std::string objectTypeString(ObjectType type); struct Stats { float health; float speed; + + DEF_SERIALIZE(Archive& ar, const unsigned int version) { + ar& health& speed; + } }; struct ItemInfo { @@ -36,6 +42,34 @@ struct ItemInfo { float scalar; float timer; ItemType type; + + DEF_SERIALIZE(Archive& ar, const unsigned int version) { + ar& held& used& scalar& timer& type; + } +}; + +enum class SurfaceType { + Wall, + Floor, + Ceiling +}; + +struct SharedSolidSurface { + /** + * @brief Dimensions of the solid surface in 3 dimensions. The position of + * the SolidSurface object is at the center of the object. + */ + glm::vec3 dimensions; + + /** + * @brief Type of solid surface, e.g. wall, floor, ceiling, etc.(relevant + * for rendering) + */ + SurfaceType surfaceType; + + DEF_SERIALIZE(Archive& ar, const unsigned int version) { + ar& dimensions& surfaceType; + } }; struct SharedPhysics { @@ -48,6 +82,10 @@ struct SharedPhysics { * @brief 3-D vector that denotes this object's facing direction. */ glm::vec3 facing; + + DEF_SERIALIZE(Archive& ar, const unsigned int version) { + ar& position& facing; + } }; /** @@ -60,14 +98,15 @@ class SharedObject { ObjectType type; SharedPhysics physics; - std::optional stats; - std::optional iteminfo; + boost::optional stats; + boost::optional iteminfo; + boost::optional solidSurface; SharedObject() {} // cppcheck-suppress uninitMemberVar ~SharedObject() {} - + DEF_SERIALIZE(Archive& ar, const unsigned int version) { - ar& globalID & type& physics.position & physics.facing; + ar& globalID & type& physics & stats & iteminfo & solidSurface; } private: }; \ No newline at end of file diff --git a/include/shared/utilities/serialize_macro.hpp b/include/shared/utilities/serialize_macro.hpp index fd8f9172..ddd269cc 100644 --- a/include/shared/utilities/serialize_macro.hpp +++ b/include/shared/utilities/serialize_macro.hpp @@ -10,6 +10,7 @@ #include #include #include +#include // Helper macro to reduce boilerplate in making boost::serialize-able structs #define DEF_SERIALIZE \ diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index 237058c6..50a40728 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -8,6 +8,7 @@ set(FILES game/spherecollider.cpp game/creature.cpp game/item.cpp + game/solidsurface.cpp game/object.cpp game/servergamestate.cpp game/objectmanager.cpp diff --git a/src/server/game/item.cpp b/src/server/game/item.cpp index 9542842c..dbb256bf 100644 --- a/src/server/game/item.cpp +++ b/src/server/game/item.cpp @@ -1,6 +1,16 @@ #include "server/game/item.hpp" #include "shared/game/sharedobject.hpp" +/* Constructors and Destructors */ +Item::Item() : Object(ObjectType::Item) { + +} + +Item::~Item() { + +} + +/* SharedGameState generation */ SharedObject Item::toShared() { auto so = Object::toShared(); so.iteminfo = this->iteminfo; diff --git a/src/server/game/objectmanager.cpp b/src/server/game/objectmanager.cpp index 6fec9598..6b2157d4 100644 --- a/src/server/game/objectmanager.cpp +++ b/src/server/game/objectmanager.cpp @@ -10,7 +10,7 @@ ObjectManager::ObjectManager() { // Initialize type-specific SmartVectors this->base_objects = SmartVector(); - this->base_items = SmartVector(); + this->items = SmartVector(); } ObjectManager::~ObjectManager() { @@ -25,23 +25,54 @@ EntityID ObjectManager::createObject(ObjectType type) { SpecificID typeID; switch (type) { - case ObjectType::Object: - // Create a new object of type Object - Object* object = new Object(ObjectType::Object); + case ObjectType::Object: { + // Create a new object of type Object + Object* object = new Object(ObjectType::Object); + + // TODO: Maybe change SmartVector's index return value? size_t is + // larger than uint32 (which is what SpecificID and EntityID are + // defined as) + // Push to type-specific base_objects vector + typeID = (SpecificID)this->base_objects.push(object); + + // Push to global objects vector + globalID = (EntityID)this->objects.push(object); + + // Set object's type and global IDs + object->typeID = typeID; + object->globalID = globalID; + break; + } + case ObjectType::Item: { + // Create a new object of type Item + Item* item = new Item(); - // TODO: Maybe change SmartVector's index return value? size_t is - // larger than uint32 (which is what SpecificID and EntityID are - // defined as) - // Push to type-specific base_objects vector - typeID = (SpecificID) this->base_objects.push(object); + // Push to type-specific items vector + typeID = (SpecificID)this->items.push(item); - // Push to global objects vector - globalID = (EntityID) this->objects.push(object); + // Push to global objects vector + globalID = (EntityID)this->objects.push(item); - // Set object's type and global IDs - object->typeID = typeID; - object->globalID = globalID; - break; + // Set items' type and global IDs + item->typeID = typeID; + item->globalID = globalID; + break; + } + case ObjectType::SolidSurface: { + // Create a new object of type SolidSurface + SolidSurface* solidSurface = new SolidSurface(); + + // Push to type-specific solid_surfaces vector + typeID = (SpecificID)this->solid_surfaces.push(solidSurface); + + // Push to global objects vector + globalID = (EntityID)this->objects.push(solidSurface); + + // Set solidSurface's type and global IDs + solidSurface->typeID = typeID; + solidSurface->globalID = globalID; + break; + } } // Return new object's global EntityID @@ -86,7 +117,11 @@ SmartVector ObjectManager::getObjects() { } SmartVector ObjectManager::getItems() { - return this->base_items; + return this->items; +} + +SmartVector ObjectManager::getSolidSurfaces() { + return this->solid_surfaces; } /* SharedGameState generation */ diff --git a/src/server/game/solidsurface.cpp b/src/server/game/solidsurface.cpp new file mode 100644 index 00000000..04d2c6db --- /dev/null +++ b/src/server/game/solidsurface.cpp @@ -0,0 +1,17 @@ +#include "server/game/solidsurface.hpp" + +/* Constructors and Destructors */ +SolidSurface::SolidSurface() : Object(ObjectType::SolidSurface) { + +} + +SolidSurface::~SolidSurface() {} + +/* SharedGameState generation */ +SharedObject SolidSurface::toShared() { + SharedObject shared = Object::toShared(); + + shared.solidSurface = this->shared; + + return shared; +} \ No newline at end of file diff --git a/src/server/server.cpp b/src/server/server.cpp index afd48ec2..3223e1a5 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -33,6 +33,53 @@ Server::Server(boost::asio::io_context& io_context, GameConfig config) state(ServerGameState(GamePhase::LOBBY, config)) { state.objects.createObject(ObjectType::Object); + + // Create a room + EntityID wall1ID = state.objects.createObject(ObjectType::SolidSurface); + EntityID wall2ID = state.objects.createObject(ObjectType::SolidSurface); + EntityID wall3ID = state.objects.createObject(ObjectType::SolidSurface); + EntityID wall4ID = state.objects.createObject(ObjectType::SolidSurface); + EntityID floorID = state.objects.createObject(ObjectType::SolidSurface); + + // Specify wall positions + // Configuration: 40 (x) x 32 (y) room example + // ##1## + // # # + // 2 3 + // # # + // ##4## + + SolidSurface* wall1 = (SolidSurface*)state.objects.getObject(wall1ID); + SolidSurface* wall2 = (SolidSurface*)state.objects.getObject(wall2ID); + SolidSurface* wall3 = (SolidSurface*)state.objects.getObject(wall3ID); + SolidSurface* wall4 = (SolidSurface*)state.objects.getObject(wall4ID); + SolidSurface* floor = (SolidSurface*)state.objects.getObject(floorID); + + // Wall1 has dimensions (40, 1, 4) and position (0, 15.5, 2) + wall1->shared.dimensions = glm::vec3(40, 1, 4); + wall1->physics.shared.position = glm::vec3(0, 15.5, 2); + wall1->physics.movable = false; + + // Wall2 has dimensions (30, 1, 4) and position (-19.5, 0, 2) + wall2->shared.dimensions = glm::vec3(30, 1, 4); + wall2->physics.shared.position = glm::vec3(-19.5, 0, 2); + wall2->physics.movable = false; + + // Wall3 has dimensions (30, 1, 4) and position (19.5, 0, 2) + wall3->shared.dimensions = glm::vec3(30, 1, 4); + wall3->physics.shared.position = glm::vec3(19.5, 0, 2); + wall3->physics.movable = false; + + // Wall4 has dimensions (40, 1, 4) and position (0, -15.5, 2) + wall4->shared.dimensions = glm::vec3(40, 1, 4); + wall4->physics.shared.position = glm::vec3(0, -15.5, 2); + wall4->physics.movable = false; + + // floor has dimensions (40, 32, 1) and position (0, 0, -0.5) + floor->shared.dimensions = glm::vec3(40, 32, 1); + floor->physics.shared.position = glm::vec3(0, 0, -0.5); + floor->physics.movable = false; + _doAccept(); // start asynchronously accepting @@ -44,6 +91,8 @@ Server::Server(boost::asio::io_context& io_context, GameConfig config) } } +// Note: This method should probably be removed since EntityIDs for objects +// are assigned by the ObjectManager EntityID Server::genNewEID() { static EntityID id = 1; return id++;