]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
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>
22 #include "GameLayer.hh"
25 void GameLayer::loadSceneLoader()
27 state_
.script
.import_standard_libraries();
28 moof::log::import(state_
.script
);
30 std::string path
= moof::resource::find_file("scenes/loader.lua");
33 throw std::runtime_error("cannot find scene loader script");
36 moof::script::status status
= state_
.script
.do_file(path
);
37 if (status
!= moof::script::success
)
40 state_
.script
[-1].get(str
);
41 throw std::runtime_error("script error: " + str
);
44 state_
.script
.globals().push_field("scenes");
45 state_
.script
.top().get(state_
.sceneList
);
46 if (state_
.sceneList
.size() == 0)
48 throw std::runtime_error("no variable `scenes' in script loader.");
52 void GameLayer::advanceScene(moof::settings
& settings
)
54 if (state_
.sceneList
.size() != 0)
56 state_
.scene
= Scene::alloc(state_
.sceneList
[0]);
57 state_
.sceneList
.erase(state_
.sceneList
.begin());
59 moof::script::status status
= state_
.scene
->load(settings
,
61 if (status
!= moof::script::success
)
64 state_
.script
[-1].get(str
);
65 throw std::runtime_error("script error: " + str
);
68 moof::script::slot table
= state_
.script
.globals().push_field("Event");
71 state_
.script
.push("Think");
72 table
.push_field("Think");
73 state_
.script
.registry().set_field();
80 GameLayer::GameLayer()
82 moof::log_info("about to load sound resource...");
83 music_
.sample("sounds/NightFusionIntro.ogg");
85 music_
.enqueue("sounds/NightFusionLoop.ogg");
86 music_
.position(moof::vector3(10.0, 5.0, 0.0));
88 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
89 0.1, moof::timer::repeat
);
91 state_
.heroine
= Heroine::alloc();
92 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
94 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
96 sceneMesh
= moof::resource::load("classic.ac");
100 void GameLayer::did_add_to_view()
103 settings().get("nomusic", isMute
);
107 advanceScene(settings()); // load the first scene
109 mHud
= Hud::alloc(state_
);
112 mRay
.direction
.set(1.0, 0.0);
117 mCircle
.point
.set(22, 5);
120 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
121 1.0, moof::timer::repeat
);
127 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
129 if (!state_
.scene
) return;
130 state_
.camera
.update(t
, dt
);
131 state_
.heroine
->update(t
, dt
);
133 state_
.scene
->checkForCollision(*state_
.heroine
);
135 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
136 state_
.camera
.position(cam
);
138 mRay
.point
= state_
.heroine
->state().position
;
140 moof::view::update(t
, dt
);
143 void GameLayer::thinkTimer()
145 state_
.script
.registry().push_field("Think");
146 if (state_
.script
[-1].is_function()) state_
.script
.call();
147 else state_
.script
.pop();
151 void GameLayer::rayTimer()
153 moof::ray2::contact meh
;
154 std::list
<moof::ray2::contact
> hits
;
157 bool bam
= mLine
.intersect_ray(mRay
, meh
);
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
;
168 bam
= mCircle
.intersect_ray(mRay
, meh
);
171 meh
.normal
.normalize();
175 if (state_
.scene
->castRay(mRay
, hits
))
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
;
186 void GameLayer::draw(moof::scalar alpha
) const
188 if (!state_
.scene
) return;
189 state_
.camera
.upload_to_gl(alpha
);
192 moof::image::reset_binding();
194 glEnableClientState(GL_VERTEX_ARRAY
);
195 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
197 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
198 sceneMesh
->draw(alpha
);
199 state_
.heroine
->draw(alpha
);
205 moof::view::draw(alpha
);
208 bool GameLayer::handle_event(const moof::event
& event
)
210 if (moof::view::handle_event(event
)) return true;
215 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
217 state_
.heroine
->animation
.startSequence("Flattened");
218 moof::log_info("thump!");
222 else if (event
.key
.keysym
.sym
== SDLK_m
)
227 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
229 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
233 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
235 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
239 else if (event
.key
.keysym
.sym
== SDLK_r
)
242 advanceScene(settings());
245 return state_
.heroine
->handle_event(event
);
248 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
250 parent().remove_child(this);
253 else if (event
.key
.keysym
.sym
== SDLK_h
)
258 return state_
.heroine
->handle_event(event
);
260 case SDL_MOUSEMOTION
:
261 case SDL_MOUSEBUTTONDOWN
:
262 state_
.camera
.handle_event(event
);
265 case SDL_VIDEORESIZE
:
266 projection(event
.resize
.w
, event
.resize
.h
);
274 void GameLayer::projection()
276 projection(video().width(), video().height());
279 void GameLayer::projection(moof::scalar width
, moof::scalar height
)
281 state_
.camera
.projection(moof::rad(60.0),
283 SCALAR(1.0), SCALAR(200.0));
This page took 0.043582 seconds and 4 git commands to generate.