forked from SYJourney/JourneyClient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera.cpp
111 lines (90 loc) · 3.26 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//////////////////////////////////////////////////////////////////////////////////
// This file is part of the continued Journey MMORPG client //
// Copyright (C) 2015-2019 Daniel Allendorf, Ryan Payton //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////
#include "Camera.h"
#include "../Constants.h"
namespace ms
{
Camera::Camera()
{
x.set(0.0);
y.set(0.0);
VWIDTH = Constants::Constants::get().get_viewwidth();
VHEIGHT = Constants::Constants::get().get_viewheight();
}
void Camera::update(Point<int16_t> position)
{
int32_t new_width = Constants::Constants::get().get_viewwidth();
int32_t new_height = Constants::Constants::get().get_viewheight();
if (VWIDTH != new_width || VHEIGHT != new_height)
{
VWIDTH = new_width;
VHEIGHT = new_height;
}
double next_x = x.get();
double hdelta = VWIDTH / 2 - position.x() - next_x;
if (std::abs(hdelta) >= 5.0)
next_x += hdelta * (12.0 / VWIDTH);
double next_y = y.get();
double vdelta = VHEIGHT / 2 - position.y() - next_y;
if (std::abs(vdelta) >= 5.0)
next_y += vdelta * (12.0 / VHEIGHT);
if (next_x > hbounds.first() || hbounds.length() < VWIDTH)
next_x = hbounds.first();
else if (next_x < hbounds.second() + VWIDTH)
next_x = hbounds.second() + VWIDTH;
if (next_y > vbounds.first() || vbounds.length() < VHEIGHT)
next_y = vbounds.first();
else if (next_y < vbounds.second() + VHEIGHT)
next_y = vbounds.second() + VHEIGHT;
x = next_x;
y = next_y;
}
void Camera::set_position(Point<int16_t> position)
{
int32_t new_width = Constants::Constants::get().get_viewwidth();
int32_t new_height = Constants::Constants::get().get_viewheight();
if (VWIDTH != new_width || VHEIGHT != new_height)
{
VWIDTH = new_width;
VHEIGHT = new_height;
}
x.set(VWIDTH / 2 - position.x());
y.set(VHEIGHT / 2 - position.y());
}
void Camera::set_view(Range<int16_t> mapwalls, Range<int16_t> mapborders)
{
hbounds = -mapwalls;
vbounds = -mapborders;
}
Point<int16_t> Camera::position() const
{
auto shortx = static_cast<int16_t>(std::round(x.get()));
auto shorty = static_cast<int16_t>(std::round(y.get()));
return { shortx, shorty };
}
Point<int16_t> Camera::position(float alpha) const
{
auto interx = static_cast<int16_t>(std::round(x.get(alpha)));
auto intery = static_cast<int16_t>(std::round(y.get(alpha)));
return { interx, intery };
}
Point<double> Camera::realposition(float alpha) const
{
return { x.get(alpha), y.get(alpha) };
}
}