Only render entities on screen
This commit is contained in:
@@ -92,6 +92,17 @@ void Camera::Update(sf::RenderWindow *window, Map *map, sf::Vector2f position) {
|
||||
}
|
||||
}
|
||||
|
||||
bool Camera::IsOnScreen(sf::RenderWindow *window, Entity *entity) {
|
||||
if(entity->getPosition().x + entity->getGlobalBounds().width / 2 > this->view.getCenter().x - window->getSize().x / 2
|
||||
&& entity->getPosition().x - entity->getGlobalBounds().width / 2 < this->view.getCenter().x + window->getSize().x / 2
|
||||
&& entity->getPosition().y + entity->getGlobalBounds().height / 2 > this->view.getCenter().y - window->getSize().y / 2
|
||||
&& entity->getPosition().y - entity->getGlobalBounds().height / 2 < this->view.getCenter().y + window->getSize().y / 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
{
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ public:
|
||||
void MoveCamera(sf::RenderWindow *window, sf::Vector2f move);
|
||||
void SetCenter(sf::RenderWindow *window, sf::Vector2f position);
|
||||
void Update(sf::RenderWindow *window, Map *map, sf::Vector2f position);
|
||||
bool IsOnScreen(sf::RenderWindow *window, Entity *entity);
|
||||
~Camera();
|
||||
|
||||
private:
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.9 KiB |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -59,12 +59,12 @@ void EntityManager::Update() {
|
||||
}
|
||||
|
||||
|
||||
void EntityManager::Render(sf::RenderWindow* window) {
|
||||
for (auto& iterator : this->entities) {
|
||||
if (iterator.second != NULL) {
|
||||
if (iterator.second->Active()) {
|
||||
window->draw(*iterator.second);
|
||||
}
|
||||
void EntityManager::Render(sf::RenderWindow* window, Camera *camera) {
|
||||
for(auto& iterator : this->entities) {
|
||||
if(iterator.second != NULL
|
||||
&& iterator.second->Active()
|
||||
&& camera->IsOnScreen(window, iterator.second)) {
|
||||
window->draw(*iterator.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "camera.h"
|
||||
#include "entity.h"
|
||||
|
||||
typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB);
|
||||
@@ -13,10 +14,10 @@ class EntityManager
|
||||
public:
|
||||
EntityManager();
|
||||
void SetCollisionMethod(CollisionUpdateEvent collisionsEvent);
|
||||
void AddEntity(std::string name, Entity* entity);
|
||||
void AddEntity(std::string name, Entity *entity);
|
||||
Entity* Get(std::string name);
|
||||
void Update();
|
||||
void Render(sf::RenderWindow* window);
|
||||
void Render(sf::RenderWindow *window, Camera *camera);
|
||||
~EntityManager();
|
||||
|
||||
private:
|
||||
|
||||
@@ -59,7 +59,6 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
sf::RenderWindow* window;
|
||||
GameState* state;
|
||||
|
||||
+2
-5
@@ -1,5 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "state/maingame.h"
|
||||
|
||||
@@ -29,12 +28,10 @@ int main()
|
||||
sf::Time timeElapsed;
|
||||
|
||||
// Run the program as long as the window is open
|
||||
while (window.isOpen())
|
||||
{
|
||||
while (window.isOpen()) {
|
||||
// Check all the window's events that were triggered since the last iteration of the loop
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
while (window.pollEvent(event)) {
|
||||
// Close window : exit
|
||||
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||
window.close();
|
||||
|
||||
+52
-27
@@ -35,63 +35,85 @@ void Map::CheckCollision(Entity *entity) {
|
||||
|
||||
// Check collision
|
||||
int index;
|
||||
sf::Sprite tile;
|
||||
bool collided = false;
|
||||
sf::Vector2i collidedTile;
|
||||
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))) {
|
||||
tile.setTexture(*this->tileSet->tile[index]);
|
||||
tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight));
|
||||
// Rotate oblique collisions
|
||||
// if(i == 1) {
|
||||
// switch(x) {
|
||||
// case 3:
|
||||
// break;
|
||||
|
||||
// case 4:
|
||||
// break;
|
||||
|
||||
// case 5:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
if (tile.getGlobalBounds().intersects(entity->getGlobalBounds())) {
|
||||
collided = true;
|
||||
collidedTile.x = x;
|
||||
collidedTile.y = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move back if entity collided)
|
||||
// Move back if entity collided
|
||||
if(collided) {
|
||||
// Get Entities center x, y position;
|
||||
sf::Vector2f entityCenter = sf::Vector2f(
|
||||
(int)((entity->getPosition().x + entity->getGlobalBounds().width / 2) / this->tileSet->tileWidth),
|
||||
(int)((entity->getPosition().y + entity->getGlobalBounds().height / 2) / this->tileSet->tileHeight)
|
||||
);
|
||||
|
||||
// DOWN_RIGHT
|
||||
if(entity->velocity.x > 0
|
||||
&& entity->velocity.y > 0) {
|
||||
entity->velocity.x -= entity->velocity.x * 2;
|
||||
entity->velocity.y = 0;
|
||||
if(collidedTile.x > entityCenter.x + 1
|
||||
&& collidedTile.y > entityCenter.y + 1) {
|
||||
|
||||
}
|
||||
// UP_RIGHT
|
||||
else if(entity->velocity.x > 0
|
||||
&& entity->velocity.y < 0) {
|
||||
entity->velocity.x = 0;
|
||||
entity->velocity.y -= entity->velocity.y * 2;
|
||||
else if(collidedTile.x > entityCenter.x + 1
|
||||
&& collidedTile.y < entityCenter.y - 1) {
|
||||
|
||||
}
|
||||
// UP_LEFT
|
||||
else if(entity->velocity.x < 0
|
||||
&& entity->velocity.y < 0) {
|
||||
entity->velocity.x = 0;
|
||||
entity->velocity.y -= entity->velocity.y * 2;
|
||||
else if(collidedTile.x < entityCenter.x - 1
|
||||
&& collidedTile.y < entityCenter.y - 1) {
|
||||
|
||||
}
|
||||
// DOWN_LEFT
|
||||
else if(entity->velocity.x < 0
|
||||
&& entity->velocity.y > 0) {
|
||||
entity->velocity.x -= entity->velocity.x * 2;
|
||||
entity->velocity.y = 0;
|
||||
else if(collidedTile.x < entityCenter.x - 1
|
||||
&& collidedTile.y > entityCenter.y + 1) {
|
||||
}
|
||||
|
||||
// RIGHT
|
||||
else if(entity->velocity.x > 0) {
|
||||
if(collidedTile.x > entityCenter.x
|
||||
&& collidedTile.y == entityCenter.y) {
|
||||
entity->velocity.x -= entity->velocity.x * 2;
|
||||
}
|
||||
// LEFT
|
||||
else if(entity->velocity.x < 0) {
|
||||
entity->velocity.x -= entity->velocity.x * 2;
|
||||
if(collidedTile.x < entityCenter.x
|
||||
&& collidedTile.y == entityCenter.y) {
|
||||
|
||||
}
|
||||
// DOWN
|
||||
else if(entity->velocity.y > 0) {
|
||||
entity->velocity.y -= entity->velocity.y * 2;
|
||||
if(collidedTile.x == entityCenter.x
|
||||
&& collidedTile.y > entityCenter.y) {
|
||||
|
||||
}
|
||||
// UP
|
||||
else if(entity->velocity.y < 0) {
|
||||
if(collidedTile.x == entityCenter.x
|
||||
&& collidedTile.y < entityCenter.y) {
|
||||
entity->velocity.y -= entity->velocity.y * 2;
|
||||
}
|
||||
|
||||
@@ -119,6 +141,9 @@ void Map::RenderAbove(sf::RenderWindow *window) {
|
||||
|
||||
layer = this->above3;
|
||||
this->Render(window, layer);
|
||||
|
||||
// layer = this->collision;
|
||||
// this->Render(window, layer);
|
||||
}
|
||||
|
||||
void Map::Render(sf::RenderWindow *window, Layer *layer) {
|
||||
|
||||
@@ -60,6 +60,19 @@ public:
|
||||
void RenderAbove(sf::RenderWindow *window);
|
||||
~Map();
|
||||
|
||||
enum Direction
|
||||
{
|
||||
NONE = -1,
|
||||
LEFT = 0,
|
||||
RIGHT = 1,
|
||||
UP = 2,
|
||||
DOWN = 3,
|
||||
TOP_LEFT = 4,
|
||||
TOP_RIGHT = 5,
|
||||
BOTTOM_LEFT = 6,
|
||||
BOTTOM_RIGHT = 7
|
||||
};
|
||||
|
||||
int height;
|
||||
Layer* ground1;
|
||||
Layer* ground2;
|
||||
|
||||
+24
-17
@@ -47,7 +47,7 @@ void MapLoad::Parser(std::string *content, rapidjson::Document *document) {
|
||||
void MapLoad::LoadLayer(const rapidjson::Document& document, Map *map) {
|
||||
int loopCount = document["layers"].Size();
|
||||
Layer* current;
|
||||
for(unsigned int i = 0; i < loopCount; i++) {
|
||||
for(int i = 0; i < loopCount; i++) {
|
||||
current = NULL;
|
||||
if(document["layers"][i]["properties"]["name"] == "Ground1") {
|
||||
current = map->ground1;
|
||||
@@ -108,27 +108,34 @@ void MapLoad::LoadTileSet(const rapidjson::Document& document, Map *map) {
|
||||
map->tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt();
|
||||
map->tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt();
|
||||
|
||||
int height = map->tileSet->imageHeight / map->tileSet->tileHeight;
|
||||
int width = map->tileSet->imageWidth / map->tileSet->tileWidth;
|
||||
int height;
|
||||
int width;
|
||||
|
||||
// Add NULL texture to fill position '0'
|
||||
sf::Texture* texture = new sf::Texture();
|
||||
map->tileSet->tile.push_back(texture);
|
||||
|
||||
// Load map tilesheet
|
||||
std::string tileSheetLocation = map->tileSet->image;
|
||||
tileSheetLocation.replace(0, 2, "data");
|
||||
sf::Image tileSheet;
|
||||
tileSheet.loadFromFile(tileSheetLocation);
|
||||
tileSheet.createMaskFromColor(sf::Color::White);
|
||||
for(int y = 0; y < height; y++) {
|
||||
for(int x = 0; x < width; x++) {
|
||||
sf::Texture* texture = new sf::Texture();
|
||||
texture->loadFromImage(tileSheet, sf::IntRect(x * map->tileSet->tileWidth,
|
||||
y * map->tileSet->tileHeight,
|
||||
map->tileSet->tileWidth,
|
||||
map->tileSet->tileHeight));
|
||||
map->tileSet->tile.push_back(texture);
|
||||
int loopCount = document["tilesets"].Size();
|
||||
for(int i = 0; i < loopCount; i++) {
|
||||
|
||||
height = document["tilesets"][i]["imageheight"].GetInt() / document["tilesets"][i]["tileheight"].GetInt();
|
||||
width = document["tilesets"][i]["imagewidth"].GetInt() / document["tilesets"][i]["tilewidth"].GetInt();
|
||||
|
||||
// Load map tilesheet
|
||||
std::string tileSheetLocation = document["tilesets"][i]["image"].GetString();
|
||||
tileSheetLocation.replace(0, 2, "data");
|
||||
sf::Image tileSheet;
|
||||
tileSheet.loadFromFile(tileSheetLocation);
|
||||
tileSheet.createMaskFromColor(sf::Color::White);
|
||||
for(int y = 0; y < height; y++) {
|
||||
for(int x = 0; x < width; x++) {
|
||||
sf::Texture* texture = new sf::Texture();
|
||||
texture->loadFromImage(tileSheet, sf::IntRect(x * document["tilesets"][i]["tilewidth"].GetInt(),
|
||||
y * document["tilesets"][i]["tileheight"].GetInt(),
|
||||
document["tilesets"][i]["tilewidth"].GetInt(),
|
||||
document["tilesets"][i]["tileheight"].GetInt()));
|
||||
map->tileSet->tile.push_back(texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,15 +37,21 @@ void MainGame::Update(sf::RenderWindow* window) {
|
||||
this->map->CheckCollision(this->player);
|
||||
this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y));
|
||||
|
||||
if(inputManager.IsPressed(InputManager::LoadMap)) {
|
||||
// Reloading of the map
|
||||
if(inputManager.IsPressed(InputManager::LoadMap)
|
||||
&& inputManager.IsKeyBoardPressed(sf::Keyboard::LControl)) {
|
||||
std::cout << "Loading Map..." << std::endl;
|
||||
MapLoad(this->map, "data/map/level1.json", 0);
|
||||
}
|
||||
else if(inputManager.IsPressed(InputManager::LoadMap)) {
|
||||
std::cout << "Updating Map..." << std::endl;
|
||||
MapLoad(this->map, "data/map/level1.json", 1);
|
||||
}
|
||||
}
|
||||
|
||||
void MainGame::Render(sf::RenderWindow* window) {
|
||||
this->map->RenderGround(window);
|
||||
this->entityManager->Render(window);
|
||||
this->entityManager->Render(window, this->camera);
|
||||
this->map->RenderAbove(window);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user