Player updates, Camera updates
Player update: Framerate independent movement Camera update: Move over camera movement to Camera class
This commit is contained in:
+83
-11
@@ -1,16 +1,6 @@
|
||||
#include "camera.h"
|
||||
|
||||
Camera::Camera()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Camera::SetNewView(sf::RenderWindow *window) {
|
||||
Camera::Camera(sf::RenderWindow *window) {
|
||||
this->view = sf::View(sf::FloatRect(0, 0, 1280, 720));
|
||||
window->setView(view);
|
||||
}
|
||||
@@ -24,3 +14,85 @@ void Camera::SetCenter(sf::RenderWindow *window, sf::Vector2f position) {
|
||||
this->view.setCenter(position.x, position.y);
|
||||
window->setView(view);
|
||||
}
|
||||
|
||||
void Camera::Update(sf::RenderWindow *window, Map *map, sf::Vector2f position) {
|
||||
// Update camera position
|
||||
//|------------|--------------|------------|
|
||||
//| 1 | 2 | 1 |
|
||||
//| | | |
|
||||
//|------------x--------------|------------|
|
||||
//| 3 | 4 | 3 |
|
||||
//| | | |
|
||||
//|------------|--------------y------------|
|
||||
//| 1 | 2 | 1 |
|
||||
//| | | |
|
||||
//|------------|--------------|------------|
|
||||
|
||||
// Get window center start and end
|
||||
// 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);
|
||||
|
||||
// If Player is in the center (4)
|
||||
if(position.x > windowCenterStart.x
|
||||
&& position.y > windowCenterStart.y
|
||||
&& position.x < windowCenterEnd.x
|
||||
&& position.y < windowCenterEnd.y) {
|
||||
this->SetCenter(window, sf::Vector2f(position.x, position.y));
|
||||
}
|
||||
|
||||
// If Player is in the edge (2,3)
|
||||
// Middle Top
|
||||
else if(position.x > windowCenterStart.x
|
||||
&& position.x < windowCenterEnd.x
|
||||
&& position.y < windowCenterStart.y) {
|
||||
this->SetCenter(window, sf::Vector2f(position.x, windowCenterStart.y));
|
||||
}
|
||||
// Middle Bottom
|
||||
else if(position.x > windowCenterStart.x
|
||||
&& position.x < windowCenterEnd.x
|
||||
&& position.y > windowCenterEnd.y) {
|
||||
this->SetCenter(window, sf::Vector2f(position.x, windowCenterEnd.y));
|
||||
}
|
||||
// Middle Left
|
||||
else if(position.y > windowCenterStart.y
|
||||
&& position.y < windowCenterEnd.y
|
||||
&& position.x < windowCenterStart.x) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterStart.x, position.y));
|
||||
}
|
||||
// Middle Right
|
||||
else if(position.y > windowCenterStart.y
|
||||
&& position.y < windowCenterEnd.y
|
||||
&& position.x > windowCenterEnd.x) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterEnd.x, position.y));
|
||||
}
|
||||
|
||||
// If Player is in one of the corners (1)
|
||||
// Top Left
|
||||
else if(position.x < windowCenterStart.x
|
||||
&& position.y < windowCenterStart.y) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterStart.x, windowCenterStart.y));
|
||||
}
|
||||
// Top Right
|
||||
else if(position.x > windowCenterEnd.x
|
||||
&& position.y < windowCenterStart.y) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterEnd.x, windowCenterStart.y));
|
||||
}
|
||||
// Bottom Left
|
||||
else if(position.x < windowCenterStart.x
|
||||
&& position.y > windowCenterEnd.y) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterStart.x, windowCenterEnd.y));
|
||||
}
|
||||
// Bottom Right
|
||||
else if(position.x > windowCenterEnd.x
|
||||
&& position.y > windowCenterEnd.y) {
|
||||
this->SetCenter(window, sf::Vector2f(windowCenterEnd.x, windowCenterEnd.y));
|
||||
}
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -3,14 +3,16 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "map.h"
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
Camera();
|
||||
~Camera();
|
||||
void SetNewView(sf::RenderWindow *window);
|
||||
Camera(sf::RenderWindow *window);
|
||||
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);
|
||||
~Camera();
|
||||
|
||||
private:
|
||||
//sf::Vector2f position;
|
||||
|
||||
+11
-88
@@ -7,99 +7,22 @@ Player::Player(EntityManager* entityManager, Map *map, Camera *camera, float x,
|
||||
|
||||
this->Load("data/gfx/player.png");
|
||||
this->setPosition(x, y);
|
||||
this->speed = 2.5f;
|
||||
this->speed = 0.00015f;
|
||||
}
|
||||
|
||||
void Player::Update(sf::RenderWindow* window, InputManager inputManager) {
|
||||
// Store currect location of the player for the Camera to use
|
||||
sf::Vector2f playerPosition = sf::Vector2f(this->getPosition().x, this->getPosition().y);;
|
||||
|
||||
void Player::Update(sf::RenderWindow* window, InputManager inputManager, int elapsedTime) {
|
||||
float speed = this->speed * elapsedTime;
|
||||
// Update player velocity
|
||||
this->velocity.x = inputManager.IsPressed(InputManager::Right) * this->speed -
|
||||
inputManager.IsPressed(InputManager::Left) * this->speed;
|
||||
this->velocity.y = inputManager.IsPressed(InputManager::Down) * this->speed -
|
||||
inputManager.IsPressed(InputManager::Up) * this->speed;
|
||||
this->velocity.x = inputManager.IsPressed(InputManager::Right) * speed -
|
||||
inputManager.IsPressed(InputManager::Left) * speed;
|
||||
this->velocity.y = inputManager.IsPressed(InputManager::Down) * speed -
|
||||
inputManager.IsPressed(InputManager::Up) * speed;
|
||||
|
||||
// Set correct speed on diagonal movement
|
||||
if((this->velocity.x == this->speed || this->velocity.x == -this->speed)
|
||||
&& (this->velocity.y == this->speed || this->velocity.y == -this->speed)) {
|
||||
this->velocity.x *= sqrt(this->speed * 2 + this->speed * 2) / (this->speed * 2);
|
||||
this->velocity.y *= sqrt(this->speed * 2 + this->speed * 2) / (this->speed * 2);
|
||||
}
|
||||
|
||||
// Update camera position
|
||||
//|------------|--------------|------------|
|
||||
//| 1 | 2 | 1 |
|
||||
//| | | |
|
||||
//|------------x--------------|------------|
|
||||
//| 3 | 4 | 3 |
|
||||
//| | | |
|
||||
//|------------|--------------y------------|
|
||||
//| 1 | 2 | 1 |
|
||||
//| | | |
|
||||
//|------------|--------------|------------|
|
||||
|
||||
// Get window center start and end
|
||||
// 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);
|
||||
|
||||
// If Player is in the center (4)
|
||||
if(playerPosition.x > windowCenterStart.x
|
||||
&& playerPosition.y > windowCenterStart.y
|
||||
&& playerPosition.x < windowCenterEnd.x
|
||||
&& playerPosition.y < windowCenterEnd.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(playerPosition.x, playerPosition.y));
|
||||
}
|
||||
|
||||
// If Player is in the edge (2,3)
|
||||
// Middle Top
|
||||
else if(playerPosition.x > windowCenterStart.x
|
||||
&& playerPosition.x < windowCenterEnd.x
|
||||
&& playerPosition.y < windowCenterStart.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(playerPosition.x, windowCenterStart.y));
|
||||
}
|
||||
// Middle Bottom
|
||||
else if(playerPosition.x > windowCenterStart.x
|
||||
&& playerPosition.x < windowCenterEnd.x
|
||||
&& playerPosition.y > windowCenterEnd.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(playerPosition.x, windowCenterEnd.y));
|
||||
}
|
||||
// Middle Left
|
||||
else if(playerPosition.y > windowCenterStart.y
|
||||
&& playerPosition.y < windowCenterEnd.y
|
||||
&& playerPosition.x < windowCenterStart.x) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterStart.x, playerPosition.y));
|
||||
}
|
||||
// Middle Right
|
||||
else if(playerPosition.y > windowCenterStart.y
|
||||
&& playerPosition.y < windowCenterEnd.y
|
||||
&& playerPosition.x > windowCenterEnd.x) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterEnd.x, playerPosition.y));
|
||||
}
|
||||
|
||||
// If Player is in one of the corners (1)
|
||||
// Top Left
|
||||
else if(playerPosition.x < windowCenterStart.x
|
||||
&& playerPosition.y < windowCenterStart.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterStart.x, windowCenterStart.y));
|
||||
}
|
||||
// Top Right
|
||||
else if(playerPosition.x > windowCenterEnd.x
|
||||
&& playerPosition.y < windowCenterStart.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterEnd.x, windowCenterStart.y));
|
||||
}
|
||||
// Bottom Left
|
||||
else if(playerPosition.x < windowCenterStart.x
|
||||
&& playerPosition.y > windowCenterEnd.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterStart.x, windowCenterEnd.y));
|
||||
}
|
||||
// Bottom Right
|
||||
else if(playerPosition.x > windowCenterEnd.x
|
||||
&& playerPosition.y > windowCenterEnd.y) {
|
||||
camera->SetCenter(window, sf::Vector2f(windowCenterEnd.x, windowCenterEnd.y));
|
||||
if((this->velocity.x == speed || this->velocity.x == -speed)
|
||||
&& (this->velocity.y == speed || this->velocity.y == -speed)) {
|
||||
this->velocity.x *= .75;
|
||||
this->velocity.y *= .75;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ 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);
|
||||
void Update(sf::RenderWindow *window, InputManager inputManager, int elapsedTime);
|
||||
float SetSpeed();
|
||||
int GetHealth();
|
||||
int GetMaxHealth();
|
||||
|
||||
@@ -21,8 +21,7 @@ void MainGame::Initialize(sf::RenderWindow* window) {
|
||||
mapLoad.Load(this->map, "data/map/level1.json");
|
||||
|
||||
// Load Camera
|
||||
this->camera = new Camera();
|
||||
this->camera->SetNewView(window);
|
||||
this->camera = new Camera(window);
|
||||
|
||||
// Load Player
|
||||
this->player = new Player(this->entityManager, this->map, this->camera, 100, 100);
|
||||
@@ -30,8 +29,13 @@ void MainGame::Initialize(sf::RenderWindow* window) {
|
||||
}
|
||||
|
||||
void MainGame::Update(sf::RenderWindow* window) {
|
||||
this->player->Update(window, inputManager);
|
||||
this->time = this->clock.getElapsedTime();
|
||||
int timeElapsed = this->time.asMicroseconds();
|
||||
this->clock.restart();
|
||||
|
||||
this->player->Update(window, inputManager, timeElapsed);
|
||||
this->entityManager->Update();
|
||||
this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y));
|
||||
|
||||
if(inputManager.IsPressed(InputManager::LoadMap)) {
|
||||
std::cout << "Loading Map..." << std::endl;
|
||||
|
||||
@@ -19,6 +19,9 @@ public:
|
||||
void Destroy(sf::RenderWindow* window);
|
||||
|
||||
private:
|
||||
sf::Clock clock;
|
||||
sf::Time time;
|
||||
|
||||
EntityManager* entityManager;
|
||||
Map* map;
|
||||
Camera* camera;
|
||||
|
||||
Reference in New Issue
Block a user