Skyscraper 2.0
movingwalkway.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Moving Walkway Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "globals.h"
25#include "sbs.h"
26#include "camera.h"
27#include "mesh.h"
28#include "floor.h"
29#include "sound.h"
30#include "texture.h"
31#include "profiler.h"
32#include "dynamicmesh.h"
33#include "step.h"
34#include "movingwalkway.h"
35
36namespace SBS {
37
38MovingWalkway::MovingWalkway(Object *parent, const std::string &name, int run, Real speed, const std::string &sound_file, const std::string &texture, const std::string &direction, Real CenterX, Real CenterZ, Real width, Real treadsize, int num_steps, Real voffset, Real tw, Real th) : Object(parent)
39{
40 //create a new moving walkway object
41 //run is either 1 for forward motion, -1 for reverse motion, 0 for stop
42 //direction is where the step base is - front, back, left, or right.
43
44 //set up SBS object
45 SetValues("MovingWalkway", "", false);
46
47 std::string Name;
48 Floor *floor = dynamic_cast<Floor*>(parent);
49 if (floor)
50 Name = "Floor" + ToString(floor->Number) + ":"+ name;
51 else
52 Name = name;
54
55 is_enabled = true;
56 SetRun(run);
57 Speed = speed;
58 start = Vector3::ZERO;
59 end = Vector3::ZERO;
60
61 //register with engine
63
64 //create sound object
65 sound = new Sound(this, name, true);
66 sound->Load(sound_file);
67
68 //move object
69 Move(CenterX, voffset, CenterZ);
70
71 //create step meshes
72 for (int i = 0; i < num_steps; i++)
73 {
74 Step *mesh = new Step(this, "Step " + ToString(i + 1), 0, 100);
75 Steps.emplace_back(mesh);
76 }
77
78 //create steps
79 CreateSteps(texture, direction, width, treadsize, tw, th);
80}
81
83{
84 if (sound)
85 {
86 sound->parent_deleting = true;
87 delete sound;
88 }
89 sound = 0;
90
91 //remove step meshes
92 for (size_t i = 0; i < Steps.size(); i++)
93 {
94 if (Steps[i])
95 {
96 Steps[i]->parent_deleting = true;
97 delete Steps[i];
98 }
99 Steps[i] = 0;
100 }
101
102 //unregister from parent
103 if (sbs->FastDelete == false)
104 {
106
107 //unregister from parent
108 if (parent_deleting == false)
109 {
110 std::string type = GetParent()->GetType();
111
112 if (type == "Floor")
113 static_cast<Floor*>(GetParent())->RemoveMovingWalkway(this);
114 }
115 }
116}
117
119{
120 //enable or disable walkway
121
122 if (is_enabled == value)
123 return;
124
125 EnableLoop(value);
126
127 for (size_t i = 0; i < Steps.size(); i++)
128 Steps[i]->Enabled(value);
129
130 if (value == false && sound->IsPlaying() == true)
131 sound->Stop();
132
133 is_enabled = value;
134}
135
137{
138 if (value == 0)
139 {
140 for (size_t i = 0; i < Steps.size(); i++)
141 {
142 Steps[i]->vector = Vector3::ZERO;
143 Steps[i]->speed = 0;
144 }
145 }
146
147 Run = value;
148}
149
150void MovingWalkway::Report(const std::string &message)
151{
152 //general reporting function
153 Object::Report("Moving Walkway " + GetName() + ": " + message);
154}
155
156bool MovingWalkway::ReportError(const std::string &message)
157{
158 //general reporting function
159 return Object::ReportError("Moving Walkway " + GetName() + ": " + message);
160}
161
163{
164 //run loop
165
166 SBS_PROFILE("MovingWalkway::Loop");
167
168 //only run if power is enabled
169 if (sbs->GetPower() == false)
170 {
171 sound->Stop();
172 return;
173 }
174
175 if (!IsEnabled() || Run == 0)
176 {
177 if (sound->IsPlaying() == true)
178 sound->Stop();
179 return;
180 }
181
182 if (sound->IsPlaying() == false)
183 {
184 sound->SetLoopState(true);
185 sound->Play();
186 }
187
188 MoveSteps();
189}
190
191void MovingWalkway::CreateSteps(const std::string &texture, const std::string &direction, Real width, Real treadsize, Real tw, Real th)
192{
193 //create steps
194 std::string Name = GetName();
196 Direction = direction;
197 this->treadsize = treadsize;
198 SetCase(Direction, false);
199 int num_steps = (int)Steps.size();
200
202 if (Direction == "right" || Direction == "back")
203 sbs->SetWallOrientation("right");
204 if (Direction == "left" || Direction == "front")
205 sbs->SetWallOrientation("left");
206
207 for (int i = 1; i <= num_steps; i++)
208 {
209 Real pos = 0;
210 std::string base = Name + ":" + ToString(i);
211
212 //create wall object
213 Wall *wall = Steps[i - 1]->CreateWallObject(base);
214
215 Real thickness = treadsize;
216
217 sbs->DrawWalls(false, true, false, false, false, false);
218
219 if (Direction == "right")
220 {
221 pos = ((treadsize * num_steps + 1) / 2) - (treadsize * i);
222 sbs->AddFloorMain(wall, base, texture, 0, 0, -(width / 2), treadsize, width / 2, 0, 0, false, false, tw, th, true);
223 Steps[i - 1]->Move(Vector3(pos, 0, 0));
224 }
225 if (Direction == "left")
226 {
227 pos = -((treadsize * num_steps + 1) / 2) + (treadsize * i);
228 sbs->AddFloorMain(wall, base, texture, 0, -treadsize, -(width / 2), 0, width / 2, 0, 0, false, false, tw, th, true);
229 Steps[i - 1]->Move(Vector3(pos, 0, 0));
230 }
231 if (Direction == "back")
232 {
233 pos = ((treadsize * num_steps + 1) / 2) - (treadsize * i);
234 sbs->AddFloorMain(wall, base, texture, 0, -(width / 2), 0, width / 2, treadsize, 0, 0, false, false, tw, th, true);
235 Steps[i - 1]->Move(Vector3(0, 0, pos));
236 }
237 if (Direction == "front")
238 {
239 pos = -((treadsize * num_steps + 1) / 2) + (treadsize * i);
240 sbs->AddFloorMain(wall, base, texture, 0, -(width / 2), -treadsize, width / 2, 0, 0, 0, false, false, tw, th, true);
241 Steps[i - 1]->Move(Vector3(0, 0, pos));
242 }
243 Steps[i - 1]->vector = Vector3::ZERO;
244 Steps[1 - 1]->speed = 0;
245
246 if (i == 1)
247 start = Steps[i - 1]->GetPosition();
248 if (i == num_steps)
249 end = Steps[i - 1]->GetPosition();
250 Steps[i - 1]->start = Steps[i - 1]->GetPosition();
251 }
252
253 sbs->ResetWalls(true);
255}
256
258{
259 if (GetPosition().distance(sbs->camera->GetPosition()) > 100)
260 return;
261
262 for (size_t i = 0; i < Steps.size(); i++)
263 {
264 if (Run == 1)
265 {
266 if (Direction == "right")
267 {
268 Real pos = Steps[i]->GetPosition().x;
269 if (pos < end.x - treadsize)
270 Steps[i]->SetPosition(start);
271 else
272 Steps[i]->Move(Vector3(-Run, 0, 0), Speed * sbs->delta);
273 }
274 if (Direction == "left")
275 {
276 Real pos = Steps[i]->GetPosition().x;
277 if (pos > end.x + treadsize)
278 Steps[i]->SetPosition(start);
279 else
280 Steps[i]->Move(Vector3(Run, 0, 0), Speed * sbs->delta);
281 }
282 if (Direction == "back")
283 {
284 Real pos = Steps[i]->GetPosition().z;
285 if (pos < end.z - treadsize)
286 Steps[i]->SetPosition(start);
287 else
288 Steps[i]->Move(Vector3(0, 0, -Run), Speed * sbs->delta);
289 }
290 if (Direction == "front")
291 {
292 Real pos = Steps[i]->GetPosition().z;
293 if (pos > end.z + treadsize)
294 Steps[i]->SetPosition(start);
295 else
296 Steps[i]->Move(Vector3(0, 0, Run), Speed * sbs->delta);
297 }
298 }
299 else if (Run == -1)
300 {
301 if (Direction == "right")
302 {
303 Real pos = Steps[i]->GetPosition().x;
304 if (pos > start.x)
305 Steps[i]->SetPosition(Vector3(end.x - treadsize, end.y, end.z));
306 else
307 Steps[i]->Move(Vector3(-Run, 0, 0), Speed * sbs->delta);
308 }
309 if (Direction == "left")
310 {
311 Real pos = Steps[i]->GetPosition().x;
312 if (pos < start.x)
313 Steps[i]->SetPosition(Vector3(end.x + treadsize, end.y, end.z));
314 else
315 Steps[i]->Move(Vector3(Run, 0, 0), Speed * sbs->delta);
316 }
317 if (Direction == "back")
318 {
319 Real pos = Steps[i]->GetPosition().z;
320 if (pos > start.z)
321 Steps[i]->SetPosition(Vector3(end.x, end.y, end.z - treadsize));
322 else
323 Steps[i]->Move(Vector3(0, 0, -Run), Speed * sbs->delta);
324 }
325 if (Direction == "front")
326 {
327 Real pos = Steps[i]->GetPosition().z;
328 if (pos < start.z)
329 Steps[i]->SetPosition(Vector3(end.x, end.y, end.z + treadsize));
330 else
331 Steps[i]->Move(Vector3(0, 0, Run), Speed * sbs->delta);
332 }
333 }
334 }
335}
336
337void MovingWalkway::OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
338{
339 //cycle run stages if shift-click is performed
340
341 //only run if power is enabled
342 if (sbs->GetPower() == false)
343 return;
344
345 if (shift == true)
346 {
347 if (Run == 1)
348 {
349 Run = 0;
350 for (size_t i = 0; i < Steps.size(); i++)
351 {
352 Steps[i]->vector = Vector3::ZERO;
353 Steps[i]->speed = 0;
354 }
355 }
356 else if (Run == 0)
357 Run = -1;
358 else if (Run == -1)
359 Run = 1;
360 }
361}
362
364{
365 //reset walkway state
366
367 Run = 0;
368 for (size_t i = 0; i < Steps.size(); i++)
369 {
370 Steps[i]->SetPosition(Steps[i]->start);
371 }
372}
373
374}
Vector3 GetPosition(bool relative=false)
Definition camera.cpp:250
int Number
Definition floor.h:36
void OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
std::string Direction
std::vector< Step * > Steps
void CreateSteps(const std::string &texture, const std::string &direction, Real width, Real treadsize, Real tw, Real th)
void Enabled(bool value)
void Report(const std::string &message)
bool ReportError(const std::string &message)
MovingWalkway(Object *parent, const std::string &name, int run, Real speed, const std::string &sound_file, const std::string &texture, const std::string &direction, Real CenterX, Real CenterZ, Real width, Real treadsize, int num_steps, Real voffset, Real tw, Real th)
void SetRun(int value)
std::string Name
Definition object.h:52
const std::string & GetName()
Definition object.cpp:53
virtual bool ReportError(const std::string &message)
Definition object.cpp:84
Object * GetParent()
Definition object.cpp:42
virtual void Report(const std::string &message)
Definition object.cpp:78
void SetName(const std::string &name)
Definition object.cpp:72
bool parent_deleting
Definition object.h:64
virtual void Move(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:253
virtual Vector3 GetPosition(bool relative=false)
Definition object.cpp:321
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
void EnableLoop(bool value)
Definition object.cpp:521
const std::string & GetType()
Definition object.cpp:177
bool GetPower()
Definition sbs.cpp:4701
void RegisterMovingWalkway(MovingWalkway *walkway)
Definition sbs.cpp:4667
void ResetWalls(bool ToDefaults=false)
Definition sbs.cpp:1854
bool FastDelete
Definition sbs.h:188
TextureManager * GetTextureManager()
Definition sbs.cpp:4558
Camera * camera
Definition sbs.h:160
Real delta
Definition sbs.h:134
bool SetWallOrientation(std::string direction)
Definition sbs.cpp:1781
void DrawWalls(bool MainN, bool MainP, bool SideN, bool SideP, bool Top, bool Bottom)
Definition sbs.cpp:1833
void UnregisterMovingWalkway(MovingWalkway *walkway)
Definition sbs.cpp:4673
bool AddFloorMain(Wall *wallobject, const std::string &name, const std::string &texture, Real thickness, Real x1, Real z1, Real x2, Real z2, Real altitude1, Real altitude2, bool reverse_axis, bool texture_direction, Real tw, Real th, bool autosize, bool legacy_behavior=false)
Definition sbs.cpp:921
bool Load(const std::string &filename, bool force=false)
Definition sound.cpp:386
void Stop()
Definition sound.cpp:292
bool Play(bool reset=true)
Definition sound.cpp:321
void SetLoopState(bool value)
Definition sound.cpp:204
bool IsPlaying()
Definition sound.cpp:254
void ResetTextureMapping(bool todefaults=false)
Definition texture.cpp:1444
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
void SetCase(std::string &string, bool uppercase)
Definition globals.cpp:172
std::string ToString(int number)
Definition globals.cpp:279
void TrimString(std::string &string)
Definition globals.cpp:188
#define SBS_PROFILE(name)
Definition profiler.h:131