Change how camera works.
This commit is contained in:
parent
a1d05dcf27
commit
7f5984b229
26
Camera.hpp
26
Camera.hpp
|
@ -13,38 +13,32 @@
|
|||
#ifndef CAMERA_HPP
|
||||
#define CAMERA_HPP
|
||||
|
||||
#include <SDL2/SDL.h> // SDL_Rect
|
||||
#include "Game.hpp" // Game::...
|
||||
#include "TileMap.hpp" // TileMap::...
|
||||
#include <SDL2/SDL.h> // SDL_Rect
|
||||
#include "Game.hpp" // Game::...
|
||||
#include "TileMap.hpp" // TileMap::...
|
||||
#include "Vector2D.hpp" // Vector2D class
|
||||
|
||||
struct Camera {
|
||||
Camera(int xpos, int ypos, int width, int height)
|
||||
Camera(int width, int height)
|
||||
{
|
||||
camR.x = xpos;
|
||||
camR.y = ypos;
|
||||
camR.w = width;
|
||||
camR.h = height;
|
||||
}
|
||||
|
||||
Camera(int xpos, int ypos, int width, int height, int z) : zoom(z)
|
||||
{
|
||||
camR.x = xpos;
|
||||
camR.y = ypos;
|
||||
camR.w = width;
|
||||
camR.h = height;
|
||||
}
|
||||
|
||||
// Verifie si la caméra ne dépasse pas de la map
|
||||
// Update camera
|
||||
void update()
|
||||
{
|
||||
camR.x = static_cast<int>(centerPos.x - (static_cast<float>(camR.w) / 2.0f));
|
||||
camR.y = static_cast<int>(centerPos.y - (static_cast<float>(camR.h) / 2.0f));
|
||||
|
||||
if(camR.x < 0) camR.x = 0;
|
||||
if(camR.y < 0) camR.y = 0;
|
||||
if(camR.x > Game::tileMap.worldWidth - camR.w) camR.x = Game::tileMap.worldWidth - camR.w;
|
||||
if(camR.y > Game::tileMap.worldHeight - camR.h) camR.y = Game::tileMap.worldHeight - camR.h;
|
||||
}
|
||||
|
||||
Vector2D<float> centerPos;
|
||||
SDL_Rect camR;
|
||||
int zoom; // TODO Zoom pas encore pris en charge
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -143,6 +143,8 @@ class PlayerSystem : public Component {
|
|||
Game::camera.camR.x = static_cast<int>(m_transform->position.x - static_cast<float>((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) / 2)));
|
||||
Game::camera.camR.y = static_cast<int>(m_transform->position.y - static_cast<float>((Game::camera.camR.h >> 1) - ((m_transform->dimension.y * m_transform->scale) / 2)));
|
||||
|
||||
Game::camera.centerPos.x = static_cast<int>(m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 1));
|
||||
Game::camera.centerPos.y = static_cast<int>(m_transform->position.y + static_cast<float>((m_transform->dimension.y * m_transform->scale) >> 1));
|
||||
playerState = State::None;
|
||||
break;
|
||||
}
|
||||
|
@ -202,6 +204,10 @@ class PlayerSystem : public Component {
|
|||
|
||||
void m_setCamera()
|
||||
{
|
||||
Vector2D<float> target;
|
||||
|
||||
target.x = m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 2);
|
||||
target.y = m_transform->position.y + static_cast<float>((m_transform->dimension.y * m_transform->scale) >> 2);
|
||||
float targetX = m_transform->position.x - static_cast<float>((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) >> 2));
|
||||
float targetY = m_transform->position.y - static_cast<float>((Game::camera.camR.h >> 1) - ((m_transform->dimension.y * m_transform->scale) >> 2));
|
||||
|
||||
|
@ -209,6 +215,8 @@ class PlayerSystem : public Component {
|
|||
|
||||
if(playerState == State::Running) smoothingFactor = 0.12f;
|
||||
|
||||
Game::camera.centerPos.x = static_cast<int>(lerp(Game::camera.centerPos.x, target.x, smoothingFactor));
|
||||
Game::camera.centerPos.y = static_cast<int>(lerp(Game::camera.centerPos.y, target.y, smoothingFactor));
|
||||
Game::camera.camR.x = static_cast<int>(lerp(Game::camera.camR.x, targetX, smoothingFactor));
|
||||
Game::camera.camR.y = static_cast<int>(lerp(Game::camera.camR.y, targetY, smoothingFactor));
|
||||
}
|
||||
|
|
10
Game.cpp
10
Game.cpp
|
@ -27,7 +27,7 @@ SDL_Renderer *Game::renderer;
|
|||
SDL_Texture *Game::textureRenderer;
|
||||
Events Game::events;
|
||||
TextureManager Game::textureManager;
|
||||
Camera Game::camera {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
|
||||
Camera Game::camera {WINDOW_WIDTH, WINDOW_HEIGHT};
|
||||
TileMap Game::tileMap;
|
||||
MapManager Game::mapManager;
|
||||
Manager Game::entityManager;
|
||||
|
@ -129,12 +129,8 @@ bool Game::Init()
|
|||
player.addComponent<AnimationSystem>();
|
||||
player.addComponent<PlayerSystem>();
|
||||
|
||||
camera.camR.x = static_cast<int>(p_transform->position.x - static_cast<float>(camera.camR.w / 2 - p_transform->dimension.x * p_transform->scale / 2));
|
||||
camera.camR.y = static_cast<int>(p_transform->position.y - static_cast<float>(camera.camR.h / 2 - p_transform->dimension.y * p_transform->scale / 2));
|
||||
|
||||
Item &item(p_inventory->addItem());
|
||||
|
||||
item.addProperty<NameProperty>("Prout");
|
||||
camera.centerPos.x = static_cast<int>(p_transform->position.x - static_cast<float>(p_transform->dimension.x) * static_cast<float>(p_transform->scale) / 2.0f);
|
||||
camera.centerPos.y = static_cast<int>(p_transform->position.y - static_cast<float>(p_transform->dimension.y) * static_cast<float>(p_transform->scale) / 2.0f);
|
||||
|
||||
m_isRunning = true;
|
||||
|
||||
|
|
Loading…
Reference in New Issue