added walls
This commit is contained in:
+69
-27
@@ -7,48 +7,59 @@
|
||||
#include <SFML/System/Angle.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
#include "maps.hpp"
|
||||
|
||||
int main() {
|
||||
std::vector<Entity> wall_list = {};
|
||||
// std::vector<Entity> wall_list = {};
|
||||
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
||||
struct {
|
||||
sf::Vector2<float> size { 16.0f, 16.0f };
|
||||
sf::Vector2<float> pos { 40, 40 };
|
||||
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 };
|
||||
float border_size { -1.0f };
|
||||
sf::Vector2f velocity { 4.0f, 0 };
|
||||
} player_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { 32.0f, 32.0f };
|
||||
sf::Vector2f size { TILESIZE, TILESIZE };
|
||||
sf::Vector2f pos { 0, 0 };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { 2.0f };
|
||||
float border_size { -2.0f };
|
||||
} tile_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { WIDTH, 2.0f };
|
||||
sf::Vector2f pos { 0, HEIGHT - 50 };
|
||||
sf::Vector2f pos { 0, HEIGHT - (TILESIZE * 1) };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { 1.0f };
|
||||
float border_size { -1.0f };
|
||||
} line_data;
|
||||
|
||||
struct {
|
||||
sf::Vector2f size { 8, 8 };
|
||||
sf::Vector2f pos { 100, 40 };
|
||||
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 };
|
||||
float border_size { -1.0f };
|
||||
} simulated_data;
|
||||
|
||||
for(int i = 0; i < (WIDTH / 32); i++) {
|
||||
wall_list.push_back(Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black });
|
||||
for(int i = 0; i < MAX_ROWS; i++) {
|
||||
for(int j = 0; j < MAX_COLS; j++) {
|
||||
if(map_data[i][j]) {
|
||||
wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black };
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < wall_list.size(); i++) {
|
||||
wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size });
|
||||
for(int i = 0; i < MAX_ROWS; i++) {
|
||||
for(int j = 0; j < MAX_COLS; j++) {
|
||||
if(wall_list[i][j] != nullptr) {
|
||||
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Player player {
|
||||
@@ -58,7 +69,7 @@ int main() {
|
||||
};
|
||||
|
||||
Entity line { line_data.size, line_data.pos, line_data.velocity, line_data.border_size, sf::Color::White };
|
||||
Entity test_square { simulated_data.size, simulated_data.pos, simulated_data.pos, simulated_data.border_size, sf::Color::Black };
|
||||
Entity test_square { simulated_data.size, simulated_data.pos, simulated_data.velocity, simulated_data.border_size, sf::Color::Black };
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||
window.setFramerateLimit(60);
|
||||
@@ -72,6 +83,7 @@ int main() {
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
||||
window.close();
|
||||
}
|
||||
|
||||
// FOR TESTING
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
||||
if(test_square.rect.getPosition().x < 30) {
|
||||
@@ -94,34 +106,34 @@ int main() {
|
||||
}
|
||||
test_square.rect.move({ test_square.velocity.x, test_square.velocity.y });
|
||||
|
||||
float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().x;
|
||||
float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().y - line_data.border_size - player_data.border_size;
|
||||
if (player.rect.getPosition().y >= groundY) {
|
||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
||||
player.velocity.y = 0;
|
||||
player.m_jumping = false;
|
||||
}
|
||||
float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness();
|
||||
float right_border = WIDTH - player.rect.getSize().x;
|
||||
if(player.rect.getPosition().x > right_border) {
|
||||
player.rect.setPosition({ right_border, player.rect.getPosition().y});
|
||||
}
|
||||
if(player.rect.getPosition().x < player.rect.getOutlineThickness()) {
|
||||
player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y});
|
||||
if(player.rect.getPosition().x < std::abs(player.rect.getOutlineThickness())) {
|
||||
player.rect.setPosition({ 0, player.rect.getPosition().y});
|
||||
}
|
||||
|
||||
float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness();
|
||||
float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - (test_square.rect.getSize().y);
|
||||
float right_border_rect = WIDTH - test_square.rect.getSize().x;
|
||||
float groundY_rect = line.rect.getPosition().y - line.rect.getSize().y - test_square.rect.getSize().y - line_data.border_size - simulated_data.border_size;
|
||||
if(test_square.rect.getPosition().x > right_border_rect) {
|
||||
test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y });
|
||||
test_square.velocity = { -20, 0 };
|
||||
}
|
||||
if(test_square.rect.getPosition().x < test_square.rect.getOutlineThickness()) {
|
||||
test_square.rect.setPosition({ test_square.rect.getOutlineThickness(), test_square.rect.getPosition().y });
|
||||
if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) {
|
||||
test_square.rect.setPosition({ 0, test_square.rect.getPosition().y });
|
||||
test_square.velocity = { 20, 0 };
|
||||
}
|
||||
if(test_square.rect.getPosition().y >= groundY_rect) {
|
||||
test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect });
|
||||
}
|
||||
|
||||
|
||||
auto collision = player.find_intersection(test_square);
|
||||
switch (collision) {
|
||||
case CollisionDir::Top:
|
||||
@@ -138,13 +150,43 @@ int main() {
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
// for(int i = 0; i < MAX_ROWS; i++) {
|
||||
// for(int j = 0; j < MAX_COLS; j++) {
|
||||
// auto collision = player.find_intersection(*wall_list[i][j]);
|
||||
// switch (collision) {
|
||||
// case CollisionDir::Top:
|
||||
// player.velocity = { player.velocity.x, -player.velocity.y };
|
||||
// break;
|
||||
// case CollisionDir::Bottom:
|
||||
// player.velocity = { player.velocity.x, -player.velocity.y };
|
||||
// break;
|
||||
// case CollisionDir::Left:
|
||||
// player.velocity = { player.velocity.x, 0 };
|
||||
// break;
|
||||
// case CollisionDir::Right:
|
||||
// player.velocity = { player.velocity.x, 0 };
|
||||
// break;
|
||||
// default: break;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
for(int i = 0; i < wall_list.size(); i++) {
|
||||
window.draw(wall_list[i].rect);
|
||||
for(int i = 0; i < MAX_ROWS; i++) {
|
||||
for(int j = 0; j < MAX_COLS; j++) {
|
||||
if(wall_list[i][j] != nullptr) {
|
||||
window.draw(wall_list[i][j]->rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
window.draw(player.rect);
|
||||
window.draw(test_square.rect);
|
||||
window.draw(line.rect);
|
||||
window.display();
|
||||
}
|
||||
for(int i = 0; i < MAX_ROWS; i++) {
|
||||
for(int j = 0; j < MAX_COLS; j++) {
|
||||
delete wall_list[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user