Skyscraper 2.0
model.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Model 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 "mesh.h"
27#include "floor.h"
28#include "elevator.h"
29#include "elevatorcar.h"
30#include "shaft.h"
31#include "stairs.h"
32#include "camera.h"
33#include "profiler.h"
34#include "model.h"
35
36namespace SBS {
37
38Model::Model(Object *parent, const std::string &name, const std::string &filename, bool center, const Vector3 &position, const Vector3 &rotation, Real max_render_distance, Real scale_multiplier, bool enable_physics, Real restitution, Real friction, Real mass) : Object(parent)
39{
40 //loads a 3D model into the simulation
41
42 //set up SBS object
43 SetValues("Model", name, false);
44 Offset = Vector3::ZERO;
45 is_key = false;
46 KeyID = 0;
47 global = IsGlobal();
48 this->center = center;
49
50 if (filename == "")
51 custom = true;
52 else
53 custom = false;
54
55 load_error = false;
56 mesh = new MeshObject(this, name, 0, filename, "", max_render_distance, scale_multiplier);
57
58 if (mesh->model_loaded == false && filename != "")
59 {
60 load_error = true;
61 return;
62 }
63
64 mesh->EnablePhysics(enable_physics, restitution, friction, mass); //this also creates a collider
65 Enabled(true);
66
67 //move to position and specified offset
68 Move(position);
69 SetRotation(rotation);
70
71 if (filename != "")
72 Init(false);
73}
74
76{
77 if (mesh)
78 {
79 mesh->parent_deleting = true;
80 delete mesh;
81 }
82 mesh = 0;
83
84 //unregister from parent
85 if (sbs->FastDelete == false && parent_deleting == false)
87}
88
89void Model::Enabled(bool value)
90{
91 mesh->Enabled(value);
92 EnableLoop(value);
93}
94
96{
97 return mesh->IsEnabled();
98}
99
101{
102 return is_key;
103}
104
106{
107 return KeyID;
108}
109
110void Model::SetKey(int keyid)
111{
112 is_key = true;
113 KeyID = keyid;
114}
115
117{
118 return mesh->IsPhysical();
119}
120
122{
123 std::string type = GetParent()->GetType();
124
125 if (type == "ElevatorCar")
126 static_cast<ElevatorCar*>(GetParent())->RemoveModel(this);
127 else if (type == "Floor")
128 static_cast<Floor*>(GetParent())->RemoveModel(this);
129 else if (type == "Shaft Level")
130 static_cast<Shaft::Level*>(GetParent())->RemoveModel(this);
131 else if (type == "Stairwell Level")
132 static_cast<Stairwell::Level*>(GetParent())->RemoveModel(this);
133 else if (type == "SBS")
134 sbs->RemoveModel(this);
135}
136
138{
139 std::string type = GetParent()->GetType();
140
141 if (type == "ElevatorCar")
142 static_cast<ElevatorCar*>(GetParent())->AddModel(this);
143 else if (type == "Floor")
144 static_cast<Floor*>(GetParent())->AddModel(this);
145 else if (type == "Shaft Level")
146 static_cast<Shaft::Level*>(GetParent())->AddModel(this);
147 else if (type == "Stairwell Level")
148 static_cast<Stairwell::Level*>(GetParent())->AddModel(this);
149 else if (type == "SBS")
150 sbs->AddModel(this);
151}
152
154{
155 //runloop, called by parent to allow for switching parents
156
157 SBS_PROFILE("Model::Loop");
158
159 Floor *floor = GetParent()->ConvertTo<Floor>();
160 Elevator *elevator = GetParent()->ConvertTo<Elevator>();
161 ::SBS::SBS *root = GetParent()->ConvertTo<SBS>();
162
163 //if model is a child of a floor or a global object, and is in an elevator, switch parent to elevator
164 if (floor || root)
165 {
166 for (int i = 1; i <= sbs->GetElevatorCount(); i++)
167 {
168 Elevator *elev = sbs->GetElevator(i);
169
170 if (elev)
171 {
172 ElevatorCar *car = elev->IsInElevator(GetPosition());
173 if (car)
174 {
176 ChangeParent(car);
177 AddToParent();
178 break;
179 }
180 }
181 }
182 }
183
184 //if model is a child of an elevator, and is moved outside to a floor, switch parent to floor (or make it global)
185 else if (elevator)
186 {
187 if (elevator->IsInElevator(GetPosition()) == 0)
188 {
189 if (global == false)
190 {
191 //switch parent back to floor object
192 int floornum = sbs->GetFloorNumber(GetPosition().y);
193 Floor *floor = sbs->GetFloor(floornum);
194
195 if (floor)
196 {
198 ChangeParent(floor);
199 AddToParent();
200 }
201 }
202 else
203 {
204 //switch parent back to engine root
207 AddToParent();
208 }
209 }
210 }
211}
212
214{
215 //pick up model (make model a child of the camera object)
216
217 if (IsPickedUp() == true)
218 return;
219
220 //pick-up model (make model a child of the camera)
223}
224
226{
227 //drop model (make model a child of the proper non-camera object)
228
229 if (IsPickedUp() == false)
230 return;
231
232 if (global == true)
234 else
235 {
236 bool found = false;
237 for (int i = 1; i < sbs->GetElevatorCount(); i++)
238 {
239 Elevator *elev = sbs->GetElevator(i);
240
241 if (elev)
242 {
243 ElevatorCar *car = elev->IsInElevator(GetPosition());
244 if (car)
245 {
246 ChangeParent(car);
247 found = true;
248 break;
249 }
250 }
251 }
252
253 if (found == false)
254 {
255 int floornum = sbs->GetFloorNumber(GetPosition().y);
256 Floor *floor = sbs->GetFloor(floornum);
257
258 if (floor)
259 ChangeParent(floor);
260 }
261 }
262
263 AddToParent();
264}
265
267{
268 return (GetParent() == sbs->camera);
269}
270
272{
273 if (center == true)
274 {
275 Offset = mesh->GetOffset();
276
277 //move mesh object to specified offset
278 mesh->Move(Offset);
279 }
280}
281
282void Model::OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
283{
284 if (right == false)
285 {
286 //if model is a key, add key to keyring and delete model
287 if (IsKey() == true)
288 {
289 sbs->AddKey(GetKeyID(), GetName());
290 SelfDestruct(); //self-destruct this model
291 return;
292 }
293 }
294}
295
296}
ElevatorCar * IsInElevator(const Vector3 &position, bool camera=false)
bool IsPhysical()
Definition mesh.cpp:491
bool model_loaded
Definition mesh.h:109
void Enabled(bool value)
Definition mesh.cpp:154
Vector3 GetOffset()
Definition mesh.cpp:496
bool IsEnabled()
Definition mesh.cpp:236
void EnablePhysics(bool value, Real restitution=0, Real friction=0, Real mass=0)
Definition mesh.cpp:1206
void PickUp()
Definition model.cpp:213
bool is_key
Definition model.h:58
void Enabled(bool value)
Definition model.cpp:89
Model(Object *parent, const std::string &name, const std::string &filename, bool center, const Vector3 &position, const Vector3 &rotation, Real max_render_distance=0, Real scale_multiplier=1, bool enable_physics=false, Real restitution=0, Real friction=0, Real mass=0)
Definition model.cpp:38
int KeyID
Definition model.h:59
bool custom
Definition model.h:62
bool load_error
Definition model.h:32
bool global
Definition model.h:60
void Loop()
Definition model.cpp:153
void AddToParent()
Definition model.cpp:137
Vector3 Offset
Definition model.h:57
MeshObject * mesh
Definition model.h:56
bool IsPhysical()
Definition model.cpp:116
void OnInit()
Definition model.cpp:271
bool IsKey()
Definition model.cpp:100
bool center
Definition model.h:61
void SetKey(int keyid)
Definition model.cpp:110
int GetKeyID()
Definition model.cpp:105
bool IsEnabled()
Definition model.cpp:95
void Drop()
Definition model.cpp:225
void OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
Definition model.cpp:282
void RemoveFromParent()
Definition model.cpp:121
bool IsPickedUp()
Definition model.cpp:266
const std::string & GetName()
Definition object.cpp:53
Object * GetParent()
Definition object.cpp:42
bool parent_deleting
Definition object.h:64
virtual void SetRotation(const Vector3 &rotation)
Definition object.cpp:332
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 ChangeParent(Object *new_parent)
Definition object.cpp:445
void Init(bool children=true)
Definition object.cpp:492
void EnableLoop(bool value)
Definition object.cpp:521
bool IsGlobal()
Definition object.cpp:482
const std::string & GetType()
Definition object.cpp:177
T * ConvertTo()
Definition object.h:119
bool SelfDestruct()
Definition object.cpp:589
Elevator * GetElevator(int number)
Definition sbs.cpp:1746
int GetElevatorCount()
Definition sbs.cpp:1703
Floor * GetFloor(int number)
Definition sbs.cpp:1739
bool FastDelete
Definition sbs.h:188
void RemoveModel(Model *model)
Definition sbs.cpp:2844
Camera * camera
Definition sbs.h:160
void AddKey(int keyid, const std::string &name)
Definition sbs.cpp:3899
int GetFloorNumber(Real altitude, int lastfloor=0, bool checklastfloor=false)
Definition sbs.cpp:1584
Model * AddModel(const std::string &name, const std::string &filename, bool center, const Vector3 &position, const Vector3 &rotation, Real max_render_distance=0, Real scale_multiplier=1, bool enable_physics=false, Real restitution=0, Real friction=0, Real mass=0)
Definition sbs.cpp:3155
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
#define SBS_PROFILE(name)
Definition profiler.h:131