]>
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_
.sample("NightFusionIntro.ogg");
85 music_
.enqueue("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
);
98 void GameLayer::did_add_to_view()
101 settings().get("nomusic", isMute
);
102 if (!isMute
) music_
.stream();
105 advanceScene(settings()); // load the first scene
107 mHud
= Hud::alloc(state_
);
110 mRay
.direction
.set(1.0, 0.0);
115 mCircle
.point
.set(22, 5);
118 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
119 1.0, moof::timer::repeat
);
125 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
127 if (!state_
.scene
) return;
128 state_
.camera
.update(t
, dt
);
129 state_
.heroine
->update(t
, dt
);
131 state_
.scene
->checkForCollision(*state_
.heroine
);
133 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
134 state_
.camera
.position(cam
);
136 mRay
.point
= state_
.heroine
->state().position
;
138 moof::view::update(t
, dt
);
141 void GameLayer::thinkTimer()
143 state_
.script
.registry().push_field("Think");
144 if (state_
.script
[-1].is_function()) state_
.script
.call();
145 else state_
.script
.pop();
149 void GameLayer::rayTimer()
151 moof::ray2::contact meh
;
152 std::list
<moof::ray2::contact
> hits
;
155 bool bam
= mLine
.intersect_ray(mRay
, meh
);
158 //meh.normal.normalize();
159 //hits.push_back(meh);
160 mRay
.solve(point
, meh
.distance
);
161 moof::log_info
<< "line: d = " << meh
.distance
<< std::endl
;
162 moof::log_info
<< " P = " << point
<< std::endl
;
163 moof::log_info
<< " n = " << meh
.normal
<< std::endl
;
166 bam
= mCircle
.intersect_ray(mRay
, meh
);
169 meh
.normal
.normalize();
173 if (state_
.scene
->castRay(mRay
, hits
))
175 hits
.front().normal
.normalize();
176 mRay
.solve(point
, hits
.front().distance
);
177 moof::log_info
<< "scene: d = " << hits
.front().distance
<< std::endl
;
178 moof::log_info
<< " P = " << point
<< std::endl
;
179 moof::log_info
<< " n = " << hits
.front().normal
<< std::endl
;
184 void GameLayer::draw(moof::scalar alpha
) const
186 if (!state_
.scene
) return;
187 state_
.camera
.upload_to_gl(alpha
);
190 moof::texture::reset_binding();
192 glEnableClientState(GL_VERTEX_ARRAY
);
193 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
195 state_
.scene
->draw_if_visible(alpha
, state_
.camera
.frustum());
196 state_
.heroine
->draw(alpha
);
202 moof::view::draw(alpha
);
205 bool GameLayer::handle_event(const moof::event
& event
)
207 if (moof::view::handle_event(event
)) return true;
212 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
214 state_
.heroine
->animation
.startSequence("Flattened");
215 moof::log_info("thump!");
219 else if (event
.key
.keysym
.sym
== SDLK_m
)
224 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
226 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
230 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
232 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
236 else if (event
.key
.keysym
.sym
== SDLK_r
)
239 advanceScene(settings());
242 return state_
.heroine
->handle_event(event
);
245 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
247 parent().remove_child(this);
250 else if (event
.key
.keysym
.sym
== SDLK_h
)
255 return state_
.heroine
->handle_event(event
);
257 case SDL_MOUSEMOTION
:
258 case SDL_MOUSEBUTTONDOWN
:
259 state_
.camera
.handle_event(event
);
262 case SDL_VIDEORESIZE
:
263 projection(event
.resize
.w
, event
.resize
.h
);
271 void GameLayer::projection()
273 projection(video().width(), video().height());
276 void GameLayer::projection(moof::scalar width
, moof::scalar height
)
278 state_
.camera
.projection(moof::rad(60.0),
280 SCALAR(1.0), SCALAR(200.0));
This page took 0.049642 seconds and 5 git commands to generate.