Mario: use object alignment

This commit is contained in:
zvon 2021-04-27 15:56:13 +02:00
parent e5d0610f6d
commit 7c38e122d0
3 changed files with 49 additions and 48 deletions

View File

@ -10,55 +10,62 @@ createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, double x, double y,
auto block = std::make_shared< SDLPP::RectangleRender >( auto block = std::make_shared< SDLPP::RectangleRender >(
x, y, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ); x, y, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src );
block->setId( id ); block->setId( id );
block->centerX(); block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
block->setStatic(); block->setStatic();
if ( collision ) if ( collision )
block->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); block->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
return block; return block;
} }
SDL_Rect getSourceRectByID(uint64_t id) { SDL_Rect getSourceRectByID( uint64_t id ) {
switch(id) { switch ( id ) {
case FLOOR_OVERWORLD_ID: case FLOOR_OVERWORLD_ID:
return FLOOR_OVERWORLD_SRC; return FLOOR_OVERWORLD_SRC;
case HILL_OVERWORLD_INCLINE_ID: case HILL_OVERWORLD_INCLINE_ID:
return HILL_OVERWORLD_INCLINE_SRC; return HILL_OVERWORLD_INCLINE_SRC;
case HILL_OVERWORLD_DECLINE_ID: case HILL_OVERWORLD_DECLINE_ID:
return HILL_OVERWORLD_DECLINE_SRC; return HILL_OVERWORLD_DECLINE_SRC;
case HILL_OVERWORLD_DOTS_RIGHT_ID: case HILL_OVERWORLD_DOTS_RIGHT_ID:
return HILL_OVERWORLD_DOTS_RIGHT_SRC; return HILL_OVERWORLD_DOTS_RIGHT_SRC;
case HILL_OVERWORLD_DOTS_LEFT_ID: case HILL_OVERWORLD_DOTS_LEFT_ID:
return HILL_OVERWORLD_DOTS_LEFT_SRC; return HILL_OVERWORLD_DOTS_LEFT_SRC;
case HILL_OVERWORLD_FILL_ID: case HILL_OVERWORLD_FILL_ID:
return HILL_OVERWORLD_FILL_SRC; return HILL_OVERWORLD_FILL_SRC;
case HILL_OVERWORLD_TOP_ID: case HILL_OVERWORLD_TOP_ID:
return HILL_OVERWORLD_TOP_SRC; return HILL_OVERWORLD_TOP_SRC;
case BUSH_OVERWORLD_LEFT_ID: case BUSH_OVERWORLD_LEFT_ID:
return BUSH_OVERWORLD_LEFT_SRC; return BUSH_OVERWORLD_LEFT_SRC;
case BUSH_OVERWORLD_MIDDLE_ID: case BUSH_OVERWORLD_MIDDLE_ID:
return BUSH_OVERWORLD_MIDDLE_SRC; return BUSH_OVERWORLD_MIDDLE_SRC;
case BUSH_OVERWORLD_RIGHT_ID: case BUSH_OVERWORLD_RIGHT_ID:
return BUSH_OVERWORLD_RIGHT_SRC; return BUSH_OVERWORLD_RIGHT_SRC;
case CLOUD_OVERWORLD_LEFT_BOTTOM_ID: case CLOUD_OVERWORLD_LEFT_BOTTOM_ID:
return CLOUD_OVERWORLD_LEFT_BOTTOM_SRC; return CLOUD_OVERWORLD_LEFT_BOTTOM_SRC;
case CLOUD_OVERWORLD_MIDDLE_BOTTOM_ID: case CLOUD_OVERWORLD_MIDDLE_BOTTOM_ID:
return CLOUD_OVERWORLD_MIDDLE_BOTTOM_SRC; return CLOUD_OVERWORLD_MIDDLE_BOTTOM_SRC;
case CLOUD_OVERWORLD_RIGHT_BOTTOM_ID: case CLOUD_OVERWORLD_RIGHT_BOTTOM_ID:
return CLOUD_OVERWORLD_RIGHT_BOTTOM_SRC; return CLOUD_OVERWORLD_RIGHT_BOTTOM_SRC;
case CLOUD_OVERWORLD_LEFT_TOP_ID: case CLOUD_OVERWORLD_LEFT_TOP_ID:
return CLOUD_OVERWORLD_LEFT_TOP_SRC; return CLOUD_OVERWORLD_LEFT_TOP_SRC;
case CLOUD_OVERWORLD_MIDDLE_TOP_ID: case CLOUD_OVERWORLD_MIDDLE_TOP_ID:
return CLOUD_OVERWORLD_MIDDLE_TOP_SRC; return CLOUD_OVERWORLD_MIDDLE_TOP_SRC;
case CLOUD_OVERWORLD_RIGHT_TOP_ID: case CLOUD_OVERWORLD_RIGHT_TOP_ID:
return CLOUD_OVERWORLD_RIGHT_TOP_SRC; return CLOUD_OVERWORLD_RIGHT_TOP_SRC;
} }
return {}; return {};
} }
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y, bool collision ) { std::shared_ptr< SDLPP::RectangleRender >
return createBlock(renderer, x, y, g_terrain_texture, getSourceRectByID(block_id), block_id, collision); createTerrainBlock( uint64_t block_id,
std::shared_ptr< SDLPP::Renderer > &renderer, double x,
double y, bool collision ) {
return createBlock( renderer, x, y, g_terrain_texture,
getSourceRectByID( block_id ), block_id, collision );
} }
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, std::shared_ptr<SDLPP::Renderer> &renderer, bool collision ) { std::shared_ptr< SDLPP::RectangleRender >
return createTerrainBlock(block_id, renderer, 0, 0, collision); createTerrainBlock( uint64_t block_id,
std::shared_ptr< SDLPP::Renderer > &renderer,
bool collision ) {
return createTerrainBlock( block_id, renderer, 0, 0, collision );
} }

