]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
d696f5ebdf851a0276f625f5c886eddb3dfd7594
[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_ = moof::resource::load("sounds/NightFusionIntro.ogg");
84 if (music_)
85 {
86 music_->loop(true);
87 music_->enqueue("NightFusionLoop");
88 }
89 else moof::log_error("music not loaded");
90
91 //music_->position(moof::vector3(10.0, 5.0, 0.0));
92
93 mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
94 0.1, moof::timer::repeat);
95
96 state_.heroine = Heroine::alloc();
97 state_.heroine->animation.startSequence("FlyDiagonallyUp");
98
99 state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate);
100 }
101
102
103 void GameLayer::did_add_to_view()
104 {
105 bool isMute = false;
106 settings().get("nomusic", isMute);
107 if (!isMute) music_->play();
108
109 loadSceneLoader();
110 advanceScene(settings()); // load the first scene
111
112 mHud = Hud::alloc(state_);
113 add_child(mHud);
114
115 mRay.direction.set(1.0, 0.0);
116
117 mLine.a.set(20, 10);
118 mLine.b.set(19, 14);
119
120 mCircle.point.set(22, 5);
121 mCircle.radius = 2;
122
123 mRayTimer.init(boost::bind(&GameLayer::rayTimer, this),
124 1.0, moof::timer::repeat);
125
126 projection();
127 }
128
129
130 void GameLayer::update(moof::scalar t, moof::scalar dt)
131 {
132 if (!state_.scene) return;
133 state_.camera.update(t, dt);
134 state_.heroine->update(t, dt);
135
136 state_.scene->checkForCollision(*state_.heroine);
137
138 moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8);
139 state_.camera.position(cam);
140
141 mRay.point = state_.heroine->state().position;
142
143 moof::view::update(t, dt);
144 }
145
146 void GameLayer::thinkTimer()
147 {
148 state_.script.registry().push_field("Think");
149 if (state_.script[-1].is_function()) state_.script.call();
150 else state_.script.pop();
151 }
152
153
154 void GameLayer::rayTimer()
155 {
156 moof::ray2::contact meh;
157 std::list<moof::ray2::contact> hits;
158 moof::vector2 point;
159
160 bool bam = mLine.intersect_ray(mRay, meh);
161 if (bam)
162 {
163 //meh.normal.normalize();
164 //hits.push_back(meh);
165 mRay.solve(point, meh.distance);
166 moof::log_info << "line: d = " << meh.distance << std::endl;
167 moof::log_info << " P = " << point << std::endl;
168 moof::log_info << " n = " << meh.normal << std::endl;
169 }
170
171 bam = mCircle.intersect_ray(mRay, meh);
172 if (bam)
173 {
174 meh.normal.normalize();
175 hits.push_back(meh);
176 }
177
178 if (state_.scene->castRay(mRay, hits))
179 {
180 hits.front().normal.normalize();
181 mRay.solve(point, hits.front().distance);
182 moof::log_info << "scene: d = " << hits.front().distance << std::endl;
183 moof::log_info << " P = " << point << std::endl;
184 moof::log_info << " n = " << hits.front().normal << std::endl;
185 }
186 }
187
188
189 void GameLayer::draw(moof::scalar alpha) const
190 {
191 if (!state_.scene) return;
192 state_.camera.upload_to_gl(alpha);
193
194 // DRAW THE SCENE
195 moof::texture::reset_binding();
196
197 glEnableClientState(GL_VERTEX_ARRAY);
198 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
199
200 state_.scene->draw_if_visible(alpha, state_.camera.frustum());
201 state_.heroine->draw(alpha);
202
203 mRay.draw();
204 mLine.draw();
205 mCircle.draw();
206
207 moof::view::draw(alpha);
208 }
209
210 bool GameLayer::handle_event(const moof::event& event)
211 {
212 if (moof::view::handle_event(event)) return true;
213
214 switch (event.type)
215 {
216 case SDL_KEYDOWN:
217 if (event.key.keysym.sym == SDLK_SPACE)
218 {
219 state_.heroine->animation.startSequence("Flattened");
220 moof::log_info("thump!");
221 //mPunchSound.play();
222 return true;
223 }
224 else if (event.key.keysym.sym == SDLK_m)
225 {
226 music_->toggle();
227 return true;
228 }
229 else if (event.key.keysym.sym == SDLK_PAGEUP)
230 {
231 mRay.direction = moof::rotate_vector_2D(mRay.direction,
232 moof::rad(10.0));
233 return true;
234 }
235 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
236 {
237 mRay.direction = moof::rotate_vector_2D(mRay.direction,
238 moof::rad(-10.0));
239 return true;
240 }
241 else if (event.key.keysym.sym == SDLK_r)
242 {
243 loadSceneLoader();
244 advanceScene(settings());
245 return true;
246 }
247 return state_.heroine->handle_event(event);
248
249 case SDL_KEYUP:
250 if (event.key.keysym.sym == SDLK_ESCAPE)
251 {
252 parent().remove_child(this);
253 return true;
254 }
255 else if (event.key.keysym.sym == SDLK_h)
256 {
257 add_child(mHud);
258 return true;
259 }
260 return state_.heroine->handle_event(event);
261
262 case SDL_MOUSEMOTION:
263 case SDL_MOUSEBUTTONDOWN:
264 state_.camera.handle_event(event);
265 return true;
266
267 case SDL_VIDEORESIZE:
268 projection(event.resize.w, event.resize.h);
269 break;
270 }
271
272 return false;
273 }
274
275
276 void GameLayer::projection()
277 {
278 projection(video().width(), video().height());
279 }
280
281 void GameLayer::projection(moof::scalar width, moof::scalar height)
282 {
283 state_.camera.projection(moof::rad(60.0),
284 width / height,
285 SCALAR(1.0), SCALAR(200.0));
286 }
287
This page took 0.039549 seconds and 3 git commands to generate.