breaking changes: optimizing code and added .clang-format

This commit is contained in:
Andrew Haynes Macbook
2026-01-09 12:38:38 -05:00
parent 55f604710e
commit b5a79de6c0
10 changed files with 431 additions and 315 deletions
+42
View File
@@ -0,0 +1,42 @@
---
# Base Style
BasedOnStyle: LLVM
Standard: Latest
ColumnLimit: 120
ReflowComments: true
SortIncludes: true
IncludeBlocks: Regroup
# Indentation
UseTab: Always
TabWidth: 8
IndentWidth: 8
ContinuationIndentWidth: 8
AlignAfterOpenBracket: Align
PointerAlignment: Right
# Braces
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: false
AfterClass: false
AfterControlStatement: false
AfterNamespace: false
IndentBraces: false
# Containers & lists
SpacesInContainerLiterals: true
Cpp11BracedListStyle: false
SpaceBeforeCpp11BracedList: true
# Control statements & loops
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
SpaceBeforeRangeBasedForLoopColon: true
AllowShortFunctionsOnASingleLine: true
# Preprocessor
IndentPPDirectives: AfterHash
PPIndentWidth: 0
+1 -1
View File
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 4.2.1)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
+22
View File
@@ -0,0 +1,22 @@
#ifndef DATA_HPP
#define DATA_HPP
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
struct Map {
const char map[MAX_ROWS][MAX_COLS];
};
struct Player {
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 };
};
#endif
+9
View File
@@ -47,4 +47,13 @@ private:
sf::Vector2f _min_velocity;
};
struct player_data {
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 };
};
#endif
+10 -7
View File
@@ -2,22 +2,25 @@
#define MAPS_HPP
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
#include "globals.hpp"
#include "elements.hpp"
extern char test_level_0[MAX_ROWS][MAX_COLS];
extern char test_level_1[MAX_ROWS][MAX_COLS];
extern char test_level_2[MAX_ROWS][MAX_COLS];
struct Level {
char data[MAX_ROWS][MAX_COLS];
std::string name;
};
void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]);
void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]);
// void create_map(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]);
extern Level levels[];
void clear_map(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS> wall_list);
void load_map(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS> wall_list, const std::array<std::array<char, MAX_COLS>, MAX_ROWS> map_data);
std::array<std::array<char, MAX_COLS>, MAX_ROWS> create_map(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]);
std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS> wall_list);
#endif
+1
View File
@@ -0,0 +1 @@
#include "data.hpp"
+74 -58
View File
@@ -1,17 +1,20 @@
#include "elements.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp>
#else
#include <ranges>
#endif
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, sf::Color inside_color, sf::Color border_color) {
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness,
sf::Color inside_color, sf::Color border_color) {
rect.setSize(size);
rect.setPosition(pos);
rect.setOutlineThickness(border_thickness);
@@ -20,21 +23,13 @@ Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f vel
_velocity = velocity;
}
void Entity::set_velocity(sf::Vector2<float> velocity) {
_velocity = velocity;
}
void Entity::set_velocity(sf::Vector2<float> velocity) { _velocity = velocity; }
void Entity::set_velocity_x(int velocity) {
_velocity.x = velocity;
}
void Entity::set_velocity_x(int velocity) { _velocity.x = velocity; }
void Entity::set_velocity_y(int velocity) {
_velocity.y = velocity;
}
void Entity::set_velocity_y(int velocity) { _velocity.y = velocity; }
sf::Vector2<float> Entity::get_velocity() {
return _velocity;
}
sf::Vector2<float> Entity::get_velocity() { return _velocity; }
CollisionDir Entity::find_intersection(Entity &entity) {
sf::FloatRect a = rect.getGlobalBounds();
@@ -51,17 +46,17 @@ CollisionDir Entity::find_intersection(Entity &entity) {
if (overlapW < overlapH) {
// Horizontal collision
return (a.position.x < b.position.x)
? CollisionDir::Right // a hit b from the left side
return (a.position.x < b.position.x) ? CollisionDir::Right // a hit b from the left side
: CollisionDir::Left; // a hit b from the right side
} else {
return (a.position.y < b.position.y)
? CollisionDir::Bottom
: CollisionDir::Top;
return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top;
}
}
Player::Player(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, sf::Color border_color) : Entity(size, pos, velocity, border_thickness, inside_color, border_color) {
Player::Player(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness,
float gravity, float max_acceleration, float dash_distance, sf::Color inside_color,
sf::Color border_color)
: Entity(size, pos, velocity, border_thickness, inside_color, border_color) {
_min_velocity = velocity;
m_acceleration = gravity;
m_jump_strength = max_acceleration;
@@ -70,24 +65,28 @@ Player::Player(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f vel
}
void Player::handle_movment() {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
rect.move({_velocity.x, 0});
} if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
rect.move({-_velocity.x, 0});
} if(timers.dash_cooldown <= 0) {
}
if (timers.dash_cooldown <= 0) {
m_dash = false;
} if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && timers.dash_cooldown <= 0) {
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && timers.dash_cooldown <= 0) {
m_dash = true;
_velocity.x = m_dash_distance;
timers.dash_cooldown = timers.dash_cooldown_max;
} if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
m_jumping = true;
_velocity.y = -m_jump_strength; // push up
}
}
void Player::handle_gravity() {
if(get_velocity().x > _min_velocity.x) {
if (get_velocity().x > _min_velocity.x) {
set_velocity_x(get_velocity().x - m_acceleration);
}
@@ -97,16 +96,17 @@ void Player::handle_gravity() {
void Player::horizontal_no_out_of_bounds(Entity &base) {
float right_border = WIDTH - rect.getSize().x;
if(rect.getPosition().x > right_border) {
rect.setPosition({ right_border, rect.getPosition().y});
if (rect.getPosition().x > right_border) {
rect.setPosition({right_border, rect.getPosition().y});
}
if(rect.getPosition().x < std::abs(rect.getOutlineThickness())) {
rect.setPosition({ 0, rect.getPosition().y});
if (rect.getPosition().x < std::abs(rect.getOutlineThickness())) {
rect.setPosition({0, rect.getPosition().y});
}
}
void Player::vertical_no_out_of_bounds(Entity &base) {
float groundY = base.rect.getPosition().y - base.rect.getSize().y - rect.getSize().y - base.rect.getOutlineThickness() - rect.getOutlineThickness();
float groundY = base.rect.getPosition().y - base.rect.getSize().y - rect.getSize().y -
base.rect.getOutlineThickness() - rect.getOutlineThickness();
if (rect.getPosition().y >= groundY) {
rect.setPosition({rect.getPosition().x, groundY});
set_velocity_y(0);
@@ -116,19 +116,25 @@ void Player::vertical_no_out_of_bounds(Entity &base) {
bool Player::get_horizontal_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
#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;
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;
auto collision = wall_list[i][j]->find_intersection(*this);
if(collision == CollisionDir::None) continue;
if(collision == CollisionDir::Left) {
rect.setPosition({ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y });
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 });
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;
}
}
@@ -137,15 +143,20 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO
bool Player::get_vertical_top_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
#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;
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;
auto collision = find_intersection(*wall_list[i][j]);
if(collision == CollisionDir::None) continue;
if(collision == CollisionDir::Top) {
rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getSize().y + wall_list[i][j]->rect.getPosition().y});
if (collision == CollisionDir::None)
continue;
if (collision == CollisionDir::Top) {
rect.setPosition({rect.getPosition().x,
wall_list[i][j]->rect.getSize().y + wall_list[i][j]->rect.getPosition().y});
set_velocity_y(0);
return true;
}
@@ -155,15 +166,20 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO
bool Player::get_vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
#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;
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;
auto collision = find_intersection(*wall_list[i][j]);
if(collision == CollisionDir::None) continue;
if(collision == CollisionDir::Bottom && get_velocity().y > 0) {
rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y });
if (collision == CollisionDir::None)
continue;
if (collision == CollisionDir::Bottom && get_velocity().y > 0) {
rect.setPosition(
{rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y});
m_jumping = false;
set_velocity_y(0);
return true;
+71 -79
View File
@@ -1,25 +1,25 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include <iostream>
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp>
#else
#include <ranges>
#endif
#include "globals.hpp"
#include "elements.hpp"
#include "globals.hpp"
#include "maps.hpp"
#include "scheduler.hpp"
@@ -31,120 +31,112 @@ int main() {
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;
int current_level = 0;
load_map(wall_list, test_level_0);
load_map(wall_list, );
// 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_data player_data;
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}), "engine", 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/engine/assets/pixel.ttf");
sf::Font font("/Users/andrew/personal/Projects/CPP/engine/assets/pixel.ttf");
#else
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)});
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((int)std::floor(local_position.x / 32)) + ", " + std::to_string((int)std::floor(local_position.y / 32)));
text.setString(std::to_string((int)std::floor(local_position.x / 32)) + ", " +
std::to_string((int)std::floor(local_position.y / 32)));
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;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
running = false;
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) {
clear_map(wall_list);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
dragging = false;
}
create_map(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";
// }
//
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;
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;
window.draw(wall_list[i][j]->rect);
}
#else
for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
if(wall_list[i][j] == nullptr) continue;
for (auto [i, j] :
std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
if (wall_list[i][j] == nullptr)
continue;
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;
}
+127 -97
View File
@@ -1,101 +1,118 @@
#include "maps.hpp"
#include "elements.hpp"
#include "globals.hpp"
#include <cmath>
#include <array>
#include <cmath>
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp>
namespace rv = ranges::views;
#else
#include <ranges>
namespace rv = std::views;
#endif
void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
#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
void clear_map(std::array<std::array<Entity *, MAX_COLS>, MAX_ROWS> wall_list) {
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) {
delete wall_list[i][j];
wall_list[i][j] = nullptr;
}
}
bool is_tiles_clear_x(std::array<std::array<char, MAX_COLS>, MAX_ROWS> draw_list, int y_level, int start_x, int end_x) {
for(int i : std::views::iota(start_x, end_x)) {
if(draw_list[i][y_level] == ' ') continue;
for (int i : rv::iota(start_x, end_x)) {
if (draw_list[i][y_level] == ' ')
continue;
return false;
}
return true;
}
bool is_tiles_clear_y(std::array<std::array<char, MAX_COLS>, MAX_ROWS> draw_list, int x_level, int start_y, int end_y) {
for(int i : std::views::iota(start_y, end_y)) {
if(draw_list[i][x_level] == ' ') continue;
for (int i : rv::iota(start_y, end_y)) {
if (draw_list[i][x_level] == ' ')
continue;
return false;
}
return true;
}
std::array<std::array<char, MAX_COLS>, MAX_ROWS> create_map(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]) {
std::array<std::array<char, MAX_COLS>, MAX_ROWS> create_map(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]) {
std::array<std::array<char, MAX_COLS>, MAX_ROWS> draw_list {};
for(auto &row : draw_list) {
for (auto &row : draw_list) {
row.fill(' ');
}
static bool vertical = false;
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) {
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) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging) {
prev_dragging = true;
end_mouse_pos = local_position;
sf::RectangleShape rect;
if(end_mouse_pos.x - start_mouse_pos.x > end_mouse_pos.y - start_mouse_pos.y) {
rect.setSize({ (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))} );
if (end_mouse_pos.x - start_mouse_pos.x > end_mouse_pos.y - start_mouse_pos.y) {
rect.setSize({ (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)) });
vertical = false;
} else {
rect.setSize({ 1.0f, (float) (end_mouse_pos.y - start_mouse_pos.y),});
rect.setPosition({ (float)(std::floor(start_mouse_pos.x / 32) * TILESIZE + ((float)TILESIZE / 2)), (float)start_mouse_pos.y} );
rect.setSize({
1.0f,
(float)(end_mouse_pos.y - start_mouse_pos.y),
});
rect.setPosition({ (float)(std::floor(start_mouse_pos.x / 32) * TILESIZE +
((float)TILESIZE / 2)),
(float)start_mouse_pos.y });
vertical = true;
}
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 (!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(!vertical) {
if(wall_list[start_integers.y][start_integers.x] == nullptr) {
wall_list[start_integers.y][start_integers.x] = new Entity {
{ float(end_integers.x - start_integers.x + 1) * TILESIZE, TILESIZE },
{ (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) },
if (!vertical) {
if (wall_list[start_integers.y][start_integers.x] == nullptr) {
wall_list[start_integers.y][start_integers.x] =
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
};
sf::Color::Black };
draw_list[start_integers.y][start_integers.x] = '1';
draw_list[end_integers.y][end_integers.x] = '2';
for(int i : std::views::iota(start_integers.x, end_integers.x)) {
for (int i : rv::iota(start_integers.x, end_integers.x)) {
draw_list[start_integers.y][i] = '-';
}
}
} else if(vertical) {
if(wall_list[start_integers.y][start_integers.x] == nullptr) {
// if(is_tiles_clear_y(draw_list, start_integers.x, start_integers.y, end_integers.y)) {
} else if (vertical) {
if (wall_list[start_integers.y][start_integers.x] == nullptr) {
// if(is_tiles_clear_y(draw_list, start_integers.x, start_integers.y,
// end_integers.y)) {
wall_list[start_integers.y][start_integers.x] = new Entity {
{ TILESIZE, float(end_integers.y - start_integers.y + 1) * TILESIZE },
{ (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) },
{ (float)(start_integers.x * TILESIZE),
(float)(start_integers.y * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
draw_list[start_integers.y][start_integers.x] = '1';
draw_list[end_integers.y][end_integers.x] = '2';
for(int i : std::views::iota(start_integers.y, end_integers.y)) {
for (int i : rv::iota(start_integers.y, end_integers.y)) {
draw_list[i][start_integers.x] = '|';
}
// }
@@ -109,87 +126,80 @@ std::array<std::array<char, MAX_COLS>, MAX_ROWS> create_map(sf::RenderWindow &wi
return draw_list;
}
void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]) {
#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(map_data[i][j] == ' ') continue;
if(map_data[i][j] == '0') {
wall_list[i][j] = new Entity {
{ TILESIZE, TILESIZE },
void load_map(std::array<std::array<Entity *, MAX_COLS>, MAX_ROWS> wall_list,
const std::array<std::array<char, MAX_COLS>, MAX_ROWS> map_data) {
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) {
if (map_data[i][j] == ' ')
continue;
if (map_data[i][j] == '0') {
wall_list[i][j] = new Entity { { TILESIZE, TILESIZE },
{ float(j * TILESIZE), float(i * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
sf::Color::Black };
}
if(map_data[i][j] == '1' && map_data[i][j + 1] == '2') {
if (map_data[i][j] == '1' && map_data[i][j + 1] == '2') {
int start = j, end = j + 1;
wall_list[i][j] = new Entity {
{ float(end - start + 1) * TILESIZE, TILESIZE },
wall_list[i][j] = new Entity { { float(end - start + 1) * TILESIZE, TILESIZE },
{ float(j * TILESIZE), float(i * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
sf::Color::Black };
}
if(map_data[i][j] == '1' && map_data[i + 1][j] == '2') {
if (map_data[i][j] == '1' && map_data[i + 1][j] == '2') {
int start = i, end = i + 1;
wall_list[i][j] = new Entity {
{ TILESIZE, float(end - start + 1) * TILESIZE },
wall_list[i][j] = new Entity { { TILESIZE, float(end - start + 1) * TILESIZE },
{ float(j * TILESIZE), float(i * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
sf::Color::Black };
}
if(map_data[i][j] == '1' && map_data[i][j + 1] == '-') {
if (map_data[i][j] == '1' && map_data[i][j + 1] == '-') {
int start = j, end = j;
while(end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++;
if(map_data[i][end + 1] == '2') end++;
wall_list[i][j] = new Entity {
{ float(end - start + 1) * TILESIZE, TILESIZE },
while (end + 1 < MAX_COLS && map_data[i][end + 1] != '2')
end++;
if (map_data[i][end + 1] == '2')
end++;
wall_list[i][j] = new Entity { { float(end - start + 1) * TILESIZE, TILESIZE },
{ float(j * TILESIZE), float(i * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
sf::Color::Black };
}
if(map_data[i][j] == '1' && map_data[i + 1][j] == '|') {
if (map_data[i][j] == '1' && map_data[i + 1][j] == '|') {
int start = i, end = i;
while(end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++;
if(map_data[end + 1][j] == '2') end++;
wall_list[i][j] = new Entity {
{ TILESIZE, float(end - start + 1) * TILESIZE },
while (end + 1 < MAX_ROWS && map_data[end + 1][j] != '2')
end++;
if (map_data[end + 1][j] == '2')
end++;
wall_list[i][j] = new Entity { { TILESIZE, float(end - start + 1) * TILESIZE },
{ float(j * TILESIZE), float(i * TILESIZE) },
{ 0, 0 },
-2.0f,
sf::Color::Black
};
sf::Color::Black };
}
}
}
char test_level_0[MAX_ROWS][MAX_COLS] = {
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
};
char test_level_1[MAX_ROWS][MAX_COLS] = {
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
Level levels[] = {
{
.data = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, },
.name = "Empty"
},
{
.data = { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
@@ -201,10 +211,11 @@ char test_level_1[MAX_ROWS][MAX_COLS] = {
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
};
char test_level_2[MAX_ROWS][MAX_COLS] = {
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, },
.name = "Default"
},
{
.data = {
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
@@ -217,5 +228,24 @@ char test_level_2[MAX_ROWS][MAX_COLS] = {
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, },
.name = "1"
},
{
.data = {
{ '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' }, },
.name = "3"
}
};
+1
View File
@@ -1,6 +1,7 @@
// timer for player cooldown
#include "scheduler.hpp"
#include <chrono>
#include <thread>
void Timer::update_timers(bool &running, Times &times) {