First attempt at Map collision*
*Unfinished
This commit is contained in:
+2
-2
@@ -32,8 +32,8 @@ void Camera::Update(sf::RenderWindow *window, Map *map, sf::Vector2f position) {
|
|||||||
// x on map scetch
|
// x on map scetch
|
||||||
sf::Vector2f windowCenterStart = sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2);
|
sf::Vector2f windowCenterStart = sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2);
|
||||||
// y on map scetch
|
// y on map scetch
|
||||||
sf::Vector2f windowCenterEnd = sf::Vector2f(map->width * map->tilewidth - windowCenterStart.x,
|
sf::Vector2f windowCenterEnd = sf::Vector2f(map->width * map->tileWidth - windowCenterStart.x,
|
||||||
map->height * map->tileheight - windowCenterStart.y);
|
map->height * map->tileHeight - windowCenterStart.y);
|
||||||
|
|
||||||
// If Player is in the center (4)
|
// If Player is in the center (4)
|
||||||
if(position.x > windowCenterStart.x
|
if(position.x > windowCenterStart.x
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+85
-4
@@ -18,6 +18,83 @@ Map::Map() {
|
|||||||
this->tileSet = new TileSet();
|
this->tileSet = new TileSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Map::CheckCollision(sf::RenderWindow *window, Entity *entity) {
|
||||||
|
// Get check section
|
||||||
|
sf::Vector2f checkWidth = sf::Vector2f(
|
||||||
|
(int)entity->getPosition().x / this->tileSet->tileWidth,
|
||||||
|
(int)entity->getPosition().x / this->tileSet->tileWidth + 3
|
||||||
|
);
|
||||||
|
sf::Vector2f checkHeight = sf::Vector2f(
|
||||||
|
(int)entity->getPosition().y / this->tileSet->tileWidth,
|
||||||
|
(int)entity->getPosition().y / this->tileSet->tileWidth + 3
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check collision
|
||||||
|
int index;
|
||||||
|
bool collided = false;
|
||||||
|
for (int y = checkHeight.x; y < checkHeight.y; y++) {
|
||||||
|
for (int x = checkWidth.x; x < checkWidth.y; x++) {
|
||||||
|
index = this->collision->data[y][x];
|
||||||
|
// If collision tile
|
||||||
|
if(index != 0) {
|
||||||
|
if (entity->getGlobalBounds().intersects(sf::FloatRect(x * this->tileSet->tileWidth,
|
||||||
|
y * this->tileSet->tileHeight,
|
||||||
|
this->tileSet->tileWidth,
|
||||||
|
this->tileSet->tileHeight))) {
|
||||||
|
collided = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move back if entity collided)
|
||||||
|
if(collided) {
|
||||||
|
// DOWN_RIGHT
|
||||||
|
if(entity->velocity.x > 0
|
||||||
|
&& entity->velocity.y > 0) {
|
||||||
|
entity->velocity.x -= entity->velocity.x * 2;
|
||||||
|
entity->velocity.y = 0;
|
||||||
|
}
|
||||||
|
// UP_RIGHT
|
||||||
|
else if(entity->velocity.x > 0
|
||||||
|
&& entity->velocity.y < 0) {
|
||||||
|
entity->velocity.x = 0;
|
||||||
|
entity->velocity.y -= entity->velocity.y * 2;
|
||||||
|
}
|
||||||
|
// UP_LEFT
|
||||||
|
else if(entity->velocity.x < 0
|
||||||
|
&& entity->velocity.y < 0) {
|
||||||
|
entity->velocity.x = 0;
|
||||||
|
entity->velocity.y -= entity->velocity.y * 2;
|
||||||
|
}
|
||||||
|
// DOWN_LEFT
|
||||||
|
else if(entity->velocity.x < 0
|
||||||
|
&& entity->velocity.y > 0) {
|
||||||
|
entity->velocity.x -= entity->velocity.x * 2;
|
||||||
|
entity->velocity.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RIGHT
|
||||||
|
else if(entity->velocity.x > 0) {
|
||||||
|
entity->velocity.x -= entity->velocity.x * 2;
|
||||||
|
}
|
||||||
|
// LEFT
|
||||||
|
else if(entity->velocity.x < 0) {
|
||||||
|
entity->velocity.x -= entity->velocity.x * 2;
|
||||||
|
}
|
||||||
|
// DOWN
|
||||||
|
else if(entity->velocity.y > 0) {
|
||||||
|
entity->velocity.y -= entity->velocity.y * 2;
|
||||||
|
}
|
||||||
|
// UP
|
||||||
|
else if(entity->velocity.y < 0) {
|
||||||
|
entity->velocity.y -= entity->velocity.y * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity->move(entity->velocity.x, entity->velocity.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Map::RenderGround(sf::RenderWindow *window) {
|
void Map::RenderGround(sf::RenderWindow *window) {
|
||||||
Layer* layer = this->ground1;
|
Layer* layer = this->ground1;
|
||||||
this->Render(window, layer);
|
this->Render(window, layer);
|
||||||
@@ -30,10 +107,14 @@ void Map::RenderAbove(sf::RenderWindow* window) {
|
|||||||
|
|
||||||
void Map::Render(sf::RenderWindow *window, Layer *layer) {
|
void Map::Render(sf::RenderWindow *window, Layer *layer) {
|
||||||
// Get render section
|
// Get render section
|
||||||
sf::Vector2f renderWidth = sf::Vector2f((window->getView().getCenter().x - window->getSize().x / 2) / this->tilewidth,
|
sf::Vector2f renderWidth = sf::Vector2f(
|
||||||
(window->getView().getCenter().x + window->getSize().x / 2) / this->tilewidth);
|
(window->getView().getCenter().x - window->getSize().x / 2) / this->tileWidth,
|
||||||
sf::Vector2f renderHeight = sf::Vector2f((window->getView().getCenter().y - window->getSize().y / 2) / this->tileheight,
|
(window->getView().getCenter().x + window->getSize().x / 2) / this->tileWidth
|
||||||
(window->getView().getCenter().y + window->getSize().y / 2) / this->tileheight);
|
);
|
||||||
|
sf::Vector2f renderHeight = sf::Vector2f(
|
||||||
|
(window->getView().getCenter().y - window->getSize().y / 2) / this->tileHeight,
|
||||||
|
(window->getView().getCenter().y + window->getSize().y / 2) / this->tileHeight
|
||||||
|
);
|
||||||
|
|
||||||
// Render map
|
// Render map
|
||||||
int index;
|
int index;
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#include "entity.h"
|
||||||
|
|
||||||
class Layer
|
class Layer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -53,12 +55,11 @@ class Map
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Map();
|
Map();
|
||||||
|
void CheckCollision(sf::RenderWindow *window, Entity *entity);
|
||||||
void RenderGround(sf::RenderWindow *window);
|
void RenderGround(sf::RenderWindow *window);
|
||||||
void RenderAbove(sf::RenderWindow *window);
|
void RenderAbove(sf::RenderWindow *window);
|
||||||
~Map();
|
~Map();
|
||||||
|
|
||||||
|
|
||||||
// Global
|
|
||||||
int height;
|
int height;
|
||||||
Layer* ground1;
|
Layer* ground1;
|
||||||
Layer* above1;
|
Layer* above1;
|
||||||
@@ -70,9 +71,9 @@ public:
|
|||||||
|
|
||||||
// },
|
// },
|
||||||
// "renderorder":"right-down",
|
// "renderorder":"right-down",
|
||||||
int tileheight;
|
int tileHeight;
|
||||||
TileSet* tileSet;
|
TileSet* tileSet;
|
||||||
int tilewidth;
|
int tileWidth;
|
||||||
// "version":1,
|
// "version":1,
|
||||||
int width;
|
int width;
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -125,9 +125,9 @@ void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) {
|
|||||||
// MAP
|
// MAP
|
||||||
map->height = document["layers"][0]["height"].GetInt();
|
map->height = document["layers"][0]["height"].GetInt();
|
||||||
// Layer
|
// Layer
|
||||||
map->tileheight = document["tileheight"].GetInt();
|
map->tileHeight = document["tileheight"].GetInt();
|
||||||
// TileSet
|
// TileSet
|
||||||
map->tilewidth = document["tilewidth"].GetInt();
|
map->tileWidth = document["tilewidth"].GetInt();
|
||||||
map->width = document["layers"][0]["width"].GetInt();
|
map->width = document["layers"][0]["width"].GetInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-2
@@ -10,8 +10,8 @@ Player::Player(EntityManager* entityManager, Map *map, Camera *camera, float x,
|
|||||||
this->speed = 0.00015f;
|
this->speed = 0.00015f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::Update(sf::RenderWindow* window, InputManager inputManager, int elapsedTime) {
|
void Player::Update(sf::RenderWindow* window, InputManager inputManager, int timeElapsed) {
|
||||||
float speed = this->speed * elapsedTime;
|
float speed = this->speed * timeElapsed;
|
||||||
// Update player velocity
|
// Update player velocity
|
||||||
this->velocity.x = inputManager.IsPressed(InputManager::Right) * speed -
|
this->velocity.x = inputManager.IsPressed(InputManager::Right) * speed -
|
||||||
inputManager.IsPressed(InputManager::Left) * speed;
|
inputManager.IsPressed(InputManager::Left) * speed;
|
||||||
@@ -26,6 +26,10 @@ void Player::Update(sf::RenderWindow* window, InputManager inputManager, int ela
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float Player::GetSpeed() {
|
||||||
|
return this->speed;
|
||||||
|
}
|
||||||
|
|
||||||
Player::~Player()
|
Player::~Player()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -13,10 +13,11 @@ class Player : public Entity
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Player(EntityManager* entityManager, Map* map, Camera* camera, float x = 0, float y = 0);
|
Player(EntityManager* entityManager, Map* map, Camera* camera, float x = 0, float y = 0);
|
||||||
void Update(sf::RenderWindow *window, InputManager inputManager, int elapsedTime);
|
void Update(sf::RenderWindow *window, InputManager inputManager, int timeElapsed);
|
||||||
float SetSpeed();
|
float SetSpeed();
|
||||||
int GetHealth();
|
int GetHealth();
|
||||||
int GetMaxHealth();
|
int GetMaxHealth();
|
||||||
|
float GetSpeed();
|
||||||
~Player();
|
~Player();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ void MainGame::Update(sf::RenderWindow* window) {
|
|||||||
|
|
||||||
this->player->Update(window, inputManager, timeElapsed);
|
this->player->Update(window, inputManager, timeElapsed);
|
||||||
this->entityManager->Update();
|
this->entityManager->Update();
|
||||||
|
this->map->CheckCollision(window, this->player);
|
||||||
this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y));
|
this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y));
|
||||||
|
|
||||||
if(inputManager.IsPressed(InputManager::LoadMap)) {
|
if(inputManager.IsPressed(InputManager::LoadMap)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user