#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"
{
Mf::Scalar z;
- mScript.getGlobalTable().pushField("GetZCoord");
- mScript.push(position[0]);
- mScript.push(position[1]);
- mScript.call(2, 1);
- mScript.getTop().get(z);
- mScript.pop();
+ 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 = mScript.doFile(loaderPath);
+ Mf::Script::Result status = mState.script.doFile(loaderPath);
if (status != Mf::Script::SUCCESS)
{
std::string str;
- mScript[-1].get(str);
+ mState.script[-1].get(str);
- Mf::logScript("%s", str.c_str());
- throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str.c_str());
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
}
- mScript.getGlobalTable().pushField("scenes");
- mScript.getTop().get(mSceneList);
- if (mSceneList.size() == 0)
+ 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");
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR,
+ "no variable `scenes' within loader");
}
}
void GameLayer::advanceScene()
{
- if (mSceneList.size() != 0)
+ if (mState.sceneList.size() != 0)
{
- mScene = Scene::alloc(mSceneList[0]);
- mSceneList.erase(mSceneList.begin());
- mScene->load(mScript);
+ mState.scene = Scene::alloc(mState.sceneList[0]);
+ mState.sceneList.erase(mState.sceneList.begin());
+
+ Mf::Script::Result status = mState.scene->load(mState.script);
+ if (status != Mf::Script::SUCCESS)
+ {
+ std::string str;
+ mState.script[-1].get(str);
+
+ throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
+ }
}
}
{
mMusic.setLooping(true);
mMusic.enqueue("NightFusionLoop");
- mMusic.stream();
+
+ bool isMute = false;
+ Mf::Settings::getInstance().get("nomusic", isMute);
+ if (!isMute) mMusic.play();
+
+ //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
loadSceneLoader();
advanceScene(); // load the first scene
- mHeroine = Heroine::alloc();
- mHeroine->animation.startSequence("FlyDiagonallyUp");
+ mState.heroine = Heroine::alloc();
+ mState.heroine->animation.startSequence("FlyDiagonallyUp");
Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
- mInterp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
+ mState.interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
setProjection();
-
- mHud = Hud::alloc();
}
void GameLayer::pushed(Mf::Engine& engine)
{
- engine.push(mHud);
+ engine.push(Hud::alloc(mState));
+
+ mRay.direction.set(1.0, 0.0);
+
+ mLine.a.set(20, 10);
+ mLine.b.set(19, 14);
+
+ mSphere.point.set(22, 5);
+ mSphere.radius = 2;
+
+ mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), 1.0, Mf::Timer::REPEAT);
}
-void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
{
- mCamera.update(t, dt);
- mHeroine->update(t, dt);
+ mState.camera.update(t, dt);
+ mState.heroine->update(t, dt);
- mScene->checkForCollision(*mHeroine);
+ mState.scene->checkForCollision(*mState.heroine);
- mCamera.setPosition(Mf::Vector3(-mHeroine->getState().position[0],
- -mHeroine->getState().position[1], -10));
- //mCamera.lookAt(Mf::promote(mHeroine->getState().position));
+ 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(mHeroine->getState().position);
- //Mf::Sound::setListenerPosition(heroinePosition);
-
- mInterp.update(t, dt);
- mHud->setBar1Progress(mInterp.getState(dt));
- mHud->setBar2Progress(1.0 - mInterp.getState(dt));
+ mRay.point = mState.heroine->getState().position;
}
-void GameLayer::draw(Mf::Scalar alpha) const
+void GameLayer::rayTimer()
{
- mCamera.uploadToGL(alpha);
+ Mf::Ray<2>::Intersection meh;
+ std::list<Mf::Ray<2>::Intersection> hits;
+ Mf::Vector2 point;
+
+ bool bam = mLine.intersectRay(mRay, meh);
+ if (bam)
+ {
+ meh.normal.normalize();
+ hits.push_back(meh);
+ }
+
+ bam = mSphere.intersectRay(mRay, meh);
+ if (bam)
+ {
+ meh.normal.normalize();
+ hits.push_back(meh);
+ }
+
+ if (mState.scene->castRay(mRay, hits))
+ {
+ hits.front().normal.normalize();
+ mRay.solve(point, hits.front().distance);
+ Mf::logDebug << "scene: d = " << hits.front().distance << std::endl;
+ Mf::logDebug << " P = " << point << std::endl;
+ Mf::logDebug << " n = " << hits.front().normal << std::endl;
+ }
+}
+
+
+void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+{
+ mState.camera.uploadToGL(alpha);
// DRAW THE SCENE
Mf::Texture::resetBind();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- mScene->drawIfVisible(alpha, mCamera.getFrustum());
+ mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
- mHeroine->setZCoord(getZCoord(mHeroine->getState().position));
- mHeroine->draw(alpha);
+ mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
+ mState.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)
{
- mHeroine->animation.startSequence("Flattened");
+ mState.heroine->animation.startSequence("Flattened");
Mf::logInfo("thump!");
mPunchSound.play();
return true;
}
- else if (event.key.keysym.sym == SDLK_p)
+ else if (event.key.keysym.sym == SDLK_m)
{
mMusic.toggle();
return true;
}
- else if (event.key.keysym.sym == SDLK_y)
+ else if (event.key.keysym.sym == SDLK_PAGEUP)
+ {
+ mRay.direction = cml::rotate_vector_2D(mRay.direction,
+ cml::rad(10.0));
+ return true;
+ }
+ 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 mHeroine->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:
- mCamera.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)
{
- mCamera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
+ mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
}