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