diff options
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 18 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 3 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 27 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 52 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 21 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 4 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/System.h | 6 |
9 files changed, 77 insertions, 62 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 676e485..8bb6465 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,24 +1,24 @@ #include <cstdint> -#include "api/Animator.h" -#include "facade/SDLContext.h" +#include "../api/Animator.h" +#include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" #include "AnimatorSystem.h" -#include "ComponentManager.h" using namespace crepe; void AnimatorSystem::update() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + 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; } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 56cc7b3..f8179a9 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -21,12 +21,11 @@ public: /** * \brief Updates the Animator components. * - * This method is called periodically (likely every frame) to update the state of all + * This method is called to update the state of all * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ void update() override; - // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index fcf7522..b14c52f 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -2,18 +2,17 @@ #include <cstdlib> #include <ctime> -#include "api/ParticleEmitter.h" -#include "api/Transform.h" -#include "api/Vector2.h" +#include "../api/ParticleEmitter.h" +#include "../api/Transform.h" +#include "../manager/ComponentManager.h" -#include "ComponentManager.h" #include "ParticleSystem.h" using namespace crepe; void ParticleSystem::update() { // Get all emitters - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>(); for (ParticleEmitter & emitter : emitters) { @@ -42,17 +41,15 @@ void ParticleSystem::update() { } void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { - constexpr double DEG_TO_RAD = M_PI / 180.0; + constexpr float DEG_TO_RAD = M_PI / 180.0; - Vector2 initial_position = emitter.data.position + transform.position; - double random_angle - = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + vec2 initial_position = emitter.data.position + transform.position; + float random_angle = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - double random_speed - = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); - double angle_radians = random_angle * DEG_TO_RAD; + float random_speed = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float angle_radians = random_angle * DEG_TO_RAD; - Vector2 velocity + vec2 velocity = {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; for (Particle & particle : emitter.data.particles) { @@ -77,7 +74,7 @@ int ParticleSystem::calculate_update(int count, double emission) const { } void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { - Vector2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; + vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; double half_width = emitter.data.boundary.width / 2.0; double half_height = emitter.data.boundary.height / 2.0; @@ -87,7 +84,7 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const double BOTTOM = offset.y + half_height; for (Particle & particle : emitter.data.particles) { - const Vector2 & position = particle.position; + const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index bcde431..eba9dfa 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,6 +1,6 @@ #include <cmath> -#include "../ComponentManager.h" +#include "../manager/ComponentManager.h" #include "../api/Config.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" @@ -11,7 +11,7 @@ using namespace crepe; void PhysicsSystem::update() { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); RefVector<Transform> transforms = mgr.get_components_by_type<Transform>(); @@ -34,7 +34,7 @@ void PhysicsSystem::update() { if (rigidbody.data.angular_damping != 0) { rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; } - if (rigidbody.data.linear_damping != Vector2{0, 0}) { + if (rigidbody.data.linear_damping != vec2{0, 0}) { rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ad510f5..4e97b3e 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,15 +2,14 @@ #include <cassert> #include <cmath> #include <functional> -#include <iostream> #include <stdexcept> #include <vector> -#include "../ComponentManager.h" +#include "../manager/ComponentManager.h" +#include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -21,8 +20,9 @@ using namespace std; void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -void RenderSystem::update_camera() { - ComponentManager & mgr = this->component_manager; + +const Camera & RenderSystem::update_camera() { + ComponentManager & mgr = this->mediator.component_manager; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); @@ -30,9 +30,13 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { 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->curr_cam_ref = &cam; + this->cam_pos = transform.position + cam.offset; + return cam; } + throw std::runtime_error("No active cameras in current scene"); } bool sorting_comparison(const Sprite & a, const Sprite & b) { @@ -51,14 +55,14 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const { void RenderSystem::update() { this->clear_screen(); - this->update_camera(); this->render(); this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, + const double & scale) { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; vector<reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id); @@ -72,19 +76,35 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, - *this->curr_cam_ref); + + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = p.position, + .angle = p.angle, + .scale = scale, + }); } } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref); +void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, + const Transform & tm) { + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = tm.position, + .angle = tm.rotation, + .scale = tm.scale, + }); } void RenderSystem::render() { + ComponentManager & mgr = this->mediator.component_manager; + const Camera & cam = this->update_camera(); - ComponentManager & mgr = this->component_manager; RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); @@ -93,10 +113,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, transform.scale); + bool rendered_particles = this->render_particle(sprite, cam, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, transform); + this->render_normal(sprite, cam, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 30b41cf..e70831e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,17 +1,17 @@ #pragma once -#include <functional> -#include <vector> +#include <cmath> #include "facade/SDLContext.h" #include "System.h" -#include <cmath> +#include "types.h" namespace crepe { class Camera; class Sprite; +class Transform; /** * \class RenderSystem @@ -37,7 +37,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - void update_camera(); + const Camera & update_camera(); //! Renders the whole screen void render(); @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale); + bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Transform & tm); + void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); /** * \brief sort a vector sprite objects with @@ -71,17 +71,14 @@ private: * \todo Include color handling for sprites. * \todo Add text rendering using SDL_ttf for text components. * \todo Implement a text component and a button component. - * \todo Ensure each sprite is checked for active status before rendering. - * \todo Sort all layers by order before rendering. * \todo Consider adding text input functionality. */ private: - //! Pointer to the current active camera for rendering - Camera * curr_cam_ref = nullptr; - // TODO: needs a better solution - SDLContext & context = SDLContext::get_instance(); + + //! camera postion in the current scene + vec2 cam_pos; }; } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 20a83f7..2e16eb0 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,4 +1,4 @@ -#include "../ComponentManager.h" +#include "../manager/ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" @@ -10,7 +10,7 @@ using namespace crepe; void ScriptSystem::update() { dbg_trace(); - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->mediator.component_manager; RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); for (BehaviorScript & behavior_script : behavior_scripts) { diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index 937a423..f68549b 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -4,4 +4,4 @@ using namespace crepe; -System::System(ComponentManager & mgr) : component_manager(mgr) { dbg_trace(); } +System::System(const Mediator & mediator) : mediator(mediator) { dbg_trace(); } diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 28ea20e..4e7fc6d 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -1,5 +1,7 @@ #pragma once +#include "../manager/Mediator.h" + namespace crepe { class ComponentManager; @@ -19,11 +21,11 @@ public: virtual void update() = 0; public: - System(ComponentManager &); + System(const Mediator & m); virtual ~System() = default; protected: - ComponentManager & component_manager; + const Mediator & mediator; }; } // namespace crepe |