data drivin all code sucess

This commit is contained in:
Andrew Haynes Macbook
2026-01-16 15:03:16 -05:00
parent 7bbf50b581
commit 06d3f27b1e
13 changed files with 358 additions and 115 deletions
+52 -12
View File
@@ -1,26 +1,51 @@
#include "data.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp>
#include <vector>
std::vector<ObjectData> object_data {
{},
};
/*
Example EntityData
[]() -> EntityData {
EntityData e;
e.size = { TILESIZE / 1.5f, TILESIZE / 1.5f };
e.pos = { 400.0f, 300.0f };
e.interior_color = sf::Color::Blue;
e.velocity = { 6.0f, 0.0f };
return e;
}(),
*/
std::vector<EntityData> entity_data {
{},
};
PlayerData player_data {
.object = {
.size = { TILESIZE / 2.0f, TILESIZE / 2.0f },
.edge = -1.0f,
std::vector<TimerData> timer_data {
{},
{
.name = "Dash",
.max_time = .5,
},
{
.name = "Level_Switch",
.max_time = 1,
},
.entity = { .velocity = { 3.0f, 0.0f } },
.dash_length = 20.0f,
.max_vertical_acceleration = 15.0f
};
PlayerData player_data = [] {
PlayerData data;
data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f };
data.edge = -1.0f;
data.velocity = { 3.0f, 0.0f };
data.dash_length = 15.0f; // 15
data.max_vertical_acceleration = 12.5f;
return data;
}();
std::vector<LevelData> level_data = {
{ .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
@@ -35,8 +60,8 @@ std::vector<LevelData> level_data = {
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ' } } },
.name = "Empty" },
' ' } } }
},
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
@@ -51,7 +76,7 @@ std::vector<LevelData> level_data = {
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
.name = "Default" },
},
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
@@ -66,7 +91,7 @@ std::vector<LevelData> level_data = {
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
.name = "Plat1" },
},
{ .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' },
@@ -81,5 +106,20 @@ std::vector<LevelData> level_data = {
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ',
'0' } } },
.name = "Check" }
},
{ .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
},
};
+31 -3
View File
@@ -52,10 +52,38 @@ CollisionDir Object::find_intersection(Object& entity) {
} else {
return (a.position.y < b.position.y) ? CollisionDir::Bottom : CollisionDir::Top;
}
}
void Player::handle_movment(Timer &dash) {
/* Bounding Box
* Create on dash in front of player and check if the box collides with a wall,
* if so find the difference of the wall and the player and set the dash_distance
* to difference.
* */
void Player::initialize_bounding_box() {
bounding_box.setOutlineColor(sf::Color::White);
bounding_box.setFillColor(sf::Color::Transparent);
bounding_box.setOutlineThickness(-1);
bounding_box.setSize({ dash_length, rect.getSize().y });
bounding_box.setOrigin({ rect.getPosition().x, rect.getPosition().y / 2 });
bounding_box.setPosition({ bounding_box.getOrigin().x, bounding_box.getOrigin().y });
}
void Player::update_bounding_box() {
// Player and box positive distances are 108 in both x and y
bounding_box.setOrigin(
{ -rect.getPosition().x + bounding_box.getGeometricCenter().x + (rect.getSize().x / 2),
-rect.getPosition().y + bounding_box.getGeometricCenter().y + (rect.getSize().y / 2) });
}
void Player::check_bounding_box(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list) {
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) {
if (wall_list[i][j] == nullptr) continue;
}
}
void Player::handle_movment(Timer& dash) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
rect.move({ _velocity.x, 0 });
}
@@ -70,7 +98,7 @@ void Player::handle_movment(Timer &dash) {
_velocity.x = dash_length;
dash.reset();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) {
m_jumping = true;
_velocity.y = -max_vertical_acceleration; // push up
}
+47 -54
View File
@@ -2,60 +2,62 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window.hpp>
#include <SFML/Window/Mouse.hpp>
#include <iostream>
#include <string>
#include <thread>
#include <iostream>
#ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp>
namespace rv = ranges::views;
std::string assets = "/Users/andrew/personal/Projects/CPP/engine/assets/";
#else
#include <ranges>
namespace rv = std::views;
std::string assets = "/home/andrew/personal/Projects/CPP/engine/assets/";
#endif
#include "data.hpp"
#include "elements.hpp"
#include "globals.hpp"
#include "maps.hpp"
#include "scheduler.hpp"
#include "data.hpp"
int main() {
Scheduler time;
Scheduler scheduler;
Manager map_manager;
std::vector<Timer> timers = {
Timer("dash", .5),
Timer("level_switch", 1),
};
map_manager.load_map(level_data[1].data);
sf::Font font(assets + "pixel.ttf");
Player player { player_data, GRAVITY };
Object test_object { object_data[0] };
sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default);
bool running { true };
sf::Text text(font, "0, 0", 16);
window.setFramerateLimit(FPS);
#ifdef __APPLE__
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
player.initialize_bounding_box();
map_manager.load(level_data[4].data);
sf::Text text(font, "0, 0", 16);
text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5) });
bool running = true;
std::thread scheduler([&] { time.update(running, timers); });
std::thread scheduler_thread([&] { scheduler.update(running); });
while (window.isOpen()) {
std::cout <<
window.clear(sf::Color::Black);
text.setString(std::to_string(timers.at(0).get_time()));
test_object.rect.setPosition(
{ player.rect.getPosition().x + 108 - player.rect.getSize().x, player.rect.getPosition().y });
test_object.rect.setPosition({ player.rect.getPosition().x - 108, player.rect.getPosition().y });
text.setString(std::to_string(scheduler.timers.at(0).get_time()));
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
running = false;
@@ -67,57 +69,48 @@ int main() {
running = false;
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) {
map_manager.clear_map();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && timers.at(1).below_or_zero()) {
map_manager.clear_map();
if (map_manager.get_current_level() == 1) {
map_manager.load_map(level_data[1].data);
map_manager.set_current_level(2);
} else if (map_manager.get_current_level() == 2) {
map_manager.load_map(level_data[2].data);
map_manager.set_current_level(3);
} else if (map_manager.get_current_level() == 3) {
map_manager.load_map(level_data[3].data);
map_manager.set_current_level(1);
} else {
map_manager.load_map(level_data[1].data);
map_manager.set_current_level(1);
for (int i = 0; i < player.bounding_box.getPointCount(); i++) {
std::cout << "(" << player.bounding_box.getPoint(i).x << ", " << player.bounding_box.getPoint(i).y
<< ")\n";
}
timers.at(1).reset();
std::cout << "\n";
// sf::FloatRect a = player.bounding_box.getGlobalBounds();
}
player.handle_movment(timers.at(time.pull(timers, "dash")));
if (map_manager.get_active() >= level_data.size()) {
map_manager.set_active(DEFAULT);
map_manager.load(level_data[DEFAULT].data);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && scheduler.timers.at(1).below_or_zero()) {
map_manager.set_next();
scheduler.timers.at(1).reset();
}
player.update_bounding_box();
player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")));
player.get_horizontal_collision(map_manager.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(map_manager.wall_list)) {
std::cout << "\n";
if (!player.get_vertical_top_collision(map_manager.wall_list)) {
std::cout << "\n";
} else {
std::cout << "Yes vertical top collision\n";
}
} else { std::cout << "Yes vertical bottom collision\n"; }
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) {
if (map_manager.wall_list[i][j] == nullptr) {
continue;
}
window.draw(map_manager.wall_list[i][j]->rect);
if (!player.get_vertical_bottom_collision(map_manager.wall_list)) {
player.get_vertical_top_collision(map_manager.wall_list);
}
map_manager.draw(window);
window.draw(player.rect);
window.draw(player.bounding_box);
window.draw(text);
window.draw(test_object.rect);
window.display();
}
scheduler.join();
scheduler_thread.join();
map_manager.clear_map();
map_manager.clear();
return 0;
}
+23 -19
View File
@@ -4,6 +4,7 @@
#include "elements.hpp"
#include "globals.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
#ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp>
@@ -14,34 +15,37 @@ namespace rv = ranges::views;
namespace rv = std::views;
#endif
void Manager::clear_map() {
void Manager::clear() {
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;
}
}
int Manager::get_current_level() { return current_level; }
int Manager::get_active() { return _active; }
void Manager::set_current_level(int level) { current_level = level; }
void Manager::set_active(int level) { _active = level; }
// 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 : 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 : rv::iota(start_y, end_y)) {
// if (draw_list[i][x_level] == ' ') continue;
// return false;
// }
// return true;
// }
void Manager::set_next() {
_active++;
clear();
load(level_data[_active].data);
}
void Manager::load_map(const std::array<std::array<char, MAX_COLS>, MAX_ROWS> map_data) {
void Manager::set_previous() {
_active--;
clear();
load(level_data[_active].data);
}
void Manager::draw(sf::RenderWindow& window) {
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) {
if (wall_list[i][j] == nullptr) continue;
window.draw(wall_list[i][j]->rect);
}
}
void Manager::load(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') {
+21 -6
View File
@@ -1,7 +1,15 @@
#include "scheduler.hpp"
#include "data.hpp"
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
Scheduler::Scheduler() {
timers.push_back(timer_data[DASH]);
timers.push_back(timer_data[LEVEL_SWITCH]);
}
bool Timer::below_or_zero() {
if(time <= 0) { return true; }
@@ -16,17 +24,24 @@ void Timer::reset() { time = max_time; }
void Timer::update() { if(!below_or_zero()) { time--; } }
int Scheduler::pull(std::vector<Timer>& times, std::string name) {
for(int i = 0; i < times.size(); i++) {
if(times[i].get_name() == name) { return i; }
int Scheduler::pull(std::string name) {
try {
for(int i = 0; i < timers.size(); i++) {
if(timers[i].get_name() == name) { return i; }
}
throw -1;
} catch (int x) {
std::cout << "No valid timer with name: (" << name << ") as a value\n";
std::cout << "Using defualt timer values\n";
return -1;
}
return -1;
return 0;
}
void Scheduler::update(bool& running, std::vector<Timer>& times) {
void Scheduler::update(bool& running) {
while (running) {
std::this_thread::sleep_for(std::chrono::seconds(1));
for(Timer &val : times) { val.update(); }
for(Timer &val : timers) { val.update(); }
}
}