X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FGameLayer.cc;h=d696f5ebdf851a0276f625f5c886eddb3dfd7594;hp=1e607586d486169afa681b8cea57015b5353e2e9;hb=449366f5f32d24f2a2a6589da6e16b2bf0d61773;hpb=7e84479de612a4ce287c6f63deb014b447a993ec diff --git a/src/GameLayer.cc b/src/GameLayer.cc index 1e60758..d696f5e 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -1,276 +1,271 @@ -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include "../config.h" + +#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); + state_.script.import_standard_libraries(); + moof::log::import(state_.script); - std::string loaderPath = Scene::getPath("loader"); - if (loaderPath == "") + std::string path("loader"); + if (!moof::resource::find(path)) { - throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader"); + throw std::runtime_error("cannot find scene loader script"); } - Mf::Script::Status status = mState.script.doFile(loaderPath); - if (status != Mf::Script::SUCCESS) + moof::script::status status = state_.script.do_file(path); + if (status != moof::script::success) { std::string str; - mState.script[-1].get(str); - - Mf::logScript("%s", str.c_str()); - throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str); + state_.script[-1].get(str); + throw std::runtime_error("script error: " + str); } - mState.script.getGlobalTable().pushField("scenes"); - mState.script.getTop().get(mState.sceneList); - if (mState.sceneList.size() == 0) + state_.script.globals().push_field("scenes"); + state_.script.top().get(state_.sceneList); + if (state_.sceneList.size() == 0) { - Mf::logScript("no variable `scenes' within loader"); - throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load"); + throw std::runtime_error("no variable `scenes' in script loader."); } } -void GameLayer::advanceScene() +void GameLayer::advanceScene(moof::settings& settings) { - if (mState.sceneList.size() != 0) + if (state_.sceneList.size() != 0) { - mState.scene = Scene::alloc(mState.sceneList[0]); - mState.sceneList.erase(mState.sceneList.begin()); + state_.scene = Scene::alloc(state_.sceneList[0]); + state_.sceneList.erase(state_.sceneList.begin()); - Mf::Script::Status status = mState.scene->load(mState.script); - if (status != Mf::Script::SUCCESS) + moof::script::status status = state_.scene->load(settings, + state_.script); + if (status != moof::script::success) { std::string str; - mState.script[-1].get(str); + state_.script[-1].get(str); + throw std::runtime_error("script error: " + str); + } - Mf::logScript("%s", str.c_str()); - throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str); + moof::script::slot table = state_.script.globals().push_field("Event"); + if (table.is_table()) + { + state_.script.push("Think"); + table.push_field("Think"); + state_.script.registry().set_field(); } + state_.script.pop(); } } -GameLayer::GameLayer() : - mMusic("NightFusionIntro"), - mPunchSound("Thump") +GameLayer::GameLayer() { - mMusic.setLooping(true); - mMusic.enqueue("NightFusionLoop"); + moof::log_info("about to load sound resource..."); + music_ = moof::resource::load("sounds/NightFusionIntro.ogg"); + if (music_) + { + music_->loop(true); + music_->enqueue("NightFusionLoop"); + } + else moof::log_error("music not loaded"); - bool isMute = false; - Mf::Settings::getInstance().get("nomusic", isMute); - if (!isMute) mMusic.play(); + //music_->position(moof::vector3(10.0, 5.0, 0.0)); - loadSceneLoader(); - advanceScene(); // load the first scene + mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this), + 0.1, moof::timer::repeat); - mState.heroine = Heroine::alloc(); - mState.heroine->animation.startSequence("FlyDiagonallyUp"); + state_.heroine = Heroine::alloc(); + state_.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(); + state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate); } -void GameLayer::pushed(Mf::Engine& engine) +void GameLayer::did_add_to_view() { - engine.push(Hud::alloc(mState)); + bool isMute = false; + settings().get("nomusic", isMute); + if (!isMute) music_->play(); + + loadSceneLoader(); + advanceScene(settings()); // load the first scene + + mHud = Hud::alloc(state_); + add_child(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; + mCircle.point.set(22, 5); + mCircle.radius = 2; + + mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), + 1.0, moof::timer::repeat); - mSphere.point.set(22, 5); - mSphere.radius = 2; + projection(); } -void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt) +void GameLayer::update(moof::scalar t, moof::scalar dt) { - mState.camera.update(t, dt); - mState.heroine->update(t, dt); + if (!state_.scene) return; + state_.camera.update(t, dt); + state_.heroine->update(t, dt); - mState.scene->checkForCollision(*mState.heroine); + state_.scene->checkForCollision(*state_.heroine); - 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)); + moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8); + state_.camera.position(cam); - //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); + mRay.point = state_.heroine->state().position; - Mf::Ray<2>::Intersection meh; - Mf::Ray<3>::Intersection meh3; + moof::view::update(t, dt); +} + +void GameLayer::thinkTimer() +{ + state_.script.registry().push_field("Think"); + if (state_.script[-1].is_function()) state_.script.call(); + else state_.script.pop(); +} + + +void GameLayer::rayTimer() +{ + moof::ray2::contact meh; + std::list hits; + moof::vector2 point; - Mf::Scalar d = mLine.intersectRay(mRay, meh); - if (d > 0.0) + bool bam = mLine.intersect_ray(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); + mRay.solve(point, meh.distance); + moof::log_info << "line: d = " << meh.distance << std::endl; + moof::log_info << " P = " << point << std::endl; + moof::log_info << " n = " << meh.normal << std::endl; } - //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 = mCircle.intersect_ray(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::Intersection> hits; - if (mState.scene->castRay(mRay, hits)) + if (state_.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); + moof::log_info << "scene: d = " << hits.front().distance << std::endl; + moof::log_info << " P = " << point << std::endl; + moof::log_info << " n = " << hits.front().normal << std::endl; } } -void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const +void GameLayer::draw(moof::scalar alpha) const { - mState.camera.uploadToGL(alpha); + if (!state_.scene) return; + state_.camera.upload_to_gl(alpha); // DRAW THE SCENE - Mf::Texture::resetBind(); + moof::texture::reset_binding(); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - mState.scene->drawIfVisible(alpha, mState.camera.getFrustum()); - - mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position)); - mState.heroine->draw(alpha); + state_.scene->draw_if_visible(alpha, state_.camera.frustum()); + state_.heroine->draw(alpha); mRay.draw(); mLine.draw(); - mSphere.draw(); + mCircle.draw(); + + moof::view::draw(alpha); } -bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event) +bool GameLayer::handle_event(const moof::event& event) { + if (moof::view::handle_event(event)) return true; + switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_SPACE) { - mState.heroine->animation.startSequence("Flattened"); - Mf::logInfo("thump!"); - mPunchSound.play(); + state_.heroine->animation.startSequence("Flattened"); + moof::log_info("thump!"); + //mPunchSound.play(); return true; } else if (event.key.keysym.sym == SDLK_m) { - mMusic.toggle(); + music_->toggle(); return true; } else if (event.key.keysym.sym == SDLK_PAGEUP) { - mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(10.0)); + mRay.direction = moof::rotate_vector_2D(mRay.direction, + moof::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 = moof::rotate_vector_2D(mRay.direction, + moof::rad(-10.0)); return true; } - return mState.heroine->handleEvent(event); + else if (event.key.keysym.sym == SDLK_r) + { + loadSceneLoader(); + advanceScene(settings()); + return true; + } + return state_.heroine->handle_event(event); case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) { - engine.pop(this); + parent().remove_child(this); return true; } else if (event.key.keysym.sym == SDLK_h) { - engine.push(Hud::alloc(mState)); + add_child(mHud); return true; } - return mState.heroine->handleEvent(event); + return state_.heroine->handle_event(event); case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: - mState.camera.handleEvent(event); + state_.camera.handle_event(event); return true; case SDL_VIDEORESIZE: - setProjection(event.resize.w, event.resize.h); + projection(event.resize.w, event.resize.h); break; } @@ -278,17 +273,15 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event) } -void GameLayer::setProjection() +void GameLayer::projection() { - Mf::VideoP video = Mf::Engine::getInstance().getVideo(); - setProjection(video->getWidth(), video->getHeight()); + projection(video().width(), video().height()); } -void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height) +void GameLayer::projection(moof::scalar width, moof::scalar height) { - mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0); + state_.camera.projection(moof::rad(60.0), + width / height, + SCALAR(1.0), SCALAR(200.0)); } - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ -