proper start to map editor

This commit is contained in:
DarthMooMancer
2025-10-31 15:09:24 -04:00
parent bdabd5a0bc
commit 72961ebf6d
5 changed files with 178 additions and 72 deletions
+2 -1
View File
@@ -49,7 +49,6 @@ CollisionDir Entity::find_intersection(Entity &entity) {
float overlapW = overlap.size.x;
float overlapH = overlap.size.y;
// Compare which axis overlap is smaller
if (overlapW < overlapH) {
// Horizontal collision
return (a.position.x < b.position.x)
@@ -126,9 +125,11 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO
if(collision == CollisionDir::None) continue;
if(collision == CollisionDir::Left) {
rect.setPosition({ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y });
return true;
}
if(collision == CollisionDir::Right) {
rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, rect.getPosition().y });
return true;
}
}
return false;
+117 -52
View File
@@ -1,10 +1,15 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cmath>
#include <cstdlib>
#include <string>
#include <thread>
#include <iostream>
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
@@ -16,87 +21,147 @@
#include "maps.hpp"
#include "scheduler.hpp"
void draw_wall(sf::RenderWindow &window, sf::Vector2i &local_position, sf::Vector2i &start_mouse_pos, sf::Vector2i &end_mouse_pos, bool &dragging, bool &prev_dragging, Entity *wall_list[MAX_ROWS][MAX_COLS]) {
if(local_position.x > 0 && local_position.x < WIDTH && local_position.y > 0 && local_position.y < HEIGHT) {
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !dragging) {
dragging = true;
start_mouse_pos = local_position;
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging) {
prev_dragging = true;
end_mouse_pos = local_position;
sf::RectangleShape rect { { (float) (end_mouse_pos.x - start_mouse_pos.x), 1.0f } };
rect.setPosition({ (float)start_mouse_pos.x, (float)(std::floor(start_mouse_pos.y / 32) * TILESIZE + ((float)TILESIZE / 2))} );
window.draw(rect);
}
if(!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging && prev_dragging) {
sf::Vector2i start_integers = { (int)std::floor(start_mouse_pos.x / 32), (int)std::floor(start_mouse_pos.y / 32) };
sf::Vector2i end_integers = { (int)std::floor(end_mouse_pos.x / 32), (int)std::floor(end_mouse_pos.y / 32) };
if(end_integers.x - start_integers.x > 0) {
if(wall_list[start_integers.x][start_integers.y] == nullptr) {
wall_list[start_integers.x][start_integers.y] = new Entity {
{ float(end_integers.x - start_integers.x + 1) * TILESIZE, TILESIZE },
{ (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
}
}
prev_dragging = false;
dragging = false;
}
}
}
int main() {
Timer time;
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
int current_level = 0;
struct {
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
sf::Vector2<float> pos { TILESIZE, TILESIZE };
float max_velocity_y { 15.0f };
float border_size { -1.0f };
float dash_distance { 20.0f };
sf::Vector2f velocity { 3.0f, 0 };
} player_data;
sf::Vector2i local_position;
sf::Vector2i start_mouse_pos;
sf::Vector2i end_mouse_pos;
bool dragging = false;
bool prev_dragging = false;
// int current_level = 0;
// struct {
// sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
// sf::Vector2<float> pos { TILESIZE, TILESIZE };
// float max_velocity_y { 15.0f };
// float border_size { -1.0f };
// float dash_distance { 20.0f };
// sf::Vector2f velocity { 3.0f, 0 };
// } player_data;
load_map(wall_list, test_level_0);
Player player {
player_data.size, player_data.pos, player_data.velocity,
player_data.border_size, GRAVITY, player_data.max_velocity_y,
player_data.dash_distance, sf::Color::Black, sf::Color::White
};
// Player player {
// player_data.size, player_data.pos, player_data.velocity,
// player_data.border_size, GRAVITY, player_data.max_velocity_y,
// player_data.dash_distance, sf::Color::Black, sf::Color::White
// };
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "engine", sf::Style::Default);
window.setFramerateLimit(FPS);
#ifdef __APPLE__
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/engine/assets/pixel.ttf");
#else
sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
sf::Font font("/home/andrew/personal/Projects/CPP/engine/assets/pixel.ttf");
#endif
sf::Text text(font, "0, 0", 16);
text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)});
bool running = true;
std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } );
// bool running = true;
// std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } );
while (window.isOpen()) {
local_position = sf::Mouse::getPosition(window);
window.clear(sf::Color::Black);
text.setString(std::to_string(player.timers.dash_cooldown));
text.setString(std::to_string((int)std::floor(local_position.x / 32)) + ", " + std::to_string((int)std::floor(local_position.y / 32)));
std::cout << dragging << "\n";
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
running = false;
// running = false;
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
running = false;
// running = false;
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) {
clear_map(wall_list);
if(current_level == 0) {
load_map(wall_list, test_level_1);
current_level = 1;
player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
}
else if(current_level == 1) {
load_map(wall_list, test_level_2);
current_level = 2;
player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
}
else if(current_level == 2) {
load_map(wall_list, test_level_0);
current_level = 0;
player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
}
}
player.handle_movment();
player.get_horizontal_collision(wall_list);
player.handle_gravity();
if(player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) {
player.set_velocity_y((TILESIZE / 2) - GRAVITY);
}
if(!player.get_vertical_bottom_collision(wall_list)) {
player.get_vertical_top_collision(wall_list);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
dragging = false;
}
draw_wall(window, local_position, start_mouse_pos, end_mouse_pos, dragging, prev_dragging, wall_list);
// if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) {
// clear_map(wall_list);
// if(current_level == 0) {
// load_map(wall_list, test_level_1);
// current_level = 1;
// player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
// }
// else if(current_level == 1) {
// load_map(wall_list, test_level_2);
// current_level = 2;
// player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
// }
// else if(current_level == 2) {
// load_map(wall_list, test_level_0);
// current_level = 0;
// player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
// }
// }
//
// player.handle_movment();
// if(!player.get_horizontal_collision(wall_list)) {
// std::cout << "\n";
// }
// else {
// std::cout << "Yes horizontal collision\n";
// }
//
// player.handle_gravity();
// if(player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) {
// player.set_velocity_y((TILESIZE / 2) - GRAVITY);
// }
// if(!player.get_vertical_bottom_collision(wall_list)) {
// std::cout << "\n";
// if(!player.get_vertical_top_collision(wall_list)) {
// std::cout << "\n";
// } else {
// std::cout << "Yes vertical top collision\n";
// }
// }
// else {
// std::cout << "Yes vertical bottom collision\n";
// }
//
#ifdef __APPLE__
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
if(wall_list[i][j] == nullptr) continue;
@@ -108,11 +173,11 @@ int main() {
window.draw(wall_list[i][j]->rect);
}
#endif
window.draw(player.rect);
// window.draw(player.rect);
window.draw(text);
window.display();
}
clear_map(wall_list);
player_dash_cooldown.join();
// player_dash_cooldown.join();
return 0;
}
+55 -16
View File
@@ -22,9 +22,48 @@ void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
#endif
}
void create_map() {
char output[MAX_ROWS][MAX_COLS] = {};
// void create_map() {
// char output[MAX_ROWS][MAX_COLS] = {};
//
// }
void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
#ifdef __APPLE__
for(auto[j, i] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
#else
for(auto[j, i] : std::views::cartesian_product(std::views::iota(0, MAX_COLS), std::views::iota(0, MAX_ROWS))) {
#endif
if(wall_list[i][j] == nullptr) continue;
if(wall_list[i][j]->rect.getSize().x != TILESIZE) continue;
int start = i, end = i;
while(end + 1 < MAX_ROWS && wall_list[end + 1][j] != nullptr) {
delete wall_list[end + 1][j];
wall_list[end + 1][j] = nullptr;
end++;
}
int merged_height = (end - start + 1) * TILESIZE;
wall_list[start][j]->rect.setSize({ wall_list[start][j]->rect.getSize().x, static_cast<float>(merged_height) });
i = end;
}
#ifdef __APPLE__
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
#else
for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
#endif
if(wall_list[i][j] == nullptr) continue;
if(wall_list[i][j]->rect.getSize().y != TILESIZE) continue;
int start = j, end = j;
while(end + 1 < MAX_COLS && wall_list[i][end + 1] != nullptr && wall_list[i][end + 1]->rect.getSize().y == TILESIZE) {
delete wall_list[i][end + 1];
wall_list[i][end + 1] = nullptr;
end++;
}
int merged_width = (end - start + 1) * TILESIZE;
wall_list[i][start]->rect.setSize({ static_cast<float>(merged_width), wall_list[i][start]->rect.getSize().y });
j = end;
}
}
void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]) {
@@ -91,19 +130,19 @@ void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROW
}
char test_level_0[MAX_ROWS][MAX_COLS] = {
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
};
char test_level_1[MAX_ROWS][MAX_COLS] = {