*******************************************************************************/
#include <Moof/Engine.hh>
+#include <Moof/Exception.hh>
#include <Moof/Log.hh>
#include <Moof/Math.hh>
#include <Moof/OpenGL.hh>
+#include <Moof/Video.hh>
#include "GameLayer.hh"
+#include "Hud.hh"
#if HAVE_CONFIG_H
#include "config.h"
#endif
-GameLayer::GameLayer() :
- music("NightFusionIntro"),
- punchSound("Thump")
+
+Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
{
- music.setLooping(true);
- music.enqueue("NightFusionLoop");
- music.stream();
+ Mf::Scalar z;
- heroine = Character::alloc("RobotTrooper");
- heroine->getAnimation().startSequence("Run");
+ 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();
- Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
- interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
+ return z;
+}
- Mf::Scalar b[2] = {1.0, 0.0};
- fadeIn.init(b, 1.0);
+void GameLayer::loadSceneLoader()
+{
+ mState.script.importStandardLibraries();
+ importLogPrintFunction(mState.script);
- octree = Mf::loadScene("Classic");
- heroine->treeNode = octree->insert(heroine);
+ 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");
+ }
+}
- camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
- camera.uploadProjectionToGL();
+void GameLayer::advanceScene()
+{
+ if (mState.sceneList.size() != 0)
+ {
+ mState.scene = Scene::alloc(mState.sceneList[0]);
+ mState.sceneList.erase(mState.sceneList.begin());
+ mState.scene->load(mState.script);
+ }
}
-void GameLayer::pushed(Mf::Engine& engine)
+GameLayer::GameLayer() :
+ mMusic("NightFusionIntro"),
+ mPunchSound("Thump")
{
- hud = Hud::alloc();
- engine.pushLayer(hud);
+ mMusic.setLooping(true);
+ mMusic.enqueue("NightFusionLoop");
+ mMusic.stream();
+
+ loadSceneLoader();
+ advanceScene(); // load the first scene
+
+ 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};
+ mState.interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
+
+ setProjection();
}
-void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::pushed(Mf::Engine& engine)
{
- //dt *= 0.7;
-
- fadeIn.update(dt);
- camera.update(t, dt);
- heroine->update(t, dt);
-
- // reinsert heroine
- heroine->treeNode = octree->reinsert(heroine, heroine->treeNode);
- octree->print(heroine->treeNode);
-
- //camera.lookAt(heroine->getSphere().point);
- camera.setPosition(Mf::Vector3(-heroine->current.position[0],
- -heroine->current.position[1], -256));
-
- Mf::Vector3 heroinePosition;
- Mf::promoteVector(heroinePosition, heroine->current.position);
- Mf::Sound::setListenerPosition(heroinePosition);
-
- interp.update(dt);
- hud->setBar1Progress(interp.getState(dt));
- hud->setBar2Progress(1.0 - interp.getState(dt));
+ engine.push(Hud::alloc(mState));
}
-void GameLayer::draw(Mf::Scalar alpha) const
+void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
{
- glMatrixMode(GL_MODELVIEW);
- glLoadMatrix(camera.getModelviewMatrix().data());
+ mState.camera.update(t, dt);
+ mState.heroine->update(t, dt);
- // DRAW THE SCENE
- Mf::Texture::resetBind();
+ mState.scene->checkForCollision(*mState.heroine);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
+ -mState.heroine->getState().position[1], -10));
+ //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
+
+ //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
+ //Mf::Sound::setListenerPosition(heroinePosition);
+}
- octree->drawIfVisible(alpha, camera.getFrustum());
- //heroine->draw(alpha);
- heroine->getAabb().draw();
+void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+{
+ mState.camera.uploadToGL(alpha);
- // DRAW FADE
- glEnable(GL_BLEND);
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
- glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
+ // DRAW THE SCENE
Mf::Texture::resetBind();
- //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
- glBegin(GL_QUADS);
- glVertex3f(-1.0, -1.0, -0.1);
- glVertex3f(1.0, -1.0, -0.1);
- glVertex3f(1.0, 1.0, -0.1);
- glVertex3f(-1.0, 1.0, -0.1);
- glEnd();
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glDisable(GL_BLEND);
+ mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
+ mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
+ mState.heroine->draw(alpha);
}
-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->getAnimation().startSequence("Punch");
+ mState.heroine->animation.startSequence("Flattened");
Mf::logInfo("thump!");
- punchSound.play();
+ mPunchSound.play();
return true;
}
- else if (event.key.keysym.sym == SDLK_p)
+ else if (event.key.keysym.sym == SDLK_m)
{
- music.toggle();
+ mMusic.toggle();
return true;
}
- else if (event.key.keysym.sym == SDLK_y)
+ return mState.heroine->handleEvent(event);
+
+ case SDL_KEYUP:
+ if (event.key.keysym.sym == SDLK_ESCAPE)
{
- Mf::Engine::getInstance().popLayer();
+ engine.pop(this);
return true;
}
-
- case SDL_KEYUP:
- heroine->handleEvent(event);
- break;
+ 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:
- glViewport(0, 0, event.resize.w, event.resize.h);
- camera.setProjection(cml::rad(60.0),
- double(event.resize.w) / double(event.resize.h), 32.0, 2500.0);
- camera.uploadProjectionToGL();
+ setProjection(event.resize.w, event.resize.h);
break;
}
}
+void GameLayer::setProjection()
+{
+ Mf::VideoP video = Mf::Engine::getInstance().getVideo();
+ setProjection(video->getWidth(), video->getHeight());
+}
+
+void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
+{
+ mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
+}
+
+
/** vim: set ts=4 sw=4 tw=80: *************************************************/