data driving walls

This commit is contained in:
Andrew Haynes Macbook
2026-01-14 16:05:17 -05:00
parent 1fa3293df0
commit ad2d01ab05
12 changed files with 319 additions and 209 deletions
+26
View File
@@ -0,0 +1,26 @@
#ifndef DATA_HPP
#define DATA_HPP
#include "globals.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
#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;
};
class EntityData : public ObjectData {
public:
sf::Vector2<float> velocity;
};
extern std::vector<ObjectData> object_data;
#endif // DATA_HPP
+33 -14
View File
@@ -3,25 +3,44 @@
#include "globals.hpp"
#include "scheduler.hpp"
#include "data.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
enum class CollisionDir { None, Left, Right, Top, Bottom };
class Entity;
class Wall;
class Player;
class Wall {
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);
sf::RectangleShape rect;
};
class Entity {
public:
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(Entity& entity);
sf::Color inside_color = sf::Color::Black, sf::Color border_color = sf::Color::White);
CollisionDir find_intersection(Wall& entity);
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:
protected:
sf::Vector2f _velocity;
sf::Color _inside_color;
sf::Color _border_color;
@@ -29,26 +48,26 @@ class Entity {
};
class Player : public Entity {
public:
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 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;
Times timers;
void handle_movment();
void handle_movment(Timer &dash);
void handle_gravity();
bool get_jumping();
void horizontal_no_out_of_bounds(Entity& base);
void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds(Entity& base);
bool get_horizontal_collision(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_top_collision(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
bool get_vertical_bottom_collision(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
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>);
private:
private:
bool get_vertical_bottom_collision_by_offset(std::array<std::array<Entity*, MAX_COLS>, MAX_ROWS>);
sf::Vector2f _min_velocity;
};
+5 -2
View File
@@ -1,10 +1,13 @@
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
#define TILESIZE 32
#define MAX_COLS 8 // 19 cols
#define MAX_ROWS 8 // 13 rows
#define MAX_COLS 19 // 19 cols
#define MAX_ROWS 13 // 13 rows
#define WIDTH TILESIZE * MAX_COLS
#define HEIGHT TILESIZE * MAX_ROWS
#define FPS 60
#define GRAVITY 1
#define dash_cooldown .5
#define level_switch_cooldown_max 1
#endif
+14 -5
View File
@@ -8,13 +8,22 @@
#include <array>
struct Level {
char data[MAX_ROWS][MAX_COLS];
std::array<std::array<char, MAX_COLS>, MAX_ROWS> data;
std::string name;
};
extern Level levels[];
extern const char map[MAX_ROWS][MAX_COLS];
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 = {};
private:
int current_level = 1;
};
extern std::vector<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 char map_data[MAX_ROWS][MAX_COLS]);
#endif
+21 -7
View File
@@ -1,14 +1,28 @@
#ifndef SCHEDULER_HPP
#define SCHEDULER_HPP
struct Times {
float dash_cooldown { 0 };
float dash_cooldown_max { .5 };
float level_switch_cooldown { 0 };
float level_switch_cooldown_max { 1 };
#include <ctime>
#include <string>
#include <vector>
class Timer {
public:
Timer(std::string name, float max_time) : name(name), max_time(max_time) {};
bool below_or_zero();
float get_time();
std::string get_name();
void reset();
void update();
private:
float time = 0;
const float max_time;
const std::string name;
};
struct Timer {
void update_timers(bool& running, Times& times);
struct Scheduler {
std::vector<Timer> timers;
void update(bool& running, std::vector<Timer>& times);
int pull(std::vector<Timer>& times, std::string name);
};
#endif