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