feat: implemented scrolling ground
parent
16fb1d26a4
commit
5109df0e6e
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue