diff --git a/include/globals.hpp b/include/globals.hpp index 6b852ac..05b3f41 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,5 +1,8 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP +#define TILESIZE 32 #define WIDTH 608 #define HEIGHT 416 +#define MAX_COLS WIDTH / TILESIZE // 19 cols +#define MAX_ROWS HEIGHT / TILESIZE // 13 rows #endif diff --git a/include/maps.hpp b/include/maps.hpp index fdc4c9b..d6f854e 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -3,9 +3,6 @@ #include "globals.hpp" -const -int map_data[WIDTH / 32][HEIGHT / 32]; - -// Add code here +extern int map_data[MAX_ROWS][MAX_COLS]; #endif diff --git a/src/main.cpp b/src/main.cpp index 38595d3..5039b2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,48 +7,59 @@ #include #include #include +#include #include #include #include "globals.hpp" #include "elements.hpp" +#include "maps.hpp" int main() { - std::vector wall_list = {}; + // std::vector wall_list = {}; + Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; struct { - sf::Vector2 size { 16.0f, 16.0f }; - sf::Vector2 pos { 40, 40 }; + sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; + sf::Vector2 pos { TILESIZE, TILESIZE }; float acceleration { 1.0f }; float max_velocity_y { 20.0f }; - float border_size { 1.0f }; + float border_size { -1.0f }; sf::Vector2f velocity { 4.0f, 0 }; } player_data; struct { - sf::Vector2f size { 32.0f, 32.0f }; + sf::Vector2f size { TILESIZE, TILESIZE }; sf::Vector2f pos { 0, 0 }; sf::Vector2f velocity { 0, 0 }; - float border_size { 2.0f }; + float border_size { -2.0f }; } tile_data; struct { sf::Vector2f size { WIDTH, 2.0f }; - sf::Vector2f pos { 0, HEIGHT - 50 }; + sf::Vector2f pos { 0, HEIGHT - (TILESIZE * 1) }; sf::Vector2f velocity { 0, 0 }; - float border_size { 1.0f }; + float border_size { -1.0f }; } line_data; struct { - sf::Vector2f size { 8, 8 }; - sf::Vector2f pos { 100, 40 }; + sf::Vector2f size { TILESIZE / 4.0f, TILESIZE / 4.0f }; + sf::Vector2f pos { TILESIZE * 3, TILESIZE }; sf::Vector2f velocity { 0, 0 }; - float border_size { 1.0f }; + float border_size { -1.0f }; } simulated_data; - for(int i = 0; i < (WIDTH / 32); i++) { - wall_list.push_back(Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }); + for(int i = 0; i < MAX_ROWS; i++) { + for(int j = 0; j < MAX_COLS; j++) { + if(map_data[i][j]) { + wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }; + } + } } - for(int i = 0; i < wall_list.size(); i++) { - wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size }); + for(int i = 0; i < MAX_ROWS; i++) { + for(int j = 0; j < MAX_COLS; j++) { + if(wall_list[i][j] != nullptr) { + wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); + } + } } Player player { @@ -58,7 +69,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.pos, 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); @@ -72,6 +83,7 @@ int main() { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { window.close(); } + // FOR TESTING if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { if(test_square.rect.getPosition().x < 30) { @@ -94,34 +106,34 @@ int main() { } test_square.rect.move({ test_square.velocity.x, test_square.velocity.y }); - float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().x; + 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) { player.rect.setPosition({player.rect.getPosition().x, groundY}); player.velocity.y = 0; player.m_jumping = false; } - float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness(); + float right_border = WIDTH - player.rect.getSize().x; if(player.rect.getPosition().x > right_border) { player.rect.setPosition({ right_border, player.rect.getPosition().y}); } - if(player.rect.getPosition().x < player.rect.getOutlineThickness()) { - player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y}); + if(player.rect.getPosition().x < std::abs(player.rect.getOutlineThickness())) { + player.rect.setPosition({ 0, player.rect.getPosition().y}); } - float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness(); - float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - (test_square.rect.getSize().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.velocity = { -20, 0 }; } - if(test_square.rect.getPosition().x < test_square.rect.getOutlineThickness()) { - test_square.rect.setPosition({ test_square.rect.getOutlineThickness(), test_square.rect.getPosition().y }); + if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) { + test_square.rect.setPosition({ 0, test_square.rect.getPosition().y }); test_square.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: @@ -138,13 +150,43 @@ int main() { break; default: break; } + // for(int i = 0; i < MAX_ROWS; i++) { + // for(int j = 0; j < MAX_COLS; j++) { + // auto collision = player.find_intersection(*wall_list[i][j]); + // switch (collision) { + // case CollisionDir::Top: + // player.velocity = { player.velocity.x, -player.velocity.y }; + // break; + // case CollisionDir::Bottom: + // player.velocity = { player.velocity.x, -player.velocity.y }; + // break; + // case CollisionDir::Left: + // player.velocity = { player.velocity.x, 0 }; + // break; + // case CollisionDir::Right: + // player.velocity = { player.velocity.x, 0 }; + // break; + // default: break; + // } + // + // } + // } - for(int i = 0; i < wall_list.size(); i++) { - window.draw(wall_list[i].rect); + for(int i = 0; i < MAX_ROWS; i++) { + for(int j = 0; j < MAX_COLS; j++) { + if(wall_list[i][j] != nullptr) { + window.draw(wall_list[i][j]->rect); + } + } } window.draw(player.rect); window.draw(test_square.rect); window.draw(line.rect); window.display(); } + for(int i = 0; i < MAX_ROWS; i++) { + for(int j = 0; j < MAX_COLS; j++) { + delete wall_list[i][j]; + } + } } diff --git a/src/maps.cpp b/src/maps.cpp index b6aa5be..21ff1e9 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,3 +1,17 @@ #include "maps.hpp" -int map_data[] = [ [ 1, 0, 1, 1, 1, 0, 0] ]; +int map_data[MAX_ROWS][MAX_COLS] = { + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +};