]>
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 **************************************************************************/
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>
20 #include "GameLayer.hh"
27 void GameLayer::loadSceneLoader()
29 state_
.script
.import_standard_libraries();
30 moof::log::import(state_
.script
);
32 std::string
path("loader");
33 if (!moof::resource::find(path
))
35 throw std::runtime_error("cannot find scene loader script");
38 moof::script::status status
= state_
.script
.do_file(path
);
39 if (status
!= moof::script::success
)
42 state_
.script
[-1].get(str
);
43 throw std::runtime_error("script error: " + str
);
46 state_
.script
.globals().push_field("scenes");
47 state_
.script
.top().get(state_
.sceneList
);
48 if (state_
.sceneList
.size() == 0)
50 throw std::runtime_error("no variable `scenes' in script loader.");
54 void GameLayer::advanceScene(moof::settings
& settings
)
56 if (state_
.sceneList
.size() != 0)
58 state_
.scene
= Scene::alloc(state_
.sceneList
[0]);
59 state_
.sceneList
.erase(state_
.sceneList
.begin());
61 moof::script::status status
= state_
.scene
->load(settings
,
63 if (status
!= moof::script::success
)
66 state_
.script
[-1].get(str
);
67 throw std::runtime_error("script error: " + str
);
70 moof::script::slot table
= state_
.script
.globals().push_field("Event");
73 state_
.script
.push("Think");
74 table
.push_field("Think");
75 state_
.script
.registry().set_field();
82 GameLayer::GameLayer()
84 moof::log_info("about to load sound resource...");
85 music_
= moof::resource::load("sounds/NightFusionIntro.ogg");
89 music_
->enqueue("NightFusionLoop");
91 else moof::log_error("music not loaded");
93 //music_->position(moof::vector3(10.0, 5.0, 0.0));
95 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
96 0.1, moof::timer::repeat
);
98 state_
.heroine
= Heroine::alloc();
99 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
101 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
105 void GameLayer::did_add_to_view()
108 settings().get("nomusic", isMute
);
109 if (!isMute
) music_
->play();
112 advanceScene(settings()); // load the first scene
114 mHud
= Hud::alloc(state_
);
117 mRay
.direction
.set(1.0, 0.0);
122 mCircle
.point
.set(22, 5);
125 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
126 1.0, moof::timer::repeat
);
132 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
134 if (!state_
.scene
) return;
135 state_
.camera
.update(t
, dt
);
136 state_
.heroine
->update(t
, dt
);
138 state_
.scene
->checkForCollision(*state_
.heroine
);
140 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
141 state_
.camera
.position(cam
);
143 mRay
.point
= state_
.heroine
->state().position
;
145 moof::view::update(t
, dt
);
148 void GameLayer::thinkTimer()
150 state_
.script
.registry().push_field("Think");
151 if (state_
.script
[-1].is_function()) state_
.script
.call();
152 else state_
.script
.pop();
156 void GameLayer::rayTimer()
158 moof::ray2::contact meh
;
159 std::list
<moof::ray2::contact
> hits
;
162 bool bam
= mLine
.intersect_ray(mRay
, meh
);
165 //meh.normal.normalize();
166 //hits.push_back(meh);
167 mRay
.solve(point
, meh
.distance
);
168 moof::log_info
<< "line: d = " << meh
.distance
<< std::endl
;
169 moof::log_info
<< " P = " << point
<< std::endl
;
170 moof::log_info
<< " n = " << meh
.normal
<< std::endl
;
173 bam
= mCircle
.intersect_ray(mRay
, meh
);
176 meh
.normal
.normalize();
180 if (state_
.scene
->castRay(mRay
, hits
))
182 hits
.front().normal
.normalize();
183 mRay
.solve(point
, hits
.front().distance
);
184 moof::log_info
<< "scene: d = " << hits
.front().distance
<< std::endl
;
185 moof::log_info
<< " P = " << point
<< std::endl
;
186 moof::log_info
<< " n = " << hits
.front().normal
<< std::endl
;
191 void GameLayer::draw(moof::scalar alpha
) const
193 if (!state_
.scene
) return;
194 state_
.camera
.upload_to_gl(alpha
);
197 moof::texture::reset_binding();
199 glEnableClientState(GL_VERTEX_ARRAY
);
200 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
202 state_
.scene
->draw_if_visible(alpha
, state_
.camera
.frustum());
203 state_
.heroine
->draw(alpha
);
209 moof::view::draw(alpha
);
212 bool GameLayer::handle_event(const moof::event
& event
)
214 if (moof::view::handle_event(event
)) return true;
219 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
221 state_
.heroine
->animation
.startSequence("Flattened");
222 moof::log_info("thump!");
223 //mPunchSound.play();
226 else if (event
.key
.keysym
.sym
== SDLK_m
)
231 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
233 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
237 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
239 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
243 else if (event
.key
.keysym
.sym
== SDLK_r
)
246 advanceScene(settings());
249 return state_
.heroine
->handle_event(event
);
252 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
254 parent().remove_child(this);
257 else if (event
.key
.keysym
.sym
== SDLK_h
)
262 return state_
.heroine
->handle_event(event
);
264 case SDL_MOUSEMOTION
:
265 case SDL_MOUSEBUTTONDOWN
:
266 state_
.camera
.handle_event(event
);
269 case SDL_VIDEORESIZE
:
270 projection(event
.resize
.w
, event
.resize
.h
);
278 void GameLayer::projection()
280 projection(video().width(), video().height());
283 void GameLayer::projection(moof::scalar width
, moof::scalar height
)
285 state_
.camera
.projection(moof::rad(60.0),
287 SCALAR(1.0), SCALAR(200.0));
This page took 0.045406 seconds and 4 git commands to generate.