-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
127 lines (120 loc) · 3.82 KB
/
camera.cpp
File metadata and controls
127 lines (120 loc) · 3.82 KB
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
#include"camera.h"
CameraBase::CameraBase (const int* screenW,const int* screenH) {
screenWidth = screenW;
screenHeight = screenH;
}
BoundableCamera::BoundableCamera (float maxXBoundary,float maxYBoundary,float minXBoundary, float minYBoundary) {
maxX = maxXBoundary;
maxY = maxYBoundary;
minX = minXBoundary;
minY = minYBoundary;
}
void BoundableCamera::setMaxBoundary (float newXBoundary, float newYBoundary) {
maxX = newXBoundary;
maxY = newYBoundary;
}
void BoundableCamera::setMinBoundary (float newXBoundary, float newYBoundary) {
minX = newXBoundary;
minY = newYBoundary;
}
void PositionableCamera::setPos(float newPosX, float newPosY) {
camX=newPosX;
camY=newPosY;
}
PositionableCamera::PositionableCamera(float posX, float posY) {
setPos(posX,posY);
}
void PositionableCamera::move (float xAmount, float yAmount, float minXBound, float maxXBound, float minYBound, float maxYBound,float screenWidth, float screenHeight) {
if (camX+xAmount >= minXBound && camX+xAmount+screenWidth <= maxXBound) {
camX+=xAmount;
}
if (camY+yAmount >= minYBound && camY+yAmount+screenHeight <= maxYBound) {
camY+=yAmount;
}
}
void FollowableCamera::attach (const float *newX, const float *newY) {
followedX = newX;
followedY = newY;
}
FollowableCamera::FollowableCamera (const float* newX, const float* newY) {
attach(newX,newY);
}
FollowCamera::FollowCamera (const float* newX, const float* newY, const int* screenW,const int* screenH,
float maxXBoundary,float maxYBoundary,float minXBoundary, float minYBoundary) :
CameraBase(screenW,screenH),
FollowableCamera(newX,newY),
BoundableCamera(maxXBoundary,maxYBoundary,minXBoundary,minYBoundary) {
}
float FollowCamera::getX () {
float camX = *followedX-(*screenWidth/2.0);
if (camX < minX) {
return minX;
//camera's position is top left while max boundary is bottom right,
//so this compares the camera's position when the max boundary would
//be drawn at bottom right
} else if (camX > maxX-(*screenWidth)) {
return maxX-(*screenWidth);
} else {
return camX;
}
}
float FollowCamera::getY () {
float camY = *followedY-(*screenHeight/2.0);
if (camY <= minY) {
return minY;
//camera's position is top left while max boundary is bottom right,
//so this compares the camera's position when the max boundary would
//be drawn at bottom right
} else if (camY > maxY-(*screenHeight)) {
return maxY-(*screenHeight);
} else {
return camY;
}
}
ControlledCamera::ControlledCamera (float posX, float posY, const int* screenW,const int* screenH,
float maxXBoundary,float maxYBoundary,float minXBoundary,float minYBoundary) :
CameraBase(screenW,screenH),
PositionableCamera(posX,posY),
BoundableCamera(maxXBoundary,maxYBoundary,minXBoundary,minYBoundary) {
}
float ControlledCamera::getX () {
return camX;
}
float ControlledCamera::getY () {
return camY;
}
EdgeFollowCamera::EdgeFollowCamera (const float* folX, const float* folY, float posX, float posY, const int* screenW,const int* screenH,
float maxXBoundary,float maxYBoundary,float minXBoundary,float minYBoundary,float edgePad,float scrollSpd) :
CameraBase(screenW,screenH),
FollowableCamera(folX,folY),
BoundableCamera(maxXBoundary,maxYBoundary,minXBoundary,minYBoundary),
PositionableCamera(posX,posY) {
edgeBound=edgePad;
scrollSpeed = scrollSpd;
}
float EdgeFollowCamera::getX () {
if (camX+edgeBound > (*followedX)) {
camX-=scrollSpeed;
} else if ((camX+(*screenWidth))-edgeBound < (*followedX)) {
camX+=scrollSpeed;
}
if (camX <= minX) {
camX=minX;
} else if (camX > maxX-(*screenWidth)) {
camX=maxX-(*screenWidth);
}
return camX;
}
float EdgeFollowCamera::getY () {
if (edgeBound > (*followedY)) {
camY-=scrollSpeed;
} else if ((*screenHeight)-edgeBound < (*followedY)) {
camY+=scrollSpeed;
}
if (camY < minY) {
camY=minY;
} else if (camY > maxY-(*screenHeight)) {
camY=maxY-(*screenHeight);
}
return camY;
}