]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
finally fixed broken main loop
[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/Error.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
39 #if HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43
44 void GameLayer::loadSceneLoader()
45 {
46 mState.script.importStandardLibraries();
47 importLogFunctions(mState.script);
48
49 std::string loaderPath = Scene::getPath("loader");
50 if (loaderPath == "")
51 {
52 throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
53 }
54
55 Mf::Script::Result status = mState.script.doFile(loaderPath);
56 if (status != Mf::Script::SUCCESS)
57 {
58 std::string str;
59 mState.script[-1].get(str);
60
61 throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
62 }
63
64 mState.script.getGlobalTable().pushField("scenes");
65 mState.script.getTop().get(mState.sceneList);
66 if (mState.sceneList.size() == 0)
67 {
68 throw Mf::Error(Mf::Error::SCRIPT_ERROR,
69 "no variable `scenes' within loader");
70 }
71 }
72
73 void GameLayer::advanceScene()
74 {
75 if (mState.sceneList.size() != 0)
76 {
77 mState.scene = Scene::alloc(mState.sceneList[0]);
78 mState.sceneList.erase(mState.sceneList.begin());
79
80 Mf::Script::Result status = mState.scene->load(mState.script);
81 if (status != Mf::Script::SUCCESS)
82 {
83 std::string str;
84 mState.script[-1].get(str);
85
86 throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
87 }
88
89 mState.script.getGlobalTable().pushField("Event");
90 if (mState.script[-1].isTable())
91 {
92 mState.script[-1].pushField("Think");
93 mState.script.set("Think", Mf::Script::REGISTRY);
94 mState.script.pop(2);
95 }
96 else
97 {
98 mState.script.pop();
99 }
100 }
101 }
102
103
104 GameLayer::GameLayer() :
105 mHud(Hud::alloc(mState)),
106 mMusic("NightFusionIntro"),
107 mPunchSound("Thump")
108 {
109 mMusic.setLooping(true);
110 mMusic.enqueue("NightFusionLoop");
111
112 bool isMute = false;
113 Mf::Settings::getInstance().get("nomusic", isMute);
114 if (!isMute) mMusic.play();
115
116 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
117
118 loadSceneLoader();
119 advanceScene(); // load the first scene
120
121 mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
122 0.1, Mf::Timer::REPEAT);
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, 5.0, Mf::Interpolator::OSCILLATE);
129
130 setProjection();
131 }
132
133
134 void GameLayer::pushedOntoEngine()
135 {
136 Mf::engine.push(mHud);
137
138 mRay.direction.set(1.0, 0.0);
139
140 mLine.a.set(20, 10);
141 mLine.b.set(19, 14);
142
143 mSphere.point.set(22, 5);
144 mSphere.radius = 2;
145
146 mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), 1.0, Mf::Timer::REPEAT);
147 }
148
149
150 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
151 {
152 mState.camera.update(t, dt);
153 mState.heroine->update(t, dt);
154
155 mState.scene->checkForCollision(*mState.heroine);
156
157 mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
158 -mState.heroine->getState().position[1], -8));
159 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
160
161 mRay.point = mState.heroine->getState().position;
162 }
163
164 void GameLayer::thinkTimer()
165 {
166 mState.script.getRegistryTable().pushField("Think");
167 if (mState.script[-1].isFunction()) mState.script.call();
168 else mState.script.pop();
169 }
170
171
172 void GameLayer::rayTimer()
173 {
174 Mf::Ray<2>::Intersection meh;
175 std::list<Mf::Ray<2>::Intersection> hits;
176 Mf::Vector2 point;
177
178 bool bam = mLine.intersectRay(mRay, meh);
179 if (bam)
180 {
181 meh.normal.normalize();
182 hits.push_back(meh);
183 }
184
185 bam = mSphere.intersectRay(mRay, meh);
186 if (bam)
187 {
188 meh.normal.normalize();
189 hits.push_back(meh);
190 }
191
192 if (mState.scene->castRay(mRay, hits))
193 {
194 hits.front().normal.normalize();
195 mRay.solve(point, hits.front().distance);
196 //Mf::logInfo << "scene: d = " << hits.front().distance << std::endl;
197 //Mf::logInfo << " P = " << point << std::endl;
198 //Mf::logInfo << " n = " << hits.front().normal << std::endl;
199 }
200 }
201
202
203 void GameLayer::draw(Mf::Scalar alpha) const
204 {
205 mState.camera.uploadToGL(alpha);
206
207 // DRAW THE SCENE
208 Mf::Texture::resetBind();
209
210 glEnableClientState(GL_VERTEX_ARRAY);
211 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
212
213 mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
214
215 mState.heroine->draw(alpha);
216
217 mRay.draw();
218 mLine.draw();
219 mSphere.draw();
220 }
221
222 bool GameLayer::handleEvent(const Mf::Event& event)
223 {
224 switch (event.type)
225 {
226 case SDL_KEYDOWN:
227 if (event.key.keysym.sym == SDLK_SPACE)
228 {
229 mState.heroine->animation.startSequence("Flattened");
230 Mf::logInfo("thump!");
231 mPunchSound.play();
232 return true;
233 }
234 else if (event.key.keysym.sym == SDLK_m)
235 {
236 mMusic.toggle();
237 return true;
238 }
239 else if (event.key.keysym.sym == SDLK_PAGEUP)
240 {
241 mRay.direction = cml::rotate_vector_2D(mRay.direction,
242 cml::rad(10.0));
243 return true;
244 }
245 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
246 {
247 mRay.direction = cml::rotate_vector_2D(mRay.direction,
248 cml::rad(-10.0));
249 return true;
250 }
251 else if (event.key.keysym.sym == SDLK_r)
252 {
253 loadSceneLoader();
254 advanceScene();
255 return true;
256 }
257 return mState.heroine->handleEvent(event);
258
259 case SDL_KEYUP:
260 if (event.key.keysym.sym == SDLK_ESCAPE)
261 {
262 Mf::engine.pop(this);
263 return true;
264 }
265 else if (event.key.keysym.sym == SDLK_h)
266 {
267 Mf::engine.push(mHud);
268 return true;
269 }
270 return mState.heroine->handleEvent(event);
271
272 case SDL_MOUSEMOTION:
273 case SDL_MOUSEBUTTONDOWN:
274 mState.camera.handleEvent(event);
275 return true;
276
277 case SDL_VIDEORESIZE:
278 setProjection(event.resize.w, event.resize.h);
279 break;
280 }
281
282 return false;
283 }
284
285
286 void GameLayer::setProjection()
287 {
288 Mf::VideoP video = Mf::engine.getVideo();
289 setProjection(video->getWidth(), video->getHeight());
290 }
291
292 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
293 {
294 mState.camera.setProjection(cml::rad(45.0), width / height, 1.0, 200.0);
295 }
296
297
298 /** vim: set ts=4 sw=4 tw=80: *************************************************/
299
This page took 0.042662 seconds and 4 git commands to generate.