SDLPP: animation/movement chagnes

This commit is contained in:
zv0n 2021-04-24 23:26:27 +02:00
parent 6cfe2046d4
commit ee82430f82
2 changed files with 19 additions and 14 deletions

View File

@ -13,7 +13,7 @@ void RenderObject::render() {
polygon->render( *renderer );
if ( texture != NULL ) {
SDL_Rect *src = NULL;
if ( animation.empty() ) {
if ( animation.empty() || !animating ) {
if ( !entireTexture() )
src = &src_rect;
} else {
@ -99,9 +99,15 @@ void RenderObject::unsetColor() {
void RenderObject::setMovementSpeed( double speed ) {
movementSpeed = speed;
}
void RenderObject::addMovement( int x, int y ) {
void RenderObject::addMovement( double x, double y ) {
movementDirection += { x, y };
}
void RenderObject::setMovement( double x, double y ) {
movementDirection = { x, y };
}
Vec2D<double> RenderObject::getMovement() {
return movementDirection;
}
void RenderObject::resetMovementX() {
movementDirection = { 0, movementDirection.getY() };
}
@ -146,7 +152,7 @@ void RenderObject::animate( int ticks ) {
if ( animation_next_frame <= 0 ) {
animation_index = ( animation_index + 1 ) % animation.size();
animation_next_frame =
( 1000 / animation_fps ) + animation_next_frame;
animation_next_frame_base + animation_next_frame;
}
}
}
@ -154,12 +160,8 @@ void RenderObject::animate( int ticks ) {
void RenderObject::move( int ticks ) {
if ( permanent )
return;
auto addx =
static_cast< double >( movementSpeed * movementDirection.getX() ) *
( static_cast< double >( ticks ) / 1000 );
auto addy =
static_cast< double >( movementSpeed * movementDirection.getY() ) *
( static_cast< double >( ticks ) / 1000 );
auto addx = movementSpeed * movementDirection.getX() * ticks / 1000.0;
auto addy = movementSpeed * movementDirection.getY() * ticks / 1000.0;
if ( std::isnan( addx ) || std::isnan( addy ) )
return;
@ -231,8 +233,8 @@ void RenderObject::resumeAnimation() {
void RenderObject::removeAnimation() {
animation.clear();
}
void RenderObject::setAnimationSpeed( const int fps ) {
void RenderObject::setAnimationSpeed( const double fps ) {
animation_fps = fps;
animation_next_frame = 1000 / fps;
animation_next_frame = animation_next_frame_base = 1000 / fps;
}
} // namespace SDLPP

View File

@ -63,7 +63,9 @@ public:
virtual void unsetColor();
// per second, relative to window width
void setMovementSpeed( double speed );
void addMovement( int x, int y );
void addMovement( double x, double y );
void setMovement( double x, double y );
Vec2D<double> getMovement();
void resetMovementX();
void resetMovementY();
void clearColided();
@ -94,7 +96,7 @@ public:
void addAnimationFrame( const SDL_Rect &frame );
void addAnimationFrame( const int x, const int y, const int w,
const int h );
void setAnimationSpeed( const int fps );
void setAnimationSpeed( const double fps );
void pauseAnimation();
void resumeAnimation();
void removeAnimation();
@ -109,7 +111,7 @@ protected:
std::shared_ptr< Renderer > renderer;
std::shared_ptr< CollisionPolygon > polygon;
double movementSpeed = 0;
Vec2D< int > movementDirection = { 0, 0 };
Vec2D< double > movementDirection = { 0, 0 };
std::vector< std::shared_ptr< RenderObject > > colidedWith;
uint64_t id = -1;
bool hidden = false;
@ -124,6 +126,7 @@ protected:
size_t animation_index = 0;
size_t animation_fps = 1;
int animation_next_frame = 1000;
int animation_next_frame_base = 0;
std::vector< SDL_Rect > animation{};
bool animating = true;