-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraManager.cpp
164 lines (157 loc) · 4.25 KB
/
CameraManager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "CameraManager.h"
#include "KeyEvent.h"
#include "CameraEvents.h"
#include "game.h"
CameraManager::CameraManager()
{
mCameraPanLeft = mCameraPanRight = mCameraPanUp = mCameraPanDown = false;
mPanSpeed = mMaxPanSpeed = mPanAcceleration = 0.f;
mZoomAmount = mMaxZoom = mMinZoom = mCurrentZoom = 0.f;
mBounds = Vector2D();
mCenter = Vector2D();
}
void CameraManager::init(const Vector2D bounds, const float panSpeed, const float panAcceleration, const float zoomAmount, const float maxZoom, const float minZoom)
{
mCameraPanDown = mCameraPanLeft = mCameraPanRight = mCameraPanUp = false;
mMaxPanSpeed = panSpeed;
mPanSpeed = 0;
mPanAcceleration = panAcceleration;
mZoomAmount = zoomAmount;
mMaxZoom = maxZoom;
mMinZoom = minZoom;
mCurrentZoom = 1;
mBounds = bounds;
mCenter = Vector2D(0, 0);
gpEventSystem->addListener(EventType::KEY_PRESSED_EVENT, this);
gpEventSystem->addListener(EventType::KEY_RELEASED_EVENT, this);
gpEventSystem->addListener(EventType::MOUSE_WHEEL_EVENT, this);
}
void CameraManager::update()
{
if(mCameraPanRight)
{
const Vector2D delta = Vector2D(std::min(mPanSpeed, std::abs(mBounds.getX() - mCenter.getX())), 0);
if(mCenter.getX() + delta.getX() <= mBounds.getX())
{
gpEventSystem->fireEvent(new PanCameraEvent(delta));
mCenter += delta;
}
}
if(mCameraPanLeft)
{
const Vector2D delta = Vector2D(std::min(-mPanSpeed, std::abs(mCenter.getX() - mBounds.getX())), 0);
if(mCenter.getX() + delta.getX() >= -mBounds.getX())
{
gpEventSystem->fireEvent(new PanCameraEvent(delta));
mCenter += delta;
}
}
if(mCameraPanUp)
{
const Vector2D delta = Vector2D(0, std::min(-mPanSpeed, std::abs(mCenter.getY() - mBounds.getY())));
if(mCenter.getY() + delta.getY() >= -mBounds.getY())
{
gpEventSystem->fireEvent(new PanCameraEvent(delta));
mCenter += delta;
}
}
if(mCameraPanDown)
{
const Vector2D delta = Vector2D(0, std::min(mPanSpeed, std::abs(mBounds.getY() - mCenter.getY())));
if(mCenter.getY() + delta.getY() <= mBounds.getY())
{
gpEventSystem->fireEvent(new PanCameraEvent(delta));
mCenter += delta;
}
}
if(mCameraPanRight || mCameraPanLeft || mCameraPanDown || mCameraPanUp)
{
mPanSpeed += mPanAcceleration;
mPanSpeed = std::min(mPanSpeed, mMaxPanSpeed);
}
else
{
mPanSpeed = 0;
}
}
void CameraManager::handleEvent(const Event &theEvent)
{
const Gamestate gameState = Game::getInstance()->getGamestate();
if(theEvent.getType() == EventType::KEY_PRESSED_EVENT)
{
const KeyPressedEvent &ev = static_cast<const KeyPressedEvent&>(theEvent);
if(gameState == Gamestate::PLAYING)
{
switch(ev.getKey())
{
case Key::LEFT:
mCameraPanLeft = true;
break;
case Key::RIGHT:
mCameraPanRight = true;
break;
case Key::UP:
mCameraPanUp = true;
break;
case Key::DOWN:
mCameraPanDown = true;
break;
default:
break;
}
}
}
else if(theEvent.getType() == EventType::KEY_RELEASED_EVENT)
{
const KeyReleasedEvent &ev = static_cast<const KeyReleasedEvent&>(theEvent);
if(gameState == Gamestate::PLAYING)
{
switch(ev.getKey())
{
case Key::LEFT:
mCameraPanLeft = false;
break;
case Key::RIGHT:
mCameraPanRight = false;
break;
case Key::UP:
mCameraPanUp = false;
break;
case Key::DOWN:
mCameraPanDown = false;
break;
default:
break;
}
}
}
else if(theEvent.getType() == EventType::MOUSE_WHEEL_EVENT)
{
const MouseWheelEvent &ev = static_cast<const MouseWheelEvent&>(theEvent);
const Vector2D zoomLocation = ev.getCursorLocation();
if(gameState == Gamestate::PLAYING)
{
switch(ev.getDirection())
{
case MouseWheel::DOWN:
if(mCurrentZoom < mMaxZoom)
{
gpEventSystem->fireEvent(new ZoomCameraEvent(mZoomAmount, zoomLocation));
mCurrentZoom = std::min(mCurrentZoom + mZoomAmount, mMaxZoom);
}
break;
case MouseWheel::UP:
if(mCurrentZoom > mMinZoom)
{
gpEventSystem->fireEvent(new ZoomCameraEvent(-mZoomAmount, zoomLocation));
mCurrentZoom = std::max(mCurrentZoom - mZoomAmount, mMinZoom);
}
break;
}
}
}
}
const Vector2D CameraManager::getOffset()
{
return mCenter;
}