diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-07 15:21:54 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-07 15:21:54 +0100 |
commit | 0f2451f4d48c78426c2663501e6cdd6871f1e94a (patch) | |
tree | 5015832f112093ec9fcefcb22800657ea28e064c /src/crepe/api | |
parent | f05458cdbf68e8efe1ed812f57e957921921941d (diff) | |
parent | fdb4c99e139a264d4e15e6913a3756fc6cccb2f2 (diff) |
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/gameloop
Diffstat (limited to 'src/crepe/api')
29 files changed, 588 insertions, 224 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 45f67f6..b8a91dc 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,21 +7,53 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) +Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, + unsigned int max_col, const Animator::Data & data) : Component(id), - spritesheet(ss), - row(row), - col(col) { + spritesheet(spritesheet), + max_rows(max_row), + max_columns(max_col), + data(data) { dbg_trace(); - this->spritesheet.mask.h /= col; - this->spritesheet.mask.w /= row; - this->spritesheet.mask.x = 0; - this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h; - this->active = false; + this->spritesheet.mask.h /= this->max_columns; + this->spritesheet.mask.w /= this->max_rows; + this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w; + this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h; // need to do this for to get the aspect ratio for a single clipping in the spritesheet this->spritesheet.aspect_ratio = static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h; } + Animator::~Animator() { dbg_trace(); } + +void Animator::loop() { this->data.looping = true; } + +void Animator::play() { this->active = true; } + +void Animator::pause() { this->active = false; } + +void Animator::stop() { + this->active = false; + this->data.col = 0; + this->data.row = 0; +} +void Animator::set_fps(int fps) { this->data.fps = fps; } + +void Animator::set_cycle_range(int start, int end) { + this->data.cycle_start = start, this->data.cycle_end = end; +} + +void Animator::set_anim(int col) { + Animator::Data & ctx = this->data; + this->spritesheet.mask.x = ctx.row = 0; + ctx.col = col; + this->spritesheet.mask.y = ctx.col * this->spritesheet.mask.h; +} + +void Animator::next_anim() { + Animator::Data & ctx = this->data; + ctx.row = ctx.row++ % this->max_rows; + this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; +} diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 6c506aa..7c850b8 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,5 +1,7 @@ #pragma once +#include "../types.h" + #include "Component.h" #include "Sprite.h" @@ -16,56 +18,86 @@ class SDLContext; * sheet. It can be used to play animations, loop them, or stop them. */ class Animator : public Component { +public: + struct Data { + //! frames per second for animation + unsigned int fps = 1; + //! The current col being animated. + unsigned int col = 0; + //! The current row being animated. + unsigned int row = 0; + //! should the animation loop + bool looping = false; + //! starting frame for cycling + unsigned int cycle_start = 0; + //! end frame for cycling (-1 = use last frame) + int cycle_end = -1; + }; public: - //TODO: need to implement this + //! Animator will repeat the animation void loop(); + //! starts the animation + void play(); + //! pauses the animation + void pause(); + /** + * \brief stops the animation + * + * sets the active on false and resets all the current rows and columns + */ void stop(); + /** + * \brief set frames per second + * + * \param fps frames per second + */ + void set_fps(int fps); + /** + * \brief set the range in the row + * + * \param start of row animation + * \param end of row animation + */ + void set_cycle_range(int start, int end); + /** + * \brief select which column to animate from + * + * \param col animation column + */ + void set_anim(int col); + //! will go to the next animaiton of current row + void next_anim(); public: /** * \brief Constructs an Animator object that will control animations for a sprite sheet. * * \param id The unique identifier for the component, typically assigned automatically. - * \param spritesheet A reference to the Sprite object which holds the sprite sheet for - * animation. - * \param row The maximum number of rows in the sprite sheet. - * \param col The maximum number of columns in the sprite sheet. - * \param col_animate The specific col index of the sprite sheet to animate. This allows - * selecting which col to animate from multiple col in the sheet. + * \param spritesheet the reference to the spritesheet + * \param max_row maximum of rows inside the given spritesheet + * \param max_col maximum of columns inside the given spritesheet + * \param data extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); - + Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, + unsigned int max_col, const Animator::Data & data); ~Animator(); // dbg_trace -private: - //! A reference to the Sprite sheet containing the animation frames. - Sprite & spritesheet; - +public: //! The maximum number of columns in the sprite sheet. - const int col; - + const unsigned int max_columns; //! The maximum number of rows in the sprite sheet. - const int row; - - //! The current col being animated. - int curr_col = 0; - - //! The current row being animated. - int curr_row = 0; - - //TODO: Is this necessary? - //int fps; + const unsigned int max_rows; + Animator::Data data; private: - //! AnimatorSystem adjust the private member parameters of Animator; - friend class AnimatorSystem; - - //! SDLContext reads the Animator member var's - friend class SDLContext; + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; + //! Uses the spritesheet + friend AnimatorSystem; }; + } // namespace crepe -// diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index fee6780..3b1cc4b 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -9,7 +9,7 @@ namespace crepe { /** * \brief The AssetManager is responsible for storing and managing assets over multiple scenes. - * + * * The AssetManager ensures that assets are loaded once and can be accessed across different * scenes. It caches assets to avoid reloading them every time a scene is loaded. Assets are * retained in memory until the AssetManager is destroyed, at which point the cached assets are @@ -46,9 +46,9 @@ public: * \param reload If true, the asset will be reloaded from the file, even if it is already * cached. * \tparam T The type of asset to cache (e.g., texture, sound, etc.). - * + * * \return A shared pointer to the cached asset. - * + * * This template function caches the asset at the given file path. If the asset is already * cached and `reload` is false, the existing cached version will be returned. Otherwise, the * asset will be reloaded and added to the cache. diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp new file mode 100644 index 0000000..c097a24 --- /dev/null +++ b/src/crepe/api/BoxCollider.cpp @@ -0,0 +1,10 @@ +#include "BoxCollider.h" + +#include "../Collider.h" + +using namespace crepe; + +BoxCollider::BoxCollider(game_object_id_t game_object_id, const vec2 & offset, + const vec2 & dimensions) + : Collider(game_object_id, offset), + dimensions(dimensions) {} diff --git a/src/crepe/api/BoxCollider.h b/src/crepe/api/BoxCollider.h new file mode 100644 index 0000000..1ac4d46 --- /dev/null +++ b/src/crepe/api/BoxCollider.h @@ -0,0 +1,22 @@ +#pragma once + +#include "../Collider.h" +#include "Vector2.h" +#include "types.h" + +namespace crepe { + +/** + * \brief A class representing a box-shaped collider. + * + * This class is used for collision detection with other colliders (e.g., CircleCollider). + */ +class BoxCollider : public Collider { +public: + BoxCollider(game_object_id_t game_object_id, const vec2 & offset, const vec2 & dimensions); + + //! Width and height of the box collider + vec2 dimensions; +}; + +} // namespace crepe diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp new file mode 100644 index 0000000..76f74f0 --- /dev/null +++ b/src/crepe/api/Button.cpp @@ -0,0 +1,11 @@ +#include "Button.h" + +namespace crepe { + +Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::function<void()> & on_click, bool is_toggle) + : UIObject(id, dimensions, offset), + is_toggle(is_toggle), + on_click(on_click) {} + +} // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h new file mode 100644 index 0000000..61b18d7 --- /dev/null +++ b/src/crepe/api/Button.h @@ -0,0 +1,67 @@ +#pragma once + +#include <functional> + +#include "UIObject.h" + +namespace crepe { + +//! Represents a clickable UI button, derived from the UiObject class. +class Button : public UIObject { +public: + /** + * \brief Constructs a Button with the specified game object ID and dimensions. + * + * \param id The unique ID of the game object associated with this button. + * \param dimensions The width and height of the UIObject + * \param offset The offset relative this GameObjects Transform + * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false. + * \param on_click callback function that will be invoked when the button is clicked. + */ + Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::function<void()> & on_click, bool is_toggle = false); + + /** + * \brief Indicates if the button is a toggle button (can be pressed and released). + * + * A toggle button allows for a pressed/released state, whereas a regular button + * typically only has an on-click state. + */ + bool is_toggle = false; + // TODO: create separate toggle button class + /** + * \brief The callback function to be executed when the button is clicked. + * + * This function is invoked whenever the button is clicked. It can be set to any + * function that matches the signature `void()`. + */ + std::function<void()> on_click = nullptr; + + /** + * \brief Callback function to be executed when the mouse enters the button's boundaries. + * + * This function is triggered when the mouse cursor moves over the button, allowing + * custom actions like visual effects, highlighting, or sound effects. + */ + std::function<void()> on_mouse_enter = nullptr; + + /** + * \brief Callback function to be executed when the mouse exits the button's boundaries. + * + * This function is triggered when the mouse cursor moves out of the button's area, + * allowing custom actions like resetting visual effects or playing exit-related effects. + */ + std::function<void()> on_mouse_exit = nullptr; + +private: + //! friend relation for is_pressed and hover variables + friend class InputSystem; + //! Indicates whether the toggle button is pressed + bool is_pressed = false; + //! Indicates whether the mouse is currently hovering over the button + bool hover = false; + +public: +}; + +} // namespace crepe diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 60d9dc5..ca4edec 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -13,10 +13,14 @@ target_sources(crepe PUBLIC Metadata.cpp Camera.cpp Animator.cpp + BoxCollider.cpp + CircleCollider.cpp LoopManager.cpp Asset.cpp EventHandler.cpp Script.cpp + Button.cpp + UIObject.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -32,16 +36,20 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.h Vector2.hpp Color.h - Texture.h - AssetManager.h + Texture.h + AssetManager.h AssetManager.hpp Scene.h Metadata.h Camera.h Animator.h + BoxCollider.h + CircleCollider.h EventHandler.h EventHandler.hpp Event.h LoopManager.h Asset.h + Button.h + UIObject.h ) diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 39d8ab0..179dc18 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,20 +1,17 @@ -#include "types.h" #include "util/Log.h" #include "Camera.h" -#include "Color.h" #include "Component.h" +#include "types.h" using namespace crepe; -Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const vec2 & viewport_size, const double & zoom, const vec2 & offset) +Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & data) : Component(id), - bg_color(bg_color), - offset(offset), screen(screen), viewport_size(viewport_size), - zoom(zoom) { + data(data) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 2d8fa48..54d9a73 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -14,23 +14,42 @@ namespace crepe { * position, and zoom level. It controls what part of the game world is visible on the screen. */ class Camera : public Component { +public: + struct Data { + /** + * \bg_color background color of the game + * + * This will make the background the same color as the given value. + */ + const Color bg_color = Color::BLACK; + + /** + * \zoom Zooming level of the game + * + * zoom = 1 --> no zoom. + * zoom < 1 --> zoom out + * zoom > 1 --> zoom in + */ + double zoom = 1; + + //! offset postion from the game object transform component + vec2 postion_offset; + }; public: /** * \brief Constructs a Camera with the specified ID and background color. * \param id Unique identifier for the camera component. - * \param bg_color Background color for the camera view. + * \param screen is the actual screen size in pixels + * \param viewport_size is the view of the world in game units + * \param data the camera component data */ - Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0}); + Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Camera::Data & data); ~Camera(); // dbg_trace only public: - //! Background color of the camera view. - const Color bg_color; - - //! offset postion from the game object transform component - vec2 offset; + Camera::Data data; //! screen the display size in pixels ( output resolution ) const ivec2 screen; @@ -38,9 +57,6 @@ public: //! viewport is the area of the world visible through the camera (in world units) const vec2 viewport_size; - //! Zoom level of the camera view. - const double zoom; - public: /** * \brief Gets the maximum number of camera instances allowed. diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp new file mode 100644 index 0000000..a4271e9 --- /dev/null +++ b/src/crepe/api/CircleCollider.cpp @@ -0,0 +1,8 @@ +#include "CircleCollider.h" + +using namespace crepe; + +CircleCollider::CircleCollider(game_object_id_t game_object_id, const vec2 & offset, + float radius) + : Collider(game_object_id, offset), + radius(radius) {} diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h index e77a592..c7bf66e 100644 --- a/src/crepe/api/CircleCollider.h +++ b/src/crepe/api/CircleCollider.h @@ -1,14 +1,22 @@ #pragma once + +#include "Vector2.h" + #include "../Collider.h" namespace crepe { +/** + * \brief A class representing a circle-shaped collider. + * + * This class is used for collision detection with other colliders (e.g., BoxCollider). + */ class CircleCollider : public Collider { public: - CircleCollider(game_object_id_t game_object_id, int radius) - : Collider(game_object_id), - radius(radius) {} - int radius; + CircleCollider(game_object_id_t game_object_id, const vec2 & offset, float radius); + + //! Radius of the circle collider. + float radius; }; } // namespace crepe diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 225e9b9..a9745c3 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,8 +1,10 @@ #pragma once +#include <string> + #include "../util/Log.h" + #include "types.h" -#include <string> namespace crepe { @@ -66,10 +68,9 @@ public: //! default window settings struct { - //TODO make this constexpr because this will never change - ivec2 default_size = {1080, 720}; + //! default screen size in pixels + ivec2 default_size = {1280, 720}; std::string window_title = "Jetpack joyride clone"; - } window_settings; //! Asset loading options diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index b267e3e..f2f3daf 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -88,12 +88,30 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; + + // Movement since last event in x + int delta_x = 0; + + // Movement since last event in y + int delta_y = 0; }; /** - * \brief Event triggered during a collision between objects. + * \brief Event triggered when the mouse is moved. */ -class CollisionEvent : public Event {}; +class MouseScrollEvent : public Event { +public: + //! X-coordinate of the mouse position at the time of the event. + int mouse_x = 0; + + //! Y-coordinate of the mouse position at the time of the event. + int mouse_y = 0; + + //! scroll direction (-1 = down, 1 = up) + int scroll_direction = 0; + //! scroll amount in y axis (from and away from the person). + float scroll_delta = 0; +}; /** * \brief Event triggered when text is submitted, e.g., from a text input. diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index ef659fd..7bb501b 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -8,12 +8,12 @@ namespace crepe { /** * \brief A type alias for an event handler function. - * - * The EventHandler is a std::function that takes an EventType reference and returns a boolean value + * + * The EventHandler is a std::function that takes an EventType reference and returns a boolean value * indicating whether the event is handled. - * + * * \tparam EventType The type of event this handler will handle. - * + * * Returning \c false from an event handler results in the event being propogated to other listeners for the same event type, while returning \c true stops propogation altogether. */ template <typename EventType> @@ -22,70 +22,70 @@ using EventHandler = std::function<bool(const EventType & e)>; /** * \class IEventHandlerWrapper * \brief An abstract base class for event handler wrappers. - * + * * This class provides the interface for handling events. Derived classes must implement the * `call()` method to process events */ class IEventHandlerWrapper { public: /** - * \brief Virtual destructor for IEventHandlerWrapper. - */ + * \brief Virtual destructor for IEventHandlerWrapper. + */ virtual ~IEventHandlerWrapper() = default; /** - * \brief Executes the handler with the given event. - * - * This method calls the `call()` method of the derived class, passing the event to the handler. - * - * \param e The event to be processed. - * \return A boolean value indicating whether the event is handled. - */ + * \brief Executes the handler with the given event. + * + * This method calls the `call()` method of the derived class, passing the event to the handler. + * + * \param e The event to be processed. + * \return A boolean value indicating whether the event is handled. + */ bool exec(const Event & e); private: /** - * \brief The method responsible for handling the event. - * - * This method is implemented by derived classes to process the event. - * - * \param e The event to be processed. - * \return A boolean value indicating whether the event is handled. - */ + * \brief The method responsible for handling the event. + * + * This method is implemented by derived classes to process the event. + * + * \param e The event to be processed. + * \return A boolean value indicating whether the event is handled. + */ virtual bool call(const Event & e) = 0; }; /** * \class EventHandlerWrapper * \brief A wrapper for event handler functions. - * - * This class wraps an event handler function of a specific event type. It implements the - * `call()` and `get_type()` methods to allow the handler to be executed and its type to be + * + * This class wraps an event handler function of a specific event type. It implements the + * `call()` and `get_type()` methods to allow the handler to be executed and its type to be * queried. - * + * * \tparam EventType The type of event this handler will handle. */ template <typename EventType> class EventHandlerWrapper : public IEventHandlerWrapper { public: /** - * \brief Constructs an EventHandlerWrapper with a given handler. - * - * The constructor takes an event handler function and stores it in the wrapper. - * - * \param handler The event handler function. - */ + * \brief Constructs an EventHandlerWrapper with a given handler. + * + * The constructor takes an event handler function and stores it in the wrapper. + * + * \param handler The event handler function. + */ explicit EventHandlerWrapper(const EventHandler<EventType> & handler); private: /** - * \brief Calls the stored event handler with the event. - * - * This method casts the event to the appropriate type and calls the handler. - * - * \param e The event to be handled. - * \return A boolean value indicating whether the event is handled. - */ + * \brief Calls the stored event handler with the event. + * + * This method casts the event to the appropriate type and calls the handler. + * + * \param e The event to be handled. + * \return A boolean value indicating whether the event is handled. + */ bool call(const Event & e) override; //! The event handler function. EventHandler<EventType> handler; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 4cd2bc0..ff80f49 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -10,7 +10,7 @@ class ComponentManager; /** * \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. */ @@ -19,7 +19,7 @@ private: /** * This constructor creates a new GameObject. It creates a new Transform and Metadata * component and adds them to the ComponentManager. - * + * * \param component_manager Reference to component_manager * \param id The id of the GameObject * \param name The name of the GameObject @@ -37,20 +37,20 @@ private: public: /** * \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 diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 9e173e0..fcfc080 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,5 +1,5 @@ #pragma once - +namespace crepe { //! Enumeration for mouse button inputs, including standard and extended buttons. enum class MouseButton { NONE = 0, //!< No mouse button input. @@ -85,9 +85,9 @@ enum class Keycode { PRINT_SCREEN = 283, //!< Print Screen key. PAUSE = 284, //!< Pause key. /** - * \name Function keys (F1-F25). - * \{ - */ + * \name Function keys (F1-F25). + * \{ + */ F1 = 290, F2 = 291, F3 = 292, @@ -115,9 +115,9 @@ enum class Keycode { F25 = 314, /// \} /** - * \name Keypad digits and operators. - * \{ - */ + * \name Keypad digits and operators. + * \{ + */ KP0 = 320, KP1 = 321, KP2 = 322, @@ -137,9 +137,9 @@ enum class Keycode { KP_EQUAL = 336, /// \} /** - * \name Modifier keys. - * \{ - */ + * \name Modifier keys. + * \{ + */ LEFT_SHIFT = 340, LEFT_CONTROL = 341, LEFT_ALT = 342, @@ -151,3 +151,4 @@ enum class Keycode { /// \} MENU = 348, //!< Menu key. }; +} // namespace crepe diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 42a1e77..c25e31e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -3,10 +3,12 @@ #include "../manager/EventManager.h" #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" +#include "../system/InputSystem.h" #include "../system/ParticleSystem.h" #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" +#include "manager/EventManager.h" #include "LoopManager.h" @@ -20,20 +22,27 @@ LoopManager::LoopManager() { this->load_system<PhysicsSystem>(); this->load_system<RenderSystem>(); this->load_system<ScriptSystem>(); + this->load_system<InputSystem>(); this->event_manager.subscribe<ShutDownEvent>( [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } void LoopManager::process_input() { - - this->sdl_context.handle_events(this->game_running); } + this->get_system<InputSystem>().update(); +} void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::fixed_update() {} +void LoopManager::fixed_update() { + EventManager & ev = this->mediator.event_manager; + ev.dispatch_events(); + this->get_system<ScriptSystem>().update(); + this->get_system<PhysicsSystem>().update(); + this->get_system<CollisionSystem>().update(); +} void LoopManager::loop() { @@ -55,14 +64,15 @@ void LoopManager::loop() { } void LoopManager::setup() { - this->game_running = true; this->loop_timer.start(); + this->scene_manager.load_next_scene(); } void LoopManager::render() { if (!this->game_running) return; + this->get_system<AnimatorSystem>().update(); this->get_system<RenderSystem>().update(); } bool LoopManager::on_shutdown(const ShutDownEvent & e) { diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index d07ef66..9986aa5 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -8,6 +8,7 @@ #include "../manager/EventManager.h" #include "../manager/LoopTimerManager.h" #include "../system/System.h" +#include "../manager/Mediator.h" #include "api/Event.h" @@ -98,7 +99,7 @@ private: LoopTimerManager loop_timer{mediator}; //! EventManager instance EventManager event_manager{mediator}; - + //! SDL context \todo no more singletons! SDLContext & sdl_context = SDLContext::get_instance(); diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h index 235d42f..f404703 100644 --- a/src/crepe/api/Metadata.h +++ b/src/crepe/api/Metadata.h @@ -9,7 +9,7 @@ namespace crepe { /** * \brief Metadata component - * + * * This class represents the Metadata component. It stores the name, tag, parent and children * of a GameObject. */ diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index 576ca45..8213afb 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -10,6 +10,4 @@ void crepe::Rigidbody::add_force_linear(const vec2 & force) { this->data.linear_velocity += force; } -void crepe::Rigidbody::add_force_angular(double force) { - this->data.angular_velocity += force; -} +void crepe::Rigidbody::add_force_angular(float force) { this->data.angular_velocity += force; } diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 3b0588f..40c6bf1 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -1,5 +1,8 @@ #pragma once +#include <cmath> +#include <set> + #include "../Component.h" #include "types.h" @@ -8,7 +11,7 @@ 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. */ @@ -16,7 +19,7 @@ class Rigidbody : public Component { public: /** * \brief BodyType enum - * + * * This enum provides three bodytypes the physics sytem and collision system use. */ enum class BodyType { @@ -29,54 +32,118 @@ public: }; /** * \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 + //! Prevent movement along X axis bool x = false; - //! Y constraint + //! Prevent movement along Y axis bool y = false; - //! rotation constraint + //! Prevent rotation 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 + float mass = 0.0; + /** + * \brief Gravity scale factor. + * + * The `gravity_scale` controls how much gravity affects the object. It is a multiplier applied to the default + * gravity force, allowing for fine-grained control over how the object responds to gravity. + * + */ + float gravity_scale = 0; + + //! Defines the type of the physics body, which determines how the physics system interacts with the object. BodyType body_type = BodyType::DYNAMIC; - //! linear velocity of object + + /** + * \name Linear (positional) motion + * + * These variables define the linear motion (movement along the position) of an object. + * The linear velocity is applied to the object's position in each update of the PhysicsSystem. + * The motion is affected by the object's linear velocity, its maximum velocity, and a coefficient + * that can scale the velocity over time. + * + * \{ + */ + //! Linear velocity of the object (speed and direction). vec2 linear_velocity; - //! maximum linear velocity of object - vec2 max_linear_velocity; - //! linear damping of object - vec2 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 + //! Maximum linear velocity of the object. This limits the object's speed. + vec2 max_linear_velocity = {INFINITY, INFINITY}; + //! Linear velocity coefficient. This scales the object's velocity for adjustment or damping. + vec2 linear_velocity_coefficient = {1, 1}; + //! \} + + /** + * \name Angular (rotational) motion + * + * These variables define the angular motion (rotation) of an object. + * The angular velocity determines how quickly the object rotates, while the maximum angular velocity + * sets a limit on the rotation speed. The angular velocity coefficient applies damping or scaling + * to the angular velocity, which can be used to simulate friction or other effects that slow down rotation. + * + * \{ + */ + //! Angular velocity of the object, representing the rate of rotation (in degrees). + float angular_velocity = 0; + //! Maximum angular velocity of the object. This limits the maximum rate of rotation. + float max_angular_velocity = INFINITY; + //! Angular velocity coefficient. This scales the object's angular velocity, typically used for damping. + float angular_velocity_coefficient = 1; + //! \} + + /** + * \brief Movement constraints for an object. + * + * The `PhysicsConstraints` struct defines the constraints that restrict an object's movement + * in certain directions or prevent rotation. These constraints effect only the physics system + * to prevent the object from moving or rotating in specified ways. + * + */ PhysicsConstraints constraints; - //! if gravity applies - bool use_gravity = true; - //! if object bounces - bool bounce = false; + + /** + * \brief Elasticity factor of the material (bounce factor). + * + * The `elasticity_coefficient` controls how much of the object's velocity is retained after a collision. + * It represents the material's ability to bounce or recover velocity upon impact. The coefficient is a value + * above 0.0. + * + */ + float elastisity_coefficient = 0.0; + + /** + * \brief Offset of all colliders relative to the object's transform position. + * + * The `offset` defines a positional shift applied to all colliders associated with the object, relative to the object's + * transform position. This allows for the colliders to be placed at a different position than the object's actual + * position, without modifying the object's transform itself. + * + */ + vec2 offset; + + /** + * \brief Defines the collision layers of a GameObject. + * + * The `collision_layers` specifies the layers that the GameObject will collide with. + * Each element represents a layer ID, and the GameObject will only detect + * collisions with other GameObjects that belong to these layers. + */ + std::set<int> collision_layers; }; public: - /** + /** * \param game_object_id id of the gameobject the rigibody is added to. * \param data struct to configure the rigidbody. */ @@ -85,18 +152,27 @@ public: 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 vec2 & 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); + void add_force_angular(float force); + +protected: + /** + * Ensures there is at most one Rigidbody component per entity. + * \return Always returns 1, indicating this constraint. + */ + virtual int get_instances_max() const { return 1; } + //! ComponentManager instantiates all components + friend class ComponentManager; }; } // namespace crepe diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 9f1e8ce..ba9bb76 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -12,7 +12,7 @@ class ComponentManager; /** * \brief Represents a Scene - * + * * This class represents a Scene. The Scene class is only used as an interface for the game * programmer. */ @@ -41,7 +41,7 @@ public: protected: /** * \name Late references - * + * * These references are set by SceneManager immediately after calling the constructor of Scene. * * \note Scene must have a constructor without arguments so the game programmer doesn't need to diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 1b339b0..d99ab0e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -4,6 +4,7 @@ #include "../manager/EventManager.h" #include "../manager/Mediator.h" +#include "../system/CollisionSystem.h" #include "../types.h" #include "../util/OptionalRef.h" @@ -19,7 +20,7 @@ class ComponentManager; * This class is used as a base class for user-defined scripts that can be added to game * objects using the \c BehaviorScript component. * - * \info Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as + * \note Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as * member or lambda methods in derivative user script classes and registered in \c init(). * * \warning Concrete scripts are allowed do create a custom constructor, but the utility diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 0a2ad4c..cc0e20a 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -6,24 +6,20 @@ #include "Component.h" #include "Sprite.h" #include "Texture.h" +#include "types.h" using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, int height) +Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & data) : Component(id), - color(color), - flip(flip), - sprite_image(std::move(image)), - sorting_in_layer(sort_layer), - order_in_layer(order_layer), - height(height) { + texture(std::move(texture)), + data(data) { dbg_trace(); - this->mask.w = sprite_image.get_size().x; - this->mask.h = sprite_image.get_size().y; + this->mask.w = this->texture.get_size().x; + this->mask.h = this->texture.get_size().y; this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h; } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a0e90a0..dbf41e4 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,11 +1,10 @@ #pragma once -#include <cstdint> - #include "../Component.h" #include "Color.h" #include "Texture.h" +#include "types.h" namespace crepe { @@ -20,60 +19,79 @@ class AnimatorSystem; * flip settings, and is managed in layers with defined sorting orders. */ class Sprite : public Component { - public: + //! settings to flip the image struct FlipSettings { + //! horizantal flip bool flip_x = false; + //! vertical flip bool flip_y = false; }; + //! Sprite data that does not have to be set in the constructor + struct Data { + /** + * \brief Sprite tint (multiplied) + * + * The sprite texture's pixels are multiplied by this color before being displayed + * (including alpha channel for transparency). + */ + Color color = Color::WHITE; + + //! Flip settings for the sprite + FlipSettings flip; + + //! Layer sorting level of the sprite + const int sorting_in_layer = 0; + + //! Order within the sorting layer + const int order_in_layer = 0; + + /** + * \brief width and height of the sprite in game units + * + * - if exclusively width is specified, the height is calculated using the texture's aspect + * ratio + * - if exclusively height is specified, the width is calculated using the texture's aspect + * ratio + * - if both are specified the texture is streched to fit the specified size + */ + vec2 size = {0, 0}; + + //! independent sprite angle. rotating clockwise direction in degrees + float angle_offset = 0; + + //! independent sprite scale multiplier + float scale_offset = 1; + + //! independent sprite offset position + vec2 position_offset; + }; + public: - // TODO: Loek comment in github #27 will be looked another time - // about shared_ptr Texture /** - * \brief Constructs a Sprite with specified parameters. * \param game_id Unique identifier for the game object this sprite belongs to. - * \param image Shared pointer to the texture for this sprite. - * \param color Color tint applied to the sprite. - * \param flip Flip settings for horizontal and vertical orientation. - * \param order_layer decides the sorting in layer of the sprite. - * \param sort_layer decides the order in layer of the sprite. - * \param height the height of the image in game units - */ - Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, int height); - - /** - * \brief Destroys the Sprite instance. + * \param texture asset of the image + * \param ctx all the sprite data */ + Sprite(game_object_id_t id, Texture & texture, const Data & data); ~Sprite(); //! Texture used for the sprite - const Texture sprite_image; - - //! Color tint of the sprite - Color color; - - //! Flip settings for the sprite - FlipSettings flip; + const Texture texture; - //! Layer sorting level of the sprite - const int sorting_in_layer; - //! Order within the sorting layer - const int order_in_layer; - - //! height in world units - const int height; + Data data; +private: /** - * \aspect_ratio ratio of the img so that scaling will not become weird + * \brief ratio of the img * - * cannot be const because if Animator component is addded then ratio becomes scuffed and - * does it need to be calculated again in the Animator + * - This will multiply one of \c size variable if it is 0. + * - Will be adjusted if \c Animator component is added to an GameObject that is why this + * value cannot be const. */ - double aspect_ratio; + float aspect_ratio; -private: //! Reads the mask of sprite friend class SDLContext; diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 6243a93..7ee6d65 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -7,7 +7,7 @@ namespace crepe { /** * \brief Transform component - * + * * This class represents the Transform component. It stores the position, rotation and scale of * a GameObject. */ @@ -15,10 +15,10 @@ class Transform : public Component { public: //! Translation (shift) vec2 position = {0, 0}; - //! Rotation, in degrees - double rotation = 0; + //! Rotation, in degrees clockwise + float rotation = 0; //! Multiplication factor - double scale = 0; + float scale = 0; protected: /** diff --git a/src/crepe/api/UIObject.cpp b/src/crepe/api/UIObject.cpp new file mode 100644 index 0000000..d239b89 --- /dev/null +++ b/src/crepe/api/UIObject.cpp @@ -0,0 +1,8 @@ +#include "UIObject.h" + +using namespace crepe; + +UIObject::UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset) + : Component(id), + dimensions(dimensions), + offset(offset) {} diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h new file mode 100644 index 0000000..f7f4fba --- /dev/null +++ b/src/crepe/api/UIObject.h @@ -0,0 +1,25 @@ +#pragma once + +#include "../Component.h" + +namespace crepe { + +/** + * \brief Represents a UI object in the game, derived from the Component class. + */ +class UIObject : public Component { +public: + /** + * \brief Constructs a UiObject with the specified game object ID. + * \param id The unique ID of the game object associated with this UI object. + * \param dimensions width and height of the UIObject + * \param offset Offset relative to the GameObject Transform + */ + UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); + //! Width and height of the UIObject + vec2 dimensions; + //! Position offset relative to this GameObjects Transform + vec2 offset; +}; + +} // namespace crepe |