-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
81 lines (69 loc) · 1.96 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "camera.h"
#include <iostream>
#include "mathfunctions.h"
#include <cmath>
using namespace std;
Camera::Camera()
{
input = Input::getInstance();
window = Window::getInstance();
objects = EntityContainer::getInstance();
speed = 10.0;
atGoal = false;
atBall = true;
}
Camera::~Camera()
{
window->release();
input->release();
}
void Camera::centerOnFocus()
{
window->setCoord(focus.getX() - (window->rightCoord() - window->leftCoord()) / 2.0,
focus.getX() + (window->rightCoord() - window->leftCoord()) / 2.0,
focus.getY() - (window->topCoord() - window->bottomCoord()) / 2.0,
focus.getY() + (window->topCoord() - window->bottomCoord()) / 2.0);
}
void Camera::update()
{
Entity* ball = objects->getEntity("playerBall");
Entity* goal = objects->getEntity("Goal");
if(input->isKeyDown("space"))
{
atBall = false;
if(!atGoal)
{
double angle = calcAngle(focus, goal->getCenter());
focus.move(cos(angle) * speed, sin(angle) * speed);
double newAngle = calcAngle(focus, goal->getCenter());
double angleDiff = newAngle - angle;
if(angleDiff > M_PI_4 or angleDiff < -M_PI_4)
{
atGoal = true;
focus = goal->getCenter();
}
}
}
else
{
atGoal = false;
if(!atBall)
{
double angle = calcAngle(focus, ball->getCenter());
focus.move(cos(angle) * speed, sin(angle) * speed);
double nextAngle = calcAngle(focus, ball->getCenter());
double diff = nextAngle - angle;
if(diff > M_PI_4 or diff < -M_PI_4)
{
atBall = true;
focus = ball->getCenter();
}
}
else
{
focus = ball->getCenter();
centerOnFocus();
}
}
centerOnFocus();
}