]>
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/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::addedToCore()
121 mRay
.direction
.set(1.0, 0.0);
126 mCircle
.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::Ray2::Contact meh
;
159 std::list
<Mf::Ray2::Contact
> hits
;
162 bool bam
= mLine
.intersectRay(mRay
, meh
);
165 meh
.normal
.normalize();
169 bam
= mCircle
.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());
198 mState
.heroine
->draw(alpha
);
205 bool GameLayer::handleEvent(const Mf::Event
& event
)
210 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
212 mState
.heroine
->animation
.startSequence("Flattened");
213 Mf::logInfo("thump!");
217 else if (event
.key
.keysym
.sym
== SDLK_m
)
222 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
224 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
228 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
230 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
234 else if (event
.key
.keysym
.sym
== SDLK_r
)
240 return mState
.heroine
->handleEvent(event
);
243 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
248 else if (event
.key
.keysym
.sym
== SDLK_h
)
253 return mState
.heroine
->handleEvent(event
);
255 case SDL_MOUSEMOTION
:
256 case SDL_MOUSEBUTTONDOWN
:
257 mState
.camera
.handleEvent(event
);
260 case SDL_VIDEORESIZE
:
261 setProjection(event
.resize
.w
, event
.resize
.h
);
269 void GameLayer::setProjection()
272 "no current video context from which to get dimensions");
273 setProjection(Mf::video
->getWidth(), Mf::video
->getHeight());
276 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
278 mState
.camera
.setProjection(cml::rad(45.0), width
/ height
, 1.0, 200.0);
This page took 0.0430739999999999 seconds and 5 git commands to generate.