]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
0498182a9888b22eaac118a55f26b16df2025f86
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 (!Scene::find_path(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() :
83 mMusic("NightFusionIntro"),
87 mMusic
.enqueue("NightFusionLoop");
89 //mMusic.setPosition(moof::vector3(10.0, 5.0, 0.0));
91 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
92 0.1, moof::timer::repeat
);
94 state_
.heroine
= Heroine::alloc();
95 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
97 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
101 void GameLayer::did_add_to_view()
104 settings().get("nomusic", isMute
);
105 if (!isMute
) mMusic
.play();
108 advanceScene(settings()); // load the first scene
110 mHud
= Hud::alloc(state_
);
113 mRay
.direction
.set(1.0, 0.0);
118 mCircle
.point
.set(22, 5);
121 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
122 1.0, moof::timer::repeat
);
128 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
130 if (!state_
.scene
) return;
131 state_
.camera
.update(t
, dt
);
132 state_
.heroine
->update(t
, dt
);
134 state_
.scene
->checkForCollision(*state_
.heroine
);
136 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
137 state_
.camera
.position(cam
);
139 mRay
.point
= state_
.heroine
->state().position
;
141 moof::view::update(t
, dt
);
144 void GameLayer::thinkTimer()
146 state_
.script
.registry().push_field("Think");
147 if (state_
.script
[-1].is_function()) state_
.script
.call();
148 else state_
.script
.pop();
152 void GameLayer::rayTimer()
154 moof::ray2::contact meh
;
155 std::list
<moof::ray2::contact
> hits
;
158 bool bam
= mLine
.intersect_ray(mRay
, meh
);
161 //meh.normal.normalize();
162 //hits.push_back(meh);
163 mRay
.solve(point
, meh
.distance
);
164 moof::log_info
<< "line: d = " << meh
.distance
<< std::endl
;
165 moof::log_info
<< " P = " << point
<< std::endl
;
166 moof::log_info
<< " n = " << meh
.normal
<< std::endl
;
169 bam
= mCircle
.intersect_ray(mRay
, meh
);
172 meh
.normal
.normalize();
176 if (state_
.scene
->castRay(mRay
, hits
))
178 hits
.front().normal
.normalize();
179 mRay
.solve(point
, hits
.front().distance
);
180 moof::log_info
<< "scene: d = " << hits
.front().distance
<< std::endl
;
181 moof::log_info
<< " P = " << point
<< std::endl
;
182 moof::log_info
<< " n = " << hits
.front().normal
<< std::endl
;
187 void GameLayer::draw(moof::scalar alpha
) const
189 if (!state_
.scene
) return;
190 state_
.camera
.upload_to_gl(alpha
);
193 moof::texture::reset_binding();
195 glEnableClientState(GL_VERTEX_ARRAY
);
196 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
198 state_
.scene
->draw_if_visible(alpha
, state_
.camera
.frustum());
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(45.0),
283 SCALAR(1.0), SCALAR(200.0));
This page took 0.049385 seconds and 4 git commands to generate.