-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera.cpp
57 lines (48 loc) · 1.17 KB
/
camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "camera.h"
#include "game.h"
#include "tile.h"
#include "world.h"
Camera::Camera(Game *game)
{
this->game = game;
this->pos = QPointF(0, 0);
}
void Camera::update(Entity *entity){
float x = entity->x - (game->WIDTH - entity->width) / 2;
float y = entity->y - (game->HEIGHT - entity->height) / 2;
setPosition(x, y);
this->checkBorders();
}
QPointF Camera::getPosition()
{
return this->pos;
}
void Camera::setPosition(float x, float y)
{
this->pos = QPointF(x, y);
this->checkBorders();
}
void Camera::setPosition(QPointF position)
{
this->pos = position;
this->checkBorders();
}
void Camera::move(float x, float y)
{
this->pos += QPointF(x, y);
this->checkBorders();
}
void Camera::move(QPointF move)
{
this->pos += move;
this->checkBorders();
}
void Camera::checkBorders()
{
int left = this->game->world->WIDTH * Tile::SIZE - game->WIDTH;
int top = this->game->world->HEIGHT * Tile::SIZE - game->HEIGHT;
if(pos.rx() < 0) pos.setX(0);
if(pos.ry() < 0) pos.setY(0);
if(pos.rx() > left) pos.setX(left);
if(pos.ry() > top) pos.setY(top);
}