]> Dogcows Code - chaz/yoink/blobdiff - src/GameLayer.cc
refactoring needed for win32 crash
[chaz/yoink] / src / GameLayer.cc
index 1e607586d486169afa681b8cea57015b5353e2e9..493aa6cfa5392334c6c4165edfcc0e7c5f0e948a 100644 (file)
@@ -26,8 +26,8 @@
 
 *******************************************************************************/
 
-#include <Moof/Engine.hh>
-#include <Moof/Exception.hh>
+#include <Moof/Core.hh>
+#include <Moof/Error.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
 
 
-
-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);
+       importLogFunctions(mState.script);
 
        std::string loaderPath = Scene::getPath("loader");
        if (loaderPath == "")
        {
-               throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
+               throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
        }
 
-       Mf::Script::Status status = mState.script.doFile(loaderPath);
+       Mf::Script::Result 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);
+               throw Mf::Error(Mf::Error::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");
+               throw Mf::Error(Mf::Error::SCRIPT_ERROR,
+                               "no variable `scenes' within loader");
        }
 }
 
@@ -94,20 +77,32 @@ void GameLayer::advanceScene()
                mState.scene = Scene::alloc(mState.sceneList[0]);
                mState.sceneList.erase(mState.sceneList.begin());
 
-               Mf::Script::Status status = mState.scene->load(mState.script);
+               Mf::Script::Result 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);
+                       throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
+               }
+
+               mState.script.getGlobalTable().pushField("Event");
+               if (mState.script[-1].isTable())
+               {
+                       mState.script[-1].pushField("Think");
+                       mState.script.set("Think", Mf::Script::REGISTRY);
+                       mState.script.pop(2);
+               }
+               else
+               {
+                       mState.script.pop();
                }
        }
 }
 
 
 GameLayer::GameLayer() :
+       mHud(Hud::alloc(mState)),
        mMusic("NightFusionIntro"),
        mPunchSound("Thump")
 {
@@ -115,41 +110,44 @@ GameLayer::GameLayer() :
        mMusic.enqueue("NightFusionLoop");
 
        bool isMute = false;
-       Mf::Settings::getInstance().get("nomusic", isMute);
+       Mf::settings.get("nomusic", isMute);
        if (!isMute) mMusic.play();
 
+       //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0));
+
        loadSceneLoader();
        advanceScene();                         // load the first scene
 
+       mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
+                       0.1, Mf::Timer::REPEAT);
+
        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);
+       mState.interp.init(a, 5.0, Mf::Interpolator::OSCILLATE);
 
        setProjection();
 }
 
 
-void GameLayer::pushed(Mf::Engine& engine)
+void GameLayer::pushedOntoEngine()
 {
-       engine.push(Hud::alloc(mState));
+       Mf::core.push(mHud);
 
        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;
+
+       mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), 1.0, Mf::Timer::REPEAT);
 }
 
 
-void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
 {
        mState.camera.update(t, dt);
        mState.heroine->update(t, dt);
@@ -157,52 +155,52 @@ void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
        mState.scene->checkForCollision(*mState.heroine);
 
        mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
-                               -mState.heroine->getState().position[1], -9));
+                               -mState.heroine->getState().position[1], -8));
        //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
 
-       //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
-       //Mf::Sound::setListenerPosition(heroinePosition);
-       
        mRay.point = mState.heroine->getState().position;
-       mRay3.point = Mf::promote(mRay.point);
-       mRay3.direction = Mf::promote(mRay.direction);
+}
 
+void GameLayer::thinkTimer()
+{
+       mState.script.getRegistryTable().pushField("Think");
+       if (mState.script[-1].isFunction()) mState.script.call();
+       else                                mState.script.pop();
+}
+
+
+void GameLayer::rayTimer()
+{
        Mf::Ray<2>::Intersection meh;
-       Mf::Ray<3>::Intersection meh3;
+       std::list<Mf::Ray<2>::Intersection> hits;
+       Mf::Vector2 point;
 
-       Mf::Scalar d = mLine.intersectRay(mRay, meh);
-       if (d > 0.0)
+       bool bam = mLine.intersectRay(mRay, meh);
+       if (bam)
        {
-               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]);
+               meh.normal.normalize();
+               hits.push_back(meh);
        }
-       //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)
+
+       bam = mSphere.intersectRay(mRay, meh);
+       if (bam)
        {
-               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]);
+               meh.normal.normalize();
+               hits.push_back(meh);
        }
 
-       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]);
+               hits.front().normal.normalize();
+               mRay.solve(point, hits.front().distance);
+               //Mf::logInfo << "scene: d = " << hits.front().distance << std::endl;
+               //Mf::logInfo << "       P = " << point << std::endl;
+               //Mf::logInfo << "       n = " << hits.front().normal << std::endl;
        }
 }
 
 
-void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+void GameLayer::draw(Mf::Scalar alpha) const
 {
        mState.camera.uploadToGL(alpha);
 
@@ -214,7 +212,6 @@ void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
 
        mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
 
-       mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
        mState.heroine->draw(alpha);
 
        mRay.draw();
@@ -222,7 +219,7 @@ void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
        mSphere.draw();
 }
 
-bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
+bool GameLayer::handleEvent(const Mf::Event& event)
 {
        switch (event.type)
        {
@@ -241,12 +238,20 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                        }
                        else if (event.key.keysym.sym == SDLK_PAGEUP)
                        {
-                               mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(10.0));
+                               mRay.direction = cml::rotate_vector_2D(mRay.direction,
+                                               cml::rad(10.0));
                                return true;
                        }
                        else if (event.key.keysym.sym == SDLK_PAGEDOWN)
                        {
-                               mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(-10.0));
+                               mRay.direction = cml::rotate_vector_2D(mRay.direction,
+                                               cml::rad(-10.0));
+                               return true;
+                       }
+                       else if (event.key.keysym.sym == SDLK_r)
+                       {
+                               loadSceneLoader();
+                               advanceScene();
                                return true;
                        }
                        return mState.heroine->handleEvent(event);
@@ -254,12 +259,12 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                case SDL_KEYUP:
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                        {
-                               engine.pop(this);
+                               Mf::core.pop(this);
                                return true;
                        }
                        else if (event.key.keysym.sym == SDLK_h)
                        {
-                               engine.push(Hud::alloc(mState));
+                               Mf::core.push(mHud);
                                return true;
                        }
                        return mState.heroine->handleEvent(event);
@@ -280,13 +285,13 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
 
 void GameLayer::setProjection()
 {
-       Mf::VideoP video = Mf::Engine::getInstance().getVideo();
-       setProjection(video->getWidth(), video->getHeight());
+       ASSERT(Mf::video && "no current video context from which to get dimensions");
+       setProjection(Mf::video->getWidth(), Mf::video->getHeight());
 }
 
 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
 {
-       mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
+       mState.camera.setProjection(cml::rad(45.0), width / height, 1.0, 200.0);
 }
 
 
This page took 0.027662 seconds and 4 git commands to generate.