fixes
This commit is contained in:
@@ -10,6 +10,7 @@ set(SOURCES
|
||||
src/main.cpp
|
||||
src/elements.cpp
|
||||
src/maps.cpp
|
||||
src/scheduler.cpp
|
||||
)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
@@ -24,12 +24,20 @@ 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, sf::Color inside_color, sf::Color border_color);
|
||||
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_timers timers;
|
||||
void handle_movment();
|
||||
void handle_gravity();
|
||||
bool get_jumping();
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef SCHEDULER_HPP
|
||||
#define SCHEDULER_HPP
|
||||
|
||||
struct Timer {
|
||||
void update_timers(bool &running, float &dash_timer);
|
||||
};
|
||||
#endif
|
||||
+11
-4
@@ -63,10 +63,12 @@ CollisionDir Entity::find_intersection(Entity &entity) {
|
||||
}
|
||||
}
|
||||
|
||||
Player::Player(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, 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;
|
||||
m_dash_distance = dash_distance;
|
||||
m_jumping = m_dash = false;
|
||||
}
|
||||
|
||||
void Player::handle_movment() {
|
||||
@@ -74,12 +76,16 @@ void Player::handle_movment() {
|
||||
rect.move({_velocity.x, 0});
|
||||
} if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
|
||||
rect.move({-_velocity.x, 0});
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
|
||||
} if(timers.dash_cooldown <= 0) {
|
||||
m_dash = false;
|
||||
} 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) {
|
||||
m_jumping = true;
|
||||
_velocity.y = -m_jump_strength; // push up
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Player::handle_gravity() {
|
||||
@@ -110,6 +116,7 @@ void Player::vertical_no_out_of_bounds(Entity &base) {
|
||||
}
|
||||
}
|
||||
|
||||
// bool Player::get_horizontal_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
|
||||
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))) {
|
||||
|
||||
+25
-36
@@ -1,17 +1,10 @@
|
||||
#include <SFML/Graphics/CircleShape.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include <SFML/System/Angle.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <SFML/System.hpp>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#ifdef __APPLE__
|
||||
#include <range/v3/view/iota.hpp>
|
||||
#include <range/v3/view/cartesian_product.hpp>
|
||||
@@ -21,8 +14,10 @@
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
#include "maps.hpp"
|
||||
#include "scheduler.hpp"
|
||||
|
||||
int main() {
|
||||
Timer time;
|
||||
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
||||
struct {
|
||||
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
|
||||
@@ -30,6 +25,7 @@ int main() {
|
||||
float acceleration { 1.0f };
|
||||
float max_velocity_y { 20.0f };
|
||||
float border_size { -1.0f };
|
||||
float dash_distance { 20.0f };
|
||||
sf::Vector2f velocity { 4.0f, 0 };
|
||||
} player_data;
|
||||
|
||||
@@ -40,20 +36,6 @@ int main() {
|
||||
float border_size { -2.0f };
|
||||
} tile_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { WIDTH, 2.0f };
|
||||
sf::Vector2f pos { 0, HEIGHT - (TILESIZE * 1) };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { -1.0f };
|
||||
} line_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { TILESIZE / 4.0f, TILESIZE / 4.0f };
|
||||
sf::Vector2f pos { TILESIZE * 3, TILESIZE };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { -1.0f };
|
||||
} simulated_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;
|
||||
@@ -64,6 +46,7 @@ int main() {
|
||||
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;
|
||||
@@ -79,11 +62,9 @@ int main() {
|
||||
Player player {
|
||||
player_data.size, player_data.pos, player_data.velocity,
|
||||
player_data.border_size, player_data.acceleration, player_data.max_velocity_y,
|
||||
sf::Color::Black, sf::Color::White
|
||||
player_data.dash_distance, sf::Color::Black, sf::Color::White
|
||||
};
|
||||
|
||||
Entity line { line_data.size, line_data.pos, line_data.velocity, line_data.border_size, sf::Color::White };
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||
window.setFramerateLimit(60);
|
||||
|
||||
@@ -96,31 +77,38 @@ int main() {
|
||||
sf::Text text(font, "0, 0", 16);
|
||||
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); } );
|
||||
while (window.isOpen()) {
|
||||
window.clear(sf::Color::Black);
|
||||
text.setString(std::to_string((int)player.rect.getPosition().x) + ", " + std::to_string((int)player.rect.getPosition().y));
|
||||
text.setString(std::to_string(player.timers.dash_cooldown));
|
||||
while (const std::optional event = window.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
if (event->is<sf::Event::Closed>()) {
|
||||
running = false;
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
||||
running = false;
|
||||
window.close();
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
|
||||
player.set_velocity_x(20);
|
||||
}
|
||||
|
||||
// Handle Horizontal movement and collisions
|
||||
player.handle_movment();
|
||||
player.get_horizontal_collision(wall_list);
|
||||
player.horizontal_no_out_of_bounds(line);
|
||||
|
||||
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_vertical_bottom_collision(wall_list)) {
|
||||
player.get_vertical_top_collision(wall_list);
|
||||
}
|
||||
player.vertical_no_out_of_bounds(line);
|
||||
|
||||
// Drawing Everything to window
|
||||
#ifdef __APPLE__
|
||||
@@ -136,7 +124,6 @@ int main() {
|
||||
}
|
||||
#endif
|
||||
window.draw(player.rect);
|
||||
window.draw(line.rect);
|
||||
window.draw(text);
|
||||
window.display();
|
||||
}
|
||||
@@ -149,4 +136,6 @@ int main() {
|
||||
delete wall_list[i][j];
|
||||
}
|
||||
#endif
|
||||
player_dash_cooldown.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
+13
-13
@@ -1,17 +1,17 @@
|
||||
#include "maps.hpp"
|
||||
|
||||
int map_data[MAX_ROWS][MAX_COLS] = {
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0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 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 }
|
||||
};
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// timer for player cooldown
|
||||
//
|
||||
#include "scheduler.hpp"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
void Timer::update_timers(bool &running, float &dash_timer) {
|
||||
while (running) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
dash_timer--;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user