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 | #ifndef CAMERA_HPP | ||||||
| #define CAMERA_HPP | #define CAMERA_HPP | ||||||
| 
 | 
 | ||||||
| #include <SDL2/SDL.h>  // SDL_Rect
 | #include <SDL2/SDL.h>   // SDL_Rect
 | ||||||
| #include "Game.hpp"    // Game::...
 | #include "Game.hpp"     // Game::...
 | ||||||
| #include "TileMap.hpp" // TileMap::...
 | #include "TileMap.hpp"  // TileMap::...
 | ||||||
|  | #include "Vector2D.hpp" // Vector2D class
 | ||||||
| 
 | 
 | ||||||
| struct Camera { | 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.w = width; | ||||||
|         camR.h = height; |         camR.h = height; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     Camera(int xpos, int ypos, int width, int height, int z) : zoom(z) |     // Update camera
 | ||||||
|     { |  | ||||||
|         camR.x = xpos; |  | ||||||
|         camR.y = ypos; |  | ||||||
|         camR.w = width; |  | ||||||
|         camR.h = height; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     // Verifie si la caméra ne dépasse pas de la map
 |  | ||||||
|     void update() |     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.x < 0) camR.x = 0; | ||||||
|         if(camR.y < 0) camR.y = 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.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; |         if(camR.y > Game::tileMap.worldHeight - camR.h) camR.y = Game::tileMap.worldHeight - camR.h; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     Vector2D<float> centerPos; | ||||||
|     SDL_Rect camR; |     SDL_Rect camR; | ||||||
|     int zoom; // TODO Zoom pas encore pris en charge
 |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| #endif | #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.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.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; |                     playerState = State::None; | ||||||
|                     break; |                     break; | ||||||
|                 } |                 } | ||||||
|  | @ -202,6 +204,10 @@ class PlayerSystem : public Component { | ||||||
| 
 | 
 | ||||||
|     void m_setCamera() |     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 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)); |         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; |         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.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)); |         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; | SDL_Texture *Game::textureRenderer; | ||||||
| Events Game::events; | Events Game::events; | ||||||
| TextureManager Game::textureManager; | TextureManager Game::textureManager; | ||||||
| Camera Game::camera {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; | Camera Game::camera {WINDOW_WIDTH, WINDOW_HEIGHT}; | ||||||
| TileMap Game::tileMap; | TileMap Game::tileMap; | ||||||
| MapManager Game::mapManager; | MapManager Game::mapManager; | ||||||
| Manager Game::entityManager; | Manager Game::entityManager; | ||||||
|  | @ -129,12 +129,8 @@ bool Game::Init() | ||||||
|     player.addComponent<AnimationSystem>(); |     player.addComponent<AnimationSystem>(); | ||||||
|     player.addComponent<PlayerSystem>(); |     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.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.camR.y = static_cast<int>(p_transform->position.y - static_cast<float>(camera.camR.h / 2 - p_transform->dimension.y * p_transform->scale / 2)); |     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); | ||||||
| 
 |  | ||||||
|     Item &item(p_inventory->addItem()); |  | ||||||
| 
 |  | ||||||
|     item.addProperty<NameProperty>("Prout"); |  | ||||||
| 
 | 
 | ||||||
|     m_isRunning = true; |     m_isRunning = true; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue