]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
5e95b7615393c57709b1cf3bb4d6b578f4d0ef80
[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 float pos[] = {state_.heroine->state().position[0],
192 state_.heroine->state().position[1], 0.0f};
193 glLightfv(GL_LIGHT0, GL_POSITION, pos);
194
195 // DRAW THE SCENE
196 moof::image::reset_binding();
197
198 glEnableClientState(GL_VERTEX_ARRAY);
199 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
200
201 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
202 sceneMesh->draw(alpha);
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 punch_sound_.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.043655 seconds and 3 git commands to generate.