]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
7d36f3312045ca00781a6294e7cd0f4e45701fc9
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/mesh.hh>
19 #include <moof/opengl.hh>
20 #include <moof/settings.hh>
21 #include <moof/video.hh>
23 #include "GameLayer.hh"
26 void GameLayer::loadSceneLoader()
28 state_
.script
.import_safe_standard_libraries();
29 moof::log::import(state_
.script
);
30 moof::mesh::import(state_
.script
, "yoink");
31 moof::sound::import(state_
.script
, "yoink");
33 std::string path
= moof::resource::find_file("scenes/loader.lua");
36 throw std::runtime_error("cannot find scene loader script");
39 moof::script::status status
= state_
.script
.do_file(path
);
40 if (status
!= moof::script::success
)
43 state_
.script
[-1].get(str
);
44 throw std::runtime_error("script error: " + str
);
47 state_
.script
.globals().push_field("scenes");
48 state_
.script
.top().get(state_
.sceneList
);
49 if (state_
.sceneList
.size() == 0)
51 throw std::runtime_error("no variable `scenes' in script loader");
55 void GameLayer::advanceScene(moof::settings
& settings
)
57 if (state_
.sceneList
.size() != 0)
59 state_
.scene
= Scene::alloc(state_
.sceneList
[0]);
60 state_
.sceneList
.erase(state_
.sceneList
.begin());
62 moof::script::status status
= state_
.scene
->load(settings
,
64 if (status
!= moof::script::success
)
67 state_
.script
[-1].get(str
);
68 throw std::runtime_error("script error: " + str
);
71 moof::script::slot table
= state_
.script
.globals().push_field("Event");
74 state_
.script
.push("Think");
75 table
.push_field("Think");
76 state_
.script
.registry().set_field();
80 table
= state_
.script
.globals().push_field("Event");
83 table
.push_field("Draw");
84 state_
.script
.registry().set_field("Draw");
91 GameLayer::GameLayer()
93 music_
.queue("NightFusionIntro");
95 music_
.queue("NightFusionLoop");
96 music_
.position(moof::vector3(10.0, 5.0, 0.0));
98 punch_sound_
.sample("Thump");
100 state_
.heroine
= Heroine::alloc();
101 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
103 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
107 void GameLayer::did_add_to_view()
110 settings().get("nomusic", isMute
);
114 advanceScene(settings()); // load the first scene
116 mHud
= Hud::alloc(state_
);
119 mRay
.direction
.set(1.0, 0.0);
124 mCircle
.point
.set(22, 5);
127 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
128 1.0, moof::timer::repeat
);
134 void GameLayer::update(moof::scalar t
, moof::scalar dt
)
136 if (!state_
.scene
) return;
137 state_
.camera
.update(t
, dt
);
138 state_
.heroine
->update(t
, dt
);
140 state_
.scene
->checkForCollision(*state_
.heroine
);
142 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
143 state_
.camera
.position(cam
);
145 mRay
.point
= state_
.heroine
->state().position
;
147 state_
.script
.registry().push_field("Think");
148 if (state_
.script
[-1].is_function()) state_
.script
.call();
149 else state_
.script
.pop();
151 moof::view::update(t
, dt
);
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
);
194 float pos
[] = {state_
.heroine
->state().position
[0],
195 state_
.heroine
->state().position
[1], 0.0f
, 1.0f
};
196 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
199 moof::image::reset_binding();
201 glEnableClientState(GL_VERTEX_ARRAY
);
202 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
204 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
207 state_
.script
.push(meh
);
210 state_
.script
.registry().push_field("Draw");
211 if (state_
.script
[-1].is_function())
213 moof::script::status result
= state_
.script
.call();
214 if (result
!= moof::script::success
)
217 state_
.script
[-1].get(str
);
218 throw std::runtime_error("script error: " + str
);
221 else state_
.script
.pop();
223 state_
.heroine
->draw(alpha
);
229 moof::view::draw(alpha
);
232 bool GameLayer::handle_event(const moof::event
& event
)
234 if (moof::view::handle_event(event
)) return true;
239 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
241 state_
.heroine
->animation
.startSequence("Flattened");
242 moof::log_info("thump!");
246 else if (event
.key
.keysym
.sym
== SDLK_m
)
251 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
253 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
257 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
259 mRay
.direction
= moof::rotate_vector_2D(mRay
.direction
,
263 else if (event
.key
.keysym
.sym
== SDLK_r
)
266 advanceScene(settings());
269 return state_
.heroine
->handle_event(event
);
272 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
274 parent().remove_child(this);
277 else if (event
.key
.keysym
.sym
== SDLK_h
)
282 return state_
.heroine
->handle_event(event
);
284 case SDL_MOUSEMOTION
:
285 case SDL_MOUSEBUTTONDOWN
:
286 state_
.camera
.handle_event(event
);
289 case SDL_VIDEORESIZE
:
290 projection(event
.resize
.w
, event
.resize
.h
);
298 void GameLayer::projection()
300 projection(video().width(), video().height());
303 void GameLayer::projection(moof::scalar width
, moof::scalar height
)
305 state_
.camera
.projection(moof::rad(60.0),
307 SCALAR(1.0), SCALAR(200.0));
This page took 0.048104 seconds and 4 git commands to generate.