FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_tunnel.inl
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace fge
18{
19
21template<class T>
23 g_gates(std::move(r.g_gates)),
24 g_anonymousGates(std::move(r.g_anonymousGates))
25{
26 for (std::size_t i = 0; i < this->g_gates.size(); ++i)
27 {
28 this->g_gates[i]->g_tunnel = this;
29 }
30 for (std::size_t i = 0; i < this->g_anonymousGates.size(); ++i)
31 {
32 this->g_anonymousGates[i]->g_tunnel = this;
33 }
35template<class T>
37{
38 this->closeAll();
39}
40
41template<class T>
42fge::Tunnel<T>& Tunnel<T>::operator=(fge::Tunnel<T>&& r) noexcept
43{
44 this->g_gates = std::move(r.g_gates);
45 this->g_anonymousGates = std::move(r.g_anonymousGates);
46
47 for (std::size_t i = 0; i < this->g_gates.size(); ++i)
48 {
49 this->g_gates[i]->g_tunnel = this;
50 }
51 for (std::size_t i = 0; i < this->g_anonymousGates.size(); ++i)
52 {
53 this->g_anonymousGates[i]->g_tunnel = this;
54 }
55}
56
57template<class T>
58bool Tunnel<T>::knock(fge::TunnelGate<T>& gate, bool anonymous)
59{
60 if ((!gate.isLocked()) || gate.status())
61 {
62 return false;
63 }
64
65 gate.g_tunnel = this;
66 if (anonymous)
67 {
68 this->g_anonymousGates.push_back(&gate);
69 return true;
70 }
71 this->g_gates.push_back(&gate);
72 return true;
73}
74template<class T>
75bool Tunnel<T>::addGate(fge::TunnelGate<T>& gate, bool anonymous)
76{
77 if (gate.isOpen())
78 {
79 return false;
80 }
81
82 gate.g_tunnel = this;
83 if (anonymous)
84 {
85 this->g_anonymousGates.push_back(&gate);
86 return true;
87 }
88 this->g_gates.push_back(&gate);
89 return true;
90}
91
92template<class T>
93bool Tunnel<T>::isAnonymous(fge::TunnelGate<T> const& gate) const
94{
95 for (std::size_t i = 0; i < this->g_anonymousGates.size(); ++i)
96 {
97 if (this->g_anonymousGates[i] == &gate)
98 {
99 return true;
100 }
101 }
102 return false;
103}
104
105template<class T>
106void Tunnel<T>::closeGate(std::size_t index)
107{
108 if (index >= this->g_gates.size())
109 {
110 return;
111 }
112 this->g_gates[index]->g_tunnel = nullptr;
113 this->g_gates.erase(this->g_gates.begin() + index);
114}
115template<class T>
116void Tunnel<T>::closeAnonymousGate(std::size_t index)
117{
118 if (index >= this->g_anonymousGates.size())
119 {
120 return;
121 }
122 this->g_anonymousGates[index]->g_tunnel = nullptr;
123 this->g_anonymousGates.erase(this->g_anonymousGates.begin() + index);
124}
125template<class T>
126void Tunnel<T>::closeGate(fge::TunnelGate<T>& gate)
127{
128 for (std::size_t i = 0; i < this->g_anonymousGates.size(); ++i)
129 {
130 if (this->g_anonymousGates[i] == &gate)
131 {
132 this->g_anonymousGates[i]->g_tunnel = nullptr;
133 this->g_anonymousGates.erase(this->g_anonymousGates.begin() + i);
134 return;
135 }
136 }
137 for (std::size_t i = 0; i < this->g_gates.size(); ++i)
138 {
139 if (this->g_gates[i] == &gate)
140 {
141 this->g_gates[i]->g_tunnel = nullptr;
142 this->g_gates.erase(this->g_gates.begin() + i);
143 return;
144 }
145 }
146}
147template<class T>
148void Tunnel<T>::closeAll()
149{
150 for (std::size_t i = 0; i < this->g_anonymousGates.size(); ++i)
151 {
152 this->g_anonymousGates[i]->g_tunnel = nullptr;
153 }
154 for (std::size_t i = 0; i < this->g_gates.size(); ++i)
155 {
156 this->g_gates[i]->g_tunnel = nullptr;
157 }
158 this->g_anonymousGates.clear();
159 this->g_gates.clear();
160}
161
162template<class T>
163T* Tunnel<T>::get(std::size_t index) const
164{
165 if (index >= this->g_gates.size())
166 {
167 return nullptr;
168 }
169 return this->g_gates[index]->g_data;
170}
171template<class T>
172T* Tunnel<T>::getAnonymous(std::size_t index) const
173{
174 if (index >= this->g_anonymousGates.size())
175 {
176 return nullptr;
177 }
178 return this->g_anonymousGates[index]->g_data;
179}
180template<class T>
181std::size_t Tunnel<T>::getGatesSize() const
182{
183 return this->g_gates.size();
184}
185template<class T>
186std::size_t Tunnel<T>::getAnonymousGatesSize() const
187{
188 return this->g_anonymousGates.size();
189}
190
191template<class T>
192T* Tunnel<T>::operator[](std::size_t index) const
193{
194 if (index >= this->g_gates.size())
195 {
196 return nullptr;
197 }
198 return this->g_gates[index]->g_data;
199}
200
202
203template<class T>
205 g_data(nullptr),
206 g_tunnel(nullptr),
207 g_locked(false)
208{}
209template<class T>
211 g_data(gate.g_data),
212 g_tunnel(nullptr),
213 g_locked(gate.g_locked)
214{
215 if (gate.g_tunnel)
216 {
217 gate.g_tunnel->addGate(*this, gate.g_tunnel->isAnonymous(gate));
218 }
219}
220template<class T>
222 g_data(gate.g_data),
223 g_tunnel(nullptr),
224 g_locked(gate.g_locked)
225{
226 if (gate.g_tunnel)
227 {
228 gate.g_tunnel->addGate(*this, gate.g_tunnel->isAnonymous(gate));
229 gate.g_tunnel->closeGate(gate);
230 }
231}
232template<class T>
234 g_data(data),
235 g_tunnel(nullptr),
236 g_locked(false)
237{}
238template<class T>
239TunnelGate<T>::~TunnelGate()
240{
241 this->close();
242}
243
244template<class T>
245fge::TunnelGate<T>& TunnelGate<T>::operator=(fge::TunnelGate<T> const& gate)
246{
247 this->g_data = nullptr;
248 this->g_tunnel = nullptr;
249 this->g_locked = gate.g_locked;
250
251 if (gate.g_tunnel)
252 {
253 gate.g_tunnel->addGate(*this, gate.g_tunnel->isAnonymous(gate));
254 }
255 return *this;
256}
257template<class T>
258fge::TunnelGate<T>& TunnelGate<T>::operator=(fge::TunnelGate<T>&& gate) noexcept
259{
260 this->g_data = nullptr;
261 this->g_tunnel = nullptr;
262 this->g_locked = gate.g_locked;
263
264 if (gate.g_tunnel)
265 {
266 gate.g_tunnel->addGate(*this, gate.g_tunnel->isAnonymous(gate));
267 gate.g_tunnel->closeGate(gate);
268 }
269 return *this;
270}
271
272template<class T>
273bool TunnelGate<T>::openTo(fge::Tunnel<T>& tunnel, bool anonymous)
274{
275 return tunnel.addGate(*this, anonymous);
276}
277template<class T>
278void TunnelGate<T>::close()
279{
280 if (this->g_tunnel)
281 {
282 this->g_tunnel->closeGate(*this);
283 }
284}
285template<class T>
286bool TunnelGate<T>::isOpen() const
287{
288 return this->g_tunnel != nullptr;
289}
290
291template<class T>
292void TunnelGate<T>::setData(T* val)
293{
294 this->g_data = val;
295}
296template<class T>
297T const* TunnelGate<T>::getData() const
298{
299 return this->g_data;
300}
301template<class T>
302T* TunnelGate<T>::getData()
303{
304 return this->g_data;
305}
306
307template<class T>
308void TunnelGate<T>::setLock(bool val)
309{
310 this->g_locked = val;
311}
312template<class T>
313bool TunnelGate<T>::isLocked() const
314{
315 return this->g_locked;
316}
317
318template<class T>
319fge::Tunnel<T>* TunnelGate<T>::getTunnel() const
320{
321 return this->g_tunnel;
322}
323
324} // namespace fge
Definition C_tunnel.hpp:64
TunnelGate()
TunnelGate.
Definition C_tunnel.inl:204
Definition C_tunnel.hpp:30