feat: implemented scrolling ground

main
Sara 2025-01-09 21:57:29 +01:00
parent 16fb1d26a4
commit 5109df0e6e
2 changed files with 49 additions and 0 deletions

32
src/scrolling_ground.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "scrolling_ground.hpp"
#include "core/sprite.hpp"
#include <SDL2/SDL.h>
#define NUM_SPRITES 2u
ScrollingGround::ScrollingGround()
: ce::Node2D("scrolling_ground") {
this->sprites[0] = this->create_child<ce::Sprite>("background_1", "background");
this->sprites[0]->set_global_transform(this->get_global_transform()
.scaled({10.f, 10.f})
);
this->sprites[1] = this->create_child<ce::Sprite>("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);
}
}

17
src/scrolling_ground.hpp Normal file
View File

@ -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