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