X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FGameLayer.cc;h=c0d35b95dadb692b085ee1d9d08bb76765800c25;hp=dfdadaa5f2c19fbe809193f9eebcc971569adfd6;hb=1fb5f7e36af1a4de040bc2989133703b0e0d4a9f;hpb=892da43bf5796e7c5f593a6d0f53bd797a36bd3e diff --git a/src/GameLayer.cc b/src/GameLayer.cc index dfdadaa..c0d35b9 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -1,181 +1,269 @@ -/******************************************************************************* - - 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 +/*] 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" -#if HAVE_CONFIG_H -#include "config.h" -#endif +void GameLayer::loadSceneLoader() +{ + state_.script.import_standard_libraries(); + moof::log::import(state_.script); + + std::string path = moof::resource::find_file("scenes/loader.lua"); + if (path.empty()) + { + throw std::runtime_error("cannot find scene loader script"); + } + + moof::script::status status = state_.script.do_file(path); + if (status != moof::script::success) + { + std::string str; + state_.script[-1].get(str); + throw std::runtime_error("script error: " + str); + } -GameLayer::GameLayer() : - music("NightFusionIntro"), - punchSound("Thump") + state_.script.globals().push_field("scenes"); + state_.script.top().get(state_.sceneList); + if (state_.sceneList.size() == 0) + { + throw std::runtime_error("no variable `scenes' in script loader."); + } +} + +void GameLayer::advanceScene(moof::settings& settings) { - music.setLooping(true); - music.enqueue("NightFusionLoop"); - music.stream(); + if (state_.sceneList.size() != 0) + { + state_.scene = Scene::alloc(state_.sceneList[0]); + state_.sceneList.erase(state_.sceneList.begin()); + + moof::script::status status = state_.scene->load(settings, + state_.script); + if (status != moof::script::success) + { + std::string str; + state_.script[-1].get(str); + throw std::runtime_error("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(); + } +} + - heroine = Character::alloc("RobotTrooper"); - heroine->getAnimation().startSequence("Run"); +GameLayer::GameLayer() +{ + moof::log_info("about to load sound resource..."); + music_.sample("sounds/NightFusionIntro.ogg"); + music_.loop(true); + music_.enqueue("sounds/NightFusionLoop.ogg"); + music_.position(moof::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); + mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this), + 0.1, moof::timer::repeat); - Mf::Scalar b[2] = {1.0, 0.0}; - fadeIn.init(b, 1.0); + state_.heroine = Heroine::alloc(); + state_.heroine->animation.startSequence("FlyDiagonallyUp"); - octree = Mf::loadScene("Classic"); - heroine->treeNode = octree->insert(heroine); + state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate); - camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0); - camera.uploadProjectionToGL(); + sceneMesh = moof::resource::load("classic.ac"); } -void GameLayer::pushed(Mf::Engine& engine) +void GameLayer::did_add_to_view() { - hud = Hud::alloc(); - engine.pushLayer(hud); + bool isMute = false; + settings().get("nomusic", isMute); + music_.stream(); + + loadSceneLoader(); + advanceScene(settings()); // load the first scene + + mHud = Hud::alloc(state_); + add_child(mHud); + + mRay.direction.set(1.0, 0.0); + + mLine.a.set(20, 10); + mLine.b.set(19, 14); + + mCircle.point.set(22, 5); + mCircle.radius = 2; + + mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), + 1.0, moof::timer::repeat); + + projection(); } -void GameLayer::update(Mf::Scalar t, Mf::Scalar dt) +void GameLayer::update(moof::scalar t, moof::scalar dt) +{ + if (!state_.scene) return; + state_.camera.update(t, dt); + state_.heroine->update(t, dt); + + state_.scene->checkForCollision(*state_.heroine); + + moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8); + state_.camera.position(cam); + + mRay.point = state_.heroine->state().position; + + moof::view::update(t, dt); +} + +void GameLayer::thinkTimer() { - //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)); + state_.script.registry().push_field("Think"); + if (state_.script[-1].is_function()) state_.script.call(); + else state_.script.pop(); } -void GameLayer::draw(Mf::Scalar alpha) const +void GameLayer::rayTimer() { - glMatrixMode(GL_MODELVIEW); - glLoadMatrix(camera.getModelviewMatrix().data()); + moof::ray2::contact meh; + std::list hits; + moof::vector2 point; + + bool bam = mLine.intersect_ray(mRay, meh); + if (bam) + { + //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; + } + + bam = mCircle.intersect_ray(mRay, meh); + if (bam) + { + meh.normal.normalize(); + hits.push_back(meh); + } + + if (state_.scene->castRay(mRay, hits)) + { + 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(moof::scalar alpha) const +{ + if (!state_.scene) return; + state_.camera.upload_to_gl(alpha); // DRAW THE SCENE - Mf::Texture::resetBind(); + moof::image::reset_binding(); 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(); - - glDisable(GL_BLEND); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); + //state_.scene->draw_if_visible(alpha, state_.camera.frustum()); + sceneMesh->draw(alpha); + state_.heroine->draw(alpha); + + mRay.draw(); + mLine.draw(); + mCircle.draw(); + + moof::view::draw(alpha); } -bool GameLayer::handleEvent(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) { - heroine->getAnimation().startSequence("Punch"); - Mf::logInfo("thump!"); - punchSound.play(); + state_.heroine->animation.startSequence("Flattened"); + moof::log_info("thump!"); + punch_sound_.play(); + return true; + } + else if (event.key.keysym.sym == SDLK_m) + { + music_.toggle(); + return true; + } + else if (event.key.keysym.sym == SDLK_PAGEUP) + { + mRay.direction = moof::rotate_vector_2D(mRay.direction, + moof::rad(10.0)); return true; } - else if (event.key.keysym.sym == SDLK_p) + else if (event.key.keysym.sym == SDLK_PAGEDOWN) { - music.toggle(); + mRay.direction = moof::rotate_vector_2D(mRay.direction, + moof::rad(-10.0)); return true; } - else if (event.key.keysym.sym == SDLK_y) + else if (event.key.keysym.sym == SDLK_r) { - Mf::Engine::getInstance().popLayer(); + loadSceneLoader(); + advanceScene(settings()); return true; } + return state_.heroine->handle_event(event); case SDL_KEYUP: - heroine->handleEvent(event); - break; + if (event.key.keysym.sym == SDLK_ESCAPE) + { + parent().remove_child(this); + return true; + } + else if (event.key.keysym.sym == SDLK_h) + { + add_child(mHud); + return true; + } + return state_.heroine->handle_event(event); case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: - camera.handleEvent(event); + state_.camera.handle_event(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(); + projection(event.resize.w, event.resize.h); break; } @@ -183,5 +271,15 @@ bool GameLayer::handleEvent(const Mf::Event& event) } -/** vim: set ts=4 sw=4 tw=80: *************************************************/ +void GameLayer::projection() +{ + projection(video().width(), video().height()); +} + +void GameLayer::projection(moof::scalar width, moof::scalar height) +{ + state_.camera.projection(moof::rad(60.0), + width / height, + SCALAR(1.0), SCALAR(200.0)); +}