View File

@ -93,15 +93,10 @@ void pollEvents( SDLPP::Scene &scene ) {
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
if ( leftStop ) { if ( leftStop ) {
auto rendDimsInt = renderer->getDimensions(); auto rendDimsInt = renderer->getDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDimsInt.getX()
<< ", " << rendDimsInt.getY() << std::endl;
auto rendDims = renderer->getDoubleDimensions(); auto rendDims = renderer->getDoubleDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDims.getX()
<< ", " << rendDims.getY() << std::endl;
auto left = rendDims.getX() < 2.0 auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1 ? -( rendDims.getX() - 1 ) / 2.0 - 0.1
: -0.5; : -0.5;
std::cout << left << std::endl;
leftStop->setPos( left, 0 ); leftStop->setPos( left, 0 );
} }
auto dimensions = renderer->getDoubleDimensions(); auto dimensions = renderer->getDoubleDimensions();
@ -193,7 +188,7 @@ int main() {
MARIO_OVERWORLD_STANDING_SRC ); MARIO_OVERWORLD_STANDING_SRC );
mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM ); mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM );
mario->setId( 2 ); mario->setId( 2 );
mario->centerX(); mario->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mario->setAnimationSpeed( 12.5 ); mario->setAnimationSpeed( 12.5 );
mario->pauseAnimation(); mario->pauseAnimation();
mario->setMovement( 0, 0 ); mario->setMovement( 0, 0 );
@ -210,7 +205,7 @@ int main() {
auto defeat = auto defeat =
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer ); std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
defeat->setId( DEATH_ID ); defeat->setId( DEATH_ID );
defeat->centerX(); defeat->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
defeat->setPermanent(); defeat->setPermanent();
auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 ); auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 );
defeatCol.setInfinite(); defeatCol.setInfinite();
@ -221,7 +216,7 @@ int main() {
leftStop = std::make_shared< SDLPP::RectangleRender >( -0.1, 0, 0.11, 0, leftStop = std::make_shared< SDLPP::RectangleRender >( -0.1, 0, 0.11, 0,
renderer ); renderer );
leftStop->setId( STOP_MOVEMENT ); leftStop->setId( STOP_MOVEMENT );
leftStop->centerX(); leftStop->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
leftStop->setPermanent(); leftStop->setPermanent();
auto leftStopCol = SDLPP::RectColider( 0, -1, 1, -1 ); auto leftStopCol = SDLPP::RectColider( 0, -1, 1, -1 );
leftStopCol.setInfinite(); leftStopCol.setInfinite();

View File

@ -4,7 +4,6 @@
void MarioVisitor::visit( const SDLPP::RenderObject &obj ) { void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId(); auto id = obj.getId();
std::cout << from << std::endl;
switch ( id ) { switch ( id ) {
case FLOOR_OVERWORLD_ID: case FLOOR_OVERWORLD_ID:
if ( from == MARIO_FLOOR_DETECT ) if ( from == MARIO_FLOOR_DETECT )