diff --git a/src/main.cpp b/src/main.cpp index 547bf55..a7556f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,7 +67,7 @@ int main() { }; Entity line { line_data.size, line_data.pos, line_data.velocity, line_data.border_size, sf::Color::White }; - Entity test_square { simulated_data.size, simulated_data.pos, simulated_data.velocity, simulated_data.border_size, sf::Color::Black }; + // Entity test_square { simulated_data.size, simulated_data.pos, simulated_data.velocity, simulated_data.border_size, sf::Color::Black }; sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default); window.setFramerateLimit(60); @@ -83,26 +83,26 @@ int main() { } // FOR TESTING - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { - if(test_square.rect.getPosition().x < 30) { - test_square.set_velocity({ 20, 0 }); - } - if(test_square.rect.getPosition().x > WIDTH - 30) { - test_square.set_velocity({ -20, 0 }); - } - } + // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { + // if(test_square.rect.getPosition().x < 30) { + // test_square.set_velocity({ 20, 0 }); + // } + // if(test_square.rect.getPosition().x > WIDTH - 30) { + // test_square.set_velocity({ -20, 0 }); + // } + // } player.handle_movment(); player.set_velocity_y(player.get_velocity().y + player.m_acceleration); player.rect.move({0, player.get_velocity().y}); - test_square.set_velocity_y(test_square.get_velocity().y + 1); - if(test_square.get_velocity().x > 0) { - test_square.set_velocity_x(test_square.get_velocity().x - 1); - } - if(test_square.get_velocity().x < 0) { - test_square.set_velocity_x(test_square.get_velocity().x + 1); - } - test_square.rect.move({ test_square.get_velocity().x, test_square.get_velocity().y }); + // test_square.set_velocity_y(test_square.get_velocity().y + 1); + // if(test_square.get_velocity().x > 0) { + // test_square.set_velocity_x(test_square.get_velocity().x - 1); + // } + // if(test_square.get_velocity().x < 0) { + // test_square.set_velocity_x(test_square.get_velocity().x + 1); + // } + // test_square.rect.move({ test_square.get_velocity().x, test_square.get_velocity().y }); float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().y - line_data.border_size - player_data.border_size; if (player.rect.getPosition().y >= groundY) { @@ -118,59 +118,59 @@ int main() { player.rect.setPosition({ 0, player.rect.getPosition().y}); } - float right_border_rect = WIDTH - test_square.rect.getSize().x; - float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - test_square.rect.getSize().y - line_data.border_size - simulated_data.border_size; - if(test_square.rect.getPosition().x > right_border_rect) { - test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y }); - test_square.set_velocity({ -20, 0 }); - } - if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) { - test_square.rect.setPosition({ 0, test_square.rect.getPosition().y }); - test_square.set_velocity({ 20, 0 }); - } - if(test_square.rect.getPosition().y >= groundY_rect) { - test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect }); - } - - auto collision = player.find_intersection(test_square); - switch (collision) { - case CollisionDir::Top: - test_square.set_velocity({ 0, -20 }); - break; - case CollisionDir::Bottom: - test_square.set_velocity({ 0, -20 }); - break; - case CollisionDir::Left: - test_square.set_velocity({ -20, 0 }); - break; - case CollisionDir::Right: - test_square.set_velocity({ 20, 0 }); - break; - default: break; - } - // for(int i = 0; i < MAX_ROWS; i++) { - // for(int j = 0; j < MAX_COLS; j++) { - // if(wall_list[i][j] == nullptr) continue; - // auto collision = wall_list[i][j]->find_intersection(player); - // switch (collision) { - // case CollisionDir::Top: - // player.velocity = { player.velocity.x, 0 }; - // player.velocity.y -= player.m_acceleration; - // break; - // case CollisionDir::Bottom: - // player.velocity = { player.velocity.x, 0 }; - // break; - // case CollisionDir::Left: - // player.velocity = { player.velocity.x, 0 }; - // break; - // case CollisionDir::Right: - // player.velocity = { player.velocity.x, 0 }; - // break; - // default: break; - // } - // - // } + // float right_border_rect = WIDTH - test_square.rect.getSize().x; + // float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - test_square.rect.getSize().y - line_data.border_size - simulated_data.border_size; + // if(test_square.rect.getPosition().x > right_border_rect) { + // test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y }); + // test_square.set_velocity({ -20, 0 }); // } + // if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) { + // test_square.rect.setPosition({ 0, test_square.rect.getPosition().y }); + // test_square.set_velocity({ 20, 0 }); + // } + // if(test_square.rect.getPosition().y >= groundY_rect) { + // test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect }); + // } + + // auto collision = player.find_intersection(test_square); + // switch (collision) { + // case CollisionDir::Top: + // test_square.set_velocity({ 0, -20 }); + // break; + // case CollisionDir::Bottom: + // test_square.set_velocity({ 0, -20 }); + // break; + // case CollisionDir::Left: + // test_square.set_velocity({ -20, 0 }); + // break; + // case CollisionDir::Right: + // test_square.set_velocity({ 20, 0 }); + // break; + // default: break; + // } + for(int i = 0; i < MAX_ROWS; i++) { + for(int j = 0; j < MAX_COLS; j++) { + if(wall_list[i][j] == nullptr) continue; + auto collision = wall_list[i][j]->find_intersection(player); + switch (collision) { + case CollisionDir::Bottom: + player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y + player.rect.getSize().y }); + // player.set_velocity_y(0); + break; + case CollisionDir::Top: + player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - player.rect.getSize().y }); + // player.set_velocity_y(0); + break; + case CollisionDir::Left: + player.rect.setPosition({ wall_list[i][j]->rect.getPosition().x - player.rect.getSize().x, player.rect.getPosition().y }); + break; + case CollisionDir::Right: + player.rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, player.rect.getPosition().y }); + break; + default: break; + } + } + } for(int i = 0; i < MAX_ROWS; i++) { for(int j = 0; j < MAX_COLS; j++) { @@ -180,7 +180,7 @@ int main() { } } window.draw(player.rect); - window.draw(test_square.rect); + // window.draw(test_square.rect); window.draw(line.rect); window.display(); } diff --git a/src/maps.cpp b/src/maps.cpp index c824cef..edf41c4 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@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