Debian Codegraph @

src/game/trackobjectfactory.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2011 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "trackobjectfactory.hpp"
17
18 #include "layers.hpp"
19 #include "pit.hpp"
20 #include "renderer.hpp"
21 #include "trackobject.hpp"
22 #include "tree.hpp"
23
24 #include <MCAssetManager>
25 #include <MCObject>
26 #include <MCObjectFactory>
27 #include <MCPhysicsComponent>
28 #include <MCRandom>
29 #include <MCShape>
30 #include <MCShapeView>
31 #include <MCSurface>
32
33 #include "simple_logger.hpp"
34
35 namespace {
36 static const float DEFAULT_DIFFUSE_COEFF = 1.5f;
37 }
38
39 TrackObjectFactory::TrackObjectFactory(MCObjectFactory & objectFactory)
40 : m_objectFactory(objectFactory)
41 {
42 }
43
44 TrackObject * TrackObjectFactory::build(
45 QString category, QString role, MCVector2dF location, int angle, bool forceStationary)
46 {
47 MCObjectPtr object;
48
49 if (role == "brake" || role == "left" || role == "right")
50 {
51 MCSurfaceObjectData data(role.toStdString());
52 data.setInitialLocation(location);
53 data.setInitialAngle(angle);
54 data.setXYFriction(1.0);
55 data.setRestitution(0.5);
56 data.setMass(1000);
57 data.setSurfaceId(role.toStdString());
58
59 object = m_objectFactory.build(data);
60 object->shape()->view()->setShaderProgram(Renderer::instance().program("defaultSpecular"));
61 object->shape()->view()->object()->material()->setDiffuseCoeff(DEFAULT_DIFFUSE_COEFF);
62 }
63 else if (role == "bushArea")
64 {
65 MCSurfaceObjectData data(role.toStdString());
66 data.setInitialLocation(location);
67 data.setInitialAngle(angle);
68 data.setIsStationary(true);
69 data.setSurfaceId(role.toStdString());
70
71 object = m_objectFactory.build(data);
72 object->setIsPhysicsObject(false);
73 }
74 else if (role == "crate")
75 {
76 MCMeshObjectData data(role.toStdString());
77 data.setInitialLocation(MCVector3dF(location.i(), location.j(), 0));
78 data.setInitialAngle(angle);
79 data.setXYFriction(1.0);
80 data.setMass(1000);
81 data.setMeshId(role.toStdString());
82 data.setSurfaceId(role.toStdString());
83 data.setRestitution(0.9f);
84
85 object = m_objectFactory.build(data);
86 object->shape()->view()->object()->material()->setDiffuseCoeff(DEFAULT_DIFFUSE_COEFF);
87 }
88 else if (role == "dustRacing2DBanner")
89 {
90 MCSurfaceObjectData data(role.toStdString());
91 data.setInitialLocation(location);
92 data.setInitialAngle(angle);
93 data.setXYFriction(1.0);
94 data.setRestitution(0.5);
95
96 data.setMass(100000);
97 data.setSurfaceId(role.toStdString());
98
99 object = m_objectFactory.build(data);
100 object->shape()->view()->setShaderProgram(Renderer::instance().program("defaultSpecular"));
101 object->shape()->view()->object()->material()->setDiffuseCoeff(DEFAULT_DIFFUSE_COEFF);
102 }
103 else if (role == "grandstand")
104 {
105 MCSurfaceObjectData data(role.toStdString());
106 data.setInitialLocation(location);
107 data.setInitialAngle(angle);
108 data.setXYFriction(1.0);
109 data.setRestitution(0.5);
110 data.setMass(100000);
111 data.setSurfaceId(role.toStdString());
112
113 object = m_objectFactory.build(data);
114 object->shape()->view()->object()->material()->setDiffuseCoeff(DEFAULT_DIFFUSE_COEFF);
115 }
116 else if (role == "plant")
117 {
118 const float plantBodyRadius = 4;
119
120 MCSurfaceObjectData data(role.toStdString());
121 data.setInitialLocation(location);
122 data.setInitialAngle(angle);
123 data.setXYFriction(1.0);
124 data.setMass(250);
125 data.setSurfaceId(role.toStdString());
126 data.setRestitution(0.25);
127 data.setShapeWidth(plantBodyRadius);
128 data.setShapeHeight(plantBodyRadius);
129
130 object = m_objectFactory.build(data);
131 }
132 else if (role == "rock")
133 {
134 MCSurfaceObjectData data(role.toStdString());
135 data.setInitialLocation(location);
136 data.setInitialAngle(angle);
137 data.setXYFriction(1.0);
138 data.setMass(2500);
139 data.setSurfaceId(role.toStdString());
140 data.setRestitution(0.9f);
141
142 object = m_objectFactory.build(data);
143 }
144 else if (
145 role == "grid" || role == "sandAreaCurve" || role == "sandAreaBig")
146 {
147 MCSurfaceObjectData data(role.toStdString());
148 data.setInitialLocation(location);
149 data.setInitialAngle(angle);
150 data.setIsStationary(true);
151 data.setSurfaceId(role.toStdString());
152
153 object = m_objectFactory.build(data);
154 object->setIsPhysicsObject(false);
155 object->shape()->view()->setHasShadow(false);
156 }
157 else if (role == "pit")
158 {
159 object = std::make_shared<Pit>(MCAssetManager::surfaceManager().surface("pit"));
160 object->setInitialLocation(location);
161 object->setInitialAngle(angle);
162 }
163 else if (role == "tire")
164 {
165 MCSurfaceObjectData data(role.toStdString());
166 data.setDefaultCircleShape(true);
167 data.setInitialLocation(location);
168 data.setInitialAngle(angle);
169 data.setXYFriction(1.0);
170 data.setRestitution(0.5);
171 data.setMass(500); // Exaggerate the mass on purpose
172 data.setSurfaceId(role.toStdString());
173 data.setXYFriction(0.25);
174
175 object = m_objectFactory.build(data);
176 object->shape()->view()->setShaderProgram(Renderer::instance().program("defaultSpecular"));
177 }
178 else if (role == "tree")
179 {
180 const int height = static_cast<int>(200 + 200 * MCRandom::getValue());
181 object = std::make_shared<Tree>(MCAssetManager::surfaceManager().surface("tree"),
182 1.0f + 0.50f * MCRandom::getValue(),
183 0.1f + 0.2f * MCRandom::getValue(),
184 height,
185 height / 10);
186 object->setInitialLocation(location);
187 object->setInitialAngle(angle);
188 }
189 else if (role == "wall" || role == "wallLong")
190 {
191 MCSurfaceObjectData data(role.toStdString());
192 data.setInitialLocation(location);
193 data.setInitialAngle(angle);
194 data.setXYFriction(1.0);
195 data.setIsStationary(true);
196 data.setRestitution(0.9f);
197 data.setInitialLocation(MCVector3dF(location.i(), location.j(), 8));
198
199 object = m_objectFactory.build(data);
200 object->shape()->view()->setShaderProgram(Renderer::instance().program("defaultSpecular"));
201 }
202
203 if (!object)
204 {
205 juzzlin::L().warning() << "Unknown or deprecated object '" << role.toStdString() << "'";
206 return nullptr;
207 }
208 else
209 {
210 // Override mass if object is forced as stationary
211 if (forceStationary)
212 {
213 object->physicsComponent().setMass(0, true);
214 }
215
216 // Wrap the MCObject in a TrackObject
217 return new TrackObject(category, role, object);
218 }
219 }