X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FGameLayer.cc;h=f61ec3d43adf07da1b7fe0d9884fa713ae0d83d1;hp=a80946f6f5ff68fd393ad165734871eaa7ba8cf7;hb=e495074443d9fd7bc16137084cf9de3d031b75c4;hpb=a31d65a998121df0651c57bfb68782e2a07d2e2f diff --git a/src/GameLayer.cc b/src/GameLayer.cc index a80946f..f61ec3d 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -27,69 +27,122 @@ *******************************************************************************/ #include +#include #include #include #include #include #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()); + mState.scene->load(mState.script); + } +} + + GameLayer::GameLayer() : - mMusic("BeatTheCube"), + mMusic("NightFusionIntro"), mPunchSound("Thump") { mMusic.setLooping(true); mMusic.enqueue("NightFusionLoop"); mMusic.stream(); - mHeroine = Heroine::alloc(); - mHeroine->animation.startSequence("FlyDiagonallyUp"); + loadSceneLoader(); + advanceScene(); // load the first scene - 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.heroine = Heroine::alloc(); + mState.heroine->animation.startSequence("FlyDiagonallyUp"); - mScene = Scene::alloc("Classic"); + 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(); - - mHud = Hud::alloc(); } void GameLayer::pushed(Mf::Engine& engine) { - engine.push(mHud); + engine.push(Hud::alloc(mState)); } -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], -256)); - //mCamera.lookAt(Mf::promote(mHeroine->getState().position)); + 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(mHeroine->getState().position); + //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position); //Mf::Sound::setListenerPosition(heroinePosition); - - mInterp.update(t, dt); - mHud->setBar1Progress(mInterp.getState(dt)); - mHud->setBar2Progress(1.0 - mInterp.getState(dt)); } -void GameLayer::draw(Mf::Scalar alpha) const +void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const { - mCamera.uploadToGL(alpha); + mState.camera.uploadToGL(alpha); // DRAW THE SCENE Mf::Texture::resetBind(); @@ -97,44 +150,51 @@ void GameLayer::draw(Mf::Scalar alpha) const glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - mScene->drawIfVisible(alpha, mCamera.getFrustum()); + mState.scene->drawIfVisible(alpha, mState.camera.getFrustum()); - mHeroine->draw(alpha); + 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) { - 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) + return mState.heroine->handleEvent(event); + + case SDL_KEYUP: + if (event.key.keysym.sym == SDLK_ESCAPE) { - Mf::Engine::getInstance().pop(); + engine.pop(this); return true; } - - case SDL_KEYUP: - return mHeroine->handleEvent(event); + 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; } @@ -144,13 +204,13 @@ bool GameLayer::handleEvent(const Mf::Event& event) 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, 32.0, 2500.0); + mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0); }