Skip to content

Commit

Permalink
Merge pull request #65 from ucsd-cse125-sp24/feat/environment
Browse files Browse the repository at this point in the history
Initial `SolidSurface` Implementation
  • Loading branch information
gilkeidar authored May 1, 2024
2 parents c964ba3 + c334467 commit 09250f8
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 23 deletions.
18 changes: 16 additions & 2 deletions include/server/game/objectmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "server/game/object.hpp"
#include "server/game/item.hpp"
#include "server/game/solidsurface.hpp"

#include "shared/utilities/smartvector.hpp"

Expand Down Expand Up @@ -58,6 +59,14 @@ class ObjectManager {
*/
SmartVector<Item*> 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<SolidSurface*> getSolidSurfaces();

/* SharedGameState generation */

/**
Expand Down Expand Up @@ -115,7 +124,12 @@ class ObjectManager {
SmartVector<Object*> base_objects;

/**
* @brief SmartVector of Item pointers to all items ObjectType::Item.
* @brief SmartVector of Item pointers to all Item objects.
*/
SmartVector<Item*> items;

/**
* @brief SmartVector of SolidSurface pointers to all SolidSurface objects.
*/
SmartVector<Item*> base_items;
SmartVector<SolidSurface*> solid_surfaces;
};
14 changes: 14 additions & 0 deletions include/server/game/solidsurface.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
49 changes: 44 additions & 5 deletions include/shared/game/sharedobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
}
};

/**
Expand All @@ -60,14 +98,15 @@ class SharedObject {
ObjectType type;
SharedPhysics physics;

std::optional<Stats> stats;
std::optional<ItemInfo> iteminfo;
boost::optional<Stats> stats;
boost::optional<ItemInfo> iteminfo;
boost::optional<SharedSolidSurface> 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:
};
1 change: 1 addition & 0 deletions include/shared/utilities/serialize_macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <boost/serialization/unique_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/optional.hpp>

// Helper macro to reduce boilerplate in making boost::serialize-able structs
#define DEF_SERIALIZE \
Expand Down
1 change: 1 addition & 0 deletions src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/server/game/item.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
67 changes: 51 additions & 16 deletions src/server/game/objectmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ObjectManager::ObjectManager() {

// Initialize type-specific SmartVectors
this->base_objects = SmartVector<Object*>();
this->base_items = SmartVector<Item*>();
this->items = SmartVector<Item*>();
}

ObjectManager::~ObjectManager() {
Expand All @@ -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
Expand Down Expand Up @@ -86,7 +117,11 @@ SmartVector<Object*> ObjectManager::getObjects() {
}

SmartVector<Item*> ObjectManager::getItems() {
return this->base_items;
return this->items;
}

SmartVector<SolidSurface*> ObjectManager::getSolidSurfaces() {
return this->solid_surfaces;
}

/* SharedGameState generation */
Expand Down
17 changes: 17 additions & 0 deletions src/server/game/solidsurface.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
49 changes: 49 additions & 0 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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++;
Expand Down

0 comments on commit 09250f8

Please sign in to comment.