added collisions
This commit is contained in:
+88
-64
@@ -8,39 +8,54 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
int main() {
|
||||
int map = [[]];
|
||||
struct {
|
||||
sf::Vector2<float> size { 16.0f, 16.0f };
|
||||
sf::Vector2<float> pos { 40, 40 };
|
||||
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 { 32.0f, 32.0f };
|
||||
sf::Vector2f pos { 0, 0 };
|
||||
sf::Vector2f velocity { 0, 0 };
|
||||
float border_size { 2.0f };
|
||||
} tile_data;
|
||||
|
||||
struct {
|
||||
|
||||
} simulated_object_data;
|
||||
std::vector<Entity> wall_list = {};
|
||||
for(int i = 0; i < 19; 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 < wall_list.size(); i++) {
|
||||
wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size });
|
||||
}
|
||||
float floor_height = 2.0f;
|
||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||
window.setFramerateLimit(60);
|
||||
|
||||
struct {
|
||||
sf::Vector2<float> size = { 16.0f, 16.0f };
|
||||
sf::Vector2<float> pos = { 40, 40 };
|
||||
float acceleration = 1.0f;
|
||||
float max_velocity_y = 20.0f;
|
||||
float border_size = 2.0f;
|
||||
float velocity_x = 4.0f;
|
||||
} player_data;
|
||||
|
||||
Player player {
|
||||
player_data.size, player_data.pos, player_data.acceleration,
|
||||
player_data.max_velocity_y, player_data.border_size, player_data.velocity_x,
|
||||
sf::Color::Black, sf::Color::White
|
||||
player_data.max_velocity_y, player_data.border_size, player_data.velocity,
|
||||
sf::Color::Black
|
||||
};
|
||||
|
||||
sf::RectangleShape line({ WIDTH, floor_height });
|
||||
line.setPosition({ 0, HEIGHT - 50});
|
||||
Entity line { { WIDTH, floor_height }, { 0, HEIGHT - 50 }, { 0, 0 }, 1.0f, sf::Color::White };
|
||||
// sf::RectangleShape line({ WIDTH, floor_height });
|
||||
// line.setPosition({ 0, HEIGHT - 50});
|
||||
|
||||
sf::RectangleShape rect({ 4, 4 });
|
||||
rect.setFillColor(sf::Color::Black);
|
||||
rect.setOutlineColor(sf::Color::White);
|
||||
rect.setOutlineThickness(2.0f);
|
||||
rect.setPosition({ 40, 40 });
|
||||
// float vel_x = 3.0f;
|
||||
float vel_y = -20.0f;
|
||||
Entity test_square { { 8, 8 }, { 100, 40 }, { 0, 0 }, 1.0f, sf::Color::Black };
|
||||
|
||||
while (window.isOpen()) {
|
||||
window.clear(sf::Color::Black);
|
||||
@@ -48,23 +63,35 @@ int main() {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
window.close();
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
||||
vel_y = -10;
|
||||
}
|
||||
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) {
|
||||
test_square.velocity = { 20, 0 };
|
||||
}
|
||||
if(test_square.rect.getPosition().x > WIDTH - 30) {
|
||||
test_square.velocity = { -20, 0 };
|
||||
}
|
||||
}
|
||||
player.handle_movment();
|
||||
player.m_velocity.y += player.m_acceleration; // always apply gravity
|
||||
player.rect.move({0, player.m_velocity.y});
|
||||
player.velocity.y += player.m_acceleration;
|
||||
player.rect.move({0, player.velocity.y});
|
||||
|
||||
vel_y += 1.0f;
|
||||
rect.move({ 0, vel_y });
|
||||
test_square.velocity.y++;
|
||||
if(test_square.velocity.x > 0) {
|
||||
test_square.velocity.x--;
|
||||
}
|
||||
if(test_square.velocity.x < 0) {
|
||||
test_square.velocity.x++;
|
||||
}
|
||||
test_square.rect.move({ test_square.velocity.x, test_square.velocity.y });
|
||||
|
||||
float groundY = line.getPosition().y - floor_height - player.rect.getSize().x;
|
||||
float groundY = line.rect.getPosition().y - floor_height - player.rect.getSize().x;
|
||||
if (player.rect.getPosition().y >= groundY) {
|
||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
||||
player.m_velocity.y = 0;
|
||||
player.velocity.y = 0;
|
||||
player.m_jumping = false;
|
||||
}
|
||||
float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness();
|
||||
@@ -74,47 +101,44 @@ int main() {
|
||||
if(player.rect.getPosition().x < player.rect.getOutlineThickness()) {
|
||||
player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y});
|
||||
}
|
||||
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
|
||||
// vel_y = -20.0f;
|
||||
// // vel_y = -20.0f;
|
||||
// }
|
||||
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
|
||||
// if()
|
||||
|
||||
float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness();
|
||||
float groundY_rect = line.rect.getPosition().y - floor_height - (test_square.rect.getSize().y);
|
||||
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 });
|
||||
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);
|
||||
|
||||
float groundY_rect = line.getPosition().y - floor_height - (rect.getSize().y);
|
||||
if(rect.getPosition().x > right_border) {
|
||||
rect.setPosition({ right_border, rect.getPosition().y });
|
||||
switch (collision) {
|
||||
case CollisionDir::Top:
|
||||
test_square.velocity = { 0, -20 };
|
||||
break;
|
||||
case CollisionDir::Bottom:
|
||||
test_square.velocity = { 0, -20 };
|
||||
break;
|
||||
case CollisionDir::Left:
|
||||
test_square.velocity = { -20, 0 };
|
||||
break;
|
||||
case CollisionDir::Right:
|
||||
test_square.velocity = { 20, 0 };
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
if(rect.getPosition().x < rect.getPosition().x + rect.getOutlineThickness()) {
|
||||
rect.setPosition({ rect.getOutlineThickness(), rect.getPosition().y });
|
||||
|
||||
for(int i = 0; i < wall_list.size(); i++) {
|
||||
window.draw(wall_list[i].rect);
|
||||
}
|
||||
if(rect.getPosition().y >= groundY_rect) {
|
||||
rect.setPosition({ rect.getPosition().x, groundY_rect });
|
||||
vel_y = 0;
|
||||
}
|
||||
//
|
||||
// auto playerBounds = player.rect.getGlobalBounds();
|
||||
// auto rectBounds = rect.getGlobalBounds();
|
||||
//
|
||||
// // Create debug rectangles
|
||||
// sf::RectangleShape playerBox(playerBounds.size);
|
||||
// playerBox.setPosition(playerBounds.position);
|
||||
// playerBox.setFillColor(sf::Color::Transparent);
|
||||
// playerBox.setOutlineColor(sf::Color::Red);
|
||||
// playerBox.setOutlineThickness(1.0f);
|
||||
//
|
||||
// sf::RectangleShape rectBox(rectBounds.size);
|
||||
// rectBox.setPosition(rectBounds.position);
|
||||
// rectBox.setFillColor(sf::Color::Transparent);
|
||||
// rectBox.setOutlineColor(sf::Color::Red);
|
||||
// rectBox.setOutlineThickness(1.0f);
|
||||
//
|
||||
// window.draw(playerBox);
|
||||
// window.draw(rectBox);
|
||||
window.draw(rect);
|
||||
window.draw(player.rect);
|
||||
window.draw(line);
|
||||
window.draw(test_square.rect);
|
||||
window.draw(line.rect);
|
||||
window.display();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user