Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GameLogic.cpp
Go to the documentation of this file.
1 #include "Application.hpp"
2 
3 
5  : m_app(app)
6  , m_activeScene(0)
7 {
8  m_scenes.push_back("Static Plane");
9  m_scenes.push_back("Static Cloud");
10  m_scenes.push_back("Dynamic Plane");
11  m_scenes.push_back("Dynamic Cloud");
12 
13  auto meshPlane = m_app->getSimpleMeshes().get("plane");
14  auto meshBoblamp = m_app->getScenes().get("bob\\boblampclean.md5mesh");
15 
16  for (size_t i = 0; i < m_scenes.size(); i++)
17  {
18  if (m_scenes[i] == m_app->getConfig().sceneName())
19  {
20  m_activeScene = i;
21  break;
22  }
23  }
24 
25  initScene();
26 }
27 void GameLogic::update(double time, double timeDelta)
28 {
29 }
31 {
32  //clear old scene
33  {
34  vector<ISceneObject*> objects(m_app->getSceneGraph().begin(),
35  m_app->getSceneGraph().end());
36 
37  for (auto obj : objects)
38  m_app->getSceneGraph().remove(obj);
39  }
40 
41  //init new scene
42  switch (m_activeScene)
43  {
44  case 0: initStaticPlane(); break;
45  case 1: initStaticCloud(); break;
46  case 2: initDynamicPlane(); break;
47  default:
48  case 3: initDynamicCloud(); break;
49  }
50 }
51 
53 {
54  auto meshPlane = m_app->getSimpleMeshes().get("plane");
55  auto meshBoblamp = m_app->getScenes().get("bob\\boblampclean.md5mesh");
56 
57  // create plane
58  {
59  auto& plane = *(meshPlane->createInstance(false));
60 
61  plane.getTransform().setPosition(vec3(0.0f, -20.0f, 0.0f));
62  plane.getTransform().setScale(1000.0f);
63  plane.uniform("color", vec3(1.0f) * 0.4f);
64  }
65 
66  list<SceneInstance*> bobs;
67 
68  // create bobs
69  {
70  for (size_t i = 0; i < 100; i++)
71  {
72  auto& boblamp = *(meshBoblamp->createInstance());
73 
74  boblamp.getTransform().setScale(Util::rand(0.1f, 0.2f));
75  boblamp.getTransform().setRotation(vec3(0.0f, Util::rand(0.0f, glm::radians(360.0f)), 0.0f));
76 
77  do
78  {
79  boblamp.getTransform().setPosition(vec3(Util::rand(-1.0f, 1.0f)*200.0f,
80  -20.0f,
81  Util::rand(-1.0f, 1.0f)*200.0f));
82  } while (any_of(bobs.begin(), bobs.end(), [&boblamp](SceneInstance * instance)
83  {
84  return glm::distance(boblamp.getTransform().getPosition(), instance->getTransform().getPosition()) <= (boblamp.getTransform().getScale() * 20 + instance->getTransform().getScale() * 20);
85  }));
86 
87  bobs.push_back(&boblamp);
88  }
89  }
90 
91  // set camera
92  {
93  auto& cam = m_app->getCamera().getTransform();
94 
95  cam.setPosition(vec3(94.0f,59.0f,83.0f));
96  cam.setRotation(vec3(-0.7f, 0.94f, 0.0f));
97  }
98 }
99 
101 {
102  auto meshPlane = m_app->getSimpleMeshes().get("plane");
103  auto meshBoblamp = m_app->getScenes().get("bob\\boblampclean.md5mesh");
104 
105  // create plane
106  {
107  auto& plane = *(meshPlane->createInstance(false));
108 
109  plane.getTransform().setPosition(vec3(0.0f, -20.0f, 0.0f));
110  plane.getTransform().setScale(1000.0f);
111  plane.uniform("color", vec3(1.0f) * 0.3f);
112  }
113 
114  list<SceneInstance*> bobs;
115 
116  // create bobs
117  {
118  for (size_t i = 0; i < 100; i++)
119  {
120  auto& boblamp = *(meshBoblamp->createInstance());
121 
122  boblamp.getTransform().setScale(Util::rand(0.1f, 0.2f));
123  boblamp.getTransform().setRotation(vec3(0.0f, Util::rand(0.0f, glm::radians(360.0f)), 0.0f));
124 
125  do
126  {
127  boblamp.getTransform().setPosition(vec3(Util::rand(-1.0f, 1.0f)*200.0f,
128  -20.0f,
129  Util::rand(-1.0f, 1.0f)*200.0f));
130  } while (any_of(bobs.begin(), bobs.end(), [&boblamp](SceneInstance * instance)
131  {
132  return glm::distance(boblamp.getTransform().getPosition(), instance->getTransform().getPosition()) <= (boblamp.getTransform().getScale() * 20 + instance->getTransform().getScale() * 20);
133  }));
134 
135 
136  boblamp.queueAnimation(Util::rand(-5.0f, 0.0f), 0u);
137 
138  bobs.push_back(&boblamp);
139  }
140  }
141 
142  // set camera
143  {
144  auto& cam = m_app->getCamera().getTransform();
145 
146  cam.setPosition(vec3(94.0f,59.0f,83.0f));
147  cam.setRotation(vec3(-0.7f, 0.94f, 0.0f));
148  }
149 }
150 
152 {
153  auto meshBoblamp = m_app->getScenes().get("bob\\boblampclean.md5mesh");
154 
155  list<SceneInstance*> bobs;
156 
157  // create bobs
158  {
159  for (size_t i = 0; i < 100; i++)
160  {
161  auto& boblamp = *(meshBoblamp->createInstance());
162 
163  boblamp.getTransform().setScale(Util::rand(0.1f, 0.2f));
164  boblamp.getTransform().setRotation(vec3(0.0f, Util::rand(0.0f, glm::radians(360.0f)), 0.0f));
165 
166  do
167  {
168  boblamp.getTransform().setPosition(vec3(Util::rand(-1.0f, 1.0f)*200.0f,
169  Util::rand(-1.0f, 1.0f)*200.0f,
170  Util::rand(-1.0f, 1.0f)*200.0f));
171  } while (any_of(bobs.begin(), bobs.end(), [&boblamp](SceneInstance * instance)
172  {
173  return glm::distance(boblamp.getTransform().getPosition(), instance->getTransform().getPosition()) <= (boblamp.getTransform().getScale() * 20 + instance->getTransform().getScale() * 20);
174  }));
175 
176 
177  boblamp.queueAnimation(Util::rand(-5.0f, 0.0f), 0u);
178 
179  bobs.push_back(&boblamp);
180  }
181  }
182 
183  // set camera
184  {
185  auto& cam = m_app->getCamera().getTransform();
186 
187  cam.setPosition(vec3(94.0f,59.0f,83.0f));
188  cam.setRotation(vec3(-0.7f, 0.94f, 0.0f));
189  }
190 }
191 
193 {
194  auto meshBoblamp = m_app->getScenes().get("bob\\boblampclean.md5mesh");
195 
196  list<SceneInstance*> bobs;
197 
198  // create bobs
199  {
200  for (size_t i = 0; i < 100; i++)
201  {
202  auto& boblamp = *(meshBoblamp->createInstance());
203 
204  boblamp.getTransform().setScale(Util::rand(0.1f, 0.2f));
205  boblamp.getTransform().setRotation(vec3(0.0f, Util::rand(0.0f, glm::radians(360.0f)), 0.0f));
206 
207  do
208  {
209  boblamp.getTransform().setPosition(vec3(Util::rand(-1.0f, 1.0f)*200.0f,
210  Util::rand(-1.0f, 1.0f)*200.0f,
211  Util::rand(-1.0f, 1.0f)*200.0f));
212  } while (any_of(bobs.begin(), bobs.end(), [&boblamp](SceneInstance * instance)
213  {
214  return glm::distance(boblamp.getTransform().getPosition(), instance->getTransform().getPosition()) <= (boblamp.getTransform().getScale() * 20 + instance->getTransform().getScale() * 20);
215  }));
216 
217 
218  bobs.push_back(&boblamp);
219  }
220  }
221 
222  // set camera
223  {
224  auto& cam = m_app->getCamera().getTransform();
225 
226  cam.setPosition(vec3(94.0f,59.0f,83.0f));
227  cam.setRotation(vec3(-0.7f, 0.94f, 0.0f));
228  }
229 }
230 void GameLogic::onKeyStateChanged(int key, bool pressed)
231 {
232  if (!pressed) return;
233 
234  if (key == GLFW_KEY_TAB)
235  nextScene();
236 }
void initScene()
Definition: GameLogic.cpp:30
string const & sceneName() const
Configuration & getConfig()
Definition: Application.hpp:36
Scenes & getScenes()
Definition: Application.hpp:71
void onKeyStateChanged(int key, bool pressed)
Definition: GameLogic.cpp:230
SimpleMeshes & getSimpleMeshes()
Definition: Application.hpp:66
object_list::iterator begin()
Definition: SceneGraph.cpp:22
void initDynamicCloud()
Definition: GameLogic.cpp:151
void initStaticPlane()
Definition: GameLogic.cpp:52
void remove(ISceneObject *&object)
Definition: SceneGraph.cpp:13
T rand(T min, T max)
Definition: Util.hpp:30
Transformation & getTransform()
Definition: Camera.hpp:23
void initDynamicPlane()
Definition: GameLogic.cpp:100
Scene * get(string const &name)
Definition: Scene.cpp:829
uint m_activeScene
Definition: GameLogic.hpp:13
Camera & getCamera()
Definition: Application.hpp:76
SimpleMesh * get(string const &name)
Definition: SimpleMesh.cpp:176
object_list::iterator end()
Definition: SceneGraph.cpp:26
SceneGraph & getSceneGraph()
Definition: Application.hpp:56
void nextScene()
Definition: GameLogic.hpp:31
Application * m_app
Definition: GameLogic.hpp:10
GameLogic(Application *app)
Definition: GameLogic.cpp:4
vector< string > m_scenes
Definition: GameLogic.hpp:12
void setPosition(vec3 const &position)
void initStaticCloud()
Definition: GameLogic.cpp:192
void update(double time, double timeDelta)
Definition: GameLogic.cpp:27