diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Animator.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 23 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 39 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 11 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 6 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 47 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Texture.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Transform.cpp | 2 |
15 files changed, 68 insertions, 87 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index fcc07ef..54b2ec3 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,7 +1,7 @@ #include <cstdint> -#include "util/log.h" +#include "util/Log.h" #include "Animator.h" #include "Component.h" diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp index b891760..3925758 100644 --- a/src/crepe/api/AssetManager.cpp +++ b/src/crepe/api/AssetManager.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "util/Log.h" #include "AssetManager.h" diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp deleted file mode 100644 index 63fd0d7..0000000 --- a/src/crepe/api/AudioSource.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include <memory> - -#include "../facade/Sound.h" - -#include "AudioSource.h" - -using namespace crepe; - -AudioSource::AudioSource(std::unique_ptr<Asset> audio_clip) { - this->sound = std::make_unique<crepe::Sound>(std::move(audio_clip)); -} - -void AudioSource::play() { return this->play(false); } - -void AudioSource::play(bool looping) { - this->sound->set_looping(looping); - this->sound->play(); -} - -void AudioSource::stop() { - this->sound->pause(); - this->sound->rewind(); -} diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h deleted file mode 100644 index 1e24ae8..0000000 --- a/src/crepe/api/AudioSource.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include <memory> - -#include "../Asset.h" -#include "../Component.h" - -namespace crepe { - -class Sound; - -//! Audio source component -class AudioSource : public Component { -public: - AudioSource(std::unique_ptr<Asset> audio_clip); - virtual ~AudioSource() = default; - -public: - //! Start or resume this audio source - void play(); - void play(bool looping); - //! Stop this audio source - void stop(); - -public: - //! Sample file location - std::unique_ptr<Asset> audio_clip; - //! TODO: ????? - bool play_on_awake; - //! Repeat the current audio clip during playback - bool loop; - //! Normalized volume (0.0 - 1.0) - float volume; - -private: - std::unique_ptr<Sound> sound; -}; - -} // namespace crepe diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index a49fc96..2982358 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -11,6 +11,13 @@ class ScriptSystem; class ComponentManager; class Script; +/** + * \brief Script component + * + * This class acts as a (component) wrapper around an instance of (a class + * derivatived from) \c Script. \c BehaviorScript is the only ECS component + * that stores member function implementations as data. + */ class BehaviorScript : public Component { protected: BehaviorScript(game_object_id_t id, ComponentManager & component_manager); @@ -18,7 +25,7 @@ protected: friend class ComponentManager; public: - virtual ~BehaviorScript() = default; + ~BehaviorScript() = default; public: /** @@ -36,7 +43,9 @@ protected: friend class ScriptSystem; //! Flag to indicate if script->init() has been called already bool initialized = false; + //! Script instance std::unique_ptr<Script> script = nullptr; + //! Reference to component manager ComponentManager & component_manager; private: diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 2bed1b7..eec26da 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -2,7 +2,7 @@ #include <type_traits> -#include "../util/log.h" +#include "../util/Log.h" #include "BehaviorScript.h" #include "Script.h" diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 232c922..5835bdd 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "util/Log.h" #include "Camera.h" #include "Color.h" diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 8c9e643..e3f86bf 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,6 +1,6 @@ #pragma once -#include "../util/log.h" +#include "../util/Log.h" namespace crepe { @@ -26,10 +26,10 @@ public: /** * \brief Log level * - * Only messages with equal or higher priority than this value will be + * Only messages with equal or higher severity than this value will be * logged. */ - LogLevel level = LogLevel::INFO; + Log::Level level = Log::Level::INFO; /** * \brief Colored log output * diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 8f09e41..b3aec22 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,7 +1,7 @@ #include <chrono> #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "LoopTimer.h" diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index 43276c5..2974b91 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -1,5 +1,5 @@ #include "../facade/DB.h" -#include "../util/log.h" +#include "../util/Log.h" #include "Config.h" #include "SaveManager.h" diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 051ea00..68a46d7 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -3,20 +3,36 @@ #include <vector> namespace crepe { -class ScriptSystem; -} - -namespace crepe { +class ScriptSystem; class BehaviorScript; class ComponentManager; +/** + * \brief Script interface + * + * This class is used as a base class for user-defined scripts that can be + * added to game objects using the \c BehaviorScript component. + */ class Script { //! ScriptSystem calls \c update() friend class crepe::ScriptSystem; protected: + /** + * \brief Script initialization function + * + * This function is called during the ScriptSystem::update() routine *before* + * Script::update() if it (a) has not yet been called and (b) the \c + * BehaviorScript component holding this script instance is active. + */ virtual void init() {} + /** + * \brief Script update function + * + * This function is called during the ScriptSystem::update() routine if the + * \c BehaviorScript component holding this script instance is active. + */ virtual void update() {} // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be // implemented as member methods in derivative user script classes and @@ -24,13 +40,28 @@ protected: // added event. protected: - //! Retrieve component from component manager (utility) + /** + * \brief Get single component of type \c T on this game object (utility) + * + * \tparam T Type of component + * + * \returns Reference to component + * + * \throws nullptr if this game object does not have a component matching + * type \c T + */ template <typename T> - T & get_component(); + T & get_component() const; - //! Retrieve components from component manager (utility) + /** + * \brief Get all components of type \c T on this game object (utility) + * + * \tparam T Type of component + * + * \returns List of component references + */ template <typename T> - std::vector<std::reference_wrapper<T>> get_components(); + std::vector<std::reference_wrapper<T>> get_components() const; protected: // NOTE: Script must have a constructor without arguments so the game diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index ee8c380..aceb38b 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -1,6 +1,7 @@ #pragma once #include "../ComponentManager.h" +#include "../Exception.h" #include "BehaviorScript.h" #include "Script.h" @@ -8,16 +9,18 @@ namespace crepe { template <typename T> -T & Script::get_component() { +T & Script::get_component() const { std::vector<std::reference_wrapper<T>> all_components = this->get_components<T>(); - if (all_components.size() < 1) throw nullptr; // TODO + if (all_components.size() < 1) + throw Exception("Script: no component found with type = {}", + typeid(T).name()); return all_components.back().get(); } template <typename T> -std::vector<std::reference_wrapper<T>> Script::get_components() { +std::vector<std::reference_wrapper<T>> Script::get_components() const { auto & parent = *this->parent_ref; auto & mgr = *this->component_manager_ref; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 6f0433f..ac0079e 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,6 +1,6 @@ #include <memory> -#include "../util/log.h" +#include "../util/Log.h" #include "facade/SDLContext.h" #include "Component.h" diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 5ebd23d..de0d0ea 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,7 +1,7 @@ #include <SDL2/SDL_render.h> #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "Asset.h" #include "Texture.h" @@ -26,7 +26,7 @@ Texture::~Texture() { void Texture::load(unique_ptr<Asset> res) { SDLContext & ctx = SDLContext::get_instance(); - this->texture = std::move(ctx.texture_from_path(res->canonical())); + this->texture = std::move(ctx.texture_from_path(res->get_canonical())); } int Texture::get_width() const { diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index e401120..e9108c4 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "../util/Log.h" #include "Transform.h" |