*******************************************************************************/
#include <Moof/Engine.hh>
+#include <Moof/Exception.hh>
#include <Moof/Log.hh>
#include <Moof/Math.hh>
#include <Moof/OpenGL.hh>
+#include <Moof/Settings.hh>
#include <Moof/Video.hh>
#include "GameLayer.hh"
+#include "Hud.hh"
#if HAVE_CONFIG_H
#include "config.h"
#endif
+
+Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
+{
+ Mf::Scalar z;
+
+ mState.script.getGlobalTable().pushField("GetZCoord");
+ mState.script.push(position[0]);
+ mState.script.push(position[1]);
+ mState.script.call(2, 1);
+ mState.script.getTop().get(z);
+ mState.script.pop();
+
+ return z;
+}
+
+void GameLayer::loadSceneLoader()
+{
+ mState.script.importStandardLibraries();
+ importLogPrintFunction(mState.script);
+
+ std::string loaderPath = Scene::getPath("loader");
+ if (loaderPath == "")
+ {
+ throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
+ }
+
+ Mf::Script::Status status = mState.script.doFile(loaderPath);
+ if (status != Mf::Script::SUCCESS)
+ {
+ std::string str;
+ mState.script[-1].get(str);
+
+ Mf::logScript("%s", str.c_str());
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
+ }
+
+ mState.script.getGlobalTable().pushField("scenes");
+ mState.script.getTop().get(mState.sceneList);
+ if (mState.sceneList.size() == 0)
+ {
+ Mf::logScript("no variable `scenes' within loader");
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load");
+ }
+}
+
+void GameLayer::advanceScene()
+{
+ if (mState.sceneList.size() != 0)
+ {
+ mState.scene = Scene::alloc(mState.sceneList[0]);
+ mState.sceneList.erase(mState.sceneList.begin());
+
+ Mf::Script::Status status = mState.scene->load(mState.script);
+ if (status != Mf::Script::SUCCESS)
+ {
+ std::string str;
+ mState.script[-1].get(str);
+
+ Mf::logScript("%s", str.c_str());
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
+ }
+ }
+}
+
+
GameLayer::GameLayer() :
- music("BeatTheCube"),
- punchSound("Thump")
+ mMusic("NightFusionIntro"),
+ mPunchSound("Thump")
{
- music.setLooping(true);
- music.enqueue("NightFusionLoop");
- music.stream();
+ mMusic.setLooping(true);
+ mMusic.enqueue("NightFusionLoop");
- heroine = Heroine::alloc();
- heroine->animation.startSequence("FlyDiagonallyUp");
+ bool isMute = false;
+ Mf::Settings::getInstance().get("nomusic", isMute);
+ if (!isMute) mMusic.play();
- Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
- interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
+ loadSceneLoader();
+ advanceScene(); // load the first scene
- scene = Scene::alloc("Classic");
+ mState.heroine = Heroine::alloc();
+ mState.heroine->animation.startSequence("FlyDiagonallyUp");
- setProjection();
+ Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
+ mState.interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
- hud = Hud::alloc();
+ setProjection();
}
void GameLayer::pushed(Mf::Engine& engine)
{
- engine.push(hud);
+ engine.push(Hud::alloc(mState));
+
+ mRay.direction.set(1.0, 0.0);
+ mRay3.direction.set(1.0, 0.0, 0.0);
+
+ mLine.a.set(20, 10);
+ mLine.b.set(19, 14);
+
+ mPlane.normal.set(-1.0, 0.0, 0.0);
+ mPlane.d = 0.0;
+
+ mSphere.point.set(22, 5);
+ mSphere.radius = 2;
}
-void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
{
- camera.update(t, dt);
- heroine->update(t, dt);
+ mState.camera.update(t, dt);
+ mState.heroine->update(t, dt);
+
+ mState.scene->checkForCollision(*mState.heroine);
- //camera.lookAt(heroine->getSphere().point);
- camera.setPosition(Mf::Vector3(-heroine->current.position[0],
- -heroine->current.position[1], -256));
+ mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
+ -mState.heroine->getState().position[1], -9));
+ //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
- //Mf::Vector3 heroinePosition = Mf::promote(heroine->current.position);
+ //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
//Mf::Sound::setListenerPosition(heroinePosition);
- interp.update(t, dt);
- hud->setBar1Progress(interp.getState(dt));
- hud->setBar2Progress(1.0 - interp.getState(dt));
+ mRay.point = mState.heroine->getState().position;
+ mRay3.point = Mf::promote(mRay.point);
+ mRay3.direction = Mf::promote(mRay.direction);
+
+ Mf::Ray<2>::Intersection meh;
+ Mf::Ray<3>::Intersection meh3;
+
+ Mf::Scalar d = mLine.intersectRay(mRay, meh);
+ if (d > 0.0)
+ {
+ Mf::logDebug("line: d = %f", d);
+ Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
+ Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
+ }
+ //d = mPlane.intersectRay(mRay3, meh3);
+ //if (d > 0.0)
+ //{
+ //Mf::logDebug("plane: d = %f", d);
+ //Mf::logDebug(" P = <%f,%f>", meh3.point[0], meh3.point[1]);
+ //Mf::logDebug(" n = <%f,%f>", meh3.normal[0], meh3.normal[1]);
+ //}
+ d = mSphere.intersectRay(mRay, meh);
+ if (d > 0.0)
+ {
+ Mf::logDebug("sphere: d = %f", d);
+ Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
+ Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
+ }
+
+ std::list<Mf::Ray<2>::Intersection> hits;
+ if (mState.scene->castRay(mRay, hits))
+ {
+ Mf::logDebug("scene: d = %f", d);
+ Mf::logDebug(" P = <%f,%f>", hits.front().point[0], hits.front().point[1]);
+ Mf::logDebug(" n = <%f,%f>", hits.front().normal[0], hits.front().normal[1]);
+ }
}
-void GameLayer::draw(Mf::Scalar alpha) const
+void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
{
- camera.uploadToGL();
+ mState.camera.uploadToGL(alpha);
// DRAW THE SCENE
Mf::Texture::resetBind();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- scene->drawIfVisible(alpha, camera.getFrustum());
+ mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
+
+ mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
+ mState.heroine->draw(alpha);
- heroine->draw(alpha);
+ mRay.draw();
+ mLine.draw();
+ mSphere.draw();
}
-bool GameLayer::handleEvent(const Mf::Event& event)
+bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
{
switch (event.type)
{
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE)
{
- heroine->animation.startSequence("Flattened");
+ mState.heroine->animation.startSequence("Flattened");
Mf::logInfo("thump!");
- punchSound.play();
+ mPunchSound.play();
+ return true;
+ }
+ else if (event.key.keysym.sym == SDLK_m)
+ {
+ mMusic.toggle();
return true;
}
- else if (event.key.keysym.sym == SDLK_p)
+ else if (event.key.keysym.sym == SDLK_PAGEUP)
{
- music.toggle();
+ mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(10.0));
return true;
}
- else if (event.key.keysym.sym == SDLK_y)
+ else if (event.key.keysym.sym == SDLK_PAGEDOWN)
{
- Mf::Engine::getInstance().pop();
+ mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(-10.0));
return true;
}
+ return mState.heroine->handleEvent(event);
case SDL_KEYUP:
- return heroine->handleEvent(event);
+ if (event.key.keysym.sym == SDLK_ESCAPE)
+ {
+ engine.pop(this);
+ return true;
+ }
+ else if (event.key.keysym.sym == SDLK_h)
+ {
+ engine.push(Hud::alloc(mState));
+ return true;
+ }
+ return mState.heroine->handleEvent(event);
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
- camera.handleEvent(event);
+ mState.camera.handleEvent(event);
return true;
case SDL_VIDEORESIZE:
- setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
+ setProjection(event.resize.w, event.resize.h);
break;
}
void GameLayer::setProjection()
{
- Mf::Video& video = Mf::Engine::getInstance().getVideo();
- setProjection(video.getWidth(), video.getHeight());
+ Mf::VideoP video = Mf::Engine::getInstance().getVideo();
+ setProjection(video->getWidth(), video->getHeight());
}
void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
{
- camera.setProjection(cml::rad(60.0), width / height, 32.0, 2500.0);
+ mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
}