]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
sockets documentation and cleanup
[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 path("loader");
32 if (!Scene::getPath(path))
33 {
34 throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
35 }
36
37 Mf::Script::Result status = mState.script.doFile(path);
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.globals().pushField("scenes");
47 mState.script.top().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 Mf::Script::Slot table = mState.script.globals().pushField("Event");
73 if (table.isTable())
74 {
75 mState.script.push("Think");
76 table.pushField("Think");
77 mState.script.registry().setField();
78 }
79 mState.script.pop();
80 }
81 }
82
83
84 GameLayer::GameLayer() :
85 mMusic("NightFusionIntro"),
86 mPunchSound("Thump")
87 {
88 mMusic.setLooping(true);
89 mMusic.enqueue("NightFusionLoop");
90
91 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
92
93 mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
94 0.1, Mf::Timer::REPEAT);
95
96 mState.heroine = Heroine::alloc();
97 mState.heroine->animation.startSequence("FlyDiagonallyUp");
98
99 mState.interp.init(0.0, 1.0);
100 mState.interp.reset(4.0, Mf::Interp::OSCILLATE);
101 }
102
103
104 void GameLayer::didAddToView()
105 {
106 bool isMute = false;
107 settings().get("nomusic", isMute);
108 if (!isMute) mMusic.play();
109
110 loadSceneLoader();
111 advanceScene(settings()); // load the first scene
112
113 mHud = Hud::alloc(mState);
114 addChild(mHud);
115
116 mRay.direction.set(1.0, 0.0);
117
118 mLine.a.set(20, 10);
119 mLine.b.set(19, 14);
120
121 mCircle.point.set(22, 5);
122 mCircle.radius = 2;
123
124 mRayTimer.init(boost::bind(&GameLayer::rayTimer, this),
125 1.0, Mf::Timer::REPEAT);
126
127 setProjection();
128 }
129
130
131 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
132 {
133 if (!mState.scene) return;
134 mState.camera.update(t, dt);
135 mState.heroine->update(t, dt);
136
137 mState.scene->checkForCollision(*mState.heroine);
138
139 Mf::Vector3 cam= -Mf::promote(mState.heroine->getState().position, 8);
140 mState.camera.setPosition(cam);
141
142 mRay.point = mState.heroine->getState().position;
143
144 Mf::View::update(t, dt);
145 }
146
147 void GameLayer::thinkTimer()
148 {
149 mState.script.registry().pushField("Think");
150 if (mState.script[-1].isFunction()) mState.script.call();
151 else mState.script.pop();
152 }
153
154
155 void GameLayer::rayTimer()
156 {
157 Mf::Ray2::Contact meh;
158 std::list<Mf::Ray2::Contact> hits;
159 Mf::Vector2 point;
160
161 bool bam = mLine.intersectRay(mRay, meh);
162 if (bam)
163 {
164 //meh.normal.normalize();
165 //hits.push_back(meh);
166 mRay.solve(point, meh.distance);
167 Mf::logInfo << "line: d = " << meh.distance << std::endl;
168 Mf::logInfo << " P = " << point << std::endl;
169 Mf::logInfo << " n = " << meh.normal << std::endl;
170 }
171
172 bam = mCircle.intersectRay(mRay, meh);
173 if (bam)
174 {
175 meh.normal.normalize();
176 hits.push_back(meh);
177 }
178
179 if (mState.scene->castRay(mRay, hits))
180 {
181 hits.front().normal.normalize();
182 mRay.solve(point, hits.front().distance);
183 Mf::logInfo << "scene: d = " << hits.front().distance << std::endl;
184 Mf::logInfo << " P = " << point << std::endl;
185 Mf::logInfo << " n = " << hits.front().normal << std::endl;
186 }
187 }
188
189
190 void GameLayer::draw(Mf::Scalar alpha) const
191 {
192 if (!mState.scene) return;
193 mState.camera.uploadToGL(alpha);
194
195 // DRAW THE SCENE
196 Mf::Texture::resetBind();
197
198 glEnableClientState(GL_VERTEX_ARRAY);
199 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
200
201 mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
202 mState.heroine->draw(alpha);
203
204 mRay.draw();
205 mLine.draw();
206 mCircle.draw();
207
208 Mf::View::draw(alpha);
209 }
210
211 bool GameLayer::handleEvent(const Mf::Event& event)
212 {
213 if (Mf::View::handleEvent(event)) return true;
214
215 switch (event.type)
216 {
217 case SDL_KEYDOWN:
218 if (event.key.keysym.sym == SDLK_SPACE)
219 {
220 mState.heroine->animation.startSequence("Flattened");
221 Mf::logInfo("thump!");
222 mPunchSound.play();
223 return true;
224 }
225 else if (event.key.keysym.sym == SDLK_m)
226 {
227 mMusic.toggle();
228 return true;
229 }
230 else if (event.key.keysym.sym == SDLK_PAGEUP)
231 {
232 mRay.direction = cml::rotate_vector_2D(mRay.direction,
233 cml::rad(10.0));
234 return true;
235 }
236 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
237 {
238 mRay.direction = cml::rotate_vector_2D(mRay.direction,
239 cml::rad(-10.0));
240 return true;
241 }
242 else if (event.key.keysym.sym == SDLK_r)
243 {
244 loadSceneLoader();
245 advanceScene(settings());
246 return true;
247 }
248 return mState.heroine->handleEvent(event);
249
250 case SDL_KEYUP:
251 if (event.key.keysym.sym == SDLK_ESCAPE)
252 {
253 parent().removeChild(this);
254 return true;
255 }
256 else if (event.key.keysym.sym == SDLK_h)
257 {
258 addChild(mHud);
259 return true;
260 }
261 return mState.heroine->handleEvent(event);
262
263 case SDL_MOUSEMOTION:
264 case SDL_MOUSEBUTTONDOWN:
265 mState.camera.handleEvent(event);
266 return true;
267
268 case SDL_VIDEORESIZE:
269 setProjection(event.resize.w, event.resize.h);
270 break;
271 }
272
273 return false;
274 }
275
276
277 void GameLayer::setProjection()
278 {
279 setProjection(video().getWidth(), video().getHeight());
280 }
281
282 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
283 {
284 mState.camera.setProjection(cml::rad(45.0),
285 width / height,
286 SCALAR(1.0), SCALAR(200.0));
287 }
288
This page took 0.048304 seconds and 4 git commands to generate.