]>
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 **************************************************************************/
12 #include <Moof/Error.hh>
13 #include <Moof/Log.hh>
14 #include <Moof/Math.hh>
15 #include <Moof/OpenGL.hh>
16 #include <Moof/Settings.hh>
17 #include <Moof/Video.hh>
19 #include "GameLayer.hh"
26 void GameLayer::loadSceneLoader()
28 mState
.script
.importStandardLibraries();
29 importLogFunctions(mState
.script
);
31 std::string
path("loader");
32 if (!Scene::getPath(path
))
34 throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND
, "loader");
37 Mf::Script::Result status
= mState
.script
.doFile(path
);
38 if (status
!= Mf::Script::SUCCESS
)
41 mState
.script
[-1].get(str
);
43 throw Mf::Error(Mf::Error::SCRIPT_ERROR
, str
);
46 mState
.script
.globals().pushField("scenes");
47 mState
.script
.top().get(mState
.sceneList
);
48 if (mState
.sceneList
.size() == 0)
50 throw Mf::Error(Mf::Error::SCRIPT_ERROR
,
51 "no variable `scenes' within loader");
55 void GameLayer::advanceScene(Mf::Settings
& settings
)
57 if (mState
.sceneList
.size() != 0)
59 mState
.scene
= Scene::alloc(mState
.sceneList
[0]);
60 mState
.sceneList
.erase(mState
.sceneList
.begin());
62 Mf::Script::Result status
= mState
.scene
->load(settings
,
64 if (status
!= Mf::Script::SUCCESS
)
67 mState
.script
[-1].get(str
);
69 throw Mf::Error(Mf::Error::SCRIPT_ERROR
, str
);
72 Mf::Script::Slot table
= mState
.script
.globals().pushField("Event");
75 mState
.script
.push("Think");
76 table
.pushField("Think");
77 mState
.script
.registry().setField();
84 GameLayer::GameLayer() :
85 mMusic("NightFusionIntro"),
88 mMusic
.setLooping(true);
89 mMusic
.enqueue("NightFusionLoop");
91 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
93 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
94 0.1, Mf::Timer::REPEAT
);
96 mState
.heroine
= Heroine::alloc();
97 mState
.heroine
->animation
.startSequence("FlyDiagonallyUp");
99 mState
.interp
.init(0.0, 1.0);
100 mState
.interp
.reset(4.0, Mf::Interp::OSCILLATE
);
104 void GameLayer::didAddToView()
107 settings().get("nomusic", isMute
);
108 if (!isMute
) mMusic
.play();
111 advanceScene(settings()); // load the first scene
113 mHud
= Hud::alloc(mState
);
116 mRay
.direction
.set(1.0, 0.0);
121 mCircle
.point
.set(22, 5);
124 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
125 1.0, Mf::Timer::REPEAT
);
131 void GameLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
133 if (!mState
.scene
) return;
134 mState
.camera
.update(t
, dt
);
135 mState
.heroine
->update(t
, dt
);
137 mState
.scene
->checkForCollision(*mState
.heroine
);
139 Mf::Vector3 cam
= -Mf::promote(mState
.heroine
->getState().position
, 8);
140 mState
.camera
.setPosition(cam
);
142 mRay
.point
= mState
.heroine
->getState().position
;
144 Mf::View::update(t
, dt
);
147 void GameLayer::thinkTimer()
149 mState
.script
.registry().pushField("Think");
150 if (mState
.script
[-1].isFunction()) mState
.script
.call();
151 else mState
.script
.pop();
155 void GameLayer::rayTimer()
157 Mf::Ray2::Contact meh
;
158 std::list
<Mf::Ray2::Contact
> hits
;
161 bool bam
= mLine
.intersectRay(mRay
, meh
);
164 //meh.normal.normalize();
165 //hits.push_back(meh);
166 mRay
.solve(point
, meh
.distance
);
167 Mf::logInfo
<< "line: d = " << meh
.distance
<< std::endl
;
168 Mf::logInfo
<< " P = " << point
<< std::endl
;
169 Mf::logInfo
<< " n = " << meh
.normal
<< std::endl
;
172 bam
= mCircle
.intersectRay(mRay
, meh
);
175 meh
.normal
.normalize();
179 if (mState
.scene
->castRay(mRay
, hits
))
181 hits
.front().normal
.normalize();
182 mRay
.solve(point
, hits
.front().distance
);
183 Mf::logInfo
<< "scene: d = " << hits
.front().distance
<< std::endl
;
184 Mf::logInfo
<< " P = " << point
<< std::endl
;
185 Mf::logInfo
<< " n = " << hits
.front().normal
<< std::endl
;
190 void GameLayer::draw(Mf::Scalar alpha
) const
192 if (!mState
.scene
) return;
193 mState
.camera
.uploadToGL(alpha
);
196 Mf::Texture::resetBind();
198 glEnableClientState(GL_VERTEX_ARRAY
);
199 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
201 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
202 mState
.heroine
->draw(alpha
);
208 Mf::View::draw(alpha
);
211 bool GameLayer::handleEvent(const Mf::Event
& event
)
213 if (Mf::View::handleEvent(event
)) return true;
218 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
220 mState
.heroine
->animation
.startSequence("Flattened");
221 Mf::logInfo("thump!");
225 else if (event
.key
.keysym
.sym
== SDLK_m
)
230 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
232 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
236 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
238 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
242 else if (event
.key
.keysym
.sym
== SDLK_r
)
245 advanceScene(settings());
248 return mState
.heroine
->handleEvent(event
);
251 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
253 parent().removeChild(this);
256 else if (event
.key
.keysym
.sym
== SDLK_h
)
261 return mState
.heroine
->handleEvent(event
);
263 case SDL_MOUSEMOTION
:
264 case SDL_MOUSEBUTTONDOWN
:
265 mState
.camera
.handleEvent(event
);
268 case SDL_VIDEORESIZE
:
269 setProjection(event
.resize
.w
, event
.resize
.h
);
277 void GameLayer::setProjection()
279 setProjection(video().getWidth(), video().getHeight());
282 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
284 mState
.camera
.setProjection(cml::rad(45.0),
286 SCALAR(1.0), SCALAR(200.0));
This page took 0.054255 seconds and 4 git commands to generate.