Skip to content

Commit

Permalink
h
Browse files Browse the repository at this point in the history
  • Loading branch information
JackAshwell11 committed Jan 20, 2024
1 parent 1dcb597 commit 7746c67
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
59 changes: 45 additions & 14 deletions src/hades_extensions/include/game_objects/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <string>
#include <typeindex>

// External headers
#include <chipmunk/chipmunk.h>

// Local headers
#include "steering.hpp"

Expand Down Expand Up @@ -82,6 +85,45 @@ class SystemBase {
const Registry *registry;
};

// ----- RAII TYPES ------------------------------
/// Allows for the RAII management of a Chipmunk2D object.
///
/// @tparam T - The type of Chipmunk2D object to manage.
/// @tparam Constructor - The constructor function for the Chipmunk2D object.
/// @tparam Destructor - The destructor function for the Chipmunk2D object.
template <typename T, T *(*Constructor)(), void (*Destructor)(T *)>
class ChipmunkHandle {
/// The Chipmunk2D object.
T *_obj;

public:
/// Initialise the object.
///
/// @tparam Args - The types of the arguments to pass to the constructor.
/// @param args - The arguments to pass to the constructor.
template <typename... Args>
explicit ChipmunkHandle(Args... args) : _obj(Constructor(args...)) {}

/// Destroy the object.
~ChipmunkHandle() {
if (_obj) {
Destructor(_obj);
}
}

/// The copy constructor.
ChipmunkHandle(const ChipmunkHandle &) = delete;

/// The copy assignment operator.
auto operator=(const ChipmunkHandle &) -> ChipmunkHandle & = delete;

/// The move constructor.
ChipmunkHandle(ChipmunkHandle &&) = delete;

/// The move assignment operator.
auto operator=(ChipmunkHandle &&) -> ChipmunkHandle & = delete;
};

// ----- EXCEPTIONS ------------------------------
/// Raised when an error occurs with the registry.
struct RegistryError final : std::runtime_error {
Expand All @@ -103,20 +145,6 @@ struct RegistryError final : std::runtime_error {
"` is not registered with the registry" + extra + ".") {}
};

// ----- STRUCTS ------------------------------
// make custom deallocators for chipmunk2d
struct ChipmunkBodyDeleter {
void operator()(cpBody *body) const { cpBodyFree(body); }
};

struct ChipmunkShapeDeleter {
void operator()(cpShape *shape) const { cpShapeFree(shape); }
};

struct ChipmunkSpaceDeleter {
void operator()(cpSpace *space) const { cpSpaceFree(space); }
};

// ----- CLASSES ------------------------------
/// Manages game objects, components, and systems that are registered.
class Registry {
Expand Down Expand Up @@ -261,4 +289,7 @@ class Registry {

/// The walls registered with the registry.
std::unordered_set<Vec2d> walls_;

/// The Chipmunk2D space.
ChipmunkHandle<cpSpace, cpSpaceNew, cpSpaceFree> space_;
};
30 changes: 30 additions & 0 deletions src/hades_extensions/include/game_objects/systems/physics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Ensure this file is only included once
#pragma once

// Local headers
#include "game_objects/registry.hpp"

// ----- COMPONENTS ------------------------------
/// Allows a game object to interact with the physics system.
struct KinematicComponent final : ComponentBase {
/// The position of the game object.
Vec2d position{0, 0};

/// The velocity of the game object.
Vec2d velocity{0, 0};

/// The rotation of the game object.
double rotation{0};

/// The Chipmunk2D body of the game object.
ChipmunkHandle<cpBody, cpBodyNew, cpBodyFree> body{cpBodyNew(1, 1)};
};

// ----- SYSTEMS ------------------------------
/// Provides facilities to manipulate
struct PhysicsSystem final : SystemBase {
/// Initialise the object.
///
/// @param registry - The registry that manages the game objects, components, and systems.
explicit PhysicsSystem(const Registry *registry) : SystemBase(registry) {}
};
1 change: 1 addition & 0 deletions src/hades_extensions/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(${CPP_LIB} STATIC
${CMAKE_SOURCE_DIR}/src/game_objects/systems/effects.cpp
${CMAKE_SOURCE_DIR}/src/game_objects/systems/inventory.cpp
${CMAKE_SOURCE_DIR}/src/game_objects/systems/movements.cpp
${CMAKE_SOURCE_DIR}/src/game_objects/systems/physics.cpp
${CMAKE_SOURCE_DIR}/src/game_objects/systems/upgrade.cpp
${CMAKE_SOURCE_DIR}/src/generation/bsp.cpp
${CMAKE_SOURCE_DIR}/src/generation/map.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/hades_extensions/src/game_objects/systems/physics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Related header
#include "game_objects/systems/physics.hpp"

// ----- FUNCTIONS ------------------------------
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// External includes
// External headers
#include <gtest/gtest.h>

// Local headers
Expand Down

0 comments on commit 7746c67

Please sign in to comment.