added walls
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
#ifndef GLOBALS_HPP
|
#ifndef GLOBALS_HPP
|
||||||
#define GLOBALS_HPP
|
#define GLOBALS_HPP
|
||||||
|
#define TILESIZE 32
|
||||||
#define WIDTH 608
|
#define WIDTH 608
|
||||||
#define HEIGHT 416
|
#define HEIGHT 416
|
||||||
|
#define MAX_COLS WIDTH / TILESIZE // 19 cols
|
||||||
|
#define MAX_ROWS HEIGHT / TILESIZE // 13 rows
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+1
-4
@@ -3,9 +3,6 @@
|
|||||||
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
|
||||||
const
|
extern int map_data[MAX_ROWS][MAX_COLS];
|
||||||
int map_data[WIDTH / 32][HEIGHT / 32];
|
|
||||||
|
|
||||||
// Add code here
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+68
-26
@@ -7,48 +7,59 @@
|
|||||||
#include <SFML/System/Angle.hpp>
|
#include <SFML/System/Angle.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
|
#include "maps.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::vector<Entity> wall_list = {};
|
// std::vector<Entity> wall_list = {};
|
||||||
|
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
||||||
struct {
|
struct {
|
||||||
sf::Vector2<float> size { 16.0f, 16.0f };
|
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
|
||||||
sf::Vector2<float> pos { 40, 40 };
|
sf::Vector2<float> pos { TILESIZE, TILESIZE };
|
||||||
float acceleration { 1.0f };
|
float acceleration { 1.0f };
|
||||||
float max_velocity_y { 20.0f };
|
float max_velocity_y { 20.0f };
|
||||||
float border_size { 1.0f };
|
float border_size { -1.0f };
|
||||||
sf::Vector2f velocity { 4.0f, 0 };
|
sf::Vector2f velocity { 4.0f, 0 };
|
||||||
} player_data;
|
} player_data;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
sf::Vector2f size { 32.0f, 32.0f };
|
sf::Vector2f size { TILESIZE, TILESIZE };
|
||||||
sf::Vector2f pos { 0, 0 };
|
sf::Vector2f pos { 0, 0 };
|
||||||
sf::Vector2f velocity { 0, 0 };
|
sf::Vector2f velocity { 0, 0 };
|
||||||
float border_size { 2.0f };
|
float border_size { -2.0f };
|
||||||
} tile_data;
|
} tile_data;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
sf::Vector2f size { WIDTH, 2.0f };
|
sf::Vector2f size { WIDTH, 2.0f };
|
||||||
sf::Vector2f pos { 0, HEIGHT - 50 };
|
sf::Vector2f pos { 0, HEIGHT - (TILESIZE * 1) };
|
||||||
sf::Vector2f velocity { 0, 0 };
|
sf::Vector2f velocity { 0, 0 };
|
||||||
float border_size { 1.0f };
|
float border_size { -1.0f };
|
||||||
} line_data;
|
} line_data;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
sf::Vector2f size { 8, 8 };
|
sf::Vector2f size { TILESIZE / 4.0f, TILESIZE / 4.0f };
|
||||||
sf::Vector2f pos { 100, 40 };
|
sf::Vector2f pos { TILESIZE * 3, TILESIZE };
|
||||||
sf::Vector2f velocity { 0, 0 };
|
sf::Vector2f velocity { 0, 0 };
|
||||||
float border_size { 1.0f };
|
float border_size { -1.0f };
|
||||||
} simulated_data;
|
} simulated_data;
|
||||||
|
|
||||||
for(int i = 0; i < (WIDTH / 32); i++) {
|
for(int i = 0; i < MAX_ROWS; i++) {
|
||||||
wall_list.push_back(Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black });
|
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++) {
|
for(int i = 0; i < MAX_ROWS; i++) {
|
||||||
wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size });
|
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 {
|
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 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);
|
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
@@ -72,6 +83,7 @@ int main() {
|
|||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FOR TESTING
|
// FOR TESTING
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
|
||||||
if(test_square.rect.getPosition().x < 30) {
|
if(test_square.rect.getPosition().x < 30) {
|
||||||
@@ -94,28 +106,28 @@ int main() {
|
|||||||
}
|
}
|
||||||
test_square.rect.move({ test_square.velocity.x, test_square.velocity.y });
|
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) {
|
if (player.rect.getPosition().y >= groundY) {
|
||||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
||||||
player.velocity.y = 0;
|
player.velocity.y = 0;
|
||||||
player.m_jumping = false;
|
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) {
|
if(player.rect.getPosition().x > right_border) {
|
||||||
player.rect.setPosition({ right_border, player.rect.getPosition().y});
|
player.rect.setPosition({ right_border, player.rect.getPosition().y});
|
||||||
}
|
}
|
||||||
if(player.rect.getPosition().x < player.rect.getOutlineThickness()) {
|
if(player.rect.getPosition().x < std::abs(player.rect.getOutlineThickness())) {
|
||||||
player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y});
|
player.rect.setPosition({ 0, player.rect.getPosition().y});
|
||||||
}
|
}
|
||||||
|
|
||||||
float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness();
|
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);
|
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) {
|
if(test_square.rect.getPosition().x > right_border_rect) {
|
||||||
test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y });
|
test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y });
|
||||||
test_square.velocity = { -20, 0 };
|
test_square.velocity = { -20, 0 };
|
||||||
}
|
}
|
||||||
if(test_square.rect.getPosition().x < test_square.rect.getOutlineThickness()) {
|
if(test_square.rect.getPosition().x < std::abs(test_square.rect.getOutlineThickness())) {
|
||||||
test_square.rect.setPosition({ test_square.rect.getOutlineThickness(), test_square.rect.getPosition().y });
|
test_square.rect.setPosition({ 0, test_square.rect.getPosition().y });
|
||||||
test_square.velocity = { 20, 0 };
|
test_square.velocity = { 20, 0 };
|
||||||
}
|
}
|
||||||
if(test_square.rect.getPosition().y >= groundY_rect) {
|
if(test_square.rect.getPosition().y >= groundY_rect) {
|
||||||
@@ -138,13 +150,43 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
default: 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++) {
|
for(int i = 0; i < MAX_ROWS; i++) {
|
||||||
window.draw(wall_list[i].rect);
|
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(player.rect);
|
||||||
window.draw(test_square.rect);
|
window.draw(test_square.rect);
|
||||||
window.draw(line.rect);
|
window.draw(line.rect);
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
|
for(int i = 0; i < MAX_ROWS; i++) {
|
||||||
|
for(int j = 0; j < MAX_COLS; j++) {
|
||||||
|
delete wall_list[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-1
@@ -1,3 +1,17 @@
|
|||||||
#include "maps.hpp"
|
#include "maps.hpp"
|
||||||
|
|
||||||
int map_data[] = [ [ 1, 0, 1, 1, 1, 0, 0] ];
|
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, 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, 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, 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, 1, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user