public
Description: openc2e
Homepage: http://openc2e.org
Clone URL: git://github.com/ccdevnet/openc2e.git
openc2e / src / Camera.cpp
100644 165 lines (137 sloc) 4.172 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
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
165
/*
* Camera.cpp
* openc2e
*
* Created by Alyssa Milburn on Tue May 25 2004.
* Copyright (c) 2004 Alyssa Milburn. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
*/
 
#include "Camera.h"
#include "CameraPart.h"
#include "World.h"
#include "Backend.h"
#include "MetaRoom.h"
 
Camera::Camera() {
metaroom = 0;
panning = false;
x = 0;
y = 0;
}
 
MetaRoom * Camera::getMetaRoom() const {
return world.map.getMetaRoom(metaroom);
}
 
void Camera::goToMetaRoom(unsigned int m) {
metaroom = m;
moveTo(getMetaRoom()->x(), getMetaRoom()->y());
}
 
void Camera::goToMetaRoom(unsigned int m, int _x, int _y, cameratransition transition) {
metaroom = m;
moveTo(_x, _y);
// TODO: transition
 
checkBounds();
}
 
void Camera::moveTo(int _x, int _y, panstyle pan) {
// TODO: we don't break tracking according to trackingstyle.. atm updateTracking() calls this, also
x = _x;
y = _y;
 
// TODO: panning
 
checkBounds();
}
 
void Camera::moveToGlobal(int _x, int _y, panstyle pan) {
MetaRoom *m = world.map.metaRoomAt(_x, _y);
if (m) {
if (m->id != metaroom) pan = jump; // inter-metaroom panning is always jump
metaroom = m->id;
}
 
moveTo(_x, _y, pan);
}
 
void MainCamera::moveTo(int _x, int _y, panstyle pan) {
int xoffset = _x - x;
int yoffset = _y - y;
Camera::moveTo(_x, _y, pan);
 
for (std::vector<AgentRef>::iterator i = floated.begin(); i != floated.end(); i++) {
assert(*i);
(*i)->moveTo((*i)->x + xoffset, (*i)->y + yoffset);
}
}
 
void MainCamera::addFloated(AgentRef a) {
assert(a);
floated.push_back(a);
}
 
void MainCamera::delFloated(AgentRef a) {
assert(a);
std::vector<AgentRef>::iterator i = std::find(floated.begin(), floated.end(), a);
if (i == floated.end()) return;
floated.erase(i);
}
 
void Camera::trackAgent(AgentRef a, int xp, int yp, trackstyle s, cameratransition transition) {
trackedagent = a;
trackingstyle = s;
updateTracking();
}
 
void Camera::checkBounds() {
MetaRoom *m = getMetaRoom();
if (!m) return;
 
if (m->wraparound()) {
// handle wrapping around, if necessary
if (x < (int)m->x()) {
moveTo(x + m->width(), y);
} else if (x > (int)m->x() + (int)m->width()) {
moveTo(x - m->width(), y);
}
} else {
// refuse to move beyond the boundaries
if (x < (int)m->x()) {
moveTo(m->x(), y);
} else if (x + getWidth() > m->x() + m->width()) {
moveTo(m->x() + m->width() - getWidth(), y);
}
}
 
// refuse to move beyond the boundaries
if (y < (int)m->y()) {
moveTo(x, m->y());
} else if (y + getHeight() > m->y() + m->height()) {
moveTo(x, m->y() + m->height() - getHeight());
}
}
 
void Camera::tick() {
updateTracking();
}
 
void Camera::updateTracking() {
if (!trackedagent) return;
 
// TODO: not very intelligent :) also, are int casts correct?
int trackx = (int)trackedagent->x + ((int)trackedagent->getWidth() / 2) - (int)(getWidth() / 2);
int tracky = (int)trackedagent->y + ((int)trackedagent->getHeight() / 2) - (int)(getHeight() / 2);
moveToGlobal(trackx, tracky);
}
 
unsigned int MainCamera::getWidth() const {
if ((!getMetaRoom()) || (backend->getMainSurface()->getWidth() < getMetaRoom()->width()))
return backend->getMainSurface()->getWidth();
else
return getMetaRoom()->width();
}
 
unsigned int MainCamera::getHeight() const {
if ((!getMetaRoom()) || (backend->getMainSurface()->getHeight() < getMetaRoom()->height()))
return backend->getMainSurface()->getHeight();
else
return getMetaRoom()->height();
}
 
unsigned int PartCamera::getWidth() const {
// TODO: update from ZOOM values
return part->cameraWidth();
}
 
unsigned int PartCamera::getHeight() const {
// TODO: update from ZOOM values
return part->cameraHeight();
}
 
/* vim: set noet: */