This commit is contained in:
Gentoo
2026-01-14 20:57:15 -05:00
parent ad2d01ab05
commit 7bbf50b581
7 changed files with 204 additions and 254 deletions
+25 -9
View File
@@ -5,22 +5,38 @@
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
#include <array>
#include <string>
#include <vector>
class ObjectData {
public:
sf::Vector2<float> size = { TILESIZE, TILESIZE };
sf::Vector2<float> pos = { TILESIZE, TILESIZE };
sf::Color interior_color = sf::Color::Black;
sf::Color exterior_color = sf::Color::White;
float edge = -2.0f;
struct ObjectData {
sf::Vector2<float> size { TILESIZE, TILESIZE };
sf::Vector2<float> pos { TILESIZE, TILESIZE };
sf::Color interior_color { sf::Color::Black };
sf::Color exterior_color { sf::Color::White };
float edge { -2.0f };
};
class EntityData : public ObjectData {
struct EntityData : ObjectData {
sf::Vector2<float> velocity { 0, 0 };
};
struct PlayerData : EntityData {
ObjectData object;
EntityData entity;
float dash_length { 0.0f };
float max_vertical_acceleration { 0.0f };
};
class LevelData {
public:
sf::Vector2<float> velocity;
std::array<std::array<char, MAX_COLS>, MAX_ROWS> data = {};
std::string name = "Null";
};
extern std::vector<ObjectData> object_data;
extern std::vector<EntityData> entity_data;
extern std::vector<LevelData> level_data;
extern PlayerData player_data;
#endif // DATA_HPP
+16 -42
View File
@@ -1,9 +1,9 @@
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include "data.hpp"
#include "globals.hpp"
#include "scheduler.hpp"
#include "data.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
@@ -11,74 +11,48 @@
enum class CollisionDir { None, Left, Right, Top, Bottom };
class Entity;
class Wall;
class Object;
class Player;
class Wall {
class Object {
public:
Wall(ObjectData data);
ObjectData data;
sf::Vector2<float> size;
sf::Vector2<float> pos;
sf::Color interior_color;
sf::Color exterior_color;
float edge;
CollisionDir find_intersection(Entity& entity);
Object(ObjectData data);
CollisionDir find_intersection(Object& entity);
sf::RectangleShape rect;
};
class Entity {
class Entity : public Object {
public:
Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness,
sf::Color inside_color = sf::Color::Black, sf::Color border_color = sf::Color::White);
CollisionDir find_intersection(Wall& entity);
Entity(EntityData data) : Object(data), _velocity(data.velocity) {};
void set_velocity(sf::Vector2f velocity);
void set_velocity_x(int velocity);
void set_velocity_y(int velocity);
sf::Vector2<float> get_velocity();
sf::RectangleShape rect;
protected:
sf::Vector2f _velocity;
sf::Color _inside_color;
sf::Color _border_color;
float _border_thickness;
};
class Player : public Entity {
public:
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);
float m_acceleration;
float m_jump_strength;
float m_dash_distance;
bool m_jumping;
bool m_dash;
Player(PlayerData data, float gravity) : Entity(data), dash_length(data.dash_length), max_vertical_acceleration(data.max_vertical_acceleration), gravity(gravity) {};
float max_vertical_acceleration;
float dash_length;
float gravity;
bool m_jumping { false };
bool m_dash { false };
void handle_movment(Timer& dash);
void handle_gravity();
bool get_jumping();
void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds(Entity& base);
bool get_horizontal_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_top_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_bottom_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS>);
bool get_horizontal_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_top_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_bottom_collision(std::array<std::array<Object*, MAX_COLS>, MAX_ROWS>);
private:
bool get_vertical_bottom_collision_by_offset(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
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
+1 -9
View File
@@ -7,23 +7,15 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <array>
struct Level {
std::array<std::array<char, MAX_COLS>, MAX_ROWS> data;
std::string name;
};
class Manager {
public:
void clear_map();
void load_map(const std::array<std::array<char, MAX_COLS>, MAX_ROWS> map_data);
int get_current_level();
void set_current_level(int level);
std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS> wall_list = {};
std::array<std::array<Object*, MAX_COLS>, MAX_ROWS> wall_list = {};
private:
int current_level = 1;
};
extern std::vector<Level> levels;
#endif
+79
View File
@@ -1,6 +1,85 @@
#include "data.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp>
std::vector<ObjectData> object_data {
{},
};
std::vector<EntityData> entity_data {
{},
};
PlayerData player_data {
.object = {
.size = { TILESIZE / 2.0f, TILESIZE / 2.0f },
.edge = -1.0f,
},
.entity = { .velocity = { 3.0f, 0.0f } },
.dash_length = 20.0f,
.max_vertical_acceleration = 15.0f
};
std::vector<LevelData> level_data = {
{ .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ' } } },
.name = "Empty" },
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
.name = "Default" },
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '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' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '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 = "Check" }
};
+11 -60
View File
@@ -1,5 +1,6 @@
#include "elements.hpp"
#include "data.hpp"
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp>
@@ -15,14 +16,7 @@ namespace rv = ranges::views;
namespace rv = std::views;
#endif
Wall::Wall(ObjectData data) {
this->data = data;
// rect.setSize(data.size);
// pos = data.pos;
// interior_color = data.interior_color;
// exterior_color = data.exterior_color;
// edge = data.edge;
Object::Object(ObjectData data) {
rect.setSize(data.size);
rect.setPosition(data.pos);
rect.setOutlineThickness(data.edge);
@@ -30,16 +24,6 @@ Wall::Wall(ObjectData data) {
rect.setOutlineColor(data.exterior_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);
rect.setFillColor(inside_color);
rect.setOutlineColor(border_color);
_velocity = velocity;
}
void Entity::set_velocity(sf::Vector2<float> velocity) { _velocity = velocity; }
void Entity::set_velocity_x(int velocity) { _velocity.x = velocity; }
@@ -48,7 +32,7 @@ void Entity::set_velocity_y(int velocity) { _velocity.y = velocity; }
sf::Vector2<float> Entity::get_velocity() { return _velocity; }
CollisionDir Wall::find_intersection(Entity& entity) {
CollisionDir Object::find_intersection(Object& entity) {
sf::FloatRect a = rect.getGlobalBounds();
sf::FloatRect b = entity.rect.getGlobalBounds();
auto inter = a.findIntersection(b);
@@ -71,39 +55,6 @@ CollisionDir Wall::find_intersection(Entity& entity) {
}
CollisionDir Entity::find_intersection(Wall& entity) {
sf::FloatRect a = rect.getGlobalBounds();
sf::FloatRect b = entity.rect.getGlobalBounds();
auto inter = a.findIntersection(b);
if (!inter) {
return CollisionDir::None;
}
const auto overlap = *inter;
float overlapW = overlap.size.x;
float overlapH = overlap.size.y;
if (overlapW < overlapH) {
// Horizontal collision
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;
}
}
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;
m_dash_distance = dash_distance;
m_jumping = m_dash = false;
}
void Player::handle_movment(Timer &dash) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
rect.move({ _velocity.x, 0 });
@@ -116,21 +67,21 @@ void Player::handle_movment(Timer &dash) {
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !m_dash && dash.get_time() <= 0) {
m_dash = true;
_velocity.x = m_dash_distance;
_velocity.x = dash_length;
dash.reset();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
m_jumping = true;
_velocity.y = -m_jump_strength; // push up
_velocity.y = -max_vertical_acceleration; // push up
}
}
void Player::handle_gravity() {
if (get_velocity().x > _min_velocity.x) {
set_velocity_x(get_velocity().x - m_acceleration);
if (get_velocity().x > player_data.velocity.x) {
set_velocity_x(get_velocity().x - gravity);
}
set_velocity_y(get_velocity().y + m_acceleration);
set_velocity_y(get_velocity().y + gravity);
rect.move({ 0, get_velocity().y });
}
@@ -154,7 +105,7 @@ void Player::vertical_no_out_of_bounds(Entity& base) {
}
}
bool Player::get_horizontal_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS> wall_list) {
bool Player::get_horizontal_collision(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;
auto collision = wall_list[i][j]->find_intersection(*this);
@@ -173,7 +124,7 @@ bool Player::get_horizontal_collision(std::array<std::array<Wall*, MAX_COLS>, MA
return false;
}
bool Player::get_vertical_top_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS> wall_list) {
bool Player::get_vertical_top_collision(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;
auto collision = find_intersection(*wall_list[i][j]);
@@ -188,7 +139,7 @@ bool Player::get_vertical_top_collision(std::array<std::array<Wall*, MAX_COLS>,
return false;
}
bool Player::get_vertical_bottom_collision(std::array<std::array<Wall*, MAX_COLS>, MAX_ROWS> wall_list) {
bool Player::get_vertical_bottom_collision(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;
auto collision = find_intersection(*wall_list[i][j]);
+30 -29
View File
@@ -1,4 +1,5 @@
#include "data.hpp"
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
@@ -6,10 +7,9 @@
#include <SFML/System/Vector2.hpp>
#include <SFML/Window.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include <iostream>
#ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp>
@@ -24,6 +24,7 @@ namespace rv = std::views;
#include "globals.hpp"
#include "maps.hpp"
#include "scheduler.hpp"
#include "data.hpp"
int main() {
Scheduler time;
@@ -33,23 +34,25 @@ int main() {
Timer("level_switch", 1),
};
map_manager.load_map(levels[1].data);
map_manager.load_map(level_data[1].data);
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 };
Player player { player_data, GRAVITY };
sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default);
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
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); });
while (window.isOpen()) {
window.clear(sf::Color::Black);
text.setString(std::to_string(timers.at(0).get_time()));
@@ -71,39 +74,37 @@ int main() {
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(levels[1].data);
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(levels[2].data);
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(levels[3].data);
map_manager.load_map(level_data[3].data);
map_manager.set_current_level(1);
} else {
map_manager.load_map(levels[1].data);
map_manager.load_map(level_data[1].data);
map_manager.set_current_level(1);
}
timers.at(1).reset();
}
// player.handle_movment(timers.at(time.pull(timers, "dash")));
// if (!player.get_horizontal_collision(map_manager.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(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"; }
//
player.handle_movment(timers.at(time.pull(timers, "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;
+24 -87
View File
@@ -25,61 +25,61 @@ int Manager::get_current_level() { return current_level; }
void Manager::set_current_level(int level) { current_level = 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;
}
// 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::load_map(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 Wall { object_data[0] };
wall_list[i][j] = new Object { object_data[0] };
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j,
wall_list[i][j]->rect.getPosition().y * i });
}
if (map_data[i][j] == '1' && map_data[i][j + 1] == '2') {
if (map_data[i][j] == '1' && j + 1 < MAX_COLS && map_data[i][j + 1] == '2') {
int start = j, end = j + 1;
wall_list[i][j] = new Wall { object_data[0] };
wall_list[i][j] = new Object { object_data[0] };
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1),
wall_list[i][j]->rect.getSize().y });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j,
wall_list[i][j]->rect.getPosition().y * i });
}
if (map_data[i][j] == '1' && map_data[i + 1][j] == '2') {
if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && map_data[i + 1][j] == '2') {
int start = i, end = i + 1;
wall_list[i][j] = new Wall { object_data[0] };
wall_list[i][j] = new Object { object_data[0] };
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x,
wall_list[i][j]->rect.getSize().y * float(end - start + 1) });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j,
wall_list[i][j]->rect.getPosition().y * i });
}
if (map_data[i][j] == '1' && map_data[i][j + 1] == '-') {
if (map_data[i][j] == '1' && j + 1 < MAX_COLS && 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 Wall { object_data[0] };
wall_list[i][j] = new Object { object_data[0] };
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x * float(end - start + 1),
wall_list[i][j]->rect.getSize().y });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j,
wall_list[i][j]->rect.getPosition().y * i });
}
if (map_data[i][j] == '1' && map_data[i + 1][j] == '|') {
if (map_data[i][j] == '1' && i + 1 < MAX_ROWS && 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 Wall { object_data[0] };
wall_list[i][j] = new Object { object_data[0] };
wall_list[i][j]->rect.setSize({ wall_list[i][j]->rect.getSize().x,
wall_list[i][j]->rect.getSize().y * float(end - start + 1) });
wall_list[i][j]->rect.setPosition({ wall_list[i][j]->rect.getPosition().x * j,
@@ -87,66 +87,3 @@ void Manager::load_map(const std::array<std::array<char, MAX_COLS>, MAX_ROWS> ma
}
}
}
std::vector<Level> levels = {
{ .data = { { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ' } } },
.name = "Empty" },
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'2' } } },
.name = "Default" },
{ .data = { { { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ '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' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0' },
{ ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ' },
{ '0', ' ', '0', ' ', '0', ' ', '0', ' ', '0', ' ', '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 = "Check" }
};