diff options
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AISystem.cpp | 186 | ||||
-rw-r--r-- | src/crepe/system/AISystem.h | 81 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 34 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 3 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 62 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 51 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 65 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 13 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.h | 10 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 131 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.h | 4 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 58 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 24 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 2 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 4 |
16 files changed, 571 insertions, 161 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp new file mode 100644 index 0000000..680dbb8 --- /dev/null +++ b/src/crepe/system/AISystem.cpp @@ -0,0 +1,186 @@ +#include <algorithm> +#include <cmath> + +#include "manager/ComponentManager.h" +#include "manager/LoopTimerManager.h" +#include "manager/Mediator.h" + +#include "AISystem.h" + +using namespace crepe; +using namespace std::chrono; + +void AISystem::update() { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + LoopTimerManager & loop_timer = mediator.loop_timer; + RefVector<AI> ai_components = mgr.get_components_by_type<AI>(); + + float dt = loop_timer.get_scaled_fixed_delta_time().count(); + + // Loop through all AI components + for (AI & ai : ai_components) { + if (!ai.active) { + continue; + } + + RefVector<Rigidbody> rigidbodies + = mgr.get_components_by_id<Rigidbody>(ai.game_object_id); + if (rigidbodies.empty()) { + throw std::runtime_error( + "AI component must be attached to a GameObject with a Rigidbody component"); + } + Rigidbody & rigidbody = rigidbodies.front().get(); + if (!rigidbody.active) { + continue; + } + if (rigidbody.data.mass <= 0) { + throw std::runtime_error("Mass must be greater than 0"); + } + + // Calculate the force to apply to the entity + vec2 force = this->calculate(ai, rigidbody); + // Calculate the acceleration (using the above calculated force) + vec2 acceleration = force / rigidbody.data.mass; + // Finally, update Rigidbody's velocity + rigidbody.data.linear_velocity += acceleration * dt; + } +} + +vec2 AISystem::calculate(AI & ai, const Rigidbody & rigidbody) { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id); + Transform & transform = transforms.front().get(); + + vec2 force; + + // Run all the behaviors that are on, and stop if the force gets too high + if (ai.on(AI::BehaviorTypeMask::FLEE)) { + vec2 force_to_add = this->flee(ai, rigidbody, transform); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } + } + if (ai.on(AI::BehaviorTypeMask::ARRIVE)) { + vec2 force_to_add = this->arrive(ai, rigidbody, transform); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } + } + if (ai.on(AI::BehaviorTypeMask::SEEK)) { + vec2 force_to_add = this->seek(ai, rigidbody, transform); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } + } + if (ai.on(AI::BehaviorTypeMask::PATH_FOLLOW)) { + vec2 force_to_add = this->path_follow(ai, rigidbody, transform); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } + } + + return force; +} + +bool AISystem::accumulate_force(const AI & ai, vec2 & running_total, vec2 & force_to_add) { + float magnitude = running_total.length(); + float magnitude_remaining = ai.max_force - magnitude; + + if (magnitude_remaining <= 0.0f) { + // If the force is already at/above the max force, return false + return false; + } + + float magnitude_to_add = force_to_add.length(); + if (magnitude_to_add < magnitude_remaining) { + // If the force to add is less than the remaining force, add it + running_total += force_to_add; + } else { + // If the force to add is greater than the remaining force, add a fraction of it + force_to_add.normalize(); + running_total += force_to_add * magnitude_remaining; + } + + return true; +} + +vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { + // Calculate the desired velocity + vec2 desired_velocity = ai.seek_target - transform.position; + desired_velocity.normalize(); + desired_velocity *= rigidbody.data.max_linear_velocity; + + return desired_velocity - rigidbody.data.linear_velocity; +} + +vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { + // Calculate the desired velocity if the entity is within the panic distance + vec2 desired_velocity = transform.position - ai.flee_target; + if (desired_velocity.length_squared() > ai.square_flee_panic_distance) { + return vec2{0, 0}; + } + desired_velocity.normalize(); + desired_velocity *= rigidbody.data.max_linear_velocity; + + return desired_velocity - rigidbody.data.linear_velocity; +} + +vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { + // Calculate the desired velocity (taking into account the deceleration rate) + vec2 to_target = ai.arrive_target - transform.position; + float distance = to_target.length(); + if (distance > 0.0f) { + if (ai.arrive_deceleration <= 0.0f) { + throw std::runtime_error("Deceleration rate must be greater than 0"); + } + + float speed = distance / ai.arrive_deceleration; + speed = std::min(speed, rigidbody.data.max_linear_velocity); + vec2 desired_velocity = to_target * (speed / distance); + + return desired_velocity - rigidbody.data.linear_velocity; + } + + return vec2{0, 0}; +} + +vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform) { + if (ai.path.empty()) { + return vec2{0, 0}; + } + + // Get the target node + vec2 target = ai.path.at(ai.path_index); + // Calculate the force to apply to the entity + vec2 to_target = target - transform.position; + if (to_target.length_squared() > ai.path_node_distance * ai.path_node_distance) { + // If the entity is not close enough to the target node, seek it + ai.seek_target = target; + ai.arrive_target = target; + } else { + // If the entity is close enough to the target node, move to the next node + ai.path_index++; + if (ai.path_index >= ai.path.size()) { + if (ai.path_loop) { + // If the path is looping, reset the path index + ai.path_index = 0; + } else { + // If the path is not looping, arrive at the last node + ai.path_index = ai.path.size() - 1; + return this->arrive(ai, rigidbody, transform); + } + } + } + + // Seek the target node + return this->seek(ai, rigidbody, transform); +} diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h new file mode 100644 index 0000000..d5f8a8e --- /dev/null +++ b/src/crepe/system/AISystem.h @@ -0,0 +1,81 @@ +#pragma once + +#include "api/AI.h" +#include "api/Rigidbody.h" + +#include "System.h" +#include "api/Transform.h" +#include "types.h" + +namespace crepe { + +/** + * \brief The AISystem is used to control the movement of entities using AI. + * + * The AISystem is used to control the movement of entities using AI. The AISystem can be used to + * implement different behaviors such as seeking, fleeing, arriving, and path following. + */ +class AISystem : public System { +public: + using System::System; + + //! Update the AI system + void update() override; + +private: + /** + * \brief Calculate the total force to apply to the entity + * + * \param ai The AI component + * \param rigidbody The Rigidbody component + */ + vec2 calculate(AI & ai, const Rigidbody & rigidbody); + /** + * \brief Accumulate the force to apply to the entity + * + * \param ai The AI component + * \param running_total The running total of the force + * \param force_to_add The force to add + * \return true if the force was added, false otherwise + */ + bool accumulate_force(const AI & ai, vec2 & running_total, vec2 & force_to_add); + + /** + * \brief Calculate the seek force + * + * \param ai The AI component + * \param rigidbody The Rigidbody component + * \param transform The Transform component + * \return The seek force + */ + vec2 seek(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; + /** + * \brief Calculate the flee force + * + * \param ai The AI component + * \param rigidbody The Rigidbody component + * \param transform The Transform component + * \return The flee force + */ + vec2 flee(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; + /** + * \brief Calculate the arrive force + * + * \param ai The AI component + * \param rigidbody The Rigidbody component + * \param transform The Transform component + * \return The arrive force + */ + vec2 arrive(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; + /** + * \brief Calculate the path follow force + * + * \param ai The AI component + * \param rigidbody The Rigidbody component + * \param transform The Transform component + * \return The path follow force + */ + vec2 path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform); +}; + +} // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 8bb6465..107b25d 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,24 +1,42 @@ -#include <cstdint> + #include "../api/Animator.h" -#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" +#include <chrono> #include "AnimatorSystem.h" using namespace crepe; +using namespace std::chrono; void AnimatorSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - + LoopTimerManager & timer = this->mediator.loop_timer; RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - uint64_t tick = SDLContext::get_instance().get_ticks(); + float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count(); + for (Animator & a : animations) { if (!a.active) continue; - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; - a.spritesheet.mask = a.spritesheet.mask; + if (a.data.fps == 0) continue; + + Animator::Data & ctx = a.data; + float frame_duration = 1.0f / ctx.fps; + + int last_frame = ctx.row; + + int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; + int total_frames = cycle_end - ctx.cycle_start; + + int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; + + ctx.row = ctx.cycle_start + curr_frame; + a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); + + if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { + a.active = false; + } } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index f8179a9..7d3f565 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -2,9 +2,6 @@ #include "System.h" -//TODO: -// control if flip works with animation system - namespace crepe { /** diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp new file mode 100644 index 0000000..b1aa0f8 --- /dev/null +++ b/src/crepe/system/AudioSystem.cpp @@ -0,0 +1,62 @@ +#include "AudioSystem.h" + +#include "../manager/ComponentManager.h" +#include "../manager/ResourceManager.h" +#include "../types.h" + +using namespace crepe; +using namespace std; + +void AudioSystem::update() { + ComponentManager & component_manager = this->mediator.component_manager; + ResourceManager & resource_manager = this->mediator.resource_manager; + RefVector<AudioSource> components + = component_manager.get_components_by_type<AudioSource>(); + + for (AudioSource & component : components) { + Sound & resource = resource_manager.get<Sound>(component.source); + + this->diff_update(component, resource); + + this->update_last(component); + } +} + +void AudioSystem::diff_update(AudioSource & component, Sound & resource) { + SoundContext & context = this->get_context(); + + if (component.active != component.last_active) { + if (!component.active) { + context.stop(component.voice); + return; + } + if (component.play_on_awake) component.oneshot_play = true; + } + if (!component.active) return; + + if (component.oneshot_play) { + component.voice = context.play(resource); + component.oneshot_play = false; + } + if (component.oneshot_stop) { + context.stop(component.voice); + component.oneshot_stop = false; + } + if (component.volume != component.last_volume) { + context.set_volume(component.voice, component.volume); + } + if (component.loop != component.last_loop) { + context.set_loop(component.voice, component.loop); + } +} + +void AudioSystem::update_last(AudioSource & component) { + component.last_active = component.active; + component.last_loop = component.loop; + component.last_volume = component.volume; +} + +SoundContext & AudioSystem::get_context() { + if (this->context == nullptr) this->context = make_unique<SoundContext>(); + return *this->context.get(); +} diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h new file mode 100644 index 0000000..2ddc443 --- /dev/null +++ b/src/crepe/system/AudioSystem.h @@ -0,0 +1,51 @@ +#pragma once + +#include "../api/AudioSource.h" +#include "../facade/Sound.h" +#include "../facade/SoundContext.h" + +#include "System.h" + +namespace crepe { + +class AudioSystem : public System { +public: + using System::System; + void update() override; + +private: + /** + * \brief Update `last_*` members of \c component + * + * Copies all component properties stored for comparison between AudioSystem::update() calls + * + * \param component AudioSource component to update + */ + void update_last(AudioSource & component); + + /** + * \brief Compare update component + * + * Compares properties of \c component and \c data, and calls SoundContext functions where + * applicable. + * + * \param component AudioSource component to update + * \param resource Sound instance for AudioSource's Asset + */ + void diff_update(AudioSource & component, Sound & resource); + +protected: + /** + * \brief Get SoundContext + * + * SoundContext is retrieved through this function instead of being a direct member of + * AudioSystem to aid with testability. + */ + virtual SoundContext & get_context(); + +private: + //! SoundContext + std::unique_ptr<SoundContext> context = nullptr; +}; + +} // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index 95f6e33..0e2db76 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -5,8 +5,10 @@ target_sources(crepe PUBLIC PhysicsSystem.cpp CollisionSystem.cpp RenderSystem.cpp + AudioSystem.cpp AnimatorSystem.cpp InputSystem.cpp + AISystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,6 +17,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES PhysicsSystem.h CollisionSystem.h RenderSystem.h + AudioSystem.h AnimatorSystem.h InputSystem.h + AISystem.h ) diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 7e893c8..5b136c6 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -37,7 +37,7 @@ private: /** * \brief A structure to store the collision data of a single collider. - * + * * This structure all components and id that are for needed within this system when calculating or handeling collisions. * The transform and rigidbody are mostly needed for location and rotation. * In rigidbody additional info is written about what the body of the object is, @@ -65,7 +65,7 @@ private: public: /** * \brief Structure representing detailed collision information between two colliders. - * + * * Includes information about the colliding objects and the resolution data for handling the collision. */ struct CollisionInfo { @@ -90,9 +90,9 @@ public: private: /** * \brief Determines the type of collider pair from two colliders. - * + * * Uses std::holds_alternative to identify the types of the provided colliders. - * + * * \param collider1 First collider variant (BoxCollider or CircleCollider). * \param collider2 Second collider variant (BoxCollider or CircleCollider). * \return The combined type of the two colliders. @@ -102,9 +102,9 @@ private: /** * \brief Calculates the current position of a collider. - * + * * Combines the Collider offset, Transform position, and Rigidbody offset to compute the position of the collider. - * + * * \param collider_offset The offset of the collider. * \param transform The Transform of the associated game object. * \param rigidbody The Rigidbody of the associated game object. @@ -116,9 +116,9 @@ private: private: /** * \brief Handles collision resolution between two colliders. - * + * * Processes collision data and adjusts objects to resolve collisions and/or calls the user oncollision script function. - * + * * \param data1 Collision data for the first collider. * \param data2 Collision data for the second collider. */ @@ -126,9 +126,9 @@ private: /** * \brief Resolves collision between two colliders and calculates the movement required. - * + * * Determines the displacement and direction needed to separate colliders based on their types. - * + * * \param data1 Collision data for the first collider. * \param data2 Collision data for the second collider. * \param type The type of collider pair. @@ -140,9 +140,9 @@ private: /** * \brief Calculates the resolution vector for two BoxColliders. - * + * * Computes the displacement required to separate two overlapping BoxColliders. - * + * * \param box_collider1 The first BoxCollider. * \param box_collider2 The second BoxCollider. * \param position1 The position of the first BoxCollider. @@ -155,13 +155,13 @@ private: /** * \brief Calculates the resolution vector for two CircleCollider. - * + * * Computes the displacement required to separate two overlapping CircleCollider. - * + * * \param circle_collider1 The first CircleCollider. * \param circle_collider2 The second CircleCollider. - * \param position1 The position of the first CircleCollider. - * \param position2 The position of the second CircleCollider. + * \param final_position1 The position of the first CircleCollider. + * \param final_position2 The position of the second CircleCollider. * \return The resolution vector for the collision. */ vec2 get_circle_circle_resolution(const CircleCollider & circle_collider1, @@ -171,14 +171,13 @@ private: /** * \brief Calculates the resolution vector for two CircleCollider. - * + * * Computes the displacement required to separate two overlapping CircleCollider. - * + * * \param circle_collider The first CircleCollider. * \param box_collider The second CircleCollider. * \param circle_position The position of the CircleCollider. * \param box_position The position of the BoxCollider. - * \param inverse Inverted true if box circle collision, false if circle box collision (inverts the direction). * \return The resolution vector for the collision. */ vec2 get_circle_box_resolution(const CircleCollider & circle_collider, @@ -188,18 +187,18 @@ private: /** * \brief Determines the appropriate collision handler for a collision. - * + * * Decides the correct resolution process based on the dynamic or static nature of the colliders involved. - * + * * \param info Collision information containing data about both colliders. */ void determine_collision_handler(CollisionInfo & info); /** * \brief Handles collisions involving static objects. - * + * * Resolves collisions by adjusting positions and modifying velocities if bounce is enabled. - * + * * \param info Collision information containing data about both colliders. */ void static_collision_handler(CollisionInfo & info); @@ -207,9 +206,9 @@ private: private: /** * \brief Checks for collisions between colliders. - * + * * Identifies collisions and generates pairs of colliding objects for further processing. - * + * * \param colliders A collection of all active colliders. * \return A list of collision pairs with their associated data. */ @@ -218,13 +217,13 @@ private: /** * \brief Checks if two collision layers have at least one common layer. - * + * * This function checks if there is any overlapping layer between the two inputs. - * It compares each layer from the first input to see - * if it exists in the second input. If at least one common layer is found, - * the function returns true, indicating that the two colliders share a common + * It compares each layer from the first input to see + * if it exists in the second input. If at least one common layer is found, + * the function returns true, indicating that the two colliders share a common * collision layer. - * + * * \param layers1 all collision layers for the first collider. * \param layers2 all collision layers for the second collider. * \return Returns true if there is at least one common layer, false otherwise. @@ -234,9 +233,9 @@ private: /** * \brief Checks for collision between two colliders. - * + * * Calls the appropriate collision detection function based on the collider types. - * + * * \param first_info Collision data for the first collider. * \param second_info Collision data for the second collider. * \param type The type of collider pair. @@ -248,7 +247,7 @@ private: /** * \brief Detects collisions between two BoxColliders. - * + * * \param box1 The first BoxCollider. * \param box2 The second BoxCollider. * \param transform1 Transform of the first object. diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 7cc8d30..a710ae2 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,6 +1,8 @@ #include "../api/Button.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" +#include "facade/SDLContext.h" +#include "util/Log.h" #include "InputSystem.h" @@ -9,7 +11,8 @@ using namespace crepe; void InputSystem::update() { ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; - std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events(); + SDLContext & context = this->mediator.sdl_context; + std::vector<SDLContext::EventData> event_list = context.get_events(); RefVector<Button> buttons = mgr.get_components_by_type<Button>(); RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); OptionalRef<Camera> curr_cam_ref; @@ -24,10 +27,10 @@ void InputSystem::update() { RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(current_cam.game_object_id); Transform & cam_transform = transform_vec.front().get(); - int camera_origin_x - = cam_transform.position.x + current_cam.offset.x - (current_cam.viewport_size.x / 2); - int camera_origin_y - = cam_transform.position.y + current_cam.offset.y - (current_cam.viewport_size.y / 2); + int camera_origin_x = cam_transform.position.x + current_cam.data.postion_offset.x + - (current_cam.viewport_size.x / 2); + int camera_origin_y = cam_transform.position.y + current_cam.data.postion_offset.y + - (current_cam.viewport_size.y / 2); for (const SDLContext::EventData & event : event_list) { int world_mouse_x = event.mouse_position.x + camera_origin_x; diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index c647284..068f01c 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -25,7 +25,7 @@ public: private: /** * \brief Emits a particle from the specified emitter based on its emission properties. - * + * * \param emitter Reference to the ParticleEmitter. * \param transform Const reference to the Transform component associated with the emitter. */ @@ -34,7 +34,7 @@ private: /** * \brief Calculates the number of times particles should be emitted based on emission rate * and update count. - * + * * \param count Current update count. * \param emission Emission rate. * \return The number of particles to emit. @@ -44,7 +44,7 @@ private: /** * \brief Checks whether particles are within the emitter’s boundary, resets or stops * particles if they exit. - * + * * \param emitter Reference to the ParticleEmitter. * \param transform Const reference to the Transform component associated with the emitter. */ @@ -52,7 +52,7 @@ private: /** * \brief Generates a random angle for particle emission within the specified range. - * + * * \param min_angle Minimum emission angle in degrees. * \param max_angle Maximum emission angle in degrees. * \return Random angle in degrees. @@ -61,7 +61,7 @@ private: /** * \brief Generates a random speed for particle emission within the specified range. - * + * * \param min_speed Minimum emission speed. * \param max_speed Maximum emission speed. * \return Random speed. diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index ebf4439..3b3b8ab 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -5,88 +5,95 @@ #include "../api/Transform.h" #include "../api/Vector2.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" +#include "../manager/Mediator.h" #include "PhysicsSystem.h" using namespace crepe; void PhysicsSystem::update() { - ComponentManager & mgr = this->mediator.component_manager; + + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + LoopTimerManager & loop_timer = mediator.loop_timer; RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); - RefVector<Transform> transforms = mgr.get_components_by_type<Transform>(); + float dt = loop_timer.get_scaled_fixed_delta_time().count(); - double gravity = Config::get_instance().physics.gravity; + float gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { if (!rigidbody.active) continue; + Transform & transform + = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get(); switch (rigidbody.data.body_type) { case Rigidbody::BodyType::DYNAMIC: - for (Transform & transform : transforms) { - if (transform.game_object_id == rigidbody.game_object_id) { + if (transform.game_object_id == rigidbody.game_object_id) { + // Add gravity - // Add gravity - if (rigidbody.data.gravity_scale > 0) { - rigidbody.data.linear_velocity.y - += (rigidbody.data.mass * rigidbody.data.gravity_scale - * gravity); - } - // Add damping - if (rigidbody.data.angular_velocity_coefficient > 0) { - rigidbody.data.angular_velocity - *= rigidbody.data.angular_velocity_coefficient; - } - if (rigidbody.data.linear_velocity_coefficient.x > 0 - && rigidbody.data.linear_velocity_coefficient.y > 0) { - rigidbody.data.linear_velocity - *= rigidbody.data.linear_velocity_coefficient; - } + if (rigidbody.data.mass <= 0) { + throw std::runtime_error("Mass must be greater than 0"); + } - // 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 (gravity <= 0) { + throw std::runtime_error("Config Gravity must be greater than 0"); + } - 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.gravity_scale > 0 && !rigidbody.data.constraints.y) { + rigidbody.data.linear_velocity.y + += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity + * dt); + } + // Add coefficient rotation + if (rigidbody.data.angular_velocity_coefficient > 0) { + rigidbody.data.angular_velocity + *= std::pow(rigidbody.data.angular_velocity_coefficient, dt); + } - 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; - } + // Add coefficient movement horizontal + if (rigidbody.data.linear_velocity_coefficient.x > 0 + && !rigidbody.data.constraints.x) { + rigidbody.data.linear_velocity.x + *= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt); + } - // 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; + // Add coefficient movement horizontal + if (rigidbody.data.linear_velocity_coefficient.y > 0 + && !rigidbody.data.constraints.y) { + rigidbody.data.linear_velocity.y + *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt); + } + + // 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; + } + + // Set max velocity to maximum length + if (rigidbody.data.linear_velocity.length() + > rigidbody.data.max_linear_velocity) { + rigidbody.data.linear_velocity.normalize(); + rigidbody.data.linear_velocity *= rigidbody.data.max_linear_velocity; + } + + // Move object + if (!rigidbody.data.constraints.rotation) { + transform.rotation += rigidbody.data.angular_velocity * dt; + 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 * dt; + } + if (!rigidbody.data.constraints.y) { + transform.position.y += rigidbody.data.linear_velocity.y * dt; + } } break; case Rigidbody::BodyType::KINEMATIC: diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index 227ab69..26152a5 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -6,7 +6,7 @@ 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. */ @@ -15,7 +15,7 @@ public: using System::System; /** * \brief updates the physics system. - * + * * It calculates new velocties and changes the postion in the transform. */ void update() override; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 92dba43..afd9548 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,20 +10,29 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "../facade/Texture.h" #include "../manager/ComponentManager.h" +#include "../manager/ResourceManager.h" #include "RenderSystem.h" +#include "types.h" using namespace crepe; using namespace std; -void RenderSystem::clear_screen() { this->context.clear_screen(); } +void RenderSystem::clear_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.clear_screen(); +} -void RenderSystem::present_screen() { this->context.present_screen(); } +void RenderSystem::present_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.present_screen(); +} -const Camera & RenderSystem::update_camera() { +void RenderSystem::update_camera() { ComponentManager & mgr = this->mediator.component_manager; - + SDLContext & ctx = this->mediator.sdl_context; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); @@ -32,16 +41,18 @@ const Camera & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); - this->context.set_camera(cam); - this->cam_pos = transform.position + cam.offset; - return cam; + vec2 new_camera_pos = transform.position + cam.data.postion_offset; + ctx.update_camera_view(cam, new_camera_pos); + return; } throw std::runtime_error("No active cameras in current scene"); } bool sorting_comparison(const Sprite & a, const Sprite & b) { - if (a.sorting_in_layer < b.sorting_in_layer) return true; - if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer; + if (a.data.sorting_in_layer != b.data.sorting_in_layer) + return a.data.sorting_in_layer < b.data.sorting_in_layer; + if (a.data.order_in_layer != b.data.order_in_layer) + return a.data.order_in_layer < b.data.order_in_layer; return false; } @@ -59,10 +70,12 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, - const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { ComponentManager & mgr = this->mediator.component_manager; + SDLContext & ctx = this->mediator.sdl_context; + ResourceManager & resource_manager = this->mediator.resource_manager; + Texture & res = resource_manager.get<Texture>(sprite.source); vector<reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id); @@ -77,10 +90,9 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw(SDLContext::RenderContext{ + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, - .cam_pos = this->cam_pos, + .texture = res, .pos = p.position, .angle = p.angle, .scale = scale, @@ -89,12 +101,14 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, - const Transform & tm) { - this->context.draw(SDLContext::RenderContext{ +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { + SDLContext & ctx = this->mediator.sdl_context; + ResourceManager & resource_manager = this->mediator.resource_manager; + const Texture & res = resource_manager.get<Texture>(sprite.source); + + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, - .cam_pos = this->cam_pos, + .texture = res, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -103,7 +117,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, void RenderSystem::render() { ComponentManager & mgr = this->mediator.component_manager; - const Camera & cam = this->update_camera(); + this->update_camera(); RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); @@ -113,10 +127,10 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, cam, transform.scale); + bool rendered_particles = this->render_particle(sprite, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, cam, transform); + this->render_normal(sprite, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 096d058..fc7b46e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -2,8 +2,6 @@ #include <cmath> -#include "facade/SDLContext.h" - #include "System.h" #include "types.h" @@ -14,11 +12,10 @@ class Sprite; class Transform; /** - * \class RenderSystem * \brief Manages rendering operations for all game objects. * * RenderSystem is responsible for rendering, clearing and presenting the screen, and - * managing the active camera. + * managing the active camera. */ class RenderSystem : public System { public: @@ -37,7 +34,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - const Camera & update_camera(); + void update_camera(); //! Renders the whole screen void render(); @@ -52,20 +49,20 @@ private: * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); + bool render_particle(const Sprite & sprite, const double & scale); /** - * \brief renders a sprite with a Transform component on the screen + * \brief renders a sprite with a Transform component on the screen * * \param sprite the sprite component that holds all the data - * \param tm the Transform component that holds the position,rotation and scale + * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); + void render_normal(const Sprite & sprite, const Transform & tm); /** * \brief sort a vector sprite objects with * - * \param objs the vector that will do a sorting algorithm on + * \param objs the vector that will do a sorting algorithm on * \return returns a sorted reference vector */ RefVector<Sprite> sort(RefVector<Sprite> & objs) const; @@ -75,13 +72,6 @@ private: * \todo Implement a text component and a button component. * \todo Consider adding text input functionality. */ - -private: - // FIXME: retrieve sdlcontext via mediator after #PR57 - SDLContext & context = SDLContext::get_instance(); - - //! camera postion in the current scene - vec2 cam_pos; }; } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index 936e9ca..3db1b1e 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -8,7 +8,7 @@ class Script; /** * \brief Script system - * + * * The script system is responsible for all \c BehaviorScript components, and * calls the methods on classes derived from \c Script. */ diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index f68549b..ecc740d 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -1,7 +1,5 @@ -#include "../util/Log.h" - #include "System.h" using namespace crepe; -System::System(const Mediator & mediator) : mediator(mediator) { dbg_trace(); } +System::System(const Mediator & mediator) : mediator(mediator) {} |