]>
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
.getGlobalTable().pushField("scenes");
47 mState
.script
.getTop().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 mState
.script
.getGlobalTable().pushField("Event");
73 if (mState
.script
[-1].isTable())
75 mState
.script
[-1].pushField("Think");
76 mState
.script
.set("Think", Mf::Script::REGISTRY
);
87 GameLayer::GameLayer() :
88 mMusic("NightFusionIntro"),
91 mMusic
.setLooping(true);
92 mMusic
.enqueue("NightFusionLoop");
94 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
96 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
97 0.1, Mf::Timer::REPEAT
);
99 mState
.heroine
= Heroine::alloc();
100 mState
.heroine
->animation
.startSequence("FlyDiagonallyUp");
102 mState
.interp
.init(0.0, 1.0);
103 mState
.interp
.reset(4.0, Mf::Interp::OSCILLATE
);
107 void GameLayer::didAddToView()
110 settings().get("nomusic", isMute
);
111 if (!isMute
) mMusic
.play();
114 advanceScene(settings()); // load the first scene
116 mHud
= Hud::alloc(mState
);
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, Mf::Timer::REPEAT
);
134 void GameLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
136 if (!mState
.scene
) return;
137 mState
.camera
.update(t
, dt
);
138 mState
.heroine
->update(t
, dt
);
140 mState
.scene
->checkForCollision(*mState
.heroine
);
142 Mf::Vector3 cam
= -Mf::promote(mState
.heroine
->getState().position
, 8);
143 mState
.camera
.setPosition(cam
);
145 mRay
.point
= mState
.heroine
->getState().position
;
147 Mf::View::update(t
, dt
);
150 void GameLayer::thinkTimer()
152 mState
.script
.getRegistryTable().pushField("Think");
153 if (mState
.script
[-1].isFunction()) mState
.script
.call();
154 else mState
.script
.pop();
158 void GameLayer::rayTimer()
160 Mf::Ray2::Contact meh
;
161 std::list
<Mf::Ray2::Contact
> hits
;
164 bool bam
= mLine
.intersectRay(mRay
, meh
);
167 //meh.normal.normalize();
168 //hits.push_back(meh);
169 mRay
.solve(point
, meh
.distance
);
170 Mf::logInfo
<< "line: d = " << meh
.distance
<< std::endl
;
171 Mf::logInfo
<< " P = " << point
<< std::endl
;
172 Mf::logInfo
<< " n = " << meh
.normal
<< std::endl
;
175 bam
= mCircle
.intersectRay(mRay
, meh
);
178 meh
.normal
.normalize();
182 if (mState
.scene
->castRay(mRay
, hits
))
184 hits
.front().normal
.normalize();
185 mRay
.solve(point
, hits
.front().distance
);
186 Mf::logInfo
<< "scene: d = " << hits
.front().distance
<< std::endl
;
187 Mf::logInfo
<< " P = " << point
<< std::endl
;
188 Mf::logInfo
<< " n = " << hits
.front().normal
<< std::endl
;
193 void GameLayer::draw(Mf::Scalar alpha
) const
195 if (!mState
.scene
) return;
196 mState
.camera
.uploadToGL(alpha
);
199 Mf::Texture::resetBind();
201 glEnableClientState(GL_VERTEX_ARRAY
);
202 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
204 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
205 mState
.heroine
->draw(alpha
);
211 Mf::View::draw(alpha
);
214 bool GameLayer::handleEvent(const Mf::Event
& event
)
216 if (Mf::View::handleEvent(event
)) return true;
221 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
223 mState
.heroine
->animation
.startSequence("Flattened");
224 Mf::logInfo("thump!");
228 else if (event
.key
.keysym
.sym
== SDLK_m
)
233 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
235 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
239 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
241 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
245 else if (event
.key
.keysym
.sym
== SDLK_r
)
248 advanceScene(settings());
251 return mState
.heroine
->handleEvent(event
);
254 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
256 parent().removeChild(this);
259 else if (event
.key
.keysym
.sym
== SDLK_h
)
264 return mState
.heroine
->handleEvent(event
);
266 case SDL_MOUSEMOTION
:
267 case SDL_MOUSEBUTTONDOWN
:
268 mState
.camera
.handleEvent(event
);
271 case SDL_VIDEORESIZE
:
272 setProjection(event
.resize
.w
, event
.resize
.h
);
280 void GameLayer::setProjection()
282 setProjection(video().getWidth(), video().getHeight());
285 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
287 mState
.camera
.setProjection(cml::rad(45.0),
289 SCALAR(1.0), SCALAR(200.0));
This page took 0.044001 seconds and 4 git commands to generate.