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
|
||||
sf::Vector2f windowCenterStart = sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2);
|
||||
// y on map scetch
|
||||
sf::Vector2f windowCenterEnd = sf::Vector2f(map->width * map->tilewidth - windowCenterStart.x,
|
||||
map->height * map->tileheight - windowCenterStart.y);
|
||||
sf::Vector2f windowCenterEnd = sf::Vector2f(map->width * map->tileWidth - windowCenterStart.x,
|
||||
map->height * map->tileHeight - windowCenterStart.y);
|
||||
|
||||
// If Player is in the center (4)
|
||||
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
+88
-7
@@ -18,22 +18,103 @@ Map::Map() {
|
||||
this->tileSet = new TileSet();
|
||||
}
|
||||
|
||||
void Map::RenderGround(sf::RenderWindow* window) {
|
||||
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) {
|
||||
Layer* layer = this->ground1;
|
||||
this->Render(window, layer);
|
||||
}
|
||||
|
||||
void Map::RenderAbove(sf::RenderWindow* window) {
|
||||
void Map::RenderAbove(sf::RenderWindow *window) {
|
||||
Layer* layer = this->above1;
|
||||
this->Render(window, layer);
|
||||
}
|
||||
|
||||
void Map::Render(sf::RenderWindow* window, Layer *layer) {
|
||||
void Map::Render(sf::RenderWindow *window, Layer *layer) {
|
||||
// Get render section
|
||||
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().y + window->getSize().y / 2) / this->tileheight);
|
||||
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().y + window->getSize().y / 2) / this->tileHeight
|
||||
);
|
||||
|
||||
// Render map
|
||||
int index;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <vector>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
@@ -53,12 +55,11 @@ class Map
|
||||
{
|
||||
public:
|
||||
Map();
|
||||
void RenderGround(sf::RenderWindow* window);
|
||||
void RenderAbove(sf::RenderWindow* window);
|
||||
void CheckCollision(sf::RenderWindow *window, Entity *entity);
|
||||
void RenderGround(sf::RenderWindow *window);
|
||||
void RenderAbove(sf::RenderWindow *window);
|
||||
~Map();
|
||||
|
||||
|
||||
// Global
|
||||
int height;
|
||||
Layer* ground1;
|
||||
Layer* above1;
|
||||
@@ -70,14 +71,14 @@ public:
|
||||
|
||||
// },
|
||||
// "renderorder":"right-down",
|
||||
int tileheight;
|
||||
int tileHeight;
|
||||
TileSet* tileSet;
|
||||
int tilewidth;
|
||||
int tileWidth;
|
||||
// "version":1,
|
||||
int width;
|
||||
|
||||
private:
|
||||
void Render(sf::RenderWindow* window, Layer *layer);
|
||||
void Render(sf::RenderWindow *window, Layer *layer);
|
||||
};
|
||||
|
||||
//sf::Sprite* sprite;
|
||||
|
||||
+2
-2
@@ -125,9 +125,9 @@ void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) {
|
||||
// MAP
|
||||
map->height = document["layers"][0]["height"].GetInt();
|
||||
// Layer
|
||||
map->tileheight = document["tileheight"].GetInt();
|
||||
map->tileHeight = document["tileheight"].GetInt();
|
||||
// TileSet
|
||||
map->tilewidth = document["tilewidth"].GetInt();
|
||||
map->tileWidth = document["tilewidth"].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;
|
||||
}
|
||||
|
||||
void Player::Update(sf::RenderWindow* window, InputManager inputManager, int elapsedTime) {
|
||||
float speed = this->speed * elapsedTime;
|
||||
void Player::Update(sf::RenderWindow* window, InputManager inputManager, int timeElapsed) {
|
||||
float speed = this->speed * timeElapsed;
|
||||
// Update player velocity
|
||||
this->velocity.x = inputManager.IsPressed(InputManager::Right) * 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()
|
||||
{
|
||||
|
||||
|
||||
+2
-1
@@ -13,10 +13,11 @@ class Player : public Entity
|
||||
{
|
||||
public:
|
||||
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();
|
||||
int GetHealth();
|
||||
int GetMaxHealth();
|
||||
float GetSpeed();
|
||||
~Player();
|
||||
|
||||
private:
|
||||
|
||||
@@ -34,6 +34,7 @@ void MainGame::Update(sf::RenderWindow* window) {
|
||||
|
||||
this->player->Update(window, inputManager, timeElapsed);
|
||||
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));
|
||||
|
||||
if(inputManager.IsPressed(InputManager::LoadMap)) {
|
||||
|
||||
Reference in New Issue
Block a user