SDLPP: add constant rotation

This commit is contained in:
zv0n 2023-04-08 20:19:22 +02:00
parent 5a0cb397b1
commit 9617499ab8
6 changed files with 35 additions and 4 deletions

View File

@ -149,6 +149,7 @@ bool MarioBlock::isTraveling() const {
}
void MarioBlock::custom_move(int ticks) {
SDLPP::RectangleRender::custom_move(ticks);
if (!_bouncing && !_traveling) {
return;
}

View File

@ -31,7 +31,9 @@ public:
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override {}
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual void custom_move( int ticks ) override {
RenderObject::custom_move(ticks);
}
virtual std::pair< Vec2D< double >, Vec2D< double > >
getDoubleRect() const override;
virtual int leftmost() override;

View File

@ -24,7 +24,9 @@ public:
virtual void setColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override{};
virtual void render() override;
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual void custom_move( int ticks ) override {
RenderObject::custom_move(ticks);
}
virtual void setPos( double x, double y ) override;
virtual void setPos( const std::pair< double, double > &pos ) override;
virtual void setPos( const Vec2D< double > &vec ) override;

View File

@ -61,7 +61,9 @@ public:
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override {}
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual void custom_move( int ticks ) override {
RenderObject::custom_move(ticks);
}
virtual std::pair< Vec2D< double >, Vec2D< double > >
getDoubleRect() const override;
virtual int leftmost() override;

View File

@ -291,6 +291,16 @@ void RenderObject::rotateClockwise( int angle ) {
void RenderObject::rotateCounterClockwise( int angle ) {
rotateClockwise( 360 - ( angle % 360 ) );
}
void RenderObject::setRotationSpeed(int speed) {
rotation_speed = speed;
}
void RenderObject::startRotation() {
rotating = true;
}
void RenderObject::stopRotation() {
rotating = false;
}
Vec2D< double > RenderObject::computeAlignmentAdditions() {
double x_addition = 0;
double y_addition = 0;
@ -322,4 +332,10 @@ void RenderObject::updateXY() {
current = { original.getX() + additions.getX(),
original.getY() + additions.getY() };
}
void RenderObject::custom_move(int ticks) {
if(isRotating()) {
auto angle = ticks * rotation_speed / 1000; // tick = millisecond
rotateCounterClockwise(angle);
}
}
} // namespace SDLPP

View File

@ -131,10 +131,16 @@ public:
void setRotationCenter( const Vec2D< double > &center );
void rotateClockwise( int angle );
void rotateCounterClockwise( int angle );
void setRotationSpeed(int speed);
void startRotation();
void stopRotation();
protected:
virtual void copyTo( std::shared_ptr< RenderObject > other );
bool entireTexture();
bool isRotating() {
return rotating;
}
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
@ -164,6 +170,8 @@ protected:
Vec2D< double > rotation_center = { -1, -1 };
SDL_Point rotation_center_point;
int rotation_angle = 0;
int rotation_speed = 720;
bool rotating = false;
private:
friend Scene;