]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
resource loading bugs and new sound class
[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 "config.h"
13
14 #include <stdexcept>
15
16 #include <moof/log.hh>
17 #include <moof/math.hh>
18 #include <moof/opengl.hh>
19 #include <moof/settings.hh>
20 #include <moof/video.hh>
21
22 #include "GameLayer.hh"
23
24
25 void GameLayer::loadSceneLoader()
26 {
27 state_.script.import_standard_libraries();
28 moof::log::import(state_.script);
29
30 std::string path("loader");
31 if (!moof::resource::find(path))
32 {
33 throw std::runtime_error("cannot find scene loader script");
34 }
35
36 moof::script::status status = state_.script.do_file(path);
37 if (status != moof::script::success)
38 {
39 std::string str;
40 state_.script[-1].get(str);
41 throw std::runtime_error("script error: " + str);
42 }
43
44 state_.script.globals().push_field("scenes");
45 state_.script.top().get(state_.sceneList);
46 if (state_.sceneList.size() == 0)
47 {
48 throw std::runtime_error("no variable `scenes' in script loader.");
49 }
50 }
51
52 void GameLayer::advanceScene(moof::settings& settings)
53 {
54 if (state_.sceneList.size() != 0)
55 {
56 state_.scene = Scene::alloc(state_.sceneList[0]);
57 state_.sceneList.erase(state_.sceneList.begin());
58
59 moof::script::status status = state_.scene->load(settings,
60 state_.script);
61 if (status != moof::script::success)
62 {
63 std::string str;
64 state_.script[-1].get(str);
65 throw std::runtime_error("script error: " + str);
66 }
67
68 moof::script::slot table = state_.script.globals().push_field("Event");
69 if (table.is_table())
70 {
71 state_.script.push("Think");
72 table.push_field("Think");
73 state_.script.registry().set_field();
74 }
75 state_.script.pop();
76 }
77 }
78
79
80 GameLayer::GameLayer()
81 {
82 moof::log_info("about to load sound resource...");
83 music_.sample("NightFusionIntro.ogg");
84 music_.loop(true);
85 music_.enqueue("NightFusionLoop.ogg");
86 music_.position(moof::vector3(10.0, 5.0, 0.0));
87
88 mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
89 0.1, moof::timer::repeat);
90
91 state_.heroine = Heroine::alloc();
92 state_.heroine->animation.startSequence("FlyDiagonallyUp");
93
94 state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate);
95 }
96
97
98 void GameLayer::did_add_to_view()
99 {
100 bool isMute = false;
101 settings().get("nomusic", isMute);
102 if (!isMute) music_.stream();
103
104 loadSceneLoader();
105 advanceScene(settings()); // load the first scene
106
107 mHud = Hud::alloc(state_);
108 add_child(mHud);
109
110 mRay.direction.set(1.0, 0.0);
111
112 mLine.a.set(20, 10);
113 mLine.b.set(19, 14);
114
115 mCircle.point.set(22, 5);
116 mCircle.radius = 2;
117
118 mRayTimer.init(boost::bind(&GameLayer::rayTimer, this),
119 1.0, moof::timer::repeat);
120
121 projection();
122 }
123
124
125 void GameLayer::update(moof::scalar t, moof::scalar dt)
126 {
127 if (!state_.scene) return;
128 state_.camera.update(t, dt);
129 state_.heroine->update(t, dt);
130
131 state_.scene->checkForCollision(*state_.heroine);
132
133 moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8);
134 state_.camera.position(cam);
135
136 mRay.point = state_.heroine->state().position;
137
138 moof::view::update(t, dt);
139 }
140
141 void GameLayer::thinkTimer()
142 {
143 state_.script.registry().push_field("Think");
144 if (state_.script[-1].is_function()) state_.script.call();
145 else state_.script.pop();
146 }
147
148
149 void GameLayer::rayTimer()
150 {
151 moof::ray2::contact meh;
152 std::list<moof::ray2::contact> hits;
153 moof::vector2 point;
154
155 bool bam = mLine.intersect_ray(mRay, meh);
156 if (bam)
157 {
158 //meh.normal.normalize();
159 //hits.push_back(meh);
160 mRay.solve(point, meh.distance);
161 moof::log_info << "line: d = " << meh.distance << std::endl;
162 moof::log_info << " P = " << point << std::endl;
163 moof::log_info << " n = " << meh.normal << std::endl;
164 }
165
166 bam = mCircle.intersect_ray(mRay, meh);
167 if (bam)
168 {
169 meh.normal.normalize();
170 hits.push_back(meh);
171 }
172
173 if (state_.scene->castRay(mRay, hits))
174 {
175 hits.front().normal.normalize();
176 mRay.solve(point, hits.front().distance);
177 moof::log_info << "scene: d = " << hits.front().distance << std::endl;
178 moof::log_info << " P = " << point << std::endl;
179 moof::log_info << " n = " << hits.front().normal << std::endl;
180 }
181 }
182
183
184 void GameLayer::draw(moof::scalar alpha) const
185 {
186 if (!state_.scene) return;
187 state_.camera.upload_to_gl(alpha);
188
189 // DRAW THE SCENE
190 moof::texture::reset_binding();
191
192 glEnableClientState(GL_VERTEX_ARRAY);
193 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
194
195 state_.scene->draw_if_visible(alpha, state_.camera.frustum());
196 state_.heroine->draw(alpha);
197
198 mRay.draw();
199 mLine.draw();
200 mCircle.draw();
201
202 moof::view::draw(alpha);
203 }
204
205 bool GameLayer::handle_event(const moof::event& event)
206 {
207 if (moof::view::handle_event(event)) return true;
208
209 switch (event.type)
210 {
211 case SDL_KEYDOWN:
212 if (event.key.keysym.sym == SDLK_SPACE)
213 {
214 state_.heroine->animation.startSequence("Flattened");
215 moof::log_info("thump!");
216 punch_sound_.play();
217 return true;
218 }
219 else if (event.key.keysym.sym == SDLK_m)
220 {
221 music_.toggle();
222 return true;
223 }
224 else if (event.key.keysym.sym == SDLK_PAGEUP)
225 {
226 mRay.direction = moof::rotate_vector_2D(mRay.direction,
227 moof::rad(10.0));
228 return true;
229 }
230 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
231 {
232 mRay.direction = moof::rotate_vector_2D(mRay.direction,
233 moof::rad(-10.0));
234 return true;
235 }
236 else if (event.key.keysym.sym == SDLK_r)
237 {
238 loadSceneLoader();
239 advanceScene(settings());
240 return true;
241 }
242 return state_.heroine->handle_event(event);
243
244 case SDL_KEYUP:
245 if (event.key.keysym.sym == SDLK_ESCAPE)
246 {
247 parent().remove_child(this);
248 return true;
249 }
250 else if (event.key.keysym.sym == SDLK_h)
251 {
252 add_child(mHud);
253 return true;
254 }
255 return state_.heroine->handle_event(event);
256
257 case SDL_MOUSEMOTION:
258 case SDL_MOUSEBUTTONDOWN:
259 state_.camera.handle_event(event);
260 return true;
261
262 case SDL_VIDEORESIZE:
263 projection(event.resize.w, event.resize.h);
264 break;
265 }
266
267 return false;
268 }
269
270
271 void GameLayer::projection()
272 {
273 projection(video().width(), video().height());
274 }
275
276 void GameLayer::projection(moof::scalar width, moof::scalar height)
277 {
278 state_.camera.projection(moof::rad(60.0),
279 width / height,
280 SCALAR(1.0), SCALAR(200.0));
281 }
282
This page took 0.047284 seconds and 4 git commands to generate.