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

View File

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