]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
46c32631aad077b362f9db36bebc7513bd179c10
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #include <Moof/Engine.hh>
30 #include <Moof/Error.hh>
31 #include <Moof/Log.hh>
32 #include <Moof/Math.hh>
33 #include <Moof/OpenGL.hh>
34 #include <Moof/Settings.hh>
35 #include <Moof/Video.hh>
37 #include "GameLayer.hh"
44 void GameLayer::loadSceneLoader()
46 mState
.script
.importStandardLibraries();
47 importLogFunctions(mState
.script
);
49 std::string loaderPath
= Scene::getPath("loader");
52 throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND
, "loader");
55 Mf::Script::Result status
= mState
.script
.doFile(loaderPath
);
56 if (status
!= Mf::Script::SUCCESS
)
59 mState
.script
[-1].get(str
);
61 throw Mf::Error(Mf::Error::SCRIPT_ERROR
, str
);
64 mState
.script
.getGlobalTable().pushField("scenes");
65 mState
.script
.getTop().get(mState
.sceneList
);
66 if (mState
.sceneList
.size() == 0)
68 throw Mf::Error(Mf::Error::SCRIPT_ERROR
,
69 "no variable `scenes' within loader");
73 void GameLayer::advanceScene()
75 if (mState
.sceneList
.size() != 0)
77 mState
.scene
= Scene::alloc(mState
.sceneList
[0]);
78 mState
.sceneList
.erase(mState
.sceneList
.begin());
80 Mf::Script::Result status
= mState
.scene
->load(mState
.script
);
81 if (status
!= Mf::Script::SUCCESS
)
84 mState
.script
[-1].get(str
);
86 throw Mf::Error(Mf::Error::SCRIPT_ERROR
, str
);
89 mState
.script
.getGlobalTable().pushField("Event");
90 if (mState
.script
[-1].isTable())
92 mState
.script
[-1].pushField("Think");
93 mState
.script
.set("Think", Mf::Script::REGISTRY
);
104 GameLayer::GameLayer() :
105 mHud(Hud::alloc(mState
)),
106 mMusic("NightFusionIntro"),
109 mMusic
.setLooping(true);
110 mMusic
.enqueue("NightFusionLoop");
113 Mf::Settings::getInstance().get("nomusic", isMute
);
114 if (!isMute
) mMusic
.play();
116 //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
119 advanceScene(); // load the first scene
121 mThinkTimer
.init(boost::bind(&GameLayer::thinkTimer
, this),
122 0.1, Mf::Timer::REPEAT
);
124 mState
.heroine
= Heroine::alloc();
125 mState
.heroine
->animation
.startSequence("FlyDiagonallyUp");
127 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
128 mState
.interp
.init(a
, 5.0, Mf::Interpolator::OSCILLATE
);
134 void GameLayer::pushedOntoEngine()
136 Mf::engine
.push(mHud
);
138 mRay
.direction
.set(1.0, 0.0);
143 mSphere
.point
.set(22, 5);
146 mRayTimer
.init(boost::bind(&GameLayer::rayTimer
, this), 1.0, Mf::Timer::REPEAT
);
150 void GameLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
152 mState
.camera
.update(t
, dt
);
153 mState
.heroine
->update(t
, dt
);
155 mState
.scene
->checkForCollision(*mState
.heroine
);
157 mState
.camera
.setPosition(Mf::Vector3(-mState
.heroine
->getState().position
[0],
158 -mState
.heroine
->getState().position
[1], -8));
159 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
161 mRay
.point
= mState
.heroine
->getState().position
;
164 void GameLayer::thinkTimer()
166 mState
.script
.getRegistryTable().pushField("Think");
167 if (mState
.script
[-1].isFunction()) mState
.script
.call();
168 else mState
.script
.pop();
172 void GameLayer::rayTimer()
174 Mf::Ray
<2>::Intersection meh
;
175 std::list
<Mf::Ray
<2>::Intersection
> hits
;
178 bool bam
= mLine
.intersectRay(mRay
, meh
);
181 meh
.normal
.normalize();
185 bam
= mSphere
.intersectRay(mRay
, meh
);
188 meh
.normal
.normalize();
192 if (mState
.scene
->castRay(mRay
, hits
))
194 hits
.front().normal
.normalize();
195 mRay
.solve(point
, hits
.front().distance
);
196 //Mf::logInfo << "scene: d = " << hits.front().distance << std::endl;
197 //Mf::logInfo << " P = " << point << std::endl;
198 //Mf::logInfo << " n = " << hits.front().normal << std::endl;
203 void GameLayer::draw(Mf::Scalar alpha
) const
205 mState
.camera
.uploadToGL(alpha
);
208 Mf::Texture::resetBind();
210 glEnableClientState(GL_VERTEX_ARRAY
);
211 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
213 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
215 mState
.heroine
->draw(alpha
);
222 bool GameLayer::handleEvent(const Mf::Event
& event
)
227 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
229 mState
.heroine
->animation
.startSequence("Flattened");
230 Mf::logInfo("thump!");
234 else if (event
.key
.keysym
.sym
== SDLK_m
)
239 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
241 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
245 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
247 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
,
251 else if (event
.key
.keysym
.sym
== SDLK_r
)
257 return mState
.heroine
->handleEvent(event
);
260 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
262 Mf::engine
.pop(this);
265 else if (event
.key
.keysym
.sym
== SDLK_h
)
267 Mf::engine
.push(mHud
);
270 return mState
.heroine
->handleEvent(event
);
272 case SDL_MOUSEMOTION
:
273 case SDL_MOUSEBUTTONDOWN
:
274 mState
.camera
.handleEvent(event
);
277 case SDL_VIDEORESIZE
:
278 setProjection(event
.resize
.w
, event
.resize
.h
);
286 void GameLayer::setProjection()
288 Mf::VideoP video
= Mf::engine
.getVideo();
289 setProjection(video
->getWidth(), video
->getHeight());
292 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
294 mState
.camera
.setProjection(cml::rad(45.0), width
/ height
, 1.0, 200.0);
298 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.047967 seconds and 4 git commands to generate.