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 >(
x, y, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src );
block->setId( id );
block->centerX();
block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
block->setStatic();
if ( collision )
block->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
return block;
}
SDL_Rect getSourceRectByID(uint64_t id) {
switch(id) {
case FLOOR_OVERWORLD_ID:
return FLOOR_OVERWORLD_SRC;
case HILL_OVERWORLD_INCLINE_ID:
return HILL_OVERWORLD_INCLINE_SRC;
case HILL_OVERWORLD_DECLINE_ID:
return HILL_OVERWORLD_DECLINE_SRC;
case HILL_OVERWORLD_DOTS_RIGHT_ID:
return HILL_OVERWORLD_DOTS_RIGHT_SRC;
case HILL_OVERWORLD_DOTS_LEFT_ID:
return HILL_OVERWORLD_DOTS_LEFT_SRC;
case HILL_OVERWORLD_FILL_ID:
return HILL_OVERWORLD_FILL_SRC;
case HILL_OVERWORLD_TOP_ID:
return HILL_OVERWORLD_TOP_SRC;
case BUSH_OVERWORLD_LEFT_ID:
return BUSH_OVERWORLD_LEFT_SRC;
case BUSH_OVERWORLD_MIDDLE_ID:
return BUSH_OVERWORLD_MIDDLE_SRC;
case BUSH_OVERWORLD_RIGHT_ID:
return BUSH_OVERWORLD_RIGHT_SRC;
case CLOUD_OVERWORLD_LEFT_BOTTOM_ID:
return CLOUD_OVERWORLD_LEFT_BOTTOM_SRC;
case CLOUD_OVERWORLD_MIDDLE_BOTTOM_ID:
return CLOUD_OVERWORLD_MIDDLE_BOTTOM_SRC;
case CLOUD_OVERWORLD_RIGHT_BOTTOM_ID:
return CLOUD_OVERWORLD_RIGHT_BOTTOM_SRC;
case CLOUD_OVERWORLD_LEFT_TOP_ID:
return CLOUD_OVERWORLD_LEFT_TOP_SRC;
case CLOUD_OVERWORLD_MIDDLE_TOP_ID:
return CLOUD_OVERWORLD_MIDDLE_TOP_SRC;
case CLOUD_OVERWORLD_RIGHT_TOP_ID:
return CLOUD_OVERWORLD_RIGHT_TOP_SRC;
SDL_Rect getSourceRectByID( uint64_t id ) {
switch ( id ) {
case FLOOR_OVERWORLD_ID:
return FLOOR_OVERWORLD_SRC;
case HILL_OVERWORLD_INCLINE_ID:
return HILL_OVERWORLD_INCLINE_SRC;
case HILL_OVERWORLD_DECLINE_ID:
return HILL_OVERWORLD_DECLINE_SRC;
case HILL_OVERWORLD_DOTS_RIGHT_ID:
return HILL_OVERWORLD_DOTS_RIGHT_SRC;
case HILL_OVERWORLD_DOTS_LEFT_ID:
return HILL_OVERWORLD_DOTS_LEFT_SRC;
case HILL_OVERWORLD_FILL_ID:
return HILL_OVERWORLD_FILL_SRC;
case HILL_OVERWORLD_TOP_ID:
return HILL_OVERWORLD_TOP_SRC;
case BUSH_OVERWORLD_LEFT_ID:
return BUSH_OVERWORLD_LEFT_SRC;
case BUSH_OVERWORLD_MIDDLE_ID:
return BUSH_OVERWORLD_MIDDLE_SRC;
case BUSH_OVERWORLD_RIGHT_ID:
return BUSH_OVERWORLD_RIGHT_SRC;
case CLOUD_OVERWORLD_LEFT_BOTTOM_ID:
return CLOUD_OVERWORLD_LEFT_BOTTOM_SRC;
case CLOUD_OVERWORLD_MIDDLE_BOTTOM_ID:
return CLOUD_OVERWORLD_MIDDLE_BOTTOM_SRC;
case CLOUD_OVERWORLD_RIGHT_BOTTOM_ID:
return CLOUD_OVERWORLD_RIGHT_BOTTOM_SRC;
case CLOUD_OVERWORLD_LEFT_TOP_ID:
return CLOUD_OVERWORLD_LEFT_TOP_SRC;
case CLOUD_OVERWORLD_MIDDLE_TOP_ID:
return CLOUD_OVERWORLD_MIDDLE_TOP_SRC;
case CLOUD_OVERWORLD_RIGHT_TOP_ID:
return CLOUD_OVERWORLD_RIGHT_TOP_SRC;
}
return {};
}
std::shared_ptr<SDLPP::RectangleRender> 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, 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 ) {
return createTerrainBlock(block_id, renderer, 0, 0, collision);
std::shared_ptr< SDLPP::RectangleRender >
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();
if ( leftStop ) {
auto rendDimsInt = renderer->getDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDimsInt.getX()
<< ", " << rendDimsInt.getY() << std::endl;
auto rendDims = renderer->getDoubleDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDims.getX()
<< ", " << rendDims.getY() << std::endl;
auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1
: -0.5;
std::cout << left << std::endl;
leftStop->setPos( left, 0 );
}
auto dimensions = renderer->getDoubleDimensions();
@ -193,7 +188,7 @@ int main() {
MARIO_OVERWORLD_STANDING_SRC );
mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM );
mario->setId( 2 );
mario->centerX();
mario->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mario->setAnimationSpeed( 12.5 );
mario->pauseAnimation();
mario->setMovement( 0, 0 );
@ -210,7 +205,7 @@ int main() {
auto defeat =
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
defeat->setId( DEATH_ID );
defeat->centerX();
defeat->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
defeat->setPermanent();
auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 );
defeatCol.setInfinite();
@ -221,7 +216,7 @@ int main() {
leftStop = std::make_shared< SDLPP::RectangleRender >( -0.1, 0, 0.11, 0,
renderer );
leftStop->setId( STOP_MOVEMENT );
leftStop->centerX();
leftStop->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
leftStop->setPermanent();
auto leftStopCol = SDLPP::RectColider( 0, -1, 1, -1 );
leftStopCol.setInfinite();

View File

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