maps are more efficient to make and run on memory
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include "scheduler.hpp"
|
||||
#include "globals.hpp"
|
||||
|
||||
enum class CollisionDir { None, Left, Right, Top, Bottom };
|
||||
@@ -24,11 +25,6 @@ protected:
|
||||
float _border_thickness;
|
||||
};
|
||||
|
||||
struct Player_timers {
|
||||
float dash_cooldown { 0 };
|
||||
float dash_cooldown_max { 5 };
|
||||
};
|
||||
|
||||
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);
|
||||
@@ -37,7 +33,7 @@ public:
|
||||
float m_dash_distance;
|
||||
bool m_jumping;
|
||||
bool m_dash;
|
||||
Player_timers timers;
|
||||
Times timers;
|
||||
void handle_movment();
|
||||
void handle_gravity();
|
||||
bool get_jumping();
|
||||
|
||||
+5
-2
@@ -4,7 +4,10 @@
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
extern int map_data[MAX_ROWS][MAX_COLS];
|
||||
void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]);
|
||||
extern char test_level_0[MAX_ROWS][MAX_COLS];
|
||||
extern char test_level_1[MAX_ROWS][MAX_COLS];
|
||||
|
||||
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]);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
#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 };
|
||||
};
|
||||
|
||||
struct Timer {
|
||||
void update_timers(bool &running, float &dash_timer);
|
||||
void update_timers(bool &running, Times ×);
|
||||
};
|
||||
#endif
|
||||
|
||||
+17
-49
@@ -19,6 +19,7 @@
|
||||
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 };
|
||||
@@ -29,37 +30,7 @@ int main() {
|
||||
sf::Vector2f velocity { 4.0f, 0 };
|
||||
} player_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { TILESIZE, TILESIZE };
|
||||
sf::Vector2f pos { 0, 0 };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { -2.0f };
|
||||
} tile_data;
|
||||
|
||||
#ifdef __APPLE__
|
||||
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
|
||||
if(map_data[i][j] != 1) continue;
|
||||
wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black };
|
||||
}
|
||||
|
||||
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;
|
||||
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) });
|
||||
}
|
||||
|
||||
#else
|
||||
for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
|
||||
if(map_data[i][j] != 1) continue;
|
||||
wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black };
|
||||
}
|
||||
|
||||
merged_walls(wall_list);
|
||||
|
||||
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;
|
||||
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) });
|
||||
}
|
||||
#endif
|
||||
load_map(wall_list, test_level_0);
|
||||
|
||||
Player player {
|
||||
player_data.size, player_data.pos, player_data.velocity,
|
||||
@@ -80,7 +51,7 @@ int main() {
|
||||
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.dash_cooldown); } );
|
||||
std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } );
|
||||
while (window.isOpen()) {
|
||||
window.clear(sf::Color::Black);
|
||||
text.setString(std::to_string(player.timers.dash_cooldown));
|
||||
@@ -94,19 +65,24 @@ int main() {
|
||||
running = false;
|
||||
window.close();
|
||||
}
|
||||
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_0);
|
||||
current_level = 0;
|
||||
player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Horizontal movement and collisions
|
||||
player.handle_movment();
|
||||
player.get_horizontal_collision(wall_list);
|
||||
|
||||
// if(player.rect.getPosition().y > HEIGHT) {
|
||||
// player.rect.setPosition({ player.rect.getPosition().x, HEIGHT - TILESIZE - player.rect.getSize().y });
|
||||
// player.set_velocity_y(0);
|
||||
// while(player.get_vertical_bottom_collision(wall_list) || player.get_vertical_top_collision(wall_list)) {
|
||||
// player.rect.setPosition({ player.rect.getPosition().x, player.rect.getPosition().y - TILESIZE - player.rect.getSize().y });
|
||||
// }
|
||||
// }
|
||||
|
||||
player.handle_gravity();
|
||||
if(player.get_velocity().y > ((float)TILESIZE / 2)) {
|
||||
player.set_velocity_y(TILESIZE / 2);
|
||||
@@ -132,15 +108,7 @@ int main() {
|
||||
window.draw(text);
|
||||
window.display();
|
||||
}
|
||||
#ifdef __APPLE__
|
||||
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
|
||||
delete wall_list[i][j];
|
||||
}
|
||||
#else
|
||||
for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
|
||||
delete wall_list[i][j];
|
||||
}
|
||||
#endif
|
||||
clear_map(wall_list);
|
||||
player_dash_cooldown.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
+100
-45
@@ -1,4 +1,5 @@
|
||||
#include "maps.hpp"
|
||||
#include "elements.hpp"
|
||||
#include "globals.hpp"
|
||||
#ifdef __APPLE__
|
||||
#include <range/v3/view/iota.hpp>
|
||||
@@ -7,57 +8,111 @@
|
||||
#include <ranges>
|
||||
#endif
|
||||
|
||||
void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
|
||||
void clear_map(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++;
|
||||
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) {
|
||||
delete wall_list[i][j];
|
||||
wall_list[i][j] = nullptr;
|
||||
}
|
||||
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
|
||||
#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;
|
||||
delete wall_list[i][j];
|
||||
wall_list[i][j] = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
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 },
|
||||
{ float(j * TILESIZE), float(i * TILESIZE) },
|
||||
{ 0, 0 },
|
||||
-2.0f,
|
||||
sf::Color::Black
|
||||
};
|
||||
}
|
||||
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 },
|
||||
{ float(j * TILESIZE), float(i * TILESIZE) },
|
||||
{ 0, 0 },
|
||||
-2.0f,
|
||||
sf::Color::Black
|
||||
};
|
||||
}
|
||||
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 },
|
||||
{ float(j * TILESIZE), float(i * TILESIZE) },
|
||||
{ 0, 0 },
|
||||
-2.0f,
|
||||
sf::Color::Black
|
||||
};
|
||||
}
|
||||
if(map_data[i][j] == '1' && map_data[i][j + 1] == '-') {
|
||||
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++;
|
||||
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
|
||||
};
|
||||
}
|
||||
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 },
|
||||
{ float(j * TILESIZE), float(i * TILESIZE) },
|
||||
{ 0, 0 },
|
||||
-2.0f,
|
||||
sf::Color::Black
|
||||
};
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
int map_data[MAX_ROWS][MAX_COLS] = {
|
||||
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
||||
{ 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
||||
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
||||
char test_level_0[MAX_ROWS][MAX_COLS] = {
|
||||
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
|
||||
{ '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '2', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
|
||||
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
|
||||
};
|
||||
|
||||
char test_level_1[MAX_ROWS][MAX_COLS] = {
|
||||
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
|
||||
{ '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
|
||||
{ '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
|
||||
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
|
||||
};
|
||||
|
||||
+3
-2
@@ -3,9 +3,10 @@
|
||||
#include "scheduler.hpp"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
void Timer::update_timers(bool &running, float &dash_timer) {
|
||||
void Timer::update_timers(bool &running, Times ×) {
|
||||
while (running) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
dash_timer--;
|
||||
times.dash_cooldown--;
|
||||
times.level_switch_cooldown--;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user