Skyscraper 2.0
primitive.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Primitive 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 "primitive.h"
35
36namespace SBS {
37
38Primitive::Primitive(Object *parent, const std::string &name) : Object(parent)
39{
40 //loads a 3D primitive into the simulation
41
42 //set up SBS object
43 SetValues("Primitive", name, false);
44 Offset = Vector3::ZERO;
45 is_key = false;
46 KeyID = 0;
47 global = IsGlobal();
48 always_visible = false;
49 mesh = 0;
50 collider_type = 0;
51}
52
54{
55 if (mesh)
56 {
57 mesh->parent_deleting = true;
58 delete mesh;
59 }
60 mesh = 0;
61
62 //unregister from parent
63 if (sbs->FastDelete == false && parent_deleting == false)
65}
66
67void Primitive::Enabled(bool value)
68{
69 if (always_visible == true)
70 value = true;
71
72 mesh->Enabled(value);
73 EnableLoop(value);
74}
75
77{
78 return mesh->IsEnabled();
79}
80
82{
83 return mesh->IsPhysical();
84}
85
87{
88 std::string type = GetParent()->GetType();
89
90 if (type == "ElevatorCar")
91 static_cast<ElevatorCar*>(GetParent())->RemovePrimitive(this);
92 else if (type == "Floor")
93 static_cast<Floor*>(GetParent())->RemovePrimitive(this);
94 else if (type == "Shaft Level")
95 static_cast<Shaft::Level*>(GetParent())->RemovePrimitive(this);
96 else if (type == "Stairwell Level")
97 static_cast<Stairwell::Level*>(GetParent())->RemovePrimitive(this);
98 else if (type == "SBS")
99 sbs->RemovePrimitive(this);
100}
101
103{
104 std::string type = GetParent()->GetType();
105
106 if (type == "ElevatorCar")
107 static_cast<ElevatorCar*>(GetParent())->AddPrimitive(this);
108 else if (type == "Floor")
109 static_cast<Floor*>(GetParent())->AddPrimitive(this);
110 else if (type == "Shaft Level")
111 static_cast<Shaft::Level*>(GetParent())->AddPrimitive(this);
112 else if (type == "Stairwell Level")
113 static_cast<Stairwell::Level*>(GetParent())->AddPrimitive(this);
114 else if (type == "SBS")
115 sbs->AddPrimitive(this);
116}
117
119{
120 //runloop, called by parent to allow for switching parents
121
122 SBS_PROFILE("Primitive::Loop");
123
124 Floor *floor = GetParent()->ConvertTo<Floor>();
125 Elevator *elevator = GetParent()->ConvertTo<Elevator>();
126 ::SBS::SBS *root = GetParent()->ConvertTo<SBS>();
127
128 //if primitive is a child of a floor or a global object, and is in an elevator, switch parent to elevator
129 if (floor || root)
130 {
131 for (int i = 1; i <= sbs->GetElevatorCount(); i++)
132 {
133 Elevator *elev = sbs->GetElevator(i);
134
135 if (elev)
136 {
137 ElevatorCar *car = elev->IsInElevator(GetPosition());
138 if (car)
139 {
141 ChangeParent(car);
142 AddToParent();
143 break;
144 }
145 }
146 }
147 }
148
149 //if primitive is a child of an elevator, and is moved outside to a floor, switch parent to floor (or make it global)
150 else if (elevator)
151 {
152 if (elevator->IsInElevator(GetPosition()) == 0)
153 {
154 if (global == false)
155 {
156 //switch parent back to floor object
157 int floornum = sbs->GetFloorNumber(GetPosition().y);
158 Floor *floor = sbs->GetFloor(floornum);
159
160 if (floor)
161 {
163 ChangeParent(floor);
164 AddToParent();
165 }
166 }
167 else
168 {
169 //switch parent back to engine root
172 AddToParent();
173 }
174 }
175 }
176}
177
179{
180 //pick up primitive (make prim a child of the camera object)
181
182 if (IsPickedUp() == true)
183 return;
184
185 //pick-up prim (make primitive a child of the camera)
188}
189
191{
192 //drop prim (make primitive a child of the proper non-camera object)
193
194 if (IsPickedUp() == false)
195 return;
196
197 if (global == true)
199 else
200 {
201 bool found = false;
202 for (int i = 1; i < sbs->GetElevatorCount(); i++)
203 {
204 Elevator *elev = sbs->GetElevator(i);
205
206 if (elev)
207 {
208 ElevatorCar *car = elev->IsInElevator(GetPosition());
209 if (car)
210 {
211 ChangeParent(car);
212 found = true;
213 break;
214 }
215 }
216 }
217
218 if (found == false)
219 {
220 int floornum = sbs->GetFloorNumber(GetPosition().y);
221 Floor *floor = sbs->GetFloor(floornum);
222
223 if (floor)
224 ChangeParent(floor);
225 }
226 }
227
228 AddToParent();
229}
230
232{
233 return (GetParent() == sbs->camera);
234}
235
236void Primitive::OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
237{
238 if (right == false)
239 {
240 //do something here
241 }
242}
243
244bool Primitive::Attach(const std::string &meshname, const Vector3 &position, const Vector3 &rotation, Real max_render_distance, Real scale_multiplier, bool enable_physics, Real restitution, Real friction, Real mass)
245{
246 if (meshname == "")
247 return false;
248
249 bool use_collider = true;
250 if (collider_type == -1)
251 use_collider = false;
252
253 mesh = new MeshObject(this, GetName(), 0, "", meshname, max_render_distance, scale_multiplier, use_collider);
254 mesh->EnablePhysics(enable_physics, restitution, friction, mass); //this also creates a collider
255
256 Enabled(true);
257
258 //move to position and specified offset
259 Move(position);
260 SetRotation(rotation);
261
262 Init(false);
263
264 return true;
265}
266
267void Primitive::SetTexture(const std::string &texture)
268{
269 //set texture to be used by this prim
270
271 mesh->SetMaterial(texture);
272}
273
274}
ElevatorCar * IsInElevator(const Vector3 &position, bool camera=false)
bool IsPhysical()
Definition mesh.cpp:491
void Enabled(bool value)
Definition mesh.cpp:154
void SetMaterial(const std::string &material)
Definition mesh.cpp:1199
bool IsEnabled()
Definition mesh.cpp:236
void EnablePhysics(bool value, Real restitution=0, Real friction=0, Real mass=0)
Definition mesh.cpp:1206
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
Vector3 Offset
Definition primitive.h:59
bool always_visible
Definition primitive.h:33
MeshObject * mesh
Definition primitive.h:58
Primitive(Object *parent, const std::string &name)
Definition primitive.cpp:38
void Enabled(bool value)
Definition primitive.cpp:67
void RemoveFromParent()
Definition primitive.cpp:86
void OnClick(Vector3 &position, bool shift, bool ctrl, bool alt, bool right)
void SetTexture(const std::string &texture)
bool IsPhysical()
Definition primitive.cpp:81
bool Attach(const std::string &meshname, 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)
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
Camera * camera
Definition sbs.h:160
Primitive * AddPrimitive(const std::string &name)
Definition sbs.cpp:3184
int GetFloorNumber(Real altitude, int lastfloor=0, bool checklastfloor=false)
Definition sbs.cpp:1584
void RemovePrimitive(Primitive *prim)
Definition sbs.cpp:2857
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
#define SBS_PROFILE(name)
Definition profiler.h:131