]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
c0d35b95dadb692b085ee1d9d08bb76765800c25
[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 = moof::resource::find_file("scenes/loader.lua");
31 if (path.empty())
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("sounds/NightFusionIntro.ogg");
84 music_.loop(true);
85 music_.enqueue("sounds/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 sceneMesh = moof::resource::load("classic.ac");
97 }
98
99
100 void GameLayer::did_add_to_view()
101 {
102 bool isMute = false;
103 settings().get("nomusic", isMute);
104 music_.stream();
105
106 loadSceneLoader();
107 advanceScene(settings()); // load the first scene
108
109 mHud = Hud::alloc(state_);
110 add_child(mHud);
111
112 mRay.direction.set(1.0, 0.0);
113
114 mLine.a.set(20, 10);
115 mLine.b.set(19, 14);
116
117 mCircle.point.set(22, 5);
118 mCircle.radius = 2;
119
120 mRayTimer.init(boost::bind(&GameLayer::rayTimer, this),
121 1.0, moof::timer::repeat);
122
123 projection();
124 }
125
126
127 void GameLayer::update(moof::scalar t, moof::scalar dt)
128 {
129 if (!state_.scene) return;
130 state_.camera.update(t, dt);
131 state_.heroine->update(t, dt);
132
133 state_.scene->checkForCollision(*state_.heroine);
134
135 moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8);
136 state_.camera.position(cam);
137
138 mRay.point = state_.heroine->state().position;
139
140 moof::view::update(t, dt);
141 }
142
143 void GameLayer::thinkTimer()
144 {
145 state_.script.registry().push_field("Think");
146 if (state_.script[-1].is_function()) state_.script.call();
147 else state_.script.pop();
148 }
149
150
151 void GameLayer::rayTimer()
152 {
153 moof::ray2::contact meh;
154 std::list<moof::ray2::contact> hits;
155 moof::vector2 point;
156
157 bool bam = mLine.intersect_ray(mRay, meh);
158 if (bam)
159 {
160 //meh.normal.normalize();
161 //hits.push_back(meh);
162 mRay.solve(point, meh.distance);
163 moof::log_info << "line: d = " << meh.distance << std::endl;
164 moof::log_info << " P = " << point << std::endl;
165 moof::log_info << " n = " << meh.normal << std::endl;
166 }
167
168 bam = mCircle.intersect_ray(mRay, meh);
169 if (bam)
170 {
171 meh.normal.normalize();
172 hits.push_back(meh);
173 }
174
175 if (state_.scene->castRay(mRay, hits))
176 {
177 hits.front().normal.normalize();
178 mRay.solve(point, hits.front().distance);
179 moof::log_info << "scene: d = " << hits.front().distance << std::endl;
180 moof::log_info << " P = " << point << std::endl;
181 moof::log_info << " n = " << hits.front().normal << std::endl;
182 }
183 }
184
185
186 void GameLayer::draw(moof::scalar alpha) const
187 {
188 if (!state_.scene) return;
189 state_.camera.upload_to_gl(alpha);
190
191 // DRAW THE SCENE
192 moof::image::reset_binding();
193
194 glEnableClientState(GL_VERTEX_ARRAY);
195 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
196
197 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
198 sceneMesh->draw(alpha);
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 punch_sound_.play();
220 return true;
221 }
222 else if (event.key.keysym.sym == SDLK_m)
223 {
224 music_.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(60.0),
282 width / height,
283 SCALAR(1.0), SCALAR(200.0));
284 }
285
This page took 0.043152 seconds and 3 git commands to generate.