]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
ray-scene intersection
[chaz/yoink] / src / GameLayer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <Moof/Engine.hh>
30 #include <Moof/Exception.hh>
31 #include <Moof/Log.hh>
32 #include <Moof/Math.hh>
33 #include <Moof/OpenGL.hh>
34 #include <Moof/Settings.hh>
35 #include <Moof/Video.hh>
36
37 #include "GameLayer.hh"
38 #include "Hud.hh"
39
40 #if HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44
45
46 Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
47 {
48 Mf::Scalar z;
49
50 mState.script.getGlobalTable().pushField("GetZCoord");
51 mState.script.push(position[0]);
52 mState.script.push(position[1]);
53 mState.script.call(2, 1);
54 mState.script.getTop().get(z);
55 mState.script.pop();
56
57 return z;
58 }
59
60 void GameLayer::loadSceneLoader()
61 {
62 mState.script.importStandardLibraries();
63 importLogPrintFunction(mState.script);
64
65 std::string loaderPath = Scene::getPath("loader");
66 if (loaderPath == "")
67 {
68 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
69 }
70
71 Mf::Script::Status status = mState.script.doFile(loaderPath);
72 if (status != Mf::Script::SUCCESS)
73 {
74 std::string str;
75 mState.script[-1].get(str);
76
77 Mf::logScript("%s", str.c_str());
78 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
79 }
80
81 mState.script.getGlobalTable().pushField("scenes");
82 mState.script.getTop().get(mState.sceneList);
83 if (mState.sceneList.size() == 0)
84 {
85 Mf::logScript("no variable `scenes' within loader");
86 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load");
87 }
88 }
89
90 void GameLayer::advanceScene()
91 {
92 if (mState.sceneList.size() != 0)
93 {
94 mState.scene = Scene::alloc(mState.sceneList[0]);
95 mState.sceneList.erase(mState.sceneList.begin());
96
97 Mf::Script::Status status = mState.scene->load(mState.script);
98 if (status != Mf::Script::SUCCESS)
99 {
100 std::string str;
101 mState.script[-1].get(str);
102
103 Mf::logScript("%s", str.c_str());
104 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
105 }
106 }
107 }
108
109
110 GameLayer::GameLayer() :
111 mMusic("NightFusionIntro"),
112 mPunchSound("Thump")
113 {
114 mMusic.setLooping(true);
115 mMusic.enqueue("NightFusionLoop");
116
117 bool isMute = false;
118 Mf::Settings::getInstance().get("nomusic", isMute);
119 if (!isMute) mMusic.play();
120
121 loadSceneLoader();
122 advanceScene(); // load the first scene
123
124 mState.heroine = Heroine::alloc();
125 mState.heroine->animation.startSequence("FlyDiagonallyUp");
126
127 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
128 mState.interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
129
130 setProjection();
131 }
132
133
134 void GameLayer::pushed(Mf::Engine& engine)
135 {
136 engine.push(Hud::alloc(mState));
137
138 mRay.direction.set(1.0, 0.0);
139 mRay3.direction.set(1.0, 0.0, 0.0);
140
141 mLine.a.set(20, 10);
142 mLine.b.set(19, 14);
143
144 mPlane.normal.set(-1.0, 0.0, 0.0);
145 mPlane.d = 0.0;
146
147 mSphere.point.set(22, 5);
148 mSphere.radius = 2;
149 }
150
151
152 void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
153 {
154 mState.camera.update(t, dt);
155 mState.heroine->update(t, dt);
156
157 mState.scene->checkForCollision(*mState.heroine);
158
159 mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
160 -mState.heroine->getState().position[1], -9));
161 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
162
163 //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
164 //Mf::Sound::setListenerPosition(heroinePosition);
165
166 mRay.point = mState.heroine->getState().position;
167 mRay3.point = Mf::promote(mRay.point);
168 mRay3.direction = Mf::promote(mRay.direction);
169
170 Mf::Ray<2>::Intersection meh;
171 Mf::Ray<3>::Intersection meh3;
172
173 Mf::Scalar d = mLine.intersectRay(mRay, meh);
174 if (d > 0.0)
175 {
176 Mf::logDebug("line: d = %f", d);
177 Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
178 Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
179 }
180 //d = mPlane.intersectRay(mRay3, meh3);
181 //if (d > 0.0)
182 //{
183 //Mf::logDebug("plane: d = %f", d);
184 //Mf::logDebug(" P = <%f,%f>", meh3.point[0], meh3.point[1]);
185 //Mf::logDebug(" n = <%f,%f>", meh3.normal[0], meh3.normal[1]);
186 //}
187 d = mSphere.intersectRay(mRay, meh);
188 if (d > 0.0)
189 {
190 Mf::logDebug("sphere: d = %f", d);
191 Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
192 Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
193 }
194
195 std::list<Mf::Ray<2>::Intersection> hits;
196 if (mState.scene->castRay(mRay, hits))
197 {
198 Mf::logDebug("scene: d = %f", d);
199 Mf::logDebug(" P = <%f,%f>", hits.front().point[0], hits.front().point[1]);
200 Mf::logDebug(" n = <%f,%f>", hits.front().normal[0], hits.front().normal[1]);
201 }
202 }
203
204
205 void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
206 {
207 mState.camera.uploadToGL(alpha);
208
209 // DRAW THE SCENE
210 Mf::Texture::resetBind();
211
212 glEnableClientState(GL_VERTEX_ARRAY);
213 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
214
215 mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
216
217 mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
218 mState.heroine->draw(alpha);
219
220 mRay.draw();
221 mLine.draw();
222 mSphere.draw();
223 }
224
225 bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
226 {
227 switch (event.type)
228 {
229 case SDL_KEYDOWN:
230 if (event.key.keysym.sym == SDLK_SPACE)
231 {
232 mState.heroine->animation.startSequence("Flattened");
233 Mf::logInfo("thump!");
234 mPunchSound.play();
235 return true;
236 }
237 else if (event.key.keysym.sym == SDLK_m)
238 {
239 mMusic.toggle();
240 return true;
241 }
242 else if (event.key.keysym.sym == SDLK_PAGEUP)
243 {
244 mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(10.0));
245 return true;
246 }
247 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
248 {
249 mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(-10.0));
250 return true;
251 }
252 return mState.heroine->handleEvent(event);
253
254 case SDL_KEYUP:
255 if (event.key.keysym.sym == SDLK_ESCAPE)
256 {
257 engine.pop(this);
258 return true;
259 }
260 else if (event.key.keysym.sym == SDLK_h)
261 {
262 engine.push(Hud::alloc(mState));
263 return true;
264 }
265 return mState.heroine->handleEvent(event);
266
267 case SDL_MOUSEMOTION:
268 case SDL_MOUSEBUTTONDOWN:
269 mState.camera.handleEvent(event);
270 return true;
271
272 case SDL_VIDEORESIZE:
273 setProjection(event.resize.w, event.resize.h);
274 break;
275 }
276
277 return false;
278 }
279
280
281 void GameLayer::setProjection()
282 {
283 Mf::VideoP video = Mf::Engine::getInstance().getVideo();
284 setProjection(video->getWidth(), video->getHeight());
285 }
286
287 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
288 {
289 mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
290 }
291
292
293 /** vim: set ts=4 sw=4 tw=80: *************************************************/
294
This page took 0.043058 seconds and 4 git commands to generate.