aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h2
-rw-r--r--src/crepe/Component.cpp3
-rw-r--r--src/crepe/Component.h30
-rw-r--r--src/crepe/ComponentManager.cpp7
-rw-r--r--src/crepe/ComponentManager.h103
-rw-r--r--src/crepe/ComponentManager.hpp59
-rw-r--r--src/crepe/api/AudioSource.h2
-rw-r--r--src/crepe/api/BehaviorScript.h5
-rw-r--r--src/crepe/api/CMakeLists.txt11
-rw-r--r--src/crepe/api/CircleCollider.h2
-rw-r--r--src/crepe/api/Config.h10
-rw-r--r--src/crepe/api/Force.cpp21
-rw-r--r--src/crepe/api/Force.h17
-rw-r--r--src/crepe/api/GameObject.cpp28
-rw-r--r--src/crepe/api/GameObject.h57
-rw-r--r--src/crepe/api/GameObject.hpp2
-rw-r--r--src/crepe/api/Metadata.cpp7
-rw-r--r--src/crepe/api/Metadata.h43
-rw-r--r--src/crepe/api/ParticleEmitter.cpp17
-rw-r--r--src/crepe/api/ParticleEmitter.h2
-rw-r--r--src/crepe/api/Point.h11
-rw-r--r--src/crepe/api/Rigidbody.cpp14
-rw-r--r--src/crepe/api/Rigidbody.h108
-rw-r--r--src/crepe/api/Scene.cpp5
-rw-r--r--src/crepe/api/Scene.h17
-rw-r--r--src/crepe/api/SceneManager.cpp38
-rw-r--r--src/crepe/api/SceneManager.h50
-rw-r--r--src/crepe/api/SceneManager.hpp18
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Sprite.h6
-rw-r--r--src/crepe/api/Transform.cpp11
-rw-r--r--src/crepe/api/Transform.h32
-rw-r--r--src/crepe/api/Vector2.cpp57
-rw-r--r--src/crepe/api/Vector2.h47
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/SDLApp.cpp71
-rw-r--r--src/crepe/facade/SDLApp.h26
-rw-r--r--src/crepe/system/PhysicsSystem.cpp106
-rw-r--r--src/crepe/system/PhysicsSystem.h17
-rw-r--r--src/crepe/system/RenderSystem.cpp2
-rw-r--r--src/crepe/system/ScriptSystem.cpp8
-rw-r--r--src/crepe/system/ScriptSystem.h5
-rw-r--r--src/crepe/system/System.h14
-rw-r--r--src/crepe/types.h9
45 files changed, 777 insertions, 331 deletions
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index 13a3f33..bbec488 100644
--- a/src/crepe/Collider.cpp
+++ b/src/crepe/Collider.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId) {}
+Collider::Collider(game_object_id_t id) : Component(id) {}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index 68a7d1d..827f83d 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -6,7 +6,7 @@ namespace crepe {
class Collider : public Component {
public:
- Collider(uint32_t game_object_id);
+ Collider(game_object_id_t id);
int size;
};
diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp
index 358ce31..73c2d07 100644
--- a/src/crepe/Component.cpp
+++ b/src/crepe/Component.cpp
@@ -1,5 +1,6 @@
#include "Component.h"
+#include "types.h"
using namespace crepe;
-Component::Component(uint32_t id) : game_object_id(id), active(true) {}
+Component::Component(game_object_id_t id) : game_object_id(id) {}
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index bc44865..0fe60b2 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -1,22 +1,46 @@
#pragma once
+#include "types.h"
+
#include <cstdint>
namespace crepe {
class ComponentManager;
+/**
+ * \brief Base class for all components
+ *
+ * This class is the base class for all components. It provides a common
+ * interface for all components.
+ */
class Component {
protected:
+ //! Only the ComponentManager can create components
friend class crepe::ComponentManager;
- Component(uint32_t id);
+ /**
+ * \param id The id of the GameObject this component belongs to
+ */
+ Component(game_object_id_t id);
public:
virtual ~Component() = default;
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * This method returns -1 by default, which means that there is no limit
+ * for the number of instances. Concrete components can override this method
+ * to set a limit.
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return -1; }
public:
- uint32_t game_object_id;
- bool active;
+ //! The id of the GameObject this component belongs to
+ const game_object_id_t game_object_id;
+ //! Whether the component is active
+ bool active = true;
};
} // namespace crepe
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp
index 8bde33a..85149c8 100644
--- a/src/crepe/ComponentManager.cpp
+++ b/src/crepe/ComponentManager.cpp
@@ -1,6 +1,7 @@
-#include "ComponentManager.h"
#include "util/log.h"
+#include "ComponentManager.h"
+
using namespace crepe;
ComponentManager & ComponentManager::get_instance() {
@@ -8,9 +9,9 @@ ComponentManager & ComponentManager::get_instance() {
return instance;
}
-void ComponentManager::delete_all_components_of_id(uint32_t id) {
+void ComponentManager::delete_all_components_of_id(game_object_id_t id) {
// Loop through all the types (in the unordered_map<>)
- for (auto & [type, componentArray] : components) {
+ for (auto & [type, componentArray] : this->components) {
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id < componentArray.size()) {
// Clear the components at this specific id
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index 2b5e1df..c8c196c 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -10,47 +10,110 @@
namespace crepe {
+/**
+ * \brief Manages all components
+ *
+ * This class manages all components. It provides methods to add, delete and get
+ * components.
+ */
class ComponentManager {
public:
- // Singleton
+ /**
+ * \brief Get the instance of the ComponentManager
+ *
+ * \return The instance of the ComponentManager
+ */
static ComponentManager & get_instance();
ComponentManager(const ComponentManager &) = delete;
ComponentManager(ComponentManager &&) = delete;
ComponentManager & operator=(const ComponentManager &) = delete;
ComponentManager & operator=(ComponentManager &&) = delete;
+ ~ComponentManager();
-public:
- //! Add a component of a specific type
+ /**
+ * \brief Add a component to the ComponentManager
+ *
+ * This method adds a component to the ComponentManager. The component is
+ * created with the given arguments and added to the ComponentManager.
+ *
+ * \tparam T The type of the component
+ * \tparam Args The types of the arguments
+ * \param id The id of the GameObject this component belongs to
+ * \param args The arguments to create the component
+ * \return The created component
+ */
template <typename T, typename... Args>
- T & add_component(uint32_t id, Args &&... args);
- //! Deletes all components of a specific type and id
+ T & add_component(game_object_id_t id, Args &&... args);
+ /**
+ * \brief Delete all components of a specific type and id
+ *
+ * This method deletes all components of a specific type and id.
+ *
+ * \tparam T The type of the component
+ * \param id The id of the GameObject this component belongs to
+ */
template <typename T>
- void delete_components_by_id(uint32_t id);
- //! Deletes all components of a specific type
+ void delete_components_by_id(game_object_id_t id);
+ /**
+ * \brief Delete all components of a specific type
+ *
+ * This method deletes all components of a specific type.
+ *
+ * \tparam T The type of the component
+ */
template <typename T>
void delete_components();
- //! Deletes all components of a specific id
- void delete_all_components_of_id(uint32_t id);
- //! Deletes all components
+ /**
+ * \brief Delete all components of a specific id
+ *
+ * This method deletes all components of a specific id.
+ *
+ * \param id The id of the GameObject this component belongs to
+ */
+ void delete_all_components_of_id(game_object_id_t id);
+ /**
+ * \brief Delete all components
+ *
+ * This method deletes all components.
+ */
void delete_all_components();
-
- //! Get a vector<> of all components at specific type and id
+ /**
+ * \brief Get all components of a specific type and id
+ *
+ * This method gets all components of a specific type and id.
+ *
+ * \tparam T The type of the component
+ * \param id The id of the GameObject this component belongs to
+ * \return A vector of all components of the specific type and id
+ */
template <typename T>
std::vector<std::reference_wrapper<T>>
- get_components_by_id(uint32_t id) const;
- //! Get a vector<> of all components of a specific type
+ get_components_by_id(game_object_id_t id) const;
+ /**
+ * \brief Get all components of a specific type
+ *
+ * This method gets all components of a specific type.
+ *
+ * \tparam T The type of the component
+ * \return A vector of all components of the specific type
+ */
template <typename T>
std::vector<std::reference_wrapper<T>> get_components_by_type() const;
private:
ComponentManager();
- virtual ~ComponentManager();
- /*
- * The std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> below might seem a bit strange, let me explain this structure:
- * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index.
- * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component.
- * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id).
+private:
+ /**
+ * \brief The components
+ *
+ * This unordered_map stores all components. The key is the type of the
+ * component and the value is a vector of vectors of unique pointers to the
+ * components.
+ * Every component type has its own vector of vectors of unique pointers to
+ * the components. The first vector is for the ids of the GameObjects and the
+ * second vector is for the components (because a GameObject might have multiple
+ * components).
*/
std::unordered_map<std::type_index,
std::vector<std::vector<std::unique_ptr<Component>>>>
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 9b07f13..98efb49 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -3,11 +3,12 @@
#include <type_traits>
#include "ComponentManager.h"
+#include "types.h"
namespace crepe {
template <class T, typename... Args>
-T & ComponentManager::add_component(uint32_t id, Args &&... args) {
+T & ComponentManager::add_component(game_object_id_t id, Args &&... args) {
using namespace std;
static_assert(is_base_of<Component, T>::value,
@@ -17,38 +18,51 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
type_index type = typeid(T);
// Check if this component type is already in the unordered_map<>
- if (components.find(type) == components.end()) {
+ if (this->components.find(type) == this->components.end()) {
//If not, create a new (empty) vector<> of vector<unique_ptr<Component>>
- components[type] = vector<vector<unique_ptr<Component>>>();
+ this->components[type] = vector<vector<unique_ptr<Component>>>();
}
// Resize the vector<> if the id is greater than the current size
- if (id >= components[type].size()) {
+ if (id >= this->components[type].size()) {
// Initialize new slots to nullptr (resize does automatically init to nullptr)
- components[type].resize(id + 1);
+ this->components[type].resize(id + 1);
}
// Create a new component of type T (arguments directly forwarded). The
// constructor must be called by ComponentManager.
- T * instance = new T(id, forward<Args>(args)...);
+ T * instance_ptr = new T(id, forward<Args>(args)...);
+ if (instance_ptr == nullptr) throw std::bad_alloc();
+
+ T & instance_ref = *instance_ptr;
+ unique_ptr<T> instance = unique_ptr<T>(instance_ptr);
+
+ // Check if the vector size is not greater than get_instances_max
+ int max_instances = instance->get_instances_max();
+ if (max_instances != -1 && components[type][id].size() >= max_instances) {
+ // TODO: Exception
+ throw std::runtime_error(
+ "Exceeded maximum number of instances for this component type");
+ }
+
// store its unique_ptr in the vector<>
- components[type][id].push_back(unique_ptr<T>(instance));
+ this->components[type][id].push_back(std::move(instance));
- return *instance;
+ return instance_ref;
}
template <typename T>
-void ComponentManager::delete_components_by_id(uint32_t id) {
+void ComponentManager::delete_components_by_id(game_object_id_t id) {
using namespace std;
// Determine the type of T (this is used as the key of the unordered_map<>)
type_index type = typeid(T);
// Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
+ if (this->components.find(type) != this->components.end()) {
// Get the correct vector<>
vector<vector<unique_ptr<Component>>> & component_array
- = components[type];
+ = this->components[type];
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id < component_array.size()) {
@@ -63,14 +77,14 @@ void ComponentManager::delete_components() {
// Determine the type of T (this is used as the key of the unordered_map<>)
std::type_index type = typeid(T);
- if (components.find(type) == components.end()) return;
+ if (this->components.find(type) == this->components.end()) return;
- components[type].clear();
+ this->components[type].clear();
}
template <typename T>
std::vector<std::reference_wrapper<T>>
-ComponentManager::get_components_by_id(uint32_t id) const {
+ComponentManager::get_components_by_id(game_object_id_t id) const {
using namespace std;
// Determine the type of T (this is used as the key of the unordered_map<>)
@@ -79,11 +93,12 @@ ComponentManager::get_components_by_id(uint32_t id) const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- if (components.find(type) == components.end()) return component_vector;
+ if (this->components.find(type) == this->components.end())
+ return component_vector;
// Get the correct vector<>
const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
+ = this->components.at(type);
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id >= component_array.size()) return component_vector;
@@ -112,15 +127,14 @@ ComponentManager::get_components_by_type() const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- // Set the id to 0 (the id will also be stored in the returned vector<>)
- // uint32_t id = 0;
// Find the type (in the unordered_map<>)
- if (components.find(type) == components.end()) return component_vector;
+ if (this->components.find(type) == this->components.end())
+ return component_vector;
// Get the correct vector<>
const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
+ = this->components.at(type);
// Loop through the whole vector<>
for (const vector<unique_ptr<Component>> & component : component_array) {
@@ -132,12 +146,9 @@ ComponentManager::get_components_by_type() const {
// Ensure that the cast was successful
if (casted_component == nullptr) continue;
- // Pair the dereferenced raw pointer and the id and add it to the vector<>
+ // Add the dereferenced raw pointer to the vector<>
component_vector.emplace_back(ref(*casted_component));
}
-
- // Increase the id (the id will also be stored in the returned vector<>)
- //++id;
}
// Return the vector<>
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
index 42add50..1e24ae8 100644
--- a/src/crepe/api/AudioSource.h
+++ b/src/crepe/api/AudioSource.h
@@ -10,7 +10,7 @@ namespace crepe {
class Sound;
//! Audio source component
-class AudioSource : Component {
+class AudioSource : public Component {
public:
AudioSource(std::unique_ptr<Asset> audio_clip);
virtual ~AudioSource() = default;
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index 21638f4..6b1fec7 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -5,12 +5,9 @@
#include "../Component.h"
namespace crepe {
+
class ScriptSystem;
class ComponentManager;
-} // namespace crepe
-
-namespace crepe {
-
class Script;
class BehaviorScript : public Component {
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index f55bd9e..3b20142 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -4,7 +4,6 @@ target_sources(crepe PUBLIC
Script.cpp
GameObject.cpp
Rigidbody.cpp
- Force.cpp
ParticleEmitter.cpp
Transform.cpp
Color.cpp
@@ -13,6 +12,10 @@ target_sources(crepe PUBLIC
Sprite.cpp
SaveManager.cpp
Config.cpp
+ Metadata.cpp
+ Scene.cpp
+ SceneManager.cpp
+ Vector2.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -25,10 +28,14 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
GameObject.hpp
Rigidbody.h
Sprite.h
- Point.h
+ Vector2.h
Color.h
Texture.h
AssetManager.h
AssetManager.hpp
SaveManager.h
+ Scene.h
+ Metadata.h
+ SceneManager.h
+ SceneManager.hpp
)
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index 931b012..caa7e43 100644
--- a/src/crepe/api/CircleCollider.h
+++ b/src/crepe/api/CircleCollider.h
@@ -5,7 +5,7 @@ namespace crepe {
class CircleCollider : public Collider {
public:
- CircleCollider(uint32_t game_object_id, int radius)
+ CircleCollider(game_object_id_t game_object_id, int radius)
: Collider(game_object_id), radius(radius) {}
int radius;
};
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index bbcca87..56e3af5 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -47,6 +47,16 @@ public:
*/
std::string location = "save.crepe.db";
} savemgr;
+
+ //! physics-related settings
+ struct {
+ /**
+ * \brief gravity value of physics system
+ *
+ * Gravity value of game.
+ */
+ double gravity = 1;
+ } physics;
};
} // namespace crepe
diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp
deleted file mode 100644
index 3c33ad3..0000000
--- a/src/crepe/api/Force.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <cmath>
-
-#include "Force.h"
-
-namespace crepe {
-
-Force::Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction)
- : Component(game_object_id) {
- // TODO: A standard angle unit should be established for the entire engine
- // and assumed to be the default everywhere. Only conversion functions should
- // explicitly contain the unit (i.e. `deg_to_rad()` & `rad_to_deg()`)
-
- // Convert direction from degrees to radians
- float radian_direction = static_cast<float>(direction) * (M_PI / 180.0f);
- force_x = static_cast<int32_t>(
- std::round(magnitude * std::cos(radian_direction)));
- force_y = static_cast<int32_t>(
- std::round(magnitude * std::sin(radian_direction)));
-}
-
-} // namespace crepe
diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h
deleted file mode 100644
index c08a8b9..0000000
--- a/src/crepe/api/Force.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include <cstdint>
-
-#include "../Component.h"
-
-namespace crepe {
-
-class Force : public Component {
-public:
- Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction);
-
- int32_t force_x;
- int32_t force_y;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp
index 445a60d..d252e77 100644
--- a/src/crepe/api/GameObject.cpp
+++ b/src/crepe/api/GameObject.cpp
@@ -1,7 +1,31 @@
+#include "api/Transform.h"
+
#include "GameObject.h"
+#include "Metadata.h"
using namespace crepe;
using namespace std;
-GameObject::GameObject(uint32_t id, string name, string tag, int layer)
- : id(id), name(name), tag(tag), active(true), layer(layer) {}
+GameObject::GameObject(game_object_id_t id, const std::string & name,
+ const std::string & tag, const Vector2 & position,
+ double rotation, double scale)
+ : id(id) {
+ // Add Transform and Metadata components
+ ComponentManager & mgr = ComponentManager::get_instance();
+ mgr.add_component<Transform>(this->id, position, rotation, scale);
+ mgr.add_component<Metadata>(this->id, name, tag);
+}
+
+void GameObject::set_parent(const GameObject & parent) {
+ ComponentManager & mgr = ComponentManager::get_instance();
+
+ // Set parent on own Metadata component
+ vector<reference_wrapper<Metadata>> this_metadata
+ = mgr.get_components_by_id<Metadata>(this->id);
+ this_metadata.at(0).get().parent = parent.id;
+
+ // Add own id to children list of parent's Metadata component
+ vector<reference_wrapper<Metadata>> parent_metadata
+ = mgr.get_components_by_id<Metadata>(parent.id);
+ parent_metadata.at(0).get().children.push_back(this->id);
+}
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
index b5d6399..d703730 100644
--- a/src/crepe/api/GameObject.h
+++ b/src/crepe/api/GameObject.h
@@ -1,22 +1,63 @@
#pragma once
-#include <cstdint>
#include <string>
+#include "types.h"
+
namespace crepe {
+class Vector2;
+
+/**
+ * \brief Represents a GameObject
+ *
+ * This class represents a GameObject. The GameObject class is only used
+ * as an interface for the game programmer. The actual implementation is
+ * done in the ComponentManager.
+ */
class GameObject {
public:
- GameObject(uint32_t id, std::string name, std::string tag, int layer);
-
+ /**
+ * This constructor creates a new GameObject. It creates a new
+ * Transform and Metadata component and adds them to the ComponentManager.
+ *
+ * \param id The id of the GameObject
+ * \param name The name of the GameObject
+ * \param tag The tag of the GameObject
+ * \param position The position of the GameObject
+ * \param rotation The rotation of the GameObject
+ * \param scale The scale of the GameObject
+ */
+ GameObject(game_object_id_t id, const std::string & name,
+ const std::string & tag, const Vector2 & position,
+ double rotation, double scale);
+ /**
+ * \brief Set the parent of this GameObject
+ *
+ * This method sets the parent of this GameObject. It sets the parent
+ * in the Metadata component of this GameObject and adds this GameObject
+ * to the children list of the parent GameObject.
+ *
+ * \param parent The parent GameObject
+ */
+ void set_parent(const GameObject & parent);
+ /**
+ * \brief Add a component to the GameObject
+ *
+ * This method adds a component to the GameObject. It forwards the
+ * arguments to the ComponentManager.
+ *
+ * \tparam T The type of the component
+ * \tparam Args The types of the arguments
+ * \param args The arguments to create the component
+ * \return The created component
+ */
template <typename T, typename... Args>
T & add_component(Args &&... args);
- uint32_t id;
- std::string name;
- std::string tag;
- bool active;
- int layer;
+public:
+ //! The id of the GameObject
+ const game_object_id_t id;
};
} // namespace crepe
diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp
index 77cf40e..bfba7fe 100644
--- a/src/crepe/api/GameObject.hpp
+++ b/src/crepe/api/GameObject.hpp
@@ -8,7 +8,7 @@ namespace crepe {
template <typename T, typename... Args>
T & GameObject::add_component(Args &&... args) {
- auto & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = ComponentManager::get_instance();
return mgr.add_component<T>(this->id, std::forward<Args>(args)...);
}
diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp
new file mode 100644
index 0000000..76f11d7
--- /dev/null
+++ b/src/crepe/api/Metadata.cpp
@@ -0,0 +1,7 @@
+#include "Metadata.h"
+
+using namespace crepe;
+using namespace std;
+
+Metadata::Metadata(game_object_id_t id, const string & name, const string & tag)
+ : Component(id), name(name), tag(tag) {}
diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h
new file mode 100644
index 0000000..c61e006
--- /dev/null
+++ b/src/crepe/api/Metadata.h
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "../Component.h"
+
+namespace crepe {
+
+/**
+ * \brief Metadata component
+ *
+ * This class represents the Metadata component. It stores the name, tag, parent
+ * and children of a GameObject.
+ */
+class Metadata : public Component {
+public:
+ /**
+ * \param game_object_id The id of the GameObject this component belongs to
+ * \param name The name of the GameObject
+ * \param tag The tag of the GameObject
+ */
+ Metadata(game_object_id_t id, const std::string & name,
+ const std::string & tag);
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return 1; }
+
+public:
+ //! The name of the GameObject
+ const std::string name;
+ //! The tag of the GameObject
+ const std::string tag;
+ //! The id of the parent GameObject (-1 if no parent)
+ game_object_id_t parent = -1;
+ //! The ids of the children GameObjects
+ std::vector<game_object_id_t> children;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp
index 0b3a9ee..3b2e2f2 100644
--- a/src/crepe/api/ParticleEmitter.cpp
+++ b/src/crepe/api/ParticleEmitter.cpp
@@ -6,15 +6,14 @@
using namespace crepe;
-ParticleEmitter::ParticleEmitter(uint32_t game_object_id,
- uint32_t max_particles, uint32_t emission_rate,
- uint32_t speed, uint32_t speed_offset,
- uint32_t angle, uint32_t angleOffset,
- float begin_lifespan, float end_lifespan)
- : Component(game_object_id), max_particles(max_particles),
- emission_rate(emission_rate), speed(speed), speed_offset(speed_offset),
- position{0, 0}, begin_lifespan(begin_lifespan),
- end_lifespan(end_lifespan) {
+ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles,
+ uint32_t emission_rate, uint32_t speed,
+ uint32_t speed_offset, uint32_t angle,
+ uint32_t angleOffset, float begin_lifespan,
+ float end_lifespan)
+ : Component(id), max_particles(max_particles), emission_rate(emission_rate),
+ speed(speed), speed_offset(speed_offset), position{0, 0},
+ begin_lifespan(begin_lifespan), end_lifespan(end_lifespan) {
std::srand(
static_cast<uint32_t>(std::time(nullptr))); // initialize random seed
std::cout << "Create emitter" << std::endl;
diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h
index 2e2e95b..5939723 100644
--- a/src/crepe/api/ParticleEmitter.h
+++ b/src/crepe/api/ParticleEmitter.h
@@ -10,7 +10,7 @@ namespace crepe {
class ParticleEmitter : public Component {
public:
- ParticleEmitter(uint32_t game_object_id, uint32_t max_particles,
+ ParticleEmitter(game_object_id_t id, uint32_t max_particles,
uint32_t emission_rate, uint32_t speed,
uint32_t speed_offset, uint32_t angle, uint32_t angleOffset,
float begin_lifespan, float end_lifespan);
diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h
deleted file mode 100644
index 575d624..0000000
--- a/src/crepe/api/Point.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-namespace crepe {
-
-class Point {
-public:
- double x;
- double y;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp
index 0a6262a..cbf1325 100644
--- a/src/crepe/api/Rigidbody.cpp
+++ b/src/crepe/api/Rigidbody.cpp
@@ -2,7 +2,13 @@
using namespace crepe;
-Rigidbody::Rigidbody(uint32_t game_object_id, int mass, int gravity_scale,
- BodyType bodyType)
- : Component(game_object_id), mass(mass), gravity_scale(gravity_scale),
- body_type(bodyType) {}
+crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data)
+ : Component(game_object_id), data(data) {}
+
+void crepe::Rigidbody::add_force_linear(const Vector2 & force) {
+ this->data.linear_velocity += force;
+}
+
+void crepe::Rigidbody::add_force_angular(double force) {
+ this->data.angular_velocity += force;
+}
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 518ed94..68481f4 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -4,27 +4,101 @@
#include "../Component.h"
-namespace crepe {
+#include "Vector2.h"
-// FIXME: can't this enum be defined inside the class declaration of Rigidbody?
-enum class BodyType {
- //! Does not move (e.g. walls, ground ...)
- STATIC,
- //! Moves and responds to forces (e.g. player, physics objects ...)
- DYNAMIC,
- //! Moves but does not respond to forces (e.g. moving platforms ...)
- KINEMATIC,
-};
+namespace crepe {
+/**
+ * \brief Rigidbody class
+ *
+ * This class is used by the physics sytem and collision system.
+ * It configures how to system interact with the gameobject for movement and collisions.
+ */
class Rigidbody : public Component {
public:
- Rigidbody(uint32_t game_object_id, int mass, int gravity_scale,
- BodyType body_type);
- int32_t velocity_x;
- int32_t velocity_y;
- int mass;
- int gravity_scale;
- BodyType body_type;
+ /**
+ * \brief BodyType enum
+ *
+ * This enum provides three bodytypes the physics sytem and collision system use.
+ */
+ enum class BodyType {
+ //! Does not move (e.g. walls, ground ...)
+ STATIC,
+ //! Moves and responds to forces (e.g. player, physics objects ...)
+ DYNAMIC,
+ //! Moves but does not respond to forces (e.g. moving platforms ...)
+ KINEMATIC,
+ };
+ /**
+ * \brief PhysicsConstraints to constrain movement
+ *
+ * This struct configures the movement constraint for this object.
+ * If a constraint is enabled the systems will not move the object.
+ */
+ struct PhysicsConstraints {
+ //! X constraint
+ bool x = false;
+ //! Y constraint
+ bool y = false;
+ //! rotation constraint
+ bool rotation = false;
+ };
+
+public:
+ /**
+ * \brief struct for Rigidbody data
+ *
+ * This struct holds the data for the Rigidbody.
+ */
+ struct Data {
+ //! objects mass
+ double mass = 0.0;
+ //! gravtiy scale
+ double gravity_scale = 0.0;
+ //! Changes if physics apply
+ BodyType body_type = BodyType::DYNAMIC;
+ //! linear velocity of object
+ Vector2 linear_velocity;
+ //! maximum linear velocity of object
+ Vector2 max_linear_velocity;
+ //! linear damping of object
+ Vector2 linear_damping;
+ //! angular velocity of object
+ double angular_velocity = 0.0;
+ //! max angular velocity of object
+ double max_angular_velocity = 0.0;
+ //! angular damping of object
+ double angular_damping = 0.0;
+ //! movements constraints of object
+ PhysicsConstraints constraints;
+ //! if gravity applies
+ bool use_gravity = true;
+ //! if object bounces
+ bool bounce = false;
+ };
+
+public:
+ /**
+ * \param game_object_id id of the gameobject the rigibody is added to.
+ * \param data struct to configure the rigidbody.
+ */
+ Rigidbody(uint32_t game_object_id, const Data & data);
+ //! struct to hold data of rigidbody
+ Data data;
+
+public:
+ /**
+ * \brief add a linear force to the Rigidbody.
+ *
+ * \param force Vector2 that is added to the linear force.
+ */
+ void add_force_linear(const Vector2 & force);
+ /**
+ * \brief add a angular force to the Rigidbody.
+ *
+ * \param force Vector2 that is added to the angular force.
+ */
+ void add_force_angular(double force);
};
} // namespace crepe
diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp
new file mode 100644
index 0000000..933edf4
--- /dev/null
+++ b/src/crepe/api/Scene.cpp
@@ -0,0 +1,5 @@
+#include "Scene.h"
+
+using namespace crepe;
+
+Scene::Scene(const std::string & name) : name(name) {}
diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h
new file mode 100644
index 0000000..f8bcc3d
--- /dev/null
+++ b/src/crepe/api/Scene.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+
+namespace crepe {
+
+class Scene {
+public:
+ Scene(const std::string & name);
+ virtual ~Scene() = default;
+ virtual void load_scene() = 0;
+
+public:
+ std::string name;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp
new file mode 100644
index 0000000..dfed6ee
--- /dev/null
+++ b/src/crepe/api/SceneManager.cpp
@@ -0,0 +1,38 @@
+#include <algorithm>
+#include <memory>
+
+#include "../ComponentManager.h"
+
+#include "SceneManager.h"
+
+using namespace crepe;
+using namespace std;
+
+SceneManager & SceneManager::get_instance() {
+ static SceneManager instance;
+ return instance;
+}
+
+void SceneManager::set_next_scene(const string & name) { next_scene = name; }
+
+void SceneManager::load_next_scene() {
+ // next scene not set
+ if (this->next_scene.empty()) return;
+
+ auto it
+ = find_if(this->scenes.begin(), this->scenes.end(),
+ [&next_scene = this->next_scene](unique_ptr<Scene> & scene) {
+ return scene->name == next_scene;
+ });
+
+ // next scene not found
+ if (it == this->scenes.end()) return;
+ unique_ptr<Scene> & scene = *it;
+
+ // Delete all components of the current scene
+ ComponentManager & mgr = ComponentManager::get_instance();
+ mgr.delete_all_components();
+
+ // Load the new scene
+ scene->load_scene();
+}
diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h
new file mode 100644
index 0000000..1e0e670
--- /dev/null
+++ b/src/crepe/api/SceneManager.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <memory>
+#include <queue>
+#include <vector>
+
+#include "Scene.h"
+
+namespace crepe {
+
+class SceneManager {
+public:
+ // Singleton
+ static SceneManager & get_instance();
+ SceneManager(const SceneManager &) = delete;
+ SceneManager(SceneManager &&) = delete;
+ SceneManager & operator=(const SceneManager &) = delete;
+ SceneManager & operator=(SceneManager &&) = delete;
+
+public:
+ /**
+ * \brief Add a new concrete scene to the scene manager
+ *
+ * \tparam T Type of concrete scene
+ * \param name Name of new scene
+ */
+ template <typename T>
+ void add_scene(const std::string & name);
+ /**
+ * \brief Set the next scene
+ *
+ * This scene will be loaded at the end of the frame
+ *
+ * \param name Name of the next scene
+ */
+ void set_next_scene(const std::string & name);
+ //! Load a new scene (if there is one)
+ void load_next_scene();
+
+private:
+ SceneManager() = default;
+
+private:
+ std::vector<std::unique_ptr<Scene>> scenes;
+ std::string next_scene;
+};
+
+} // namespace crepe
+
+#include "SceneManager.hpp"
diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp
new file mode 100644
index 0000000..8bad7b2
--- /dev/null
+++ b/src/crepe/api/SceneManager.hpp
@@ -0,0 +1,18 @@
+#include "SceneManager.h"
+
+namespace crepe {
+
+template <typename T>
+void SceneManager::add_scene(const std::string & name) {
+ static_assert(std::is_base_of<Scene, T>::value,
+ "T must be derived from Scene");
+
+ scenes.emplace_back(make_unique<T>(name));
+
+ // The first scene added, is the one that will be loaded at the beginning
+ if (next_scene.empty()) {
+ next_scene = name;
+ }
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 3dd44f2..d3465c7 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -10,8 +10,8 @@
using namespace std;
using namespace crepe;
-Sprite::Sprite(uint32_t id, shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip)
+Sprite::Sprite(game_object_id_t id, shared_ptr<Texture> image,
+ const Color & color, const FlipSettings & flip)
: Component(id), color(color), flip(flip), sprite_image(image) {
dbg_trace();
}
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index bdb4da9..00dcb27 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -12,14 +12,14 @@
namespace crepe {
struct FlipSettings {
- bool flip_x = 1;
- bool flip_y = 1;
+ bool flip_x = true;
+ bool flip_y = true;
};
class Sprite : public Component {
public:
- Sprite(uint32_t game_id, std::shared_ptr<Texture> image,
+ Sprite(game_object_id_t id, std::shared_ptr<Texture> image,
const Color & color, const FlipSettings & flip);
~Sprite();
std::shared_ptr<Texture> sprite_image;
diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp
index 70b16bd..a244bc5 100644
--- a/src/crepe/api/Transform.cpp
+++ b/src/crepe/api/Transform.cpp
@@ -1,16 +1,11 @@
-#include <cstdint>
-
-#include "api/Point.h"
#include "util/log.h"
-#include "Component.h"
#include "Transform.h"
using namespace crepe;
-Transform::Transform(uint32_t game_id, const Point & point, double rot,
- double scale)
- : Component(game_id), position(point), rotation(rot), scale(scale) {
+Transform::Transform(game_object_id_t id, const Vector2 & point,
+ double rotation, double scale)
+ : Component(id), position(point), rotation(rotation), scale(scale) {
dbg_trace();
}
-Transform::~Transform() { dbg_trace(); }
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index d416088..d7a5b8a 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -1,23 +1,37 @@
#pragma once
-#include <cstdint>
-
-#include "api/Point.h"
+#include "api/Vector2.h"
#include "Component.h"
namespace crepe {
+/**
+ * \brief Transform component
+ *
+ * This class represents the Transform component. It stores the position,
+ * rotation and scale of a GameObject.
+ */
class Transform : public Component {
- // FIXME: What's the difference between the `Point` and `Position`
- // classes/structs? How about we replace both with a universal `Vec2` that
- // works similar (or the same) as those found in GLSL?
+public:
+ /**
+ * \param id The id of the GameObject this component belongs to
+ * \param point The position of the GameObject
+ * \param rotation The rotation of the GameObject
+ * \param scale The scale of the GameObject
+ */
+ Transform(game_object_id_t id, const Vector2 & point, double rotation,
+ double scale);
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return 1; }
public:
- Transform(uint32_t id, const Point &, double, double);
- ~Transform();
//! Translation (shift)
- Point position;
+ Vector2 position;
//! Rotation, in radians
double rotation;
//! Multiplication factor
diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp
new file mode 100644
index 0000000..09bb59b
--- /dev/null
+++ b/src/crepe/api/Vector2.cpp
@@ -0,0 +1,57 @@
+#include "Vector2.h"
+
+namespace crepe {
+
+// Constructor with initial values
+Vector2::Vector2(float x, float y) : x(x), y(y) {}
+
+// Subtracts another vector from this vector and returns the result.
+Vector2 Vector2::operator-(const Vector2 & other) const {
+ return {x - other.x, y - other.y};
+}
+
+// Adds another vector to this vector and returns the result.
+Vector2 Vector2::operator+(const Vector2 & other) const {
+ return {x + other.x, y + other.y};
+}
+
+// Multiplies this vector by a scalar and returns the result.
+Vector2 Vector2::operator*(float scalar) const {
+ return {x * scalar, y * scalar};
+}
+
+// Multiplies this vector by another vector element-wise and updates this vector.
+Vector2 & Vector2::operator*=(const Vector2 & other) {
+ x *= other.x;
+ y *= other.y;
+ return *this;
+}
+
+// Adds another vector to this vector and updates this vector.
+Vector2 & Vector2::operator+=(const Vector2 & other) {
+ x += other.x;
+ y += other.y;
+ return *this;
+}
+
+// Adds a scalar value to both components of this vector and updates this vector.
+Vector2 & Vector2::operator+=(float other) {
+ x += other;
+ y += other;
+ return *this;
+}
+
+// Returns the negation of this vector.
+Vector2 Vector2::operator-() const { return {-x, -y}; }
+
+// Checks if this vector is equal to another vector.
+bool Vector2::operator==(const Vector2 & other) const {
+ return x == other.x && y == other.y;
+}
+
+// Checks if this vector is not equal to another vector.
+bool Vector2::operator!=(const Vector2 & other) const {
+ return !(*this == other);
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
new file mode 100644
index 0000000..741951b
--- /dev/null
+++ b/src/crepe/api/Vector2.h
@@ -0,0 +1,47 @@
+#pragma once
+
+namespace crepe {
+
+//! Vector2 class
+class Vector2 {
+public:
+ //! X component of the vector
+ float x;
+ //! Y component of the vector
+ float y;
+
+ //! Default constructor
+ Vector2() = default;
+
+ //! Constructor with initial values
+ Vector2(float x, float y);
+
+ //! Subtracts another vector from this vector and returns the result.
+ Vector2 operator-(const Vector2 & other) const;
+
+ //! Adds another vector to this vector and returns the result.
+ Vector2 operator+(const Vector2 & other) const;
+
+ //! Multiplies this vector by a scalar and returns the result.
+ Vector2 operator*(float scalar) const;
+
+ //! Multiplies this vector by another vector element-wise and updates this vector.
+ Vector2 & operator*=(const Vector2 & other);
+
+ //! Adds another vector to this vector and updates this vector.
+ Vector2 & operator+=(const Vector2 & other);
+
+ //! Adds a scalar value to both components of this vector and updates this vector.
+ Vector2 & operator+=(float other);
+
+ //! Returns the negation of this vector.
+ Vector2 operator-() const;
+
+ //! Checks if this vector is equal to another vector.
+ bool operator==(const Vector2 & other) const;
+
+ //! Checks if this vector is not equal to another vector.
+ bool operator!=(const Vector2 & other) const;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index bb52e7a..4cc53bc 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -1,7 +1,6 @@
target_sources(crepe PUBLIC
Sound.cpp
SoundContext.cpp
- SDLApp.cpp
SDLContext.cpp
DB.cpp
)
@@ -10,7 +9,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Sound.h
SoundContext.h
SDLContext.h
- SDLContext.h
DB.h
)
diff --git a/src/crepe/facade/SDLApp.cpp b/src/crepe/facade/SDLApp.cpp
deleted file mode 100644
index c6ddeaa..0000000
--- a/src/crepe/facade/SDLApp.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <iostream>
-
-#include "SDLApp.h"
-
-SDLApp::SDLApp(int window_width, int window_height)
- : window_width(window_width), window_height(window_height), window(nullptr),
- renderer(nullptr) {}
-
-// FIXME: why is there clean_up and ~SDLApp?
-SDLApp::~SDLApp() { clean_up(); }
-
-bool SDLApp::initialize() {
- if (SDL_Init(SDL_INIT_VIDEO) != 0) {
- // FIXME: throw exception
- std::cerr << "SDL Initialization Error: " << SDL_GetError()
- << std::endl;
- return false;
- }
-
- window = SDL_CreateWindow("Particle System", SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, window_width,
- window_height, SDL_WINDOW_SHOWN);
- if (!window) {
- // FIXME: throw exception
- std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl;
- return false;
- }
-
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (!renderer) {
- // FIXME: throw exception
- std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl;
- return false;
- }
-
- return true;
-}
-
-void SDLApp::handle_events(bool & running) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_QUIT) {
- running = false;
- }
- }
-}
-
-void SDLApp::clear_screen() {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
-}
-
-void SDLApp::present_screen() { SDL_RenderPresent(renderer); }
-
-void SDLApp::draw_square(int x, int y, int size) {
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_Rect rect = {x, y, size, size};
- SDL_RenderFillRect(renderer, &rect);
-}
-
-SDL_Texture * square_texture = nullptr; // Load this with an image or create it
-
-void SDLApp::clean_up() {
- if (renderer) {
- SDL_DestroyRenderer(renderer);
- }
- if (window) {
- SDL_DestroyWindow(window);
- }
- SDL_Quit();
-}
diff --git a/src/crepe/facade/SDLApp.h b/src/crepe/facade/SDLApp.h
deleted file mode 100644
index 6d8f3f4..0000000
--- a/src/crepe/facade/SDLApp.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-#include <SDL2/SDL.h>
-
-#include "../api/ParticleEmitter.h"
-
-class SDLApp {
-public:
- SDLApp(int window_width, int window_height);
- ~SDLApp();
-
- bool initialize();
- void handle_events(bool & running);
- void clear_screen();
- void present_screen();
- void draw_square(int x, int y, int size);
- void clean_up();
- void draw_particles(const std::vector<crepe::ParticleEmitter> & emitters);
- void draw_multiple_squares(const std::vector<SDL_Rect> & squares);
-
-private:
- int window_width;
- int window_height;
- SDL_Window * window;
- SDL_Renderer * renderer;
-};
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index cea8062..eb54ad3 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -1,16 +1,15 @@
-#include <iostream>
+#include <cmath>
#include "../ComponentManager.h"
-#include "../api/Force.h"
+#include "../api/Config.h"
#include "../api/Rigidbody.h"
#include "../api/Transform.h"
+#include "../api/Vector2.h"
#include "PhysicsSystem.h"
using namespace crepe;
-PhysicsSystem::PhysicsSystem() {}
-
void PhysicsSystem::update() {
ComponentManager & mgr = ComponentManager::get_instance();
std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
@@ -18,41 +17,86 @@ void PhysicsSystem::update() {
std::vector<std::reference_wrapper<Transform>> transforms
= mgr.get_components_by_type<Transform>();
+ double gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
+ if (!rigidbody.active) continue;
- switch (rigidbody.body_type) {
- case BodyType::DYNAMIC:
+ switch (rigidbody.data.body_type) {
+ case Rigidbody::BodyType::DYNAMIC:
for (Transform & transform : transforms) {
if (transform.game_object_id == rigidbody.game_object_id) {
- rigidbody.velocity_x = 0;
- rigidbody.velocity_y = 0;
- std::vector<std::reference_wrapper<Force>> forces
- = mgr.get_components_by_id<Force>(
- rigidbody.game_object_id);
- rigidbody.velocity_y
- += rigidbody.gravity_scale * 1 * rigidbody.mass;
-
- for (Force & force : forces) {
- rigidbody.velocity_x += force.force_x;
- rigidbody.velocity_y += force.force_y;
- }
-
- std::cout << "before transform.postion.x "
- << transform.position.x << std::endl;
- std::cout << "before transform.postion.y "
- << transform.position.y << std::endl;
- transform.position.x += rigidbody.velocity_x;
- transform.position.y += rigidbody.velocity_y;
- std::cout << "after transform.postion.x "
- << transform.position.x << std::endl;
- std::cout << "after transform.postion.y "
- << transform.position.y << std::endl;
+
+ // Add gravity
+ if (rigidbody.data.use_gravity) {
+ rigidbody.data.linear_velocity.y
+ += (rigidbody.data.mass
+ * rigidbody.data.gravity_scale * gravity);
+ }
+ // Add damping
+ if (rigidbody.data.angular_damping != 0) {
+ rigidbody.data.angular_velocity
+ *= rigidbody.data.angular_damping;
+ }
+ if (rigidbody.data.linear_damping != Vector2{0, 0}) {
+ rigidbody.data.linear_velocity
+ *= rigidbody.data.linear_damping;
+ }
+
+ // Max velocity check
+ if (rigidbody.data.angular_velocity
+ > rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity
+ = rigidbody.data.max_angular_velocity;
+ } else if (rigidbody.data.angular_velocity
+ < -rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity
+ = -rigidbody.data.max_angular_velocity;
+ }
+
+ if (rigidbody.data.linear_velocity.x
+ > rigidbody.data.max_linear_velocity.x) {
+ rigidbody.data.linear_velocity.x
+ = rigidbody.data.max_linear_velocity.x;
+ } else if (rigidbody.data.linear_velocity.x
+ < -rigidbody.data.max_linear_velocity.x) {
+ rigidbody.data.linear_velocity.x
+ = -rigidbody.data.max_linear_velocity.x;
+ }
+
+ if (rigidbody.data.linear_velocity.y
+ > rigidbody.data.max_linear_velocity.y) {
+ rigidbody.data.linear_velocity.y
+ = rigidbody.data.max_linear_velocity.y;
+ } else if (rigidbody.data.linear_velocity.y
+ < -rigidbody.data.max_linear_velocity.y) {
+ rigidbody.data.linear_velocity.y
+ = -rigidbody.data.max_linear_velocity.y;
+ }
+
+ // Move object
+ if (!rigidbody.data.constraints.rotation) {
+ transform.rotation
+ += rigidbody.data.angular_velocity;
+ transform.rotation
+ = std::fmod(transform.rotation, 360.0);
+ if (transform.rotation < 0) {
+ transform.rotation += 360.0;
+ }
+ }
+ if (!rigidbody.data.constraints.x) {
+ transform.position.x
+ += rigidbody.data.linear_velocity.x;
+ }
+ if (!rigidbody.data.constraints.y) {
+ transform.position.y
+ += rigidbody.data.linear_velocity.y;
+ }
}
}
break;
- case BodyType::KINEMATIC:
+ case Rigidbody::BodyType::KINEMATIC:
break; //(scripts)
- case BodyType::STATIC:
+ case Rigidbody::BodyType::STATIC:
break; //(unmoveable objects)
default:
break;
diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h
index 33b4072..cc13b70 100644
--- a/src/crepe/system/PhysicsSystem.h
+++ b/src/crepe/system/PhysicsSystem.h
@@ -1,10 +1,23 @@
#pragma once
namespace crepe {
-
+/**
+ * \brief System that controls all physics
+ *
+ * This class is a physics system that uses a rigidbody and transform
+ * to add physics to a game object.
+ */
class PhysicsSystem {
public:
- PhysicsSystem();
+ /**
+ * Constructor is default
+ */
+ PhysicsSystem() = default;
+ /**
+ * \brief updates the physics system.
+ *
+ * It calculates new velocties and changes the postion in the transform.
+ */
void update();
};
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 96c94e9..5a07cc2 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -2,9 +2,9 @@
#include <vector>
#include "../ComponentManager.h"
-#include "../facade/SDLContext.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
+#include "../facade/SDLContext.h"
#include "../util/log.h"
#include "RenderSystem.h"
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
index f1fae4d..f2673e7 100644
--- a/src/crepe/system/ScriptSystem.cpp
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -12,14 +12,6 @@
using namespace std;
using namespace crepe;
-ScriptSystem::ScriptSystem() { dbg_trace(); }
-ScriptSystem::~ScriptSystem() { dbg_trace(); }
-
-ScriptSystem & ScriptSystem::get_instance() {
- static ScriptSystem instance;
- return instance;
-}
-
void ScriptSystem::update() {
using namespace std;
dbg_trace();
diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h
index 32e793c..4fa6141 100644
--- a/src/crepe/system/ScriptSystem.h
+++ b/src/crepe/system/ScriptSystem.h
@@ -10,14 +10,9 @@ class Script;
class ScriptSystem : public System {
public:
- static ScriptSystem & get_instance();
void update();
private:
- ScriptSystem();
- ~ScriptSystem();
-
-private:
// TODO: to forward_list<reference_wrapper>
std::forward_list<Script *> get_scripts();
};
diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h
index ecbb7f5..3b81bef 100644
--- a/src/crepe/system/System.h
+++ b/src/crepe/system/System.h
@@ -4,19 +4,11 @@ namespace crepe {
class System {
public:
- static System & get_instance();
virtual void update() = 0;
-protected:
- System() {};
- virtual ~System() {};
-
-private:
- // singleton
- System(const System &) = delete;
- System(System &&) = delete;
- System & operator=(const System &) = delete;
- System & operator=(System &&) = delete;
+public:
+ System() = default;
+ virtual ~System() = default;
};
} // namespace crepe
diff --git a/src/crepe/types.h b/src/crepe/types.h
new file mode 100644
index 0000000..0d459e8
--- /dev/null
+++ b/src/crepe/types.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <cstdint>
+
+namespace crepe {
+
+typedef uint32_t game_object_id_t;
+
+}