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