]>
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("loader");
31 if (!moof::resource::find(path
))
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_
= moof::resource::load("NightFusionIntro.ogg");
87 music_
->enqueue("NightFusionLoop");
89 else moof::log_error("music not loaded");
91 //music_->position(moof::vector3(10.0, 5.0, 0.0));
93 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
94 0.1, moof::timer::repeat
);
96 state_
.heroine
= Heroine::alloc();
97 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
99 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
103 void GameLayer::did_add_to_view()
106 settings().get("nomusic", isMute
);
107 if (!isMute
) music_
->play();
110 advanceScene(settings()); // load the first scene
112 mHud
= Hud::alloc(state_
);
115 mRay
.direction
.set(1.0, 0.0);
120 mCircle
.point
.set(22, 5);
123 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
124 1.0, moof::timer::repeat
);
130 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
132 if (!state_
.scene
) return;
133 state_
.camera
.update(t
, dt
);
134 state_
.heroine
->update(t
, dt
);
136 state_
.scene
->checkForCollision(*state_
.heroine
);
138 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
139 state_
.camera
.position(cam
);
141 mRay
.point
= state_
.heroine
->state().position
;
143 moof::view::update(t
, dt
);
146 void GameLayer::thinkTimer()
148 state_
.script
.registry().push_field("Think");
149 if (state_
.script
[-1].is_function()) state_
.script
.call();
150 else state_
.script
.pop();
154 void GameLayer::rayTimer()
156 moof::ray2::contact meh
;
157 std::list
<moof::ray2::contact
> hits
;
160 bool bam
= mLine
.intersect_ray(mRay
, meh
);
163 //meh.normal.normalize();
164 //hits.push_back(meh);
165 mRay
.solve(point
, meh
.distance
);
166 moof::log_info
<< "line: d = " << meh
.distance
<< std::endl
;
167 moof::log_info
<< " P = " << point
<< std::endl
;
168 moof::log_info
<< " n = " << meh
.normal
<< std::endl
;
171 bam
= mCircle
.intersect_ray(mRay
, meh
);
174 meh
.normal
.normalize();
178 if (state_
.scene
->castRay(mRay
, hits
))
180 hits
.front().normal
.normalize();
181 mRay
.solve(point
, hits
.front().distance
);
182 moof::log_info
<< "scene: d = " << hits
.front().distance
<< std::endl
;
183 moof::log_info
<< " P = " << point
<< std::endl
;
184 moof::log_info
<< " n = " << hits
.front().normal
<< std::endl
;
189 void GameLayer::draw(moof::scalar alpha
) const
191 if (!state_
.scene
) return;
192 state_
.camera
.upload_to_gl(alpha
);
195 moof::texture::reset_binding();
197 glEnableClientState(GL_VERTEX_ARRAY
);
198 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
200 state_
.scene
->draw_if_visible(alpha
, state_
.camera
.frustum());
201 state_
.heroine
->draw(alpha
);
207 moof::view::draw(alpha
);
210 bool GameLayer::handle_event(const moof::event
& event
)
212 if (moof::view::handle_event(event
)) return true;
217 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
219 state_
.heroine
->animation
.startSequence("Flattened");
220 moof::log_info("thump!");
221 //mPunchSound.play();
224 else if (event
.key
.keysym
.sym
== SDLK_m
)
229 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
231 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
235 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
237 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
241 else if (event
.key
.keysym
.sym
== SDLK_r
)
244 advanceScene(settings());
247 return state_
.heroine
->handle_event(event
);
250 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
252 parent().remove_child(this);
255 else if (event
.key
.keysym
.sym
== SDLK_h
)
260 return state_
.heroine
->handle_event(event
);
262 case SDL_MOUSEMOTION
:
263 case SDL_MOUSEBUTTONDOWN
:
264 state_
.camera
.handle_event(event
);
267 case SDL_VIDEORESIZE
:
268 projection(event
.resize
.w
, event
.resize
.h
);
276 void GameLayer::projection()
278 projection(video().width(), video().height());
281 void GameLayer::projection(moof::scalar width
, moof::scalar height
)
283 state_
.camera
.projection(moof::rad(60.0),
285 SCALAR(1.0), SCALAR(200.0));
This page took 0.044189 seconds and 4 git commands to generate.