]>
Dogcows Code - chaz/yoink/blob - GameLayer.cc
3a0ea09eba80e44fb81dda883975bf5bd1c66f43
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/Core.hh>
13 #include <Moof/Error.hh>
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 mState
.script
.importStandardLibraries();
30 importLogFunctions(mState
.script
);
32 std::string loaderPath
= Scene::getPath("loader");
35 throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND
, "loader");
38 Mf::Script::Result status
= mState
.script
.doFile(loaderPath
);
39 if (status
!= Mf::Script::SUCCESS
)
42 mState
.script
[-1].get(str
);
44 throw Mf::Error(Mf::Error::SCRIPT_ERROR
, str
);
47 mState
.script
.getGlobalTable().pushField("scenes");
48 mState
.script
.getTop().get(mState
.sceneList
);
49 if (mState
.sceneList
.size() == 0)
51 throw Mf::Error(Mf::Error::SCRIPT_ERROR
,
52 "no variable `scenes' within loader");
56 void GameLayer::advanceScene()
58 if (mState
.sceneList
.size() != 0)
60 mState
.scene
= Scene::alloc(mState
.sceneList
[0]);
61 mState
.sceneList
.erase(mState
.sceneList
.begin());
63 Mf::Script::Result status
= mState
.scene
->load(mState
.script
);
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 mHud(Hud::alloc(mState
)),
89 mMusic("NightFusionIntro"),
92 mMusic
.setLooping(true);
93 mMusic
.enqueue("NightFusionLoop");
96 Mf::settings
.get("nomusic", isMute
);
97 if (!isMute
) mMusic
.play();
99 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
102 advanceScene(); // load the first scene
104 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
105 0.1, Mf::Timer::REPEAT
);
107 mState
.heroine
= Heroine::alloc();
108 mState
.heroine
->animation
.startSequence("FlyDiagonallyUp");
110 mState
.interp
.init(0.0, 1.0);
111 mState
.interp
.reset(4.0, Mf::Interp::OSCILLATE
);
117 void GameLayer::pushedOntoEngine()
121 mRay
.direction
.set(1.0, 0.0);
126 mSphere
.point
.set(22, 5);
129 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this),
130 1.0, Mf::Timer::REPEAT
);
134 void GameLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
136 mState
.camera
.update(t
, dt
);
137 mState
.heroine
->update(t
, dt
);
139 mState
.scene
->checkForCollision(*mState
.heroine
);
141 Mf::Vector3
camPosition(-mState
.heroine
->getState().position
[0],
142 -mState
.heroine
->getState().position
[1], -8);
143 mState
.camera
.setPosition(camPosition
);
145 mRay
.point
= mState
.heroine
->getState().position
;
148 void GameLayer::thinkTimer()
150 mState
.script
.getRegistryTable().pushField("Think");
151 if (mState
.script
[-1].isFunction()) mState
.script
.call();
152 else mState
.script
.pop();
156 void GameLayer::rayTimer()
158 Mf::Ray
<2>::Intersection meh
;
159 std::list
<Mf::Ray
<2>::Intersection
> hits
;
162 bool bam
= mLine
.intersectRay(mRay
, meh
);
165 meh
.normal
.normalize();
169 bam
= mSphere
.intersectRay(mRay
, meh
);
172 meh
.normal
.normalize();
176 if (mState
.scene
->castRay(mRay
, hits
))
178 hits
.front().normal
.normalize();
179 mRay
.solve(point
, hits
.front().distance
);
180 //Mf::logInfo << "scene: d = " << hits.front().distance << std::endl;
181 //Mf::logInfo << " P = " << point << std::endl;
182 //Mf::logInfo << " n = " << hits.front().normal << std::endl;
187 void GameLayer::draw(Mf::Scalar alpha
) const
189 mState
.camera
.uploadToGL(alpha
);
192 Mf::Texture::resetBind();
194 glEnableClientState(GL_VERTEX_ARRAY
);
195 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
197 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
199 mState
.heroine
->draw(alpha
);
206 bool GameLayer::handleEvent(const Mf::Event
& event
)
211 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
213 mState
.heroine
->animation
.startSequence("Flattened");
214 Mf::logInfo("thump!");
218 else if (event
.key
.keysym
.sym
== SDLK_m
)
223 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
225 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
229 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
231 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
235 else if (event
.key
.keysym
.sym
== SDLK_r
)
241 return mState
.heroine
->handleEvent(event
);
244 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
249 else if (event
.key
.keysym
.sym
== SDLK_h
)
254 return mState
.heroine
->handleEvent(event
);
256 case SDL_MOUSEMOTION
:
257 case SDL_MOUSEBUTTONDOWN
:
258 mState
.camera
.handleEvent(event
);
261 case SDL_VIDEORESIZE
:
262 setProjection(event
.resize
.w
, event
.resize
.h
);
270 void GameLayer::setProjection()
273 "no current video context from which to get dimensions");
274 setProjection(Mf::video
->getWidth(), Mf::video
->getHeight());
277 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
279 mState
.camera
.setProjection(cml::rad(45.0), width
/ height
, 1.0, 200.0);
This page took 0.044003 seconds and 3 git commands to generate.