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