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