]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
arch linux prefers pkgconfig for finding lua
[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::addedToCore()
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 mCircle.point.set(22, 5);
127 mCircle.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::Ray2::Contact meh;
159 std::list<Mf::Ray2::Contact> 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 = mCircle.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 mState.heroine->draw(alpha);
199
200 mRay.draw();
201 mLine.draw();
202 mCircle.draw();
203 }
204
205 bool GameLayer::handleEvent(const Mf::Event& event)
206 {
207 switch (event.type)
208 {
209 case SDL_KEYDOWN:
210 if (event.key.keysym.sym == SDLK_SPACE)
211 {
212 mState.heroine->animation.startSequence("Flattened");
213 Mf::logInfo("thump!");
214 mPunchSound.play();
215 return true;
216 }
217 else if (event.key.keysym.sym == SDLK_m)
218 {
219 mMusic.toggle();
220 return true;
221 }
222 else if (event.key.keysym.sym == SDLK_PAGEUP)
223 {
224 mRay.direction = cml::rotate_vector_2D(mRay.direction,
225 cml::rad(10.0));
226 return true;
227 }
228 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
229 {
230 mRay.direction = cml::rotate_vector_2D(mRay.direction,
231 cml::rad(-10.0));
232 return true;
233 }
234 else if (event.key.keysym.sym == SDLK_r)
235 {
236 loadSceneLoader();
237 advanceScene();
238 return true;
239 }
240 return mState.heroine->handleEvent(event);
241
242 case SDL_KEYUP:
243 if (event.key.keysym.sym == SDLK_ESCAPE)
244 {
245 Mf::core.pop(this);
246 return true;
247 }
248 else if (event.key.keysym.sym == SDLK_h)
249 {
250 Mf::core.push(mHud);
251 return true;
252 }
253 return mState.heroine->handleEvent(event);
254
255 case SDL_MOUSEMOTION:
256 case SDL_MOUSEBUTTONDOWN:
257 mState.camera.handleEvent(event);
258 return true;
259
260 case SDL_VIDEORESIZE:
261 setProjection(event.resize.w, event.resize.h);
262 break;
263 }
264
265 return false;
266 }
267
268
269 void GameLayer::setProjection()
270 {
271 ASSERT(Mf::video &&
272 "no current video context from which to get dimensions");
273 setProjection(Mf::video->getWidth(), Mf::video->getHeight());
274 }
275
276 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
277 {
278 mState.camera.setProjection(cml::rad(45.0), width / height, 1.0, 200.0);
279 }
280
This page took 0.043354 seconds and 4 git commands to generate.