X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FGameLayer.cc;h=6db27b16bb8b34011f9a6829c3ea81eca711d81f;hp=dfdadaa5f2c19fbe809193f9eebcc971569adfd6;hb=4f9eb9259092994de9690cf12f11437c35a6791e;hpb=892da43bf5796e7c5f593a6d0f53bd797a36bd3e diff --git a/src/GameLayer.cc b/src/GameLayer.cc index dfdadaa..6db27b1 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -26,10 +26,13 @@ *******************************************************************************/ -#include +#include +#include #include #include #include +#include +#include #include "GameLayer.hh" @@ -38,68 +41,168 @@ #endif +void GameLayer::loadSceneLoader() +{ + mState.script.importStandardLibraries(); + importLogFunctions(mState.script); + + std::string loaderPath = Scene::getPath("loader"); + if (loaderPath == "") + { + throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader"); + } + + Mf::Script::Result status = mState.script.doFile(loaderPath); + if (status != Mf::Script::SUCCESS) + { + std::string str; + mState.script[-1].get(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) + { + throw Mf::Error(Mf::Error::SCRIPT_ERROR, + "no variable `scenes' within loader"); + } +} + +void GameLayer::advanceScene() +{ + if (mState.sceneList.size() != 0) + { + 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::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() : - music("NightFusionIntro"), - punchSound("Thump") + mHud(Hud::alloc(mState)), + mMusic("NightFusionIntro"), + mPunchSound("Thump") { - music.setLooping(true); - music.enqueue("NightFusionLoop"); - music.stream(); + mMusic.setLooping(true); + mMusic.enqueue("NightFusionLoop"); + + bool isMute = false; + Mf::settings.get("nomusic", isMute); + if (!isMute) mMusic.play(); - heroine = Character::alloc("RobotTrooper"); - heroine->getAnimation().startSequence("Run"); + //mMusic.setPosition(Mf::Vector3(10.0, 5.0, 0.0)); - 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 - Mf::Scalar b[2] = {1.0, 0.0}; - fadeIn.init(b, 1.0); + mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this), + 0.1, Mf::Timer::REPEAT); - octree = Mf::loadScene("Classic"); - heroine->treeNode = octree->insert(heroine); + mState.heroine = Heroine::alloc(); + mState.heroine->animation.startSequence("FlyDiagonallyUp"); - camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0); - camera.uploadProjectionToGL(); + mState.interp.init(0.0, 1.0); + mState.interp.reset(4.0, Mf::Interp::OSCILLATE); + + setProjection(); } -void GameLayer::pushed(Mf::Engine& engine) +void GameLayer::pushedOntoEngine() { - hud = Hud::alloc(); - engine.pushLayer(hud); + Mf::core.push(mHud); + + 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) { - //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)); + mState.camera.update(t, dt); + mState.heroine->update(t, dt); + + mState.scene->checkForCollision(*mState.heroine); + + mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0], + -mState.heroine->getState().position[1], -8)); + //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position)); + + mRay.point = mState.heroine->getState().position; +} + +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; + std::list::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::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::Scalar alpha) const { - glMatrixMode(GL_MODELVIEW); - glLoadMatrix(camera.getModelviewMatrix().data()); + mState.camera.uploadToGL(alpha); // DRAW THE SCENE Mf::Texture::resetBind(); @@ -107,36 +210,13 @@ void GameLayer::draw(Mf::Scalar alpha) const glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - octree->drawIfVisible(alpha, camera.getFrustum()); - - //heroine->draw(alpha); - heroine->getAabb().draw(); - - // 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)); - 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(); + mState.scene->drawIfVisible(alpha, mState.camera.getFrustum()); - glDisable(GL_BLEND); + mState.heroine->draw(alpha); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); + mRay.draw(); + mLine.draw(); + mSphere.draw(); } bool GameLayer::handleEvent(const Mf::Event& event) @@ -146,36 +226,56 @@ bool GameLayer::handleEvent(const Mf::Event& event) 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_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().popLayer(); + 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); case SDL_KEYUP: - heroine->handleEvent(event); - break; + if (event.key.keysym.sym == SDLK_ESCAPE) + { + Mf::core.pop(this); + return true; + } + else if (event.key.keysym.sym == SDLK_h) + { + Mf::core.push(mHud); + 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; } @@ -183,5 +283,17 @@ bool GameLayer::handleEvent(const Mf::Event& event) } +void GameLayer::setProjection() +{ + 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(45.0), width / height, 1.0, 200.0); +} + + /** vim: set ts=4 sw=4 tw=80: *************************************************/