diff options
Diffstat (limited to 'src')
39 files changed, 164 insertions, 158 deletions
| diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index c133739..d11a37e 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,5 +2,5 @@  using namespace crepe; -Component::Component(const Data & data) : data(data) {} +Component::Component(game_object_id_t id) : game_object_id(id) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index c6d72df..12c10cb 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -14,23 +14,20 @@ class ComponentManager;   */  class Component {  public: -	struct Data { -		//! The ID of the GameObject this component belongs to -		const game_object_id_t id; -		//! The manager of this component -		ComponentManager & component_manager; -	}; +	//! Whether the component is active +	bool active = true; +	//! The id of the GameObject this component belongs to +	const game_object_id_t game_object_id;  protected:  	/** -	 * \param base Data +	 * \param id The id of the GameObject this component belongs to  	 */ -	Component(const Data & base); +	Component(game_object_id_t id);  	//! Only the ComponentManager can create components -	friend class crepe::ComponentManager; +	friend class ComponentManager;  public: -	virtual ~Component() = default;  	/**  	 * \brief Get the maximum number of instances for this component  	 * @@ -41,11 +38,6 @@ public:  	 * \return The maximum number of instances for this component  	 */  	virtual int get_instances_max() const { return -1; } - -public: -	Data data; -	//! Whether the component is active -	bool active = true;  };  } // namespace crepe diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index e37bc4a..1d67e69 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -24,7 +24,7 @@ public:  	ComponentManager(); // dbg_trace  	~ComponentManager(); // dbg_trace -public: +protected:  	/**  	 * \brief Add a component to the ComponentManager  	 *  @@ -39,6 +39,11 @@ public:  	 */  	template <typename T, typename... Args>  	T & add_component(game_object_id_t id, Args &&... args); +	//! GameObject is used as an interface to add components instead of the +	// component manager directly +	friend class GameObject; + +public:  	/**  	 * \brief Delete all components of a specific type and id  	 *  diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 89a8536..98efb49 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,10 +31,7 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) {  	// Create a new component of type T (arguments directly forwarded). The  	// constructor must be called by ComponentManager. -	T * instance_ptr = new T(Component::Data { -		.id = id, -		.component_manager = *this, -	}, 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; diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index ad60981..cbf415d 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,8 @@  using namespace crepe; -Animator::Animator(const Component::Data & data, Sprite & ss, int row, int col, int col_animator) -	: Component(data), +Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) +	: Component(id),  	  spritesheet(ss),  	  row(row),  	  col(col) { diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index f66dc1a..75b8139 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -36,7 +36,7 @@ public:  	 *  	 * This constructor sets up the Animator with the given parameters, and initializes the animation system.  	 */ -	Animator(const Component::Data & data, Sprite & spritesheet, int row, int col, +	Animator(game_object_id_t id, Sprite & spritesheet, int row, int col,  			 int col_animate);  	~Animator(); // dbg_trace diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index ce1cfde..41c144c 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -1,4 +1,7 @@  #include "BehaviorScript.h" +#include "Component.h"  using namespace crepe; +BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) : Component(id), component_manager(mgr) {} + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 8d21a72..f156081 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -12,7 +12,7 @@ class Script;  class BehaviorScript : public Component {  protected: -	using Component::Component; +	BehaviorScript(game_object_id_t id, ComponentManager & component_manager);  	//! Only ComponentManager is allowed to instantiate BehaviorScript  	friend class ComponentManager; @@ -36,6 +36,7 @@ protected:  	//! Flag to indicate if script->init() has been called already  	bool initialized = false;  	std::unique_ptr<Script> script = nullptr; +	ComponentManager & component_manager;  private:  	//! Script accesses the component manager directly via its parent diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index bb5d829..2bed1b7 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -15,6 +15,7 @@ BehaviorScript & BehaviorScript::set_script() {  	static_assert(std::is_base_of<Script, T>::value);  	Script * s = new T();  	s->parent_ref = this; +	s->component_manager_ref = &this->component_manager;  	this->script = std::unique_ptr<Script>(s);  	return *this;  } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index d9cda57..ee77947 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -14,7 +14,7 @@ target_sources(crepe PUBLIC  	Config.cpp  	Metadata.cpp  	Scene.cpp -	# SceneManager.cpp +	SceneManager.cpp  	Vector2.cpp  	Camera.cpp  	Animator.cpp @@ -40,8 +40,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES  	SaveManager.h  	Scene.h  	Metadata.h -	# SceneManager.h -	# SceneManager.hpp +	SceneManager.h +	SceneManager.hpp  	Camera.h  	Animator.h  	# LoopManager.h diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index fece199..232c922 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,8 +6,8 @@  using namespace crepe; -Camera::Camera(const Component::Data & data, const Color & bg_color) -	: Component(data), +Camera::Camera(game_object_id_t id, const Color & bg_color) +	: Component(id),  	  bg_color(bg_color) {  	dbg_trace();  } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index a52f2f7..d8c08a6 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -23,7 +23,7 @@ public:  	 * \param id Unique identifier for the camera component.  	 * \param bg_color Background color for the camera view.  	 */ -	Camera(const Component::Data & data, const Color & bg_color); +	Camera(game_object_id_t id, const Color & bg_color);  	~Camera(); // dbg_trace only  public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index bba1f26..dd20b7f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -2,6 +2,7 @@  #include "GameObject.h"  #include "Metadata.h" +#include "BehaviorScript.h"  using namespace crepe;  using namespace std; @@ -30,3 +31,10 @@ void GameObject::set_parent(const GameObject & parent) {  		= mgr.get_components_by_id<Metadata>(parent.id);  	parent_metadata.at(0).get().children.push_back(this->id);  } + +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>() { +	ComponentManager & mgr = this->component_manager; +	return mgr.add_component<BehaviorScript>(this->id, mgr); +} + diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index d09938a..7751fd0 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -8,6 +8,7 @@  namespace crepe {  class ComponentManager; +class BehaviorScript;  /**   * \brief Represents a GameObject @@ -67,6 +68,18 @@ protected:  	ComponentManager & component_manager;  }; +/** + * \brief Add a BehaviorScript component to this game object + * + * The \c BehaviorScript class is the only exception to the ECS harmony, and + * requires a reference to the component manager passed to its constructor in + * order to function normally. This is because the \c BehaviorScript (and \c + * Script) classes are the only component-related classes that store + * implemented member functions as data. + */ +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>(); +  } // namespace crepe  #include "GameObject.hpp" diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp index 6f0a280..d421de5 100644 --- a/src/crepe/api/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp @@ -3,7 +3,7 @@  using namespace crepe;  using namespace std; -Metadata::Metadata(const Component::Data & data, const string & name, const string & tag) -	: Component(data), +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 index 8c91797..c61e006 100644 --- a/src/crepe/api/Metadata.h +++ b/src/crepe/api/Metadata.h @@ -20,7 +20,7 @@ public:  	 * \param name The name of the GameObject  	 * \param tag The tag of the GameObject  	 */ -	Metadata(const Component::Data & data, const std::string & name, +	Metadata(game_object_id_t id, const std::string & name,  			 const std::string & tag);  	/**  	 * \brief Get the maximum number of instances for this component diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index ed4fec8..0bc2197 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -6,12 +6,12 @@  using namespace crepe; -ParticleEmitter::ParticleEmitter(const Component::Data & data, uint32_t max_particles, +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(data), +	: Component(id),  	  max_particles(max_particles),  	  emission_rate(emission_rate),  	  speed(speed), diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index ba89538..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(const Component::Data & data, 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/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index be6bb93..6b87695 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,8 +2,8 @@  using namespace crepe; -crepe::Rigidbody::Rigidbody(const Component::Data & component_data, const Data & data) -	: Component(component_data), +crepe::Rigidbody::Rigidbody(game_object_id_t id, const Data & data) +	: Component(id),  	  data(data) {}  void crepe::Rigidbody::add_force_linear(const Vector2 & force) { diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 99837a6..2e20288 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -82,7 +82,7 @@ public:  	 * \param game_object_id id of the gameobject the rigibody is added to.  	 * \param data struct to configure the rigidbody.  	 */ -	Rigidbody(const Component::Data & component_data, const Data & data); +	Rigidbody(game_object_id_t id, const Data & data);  	//! struct to hold data of rigidbody  	Data data; diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index dfed6ee..814b7d7 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -8,10 +8,7 @@  using namespace crepe;  using namespace std; -SceneManager & SceneManager::get_instance() { -	static SceneManager instance; -	return instance; -} +SceneManager::SceneManager(ComponentManager & mgr) : component_manager(mgr) {}  void SceneManager::set_next_scene(const string & name) { next_scene = name; } @@ -19,18 +16,18 @@ 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; -				  }); +	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(); +	ComponentManager & mgr = this->component_manager;  	mgr.delete_all_components();  	// Load the new scene diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 1e0e670..553194f 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -8,14 +8,12 @@  namespace crepe { +class ComponentManager; +  class SceneManager {  public: -	// Singleton -	static SceneManager & get_instance(); -	SceneManager(const SceneManager &) = delete; -	SceneManager(SceneManager &&) = delete; -	SceneManager & operator=(const SceneManager &) = delete; -	SceneManager & operator=(SceneManager &&) = delete; +	SceneManager(ComponentManager & mgr); +	virtual ~SceneManager() = default;  public:  	/** @@ -38,11 +36,11 @@ public:  	void load_next_scene();  private: -	SceneManager() = default; - -private:  	std::vector<std::unique_ptr<Scene>> scenes;  	std::string next_scene; + +protected: +	ComponentManager & component_manager;  };  } // namespace crepe diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index d543df8..c5e3cc4 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -9,6 +9,7 @@ class ScriptSystem;  namespace crepe {  class BehaviorScript; +class ComponentManager;  class Script {  	//! ScriptSystem calls \c update() @@ -31,14 +32,18 @@ protected:  	template <typename T>  	std::vector<std::reference_wrapper<T>> get_components(); -protected: +private:  	// NOTE: Script must have a constructor without arguments so the game  	// programmer doesn't need to manually add `using Script::Script` to their  	// concrete script class.  	Script() = default;  	//! Only \c BehaviorScript instantiates Script  	friend class BehaviorScript; + +	// These references are set by BehaviorScript immediately after calling the +	// constructor of Script.  	BehaviorScript * parent_ref = nullptr; +	ComponentManager * component_manager_ref = nullptr;  };  } // namespace crepe diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 68bcc29..ee8c380 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -19,9 +19,9 @@ T & Script::get_component() {  template <typename T>  std::vector<std::reference_wrapper<T>> Script::get_components() {  	auto & parent = *this->parent_ref; -	auto & mgr = parent.data.component_manager; +	auto & mgr = *this->component_manager_ref; -	return mgr.get_components_by_id<T>(parent.data.id); +	return mgr.get_components_by_id<T>(parent.game_object_id);  }  } // namespace crepe diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 716609f..6f0433f 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,9 +10,9 @@  using namespace std;  using namespace crepe; -Sprite::Sprite(const Component::Data & data, const shared_ptr<Texture> image, +Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image,  			   const Color & color, const FlipSettings & flip) -	: Component(data), +	: Component(id),  	  color(color),  	  flip(flip),  	  sprite_image(image) { diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 171dfeb..deb3f93 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -43,7 +43,7 @@ public:  	 * \param color Color tint applied to the sprite.  	 * \param flip Flip settings for horizontal and vertical orientation.  	 */ -	Sprite(const Component::Data & data, const std::shared_ptr<Texture> image, +	Sprite(game_object_id_t id, const std::shared_ptr<Texture> image,  		   const Color & color, const FlipSettings & flip);  	/** diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 9645d03..e401120 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -4,9 +4,9 @@  using namespace crepe; -Transform::Transform(const Component::Data & data, const Vector2 & point, +Transform::Transform(game_object_id_t id, const Vector2 & point,  					 double rotation, double scale) -	: Component(data), +	: Component(id),  	  position(point),  	  rotation(rotation),  	  scale(scale) { diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index a7bcd89..a1ef406 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -14,28 +14,26 @@ namespace crepe {   */  class Transform : public Component {  public: +	//! Translation (shift) +	Vector2 position = { 0, 0 }; +	//! Rotation, in degrees +	double rotation = 0; +	//! Multiplication factor +	double scale = 0; + +protected:  	/**  	 * \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(const Component::Data & data, 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 -	 */ +	Transform(game_object_id_t id, const Vector2 & point, double rotation = 0, +			  double scale = 0); +	//! There is always exactly one transform component per entity  	virtual int get_instances_max() const { return 1; } - -public: -	//! Translation (shift) -	Vector2 position; -	//! Rotation, in degrees -	double rotation; -	//! Multiplication factor -	double scale; +	//! ComponentManager instantiates all components +	friend class ComponentManager;  };  } // namespace crepe diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 09bb59b..aa391fa 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -1,57 +1,44 @@  #include "Vector2.h" -namespace crepe { +using 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 index 741951b..a069a57 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -2,19 +2,12 @@  namespace crepe { -//! Vector2 class -class Vector2 { -public: +//! 2D vector +struct Vector2 {  	//! X component of the vector -	float x; +	float x = 0;  	//! Y component of the vector -	float y; - -	//! Default constructor -	Vector2() = default; - -	//! Constructor with initial values -	Vector2(float x, float y); +	float y = 0;  	//! Subtracts another vector from this vector and returns the result.  	Vector2 operator-(const Vector2 & other) const; diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index 9ee12f6..2fb58dc 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -1,8 +1,8 @@  target_sources(crepe PUBLIC  	System.cpp -	# ParticleSystem.cpp +	ParticleSystem.cpp  	ScriptSystem.cpp -	# PhysicsSystem.cpp +	PhysicsSystem.cpp  	# CollisionSystem.cpp  	# RenderSystem.cpp  	# AnimatorSystem.cpp @@ -11,7 +11,7 @@ target_sources(crepe PUBLIC  target_sources(crepe PUBLIC FILE_SET HEADERS FILES  	System.h  	ScriptSystem.h -	# PhysicsSystem.h +	PhysicsSystem.h  	# CollisionSystem.h  	# RenderSystem.h  	# AnimatorSystem.h diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 397b586..64fee4e 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -8,10 +8,8 @@  using namespace crepe; -ParticleSystem::ParticleSystem() : elapsed_time(0.0f) {} -  void ParticleSystem::update() { -	ComponentManager & mgr = ComponentManager::get_instance(); +	ComponentManager & mgr = this->component_manager;  	std::vector<std::reference_wrapper<ParticleEmitter>> emitters  		= mgr.get_components_by_type<ParticleEmitter>();  	float delta_time = 0.10; @@ -60,3 +58,4 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter) {  		}  	}  } + diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 3ac1d3f..f171c62 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -2,17 +2,22 @@  #include "../api/ParticleEmitter.h" +#include "System.h" +  namespace crepe { -class ParticleSystem { +class ParticleSystem : public System {  public: -	ParticleSystem(); -	void update(); +	using System::System; +	void update() override;  private: -	void emit_particle(ParticleEmitter & emitter); //emits a new particle +	//! Emits a new particle +	void emit_particle(ParticleEmitter & emitter); -	float elapsed_time; //elapsed time since the last emission +	//! Elapsed time since the last emission +	float elapsed_time = 0.0; +	// TODO: to std::chrono::duration  };  } // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index eb54ad3..da79707 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -11,7 +11,7 @@  using namespace crepe;  void PhysicsSystem::update() { -	ComponentManager & mgr = ComponentManager::get_instance(); +	ComponentManager & mgr = this->component_manager;  	std::vector<std::reference_wrapper<Rigidbody>> rigidbodies  		= mgr.get_components_by_type<Rigidbody>();  	std::vector<std::reference_wrapper<Transform>> transforms diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index cc13b70..5433a0f 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -1,24 +1,24 @@  #pragma once +#include "System.h" +  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 { +class PhysicsSystem : public System {  public: -	/** -	 * Constructor is default -	 */ -	PhysicsSystem() = default; +	using System::System;  	/**  	 * \brief updates the physics system.  	 *   	 * It calculates new velocties and changes the postion in the transform.  	 */ -	void update(); +	void update() override;  };  } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index b52e825..b0b4185 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -7,12 +7,11 @@  namespace crepe {  class Script; -class BehaviorScript;  class ScriptSystem : public System {  public:  	using System::System; -	void update(); +	void update() override;  private:  	std::forward_list<std::reference_wrapper<Script>> get_scripts(); diff --git a/src/example/script.cpp b/src/example/script.cpp index 9e8b147..4ba2b4b 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -35,15 +35,15 @@ class MyScript : public Script {  };  int main() { +	ComponentManager component_manager {}; +	ScriptSystem system { component_manager }; +  	// Create game object with Transform and BehaviorScript components -	auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); +	auto & obj = component_manager.new_object("name");  	obj.add_component<BehaviorScript>().set_script<MyScript>(); -	// Get ScriptSystem singleton instance (this would normally be done from the -	// game loop) -	ScriptSystem sys;  	// Update all scripts. This should result in MyScript::update being called -	sys.update(); +	system.update();  	return EXIT_SUCCESS;  } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 9d303bc..4dd5010 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,6 +1,6 @@  target_sources(test_main PUBLIC  	main.cpp -	# PhysicsTest.cpp +	PhysicsTest.cpp  	ScriptTest.cpp  ) diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 538d244..5399d54 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -12,17 +12,16 @@ using namespace crepe;  class PhysicsTest : public ::testing::Test {  public: -	GameObject * game_object;  	ComponentManager component_manager; -	PhysicsSystem physics_system; +	PhysicsSystem system { component_manager };  	void SetUp() override { -		ComponentManager & mgr = ComponentManager::get_instance(); -		std::vector<std::reference_wrapper<Transform>> transforms +		ComponentManager & mgr = this->component_manager; +		vector<reference_wrapper<Transform>> transforms  			= mgr.get_components_by_id<Transform>(0);  		if (transforms.empty()) { -			game_object = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); -			game_object->add_component<Rigidbody>(Rigidbody::Data{ +			auto & entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); +			entity.add_component<Rigidbody>(Rigidbody::Data{  				.mass = 1,  				.gravity_scale = 1,  				.body_type = Rigidbody::BodyType::DYNAMIC, @@ -38,7 +37,7 @@ public:  		transform.position.x = 0.0;  		transform.position.y = 0.0;  		transform.rotation = 0.0; -		std::vector<std::reference_wrapper<Rigidbody>> rigidbodies +		vector<reference_wrapper<Rigidbody>> rigidbodies  			= mgr.get_components_by_id<Rigidbody>(0);  		Rigidbody & rigidbody = rigidbodies.front().get();  		rigidbody.data.angular_velocity = 0; @@ -49,34 +48,37 @@ public:  TEST_F(PhysicsTest, gravity) {  	Config::get_instance().physics.gravity = 1; -	ComponentManager & mgr = ComponentManager::get_instance(); -	std::vector<std::reference_wrapper<Transform>> transforms -		= mgr.get_components_by_id<Transform>(0); +	ComponentManager & mgr = this->component_manager; +	vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);  	const Transform & transform = transforms.front().get();  	ASSERT_FALSE(transforms.empty());  	EXPECT_EQ(transform.position.y, 0); -	physics_system.update(); + +	system.update();  	EXPECT_EQ(transform.position.y, 1); -	physics_system.update(); + +	system.update();  	EXPECT_EQ(transform.position.y, 3);  }  TEST_F(PhysicsTest, max_velocity) { -	ComponentManager & mgr = ComponentManager::get_instance(); -	std::vector<std::reference_wrapper<Rigidbody>> rigidbodies +	ComponentManager & mgr = this->component_manager; +	vector<reference_wrapper<Rigidbody>> rigidbodies  		= mgr.get_components_by_id<Rigidbody>(0);  	Rigidbody & rigidbody = rigidbodies.front().get();  	ASSERT_FALSE(rigidbodies.empty());  	EXPECT_EQ(rigidbody.data.linear_velocity.y, 0); +  	rigidbody.add_force_linear({100, 100});  	rigidbody.add_force_angular(100); -	physics_system.update(); +	system.update();  	EXPECT_EQ(rigidbody.data.linear_velocity.y, 10);  	EXPECT_EQ(rigidbody.data.linear_velocity.x, 10);  	EXPECT_EQ(rigidbody.data.angular_velocity, 10); +  	rigidbody.add_force_linear({-100, -100});  	rigidbody.add_force_angular(-100); -	physics_system.update(); +	system.update();  	EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);  	EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);  	EXPECT_EQ(rigidbody.data.angular_velocity, -10); @@ -84,39 +86,42 @@ TEST_F(PhysicsTest, max_velocity) {  TEST_F(PhysicsTest, movement) {  	Config::get_instance().physics.gravity = 0; -	ComponentManager & mgr = ComponentManager::get_instance(); -	std::vector<std::reference_wrapper<Rigidbody>> rigidbodies -		= mgr.get_components_by_id<Rigidbody>(0); +	ComponentManager & mgr = this->component_manager; +	vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);  	Rigidbody & rigidbody = rigidbodies.front().get(); -	std::vector<std::reference_wrapper<Transform>> transforms -		= mgr.get_components_by_id<Transform>(0); +	vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);  	const Transform & transform = transforms.front().get();  	ASSERT_FALSE(rigidbodies.empty());  	ASSERT_FALSE(transforms.empty()); +  	rigidbody.add_force_linear({1, 1});  	rigidbody.add_force_angular(1); -	physics_system.update(); +	system.update();  	EXPECT_EQ(transform.position.x, 1);  	EXPECT_EQ(transform.position.y, 1);  	EXPECT_EQ(transform.rotation, 1); +  	rigidbody.data.constraints = {1, 1, 1};  	EXPECT_EQ(transform.position.x, 1);  	EXPECT_EQ(transform.position.y, 1);  	EXPECT_EQ(transform.rotation, 1); +  	rigidbody.data.linear_damping.x = 0.5;  	rigidbody.data.linear_damping.y = 0.5;  	rigidbody.data.angular_damping = 0.5; -	physics_system.update(); +	system.update();  	EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5);  	EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5);  	EXPECT_EQ(rigidbody.data.angular_velocity, 0.5); +  	rigidbody.data.constraints = {1, 1, 0};  	rigidbody.data.angular_damping = 0;  	rigidbody.data.max_angular_velocity = 1000;  	rigidbody.data.angular_velocity = 360; -	physics_system.update(); +	system.update();  	EXPECT_EQ(transform.rotation, 1); +  	rigidbody.data.angular_velocity = -360; -	physics_system.update(); +	system.update();  	EXPECT_EQ(transform.rotation, 1);  } |