Skyscraper 2.0
revolvingdoor.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Revolving Door 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 "manager.h"
27#include "mesh.h"
28#include "floor.h"
29#include "texture.h"
30#include "sound.h"
31#include "profiler.h"
32#include "revolvingdoor.h"
33
34namespace SBS {
35
36RevolvingDoor::RevolvingDoor(Object *parent, DynamicMesh *wrapper, const std::string &name, bool run, const std::string &soundfile, const std::string &texture, Real thickness, bool clockwise, int segments, Real speed, Real rotation, Real CenterX, Real CenterZ, Real width, Real height, Real voffset, Real tw, Real th) : Object(parent), Lock(this)
37{
38 //creates a revolving door
39 //wall cuts must be performed by the calling (parent) function
40 //segments can be 2-4
41
42 //set up SBS object
43 SetValues("RevolvingDoor", name, false);
44
45 is_enabled = true;
46 IsMoving = false;
47 this->rotation = 0;
48 this->soundfile = soundfile;
49 Speed = speed;
50 sound = 0;
51 brake = false;
52 this->run = false;
53
54 //set speed to default value if invalid
55 if (Speed <= 0)
56 Speed = sbs->GetConfigFloat("Skyscraper.SBS.RevolvingDoorSpeed", 75.0);
57
58 if (Speed <= 0)
59 Speed = 75;
60
61 Vector3 position (CenterX, voffset, CenterZ);
62
63 Clockwise = clockwise;
64
65 //Create mesh
66 DoorMesh = new MeshObject(this, name, wrapper);
67 Move(position);
68
69 //create sound object
70 if (soundfile != "")
71 sound = new Sound(this, "DoorSound", true);
72
73 //create door
74 sbs->DrawWalls(true, true, true, true, true, true);
76
77 Wall *wall = DoorMesh->CreateWallObject(name);
78
79 Segments = segments;
80 width = width / 2;
81 if (Segments == 3)
82 {
83 sbs->GetTextureManager()->SetTextureFlip(1, 0, 0, 0, 0, 0); //flip texture on rear side of door
84 sbs->AddWallMain(wall, name, texture, thickness, -width, -width, 0, 0, height, height, 0, 0, tw, th, false);
85 sbs->AddWallMain(wall, name, texture, thickness, 0, 0, width, -width, height, height, 0, 0, tw, th, false);
86 sbs->GetTextureManager()->SetTextureFlip(0, 1, 0, 0, 0, 0); //flip texture on rear side of door
87 sbs->AddWallMain(wall, name, texture, thickness, 0, 0, 0, width, height, height, 0, 0, tw, th, false);
88 }
89 else
90 {
91 sbs->GetTextureManager()->SetTextureFlip(0, 1, 0, 0, 0, 0); //flip texture on rear side of door
92 sbs->AddWallMain(wall, name, texture, thickness, -width, 0, 0, 0, height, height, 0, 0, tw, th, false);
93 sbs->GetTextureManager()->SetTextureFlip(1, 0, 0, 0, 0, 0); //flip texture on rear side of door
94 sbs->AddWallMain(wall, name, texture, thickness, 0, 0, width, 0, height, height, 0, 0, tw, th, false);
95 if (Segments == 4)
96 {
97 sbs->GetTextureManager()->SetTextureFlip(1, 0, 0, 0, 0, 0); //flip texture on rear side of door
98 sbs->AddWallMain(wall, name, texture, thickness, 0, -width, 0, 0, height, height, 0, 0, tw, th, false);
99 sbs->GetTextureManager()->SetTextureFlip(0, 1, 0, 0, 0, 0); //flip texture on rear side of door
100 sbs->AddWallMain(wall, name, texture, thickness, 0, 0, 0, width, height, height, 0, 0, tw, th, false);
101 }
102 }
103 sbs->ResetWalls();
105
107
108 //start motion on startup, if specified
109 if (run == true)
110 Run(true);
111}
112
114{
115 //destructor
116
117 if (sound)
118 {
119 sound->parent_deleting = true;
120 delete sound;
121 }
122 sound = 0;
123
124 if (DoorMesh)
125 {
127 delete DoorMesh;
128 }
129 DoorMesh = 0;
130
131 //unregister from parent
132 if (sbs->FastDelete == false)
133 {
134 if (parent_deleting == false)
135 {
136 std::string type = GetParent()->GetType();
137
138 if (type == "Floor")
139 static_cast<Floor*>(GetParent())->RemoveRevolvingDoor(this);
140 else if (type == "RevolvingDoorManager")
142 }
143 }
144}
145
147{
148 if (sbs->GetPower() == false)
149 return;
150
151 if (sbs->Verbose)
152 Report("Moving");
153
154 EnableLoop(true);
155
156 //check lock state
157 if (IsLocked() == true)
158 {
159 ReportError("Is locked");
160 return;
161 }
162
163 if (sound)
164 {
166 sound->Play();
167 }
168
169 IsMoving = true;
170 brake = false;
171}
172
174{
175 if (is_enabled == value)
176 return;
177
178 DoorMesh->Enabled(value);
179 is_enabled = value;
180}
181
183{
184 SBS_PROFILE("RevolvingDoor::Loop");
185
186 if (sbs->GetPower() == false)
187 return;
188
189 if (IsMoving == true)
190 MoveDoor();
191 else
192 EnableLoop(false);
193}
194
196{
197 SBS_PROFILE("RevolvingDoor::MoveDoor");
198
199 //door movement callback function
200 if ((Clockwise == true && brake == false) || (Clockwise == false && brake == true))
201 {
202 if (rotation < Speed)
203 rotation += Speed * sbs->delta;
204 else if (run == false)
205 brake = !brake;
206 }
207 else if ((Clockwise == false && brake == false) || (Clockwise == true && brake == true))
208 {
209 if (rotation > -Speed)
210 rotation -= Speed * sbs->delta;
211 else if (run == false)
212 brake = !brake;
213 }
214
215 if (brake == true)
216 {
217 if ((Clockwise == true && rotation <= 0) || (Clockwise == false && rotation >= 0))
218 IsMoving = false;
219 }
220
221 Rotate(0, rotation, 0);
222}
223
224void RevolvingDoor::OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
225{
226 if (right == false)
227 {
228 //toggle lock status if ctrl and shift are pressed
229 if (ctrl == true && shift == true)
230 ToggleLock();
231 }
232}
233
234void RevolvingDoor::Report(const std::string &message)
235{
236 //general reporting function
237 Object::Report("Revolving Door " + GetName() + ": " + message);
238}
239
240bool RevolvingDoor::ReportError(const std::string &message)
241{
242 //general error reporting function
243 return Object::ReportError("Revolving Door " + GetName() + ": " + message);
244}
245
246void RevolvingDoor::Run(bool value)
247{
248 //start or stop the revolving door
249
250 if (sbs->GetPower() == false)
251 return;
252
253 run = value;
254
255 if (value == true)
256 OnHit();
257 else
258 brake = true;
259}
260
261}
bool IsLocked()
Definition lock.cpp:66
bool ToggleLock(bool force=false)
Definition lock.cpp:44
void Enabled(bool value)
Definition mesh.cpp:154
Wall * CreateWallObject(const std::string &name)
Definition mesh.cpp:208
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
bool parent_deleting
Definition object.h:64
virtual void Move(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:253
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
virtual Vector3 GetRotation()
Definition object.cpp:367
void EnableLoop(bool value)
Definition object.cpp:521
const std::string & GetType()
Definition object.cpp:177
virtual void Rotate(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:353
void RemoveDoor(RevolvingDoor *door)
Definition manager.cpp:835
MeshObject * DoorMesh
bool ReportError(const std::string &message)
void Enabled(bool value)
void OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
std::string soundfile
void Run(bool value)
RevolvingDoor(Object *parent, DynamicMesh *wrapper, const std::string &name, bool run, const std::string &soundfile, const std::string &texture, Real thickness, bool clockwise, int segments, Real speed, Real rotation, Real CenterX, Real CenterZ, Real width, Real height, Real voffset, Real tw, Real th)
void Report(const std::string &message)
bool GetPower()
Definition sbs.cpp:4701
Real GetConfigFloat(const std::string &key, Real default_value)
Definition sbs.cpp:3249
bool AddWallMain(Wall *wallobject, const std::string &name, const std::string &texture, Real thickness, Real x1, Real z1, Real x2, Real z2, Real height_in1, Real height_in2, Real altitude1, Real altitude2, Real tw, Real th, bool autosize)
Definition sbs.cpp:690
void ResetWalls(bool ToDefaults=false)
Definition sbs.cpp:1854
bool FastDelete
Definition sbs.h:188
TextureManager * GetTextureManager()
Definition sbs.cpp:4558
Real delta
Definition sbs.h:134
void DrawWalls(bool MainN, bool MainP, bool SideN, bool SideP, bool Top, bool Bottom)
Definition sbs.cpp:1833
RevolvingDoorManager * GetRevolvingDoorManager()
Definition sbs.cpp:4563
bool Verbose
Definition sbs.h:186
bool Load(const std::string &filename, bool force=false)
Definition sound.cpp:386
bool Play(bool reset=true)
Definition sound.cpp:321
void SetTextureFlip(int mainneg, int mainpos, int sideneg, int sidepos, int top, int bottom)
Definition texture.cpp:1569
void ResetTextureMapping(bool todefaults=false)
Definition texture.cpp:1444
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
#define SBS_PROFILE(name)
Definition profiler.h:131