From 5109df0e6ef5188388500233ca498164ee5a1b44 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 9 Jan 2025 21:57:29 +0100 Subject: [PATCH] feat: implemented scrolling ground --- src/scrolling_ground.cpp | 32 ++++++++++++++++++++++++++++++++ src/scrolling_ground.hpp | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/scrolling_ground.cpp create mode 100644 src/scrolling_ground.hpp diff --git a/src/scrolling_ground.cpp b/src/scrolling_ground.cpp new file mode 100644 index 0000000..2ecf4c4 --- /dev/null +++ b/src/scrolling_ground.cpp @@ -0,0 +1,32 @@ +#include "scrolling_ground.hpp" +#include "core/sprite.hpp" +#include + +#define NUM_SPRITES 2u + +ScrollingGround::ScrollingGround() +: ce::Node2D("scrolling_ground") { + this->sprites[0] = this->create_child("background_1", "background"); + this->sprites[0]->set_global_transform(this->get_global_transform() + .scaled({10.f, 10.f}) + ); + this->sprites[1] = this->create_child("background_2", "background"); + this->sprites[1]->set_global_transform(this->sprites[1]->get_global_transform() + .scaled({10.f, 10.f}) + .translated({ + .x = 0.f, + .y = this->sprites[1]->get_size().y * 10.f + }) + ); +} + +void ScrollingGround::_tick(double const &delta) { + ce::Transform trans; + for(size_t i{0u}; i < NUM_SPRITES; ++i) { + trans = this->sprites[i]->get_global_transform() + .translated({0.f, float(this->SPEED * delta)}); + if(trans.position.y >= this->sprites[i]->get_size().y) + trans.position.y -= this->sprites[i]->get_size().y * 2; + this->sprites[i]->set_global_transform(trans); + } +} diff --git a/src/scrolling_ground.hpp b/src/scrolling_ground.hpp new file mode 100644 index 0000000..5add713 --- /dev/null +++ b/src/scrolling_ground.hpp @@ -0,0 +1,17 @@ +#ifndef SCROLLING_GROUND_HPP +#define SCROLLING_GROUND_HPP + +#include "core/node2d.hpp" +namespace ce { +class Sprite; +} + +class ScrollingGround : public ce::Node2D { + float const SPEED{10.f}; + ce::Sprite *sprites[2]{nullptr, nullptr}; +public: + ScrollingGround(); + virtual void _tick(double const &delta) override; +}; + +#endif // !SCROLLING_GROUND_HPP