130 lines
4.2 KiB
C++
130 lines
4.2 KiB
C++
#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 <cstdlib>
|
|
#include <string>
|
|
#include <ranges>
|
|
#include <iostream>
|
|
#include "globals.hpp"
|
|
#include "elements.hpp"
|
|
#include "maps.hpp"
|
|
|
|
int main() {
|
|
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
|
struct {
|
|
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
|
|
sf::Vector2<float> pos { TILESIZE, TILESIZE };
|
|
float acceleration { 1.0f };
|
|
float max_velocity_y { 20.0f };
|
|
float border_size { -1.0f };
|
|
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;
|
|
|
|
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;
|
|
|
|
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 };
|
|
}
|
|
|
|
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) });
|
|
}
|
|
|
|
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
|
|
};
|
|
|
|
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);
|
|
|
|
#ifdef __APPLE__
|
|
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
|
#endif
|
|
#ifdef __linux__
|
|
sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
|
#endif
|
|
|
|
sf::Text text(font, "0, 0", 16);
|
|
text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)});
|
|
|
|
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));
|
|
while (const std::optional event = window.pollEvent()) {
|
|
if (event->is<sf::Event::Closed>())
|
|
window.close();
|
|
}
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
|
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.get_velocity().x > 4) {
|
|
player.set_velocity_x(player.get_velocity().x - player.m_acceleration);
|
|
}
|
|
|
|
// Handle Vertical movement and collisions
|
|
player.set_velocity_y(player.get_velocity().y + player.m_acceleration);
|
|
player.rect.move({0, player.get_velocity().y});
|
|
|
|
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
|
|
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;
|
|
window.draw(wall_list[i][j]->rect);
|
|
}
|
|
window.draw(player.rect);
|
|
window.draw(line.rect);
|
|
window.draw(text);
|
|
window.display();
|
|
}
|
|
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];
|
|
}
|
|
}
|