]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
1e607586d486169afa681b8cea57015b5353e2e9
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/Exception.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"
46 Mf::Scalar
GameLayer::getZCoord(const Mf::Vector2
& position
) const
50 mState
.script
.getGlobalTable().pushField("GetZCoord");
51 mState
.script
.push(position
[0]);
52 mState
.script
.push(position
[1]);
53 mState
.script
.call(2, 1);
54 mState
.script
.getTop().get(z
);
60 void GameLayer::loadSceneLoader()
62 mState
.script
.importStandardLibraries();
63 importLogPrintFunction(mState
.script
);
65 std::string loaderPath
= Scene::getPath("loader");
68 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND
, "loader");
71 Mf::Script::Status status
= mState
.script
.doFile(loaderPath
);
72 if (status
!= Mf::Script::SUCCESS
)
75 mState
.script
[-1].get(str
);
77 Mf::logScript("%s", str
.c_str());
78 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, str
);
81 mState
.script
.getGlobalTable().pushField("scenes");
82 mState
.script
.getTop().get(mState
.sceneList
);
83 if (mState
.sceneList
.size() == 0)
85 Mf::logScript("no variable `scenes' within loader");
86 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, "no scenes to load");
90 void GameLayer::advanceScene()
92 if (mState
.sceneList
.size() != 0)
94 mState
.scene
= Scene::alloc(mState
.sceneList
[0]);
95 mState
.sceneList
.erase(mState
.sceneList
.begin());
97 Mf::Script::Status status
= mState
.scene
->load(mState
.script
);
98 if (status
!= Mf::Script::SUCCESS
)
101 mState
.script
[-1].get(str
);
103 Mf::logScript("%s", str
.c_str());
104 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, str
);
110 GameLayer::GameLayer() :
111 mMusic("NightFusionIntro"),
114 mMusic
.setLooping(true);
115 mMusic
.enqueue("NightFusionLoop");
118 Mf::Settings::getInstance().get("nomusic", isMute
);
119 if (!isMute
) mMusic
.play();
122 advanceScene(); // load the first scene
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
, 2.0, Mf::Interpolator::OSCILLATE
);
134 void GameLayer::pushed(Mf::Engine
& engine
)
136 engine
.push(Hud::alloc(mState
));
138 mRay
.direction
.set(1.0, 0.0);
139 mRay3
.direction
.set(1.0, 0.0, 0.0);
144 mPlane
.normal
.set(-1.0, 0.0, 0.0);
147 mSphere
.point
.set(22, 5);
152 void GameLayer::update(Mf::Engine
& engine
, Mf::Scalar t
, Mf::Scalar dt
)
154 mState
.camera
.update(t
, dt
);
155 mState
.heroine
->update(t
, dt
);
157 mState
.scene
->checkForCollision(*mState
.heroine
);
159 mState
.camera
.setPosition(Mf::Vector3(-mState
.heroine
->getState().position
[0],
160 -mState
.heroine
->getState().position
[1], -9));
161 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
163 //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
164 //Mf::Sound::setListenerPosition(heroinePosition);
166 mRay
.point
= mState
.heroine
->getState().position
;
167 mRay3
.point
= Mf::promote(mRay
.point
);
168 mRay3
.direction
= Mf::promote(mRay
.direction
);
170 Mf::Ray
<2>::Intersection meh
;
171 Mf::Ray
<3>::Intersection meh3
;
173 Mf::Scalar d
= mLine
.intersectRay(mRay
, meh
);
176 Mf::logDebug("line: d = %f", d
);
177 Mf::logDebug(" P = <%f,%f>", meh
.point
[0], meh
.point
[1]);
178 Mf::logDebug(" n = <%f,%f>", meh
.normal
[0], meh
.normal
[1]);
180 //d = mPlane.intersectRay(mRay3, meh3);
183 //Mf::logDebug("plane: d = %f", d);
184 //Mf::logDebug(" P = <%f,%f>", meh3.point[0], meh3.point[1]);
185 //Mf::logDebug(" n = <%f,%f>", meh3.normal[0], meh3.normal[1]);
187 d
= mSphere
.intersectRay(mRay
, meh
);
190 Mf::logDebug("sphere: d = %f", d
);
191 Mf::logDebug(" P = <%f,%f>", meh
.point
[0], meh
.point
[1]);
192 Mf::logDebug(" n = <%f,%f>", meh
.normal
[0], meh
.normal
[1]);
195 std::list
<Mf::Ray
<2>::Intersection
> hits
;
196 if (mState
.scene
->castRay(mRay
, hits
))
198 Mf::logDebug("scene: d = %f", d
);
199 Mf::logDebug(" P = <%f,%f>", hits
.front().point
[0], hits
.front().point
[1]);
200 Mf::logDebug(" n = <%f,%f>", hits
.front().normal
[0], hits
.front().normal
[1]);
205 void GameLayer::draw(Mf::Engine
& engine
, Mf::Scalar alpha
) const
207 mState
.camera
.uploadToGL(alpha
);
210 Mf::Texture::resetBind();
212 glEnableClientState(GL_VERTEX_ARRAY
);
213 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
215 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
217 mState
.heroine
->setZCoord(getZCoord(mState
.heroine
->getState().position
));
218 mState
.heroine
->draw(alpha
);
225 bool GameLayer::handleEvent(Mf::Engine
& engine
, const Mf::Event
& event
)
230 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
232 mState
.heroine
->animation
.startSequence("Flattened");
233 Mf::logInfo("thump!");
237 else if (event
.key
.keysym
.sym
== SDLK_m
)
242 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
244 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
, cml::rad(10.0));
247 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
249 mRay
.direction
= cml::rotate_vector_2D(mRay
.direction
, cml::rad(-10.0));
252 return mState
.heroine
->handleEvent(event
);
255 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
260 else if (event
.key
.keysym
.sym
== SDLK_h
)
262 engine
.push(Hud::alloc(mState
));
265 return mState
.heroine
->handleEvent(event
);
267 case SDL_MOUSEMOTION
:
268 case SDL_MOUSEBUTTONDOWN
:
269 mState
.camera
.handleEvent(event
);
272 case SDL_VIDEORESIZE
:
273 setProjection(event
.resize
.w
, event
.resize
.h
);
281 void GameLayer::setProjection()
283 Mf::VideoP video
= Mf::Engine::getInstance().getVideo();
284 setProjection(video
->getWidth(), video
->getHeight());
287 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
289 mState
.camera
.setProjection(cml::rad(60.0), width
/ height
, 1.0, 200.0);
293 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.041059 seconds and 3 git commands to generate.