+++ /dev/null
-
-/*] 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 <stdexcept>
-
-#include <moof/log.hh>
-#include <moof/math.hh>
-#include <moof/mesh.hh>
-#include <moof/opengl.hh>
-#include <moof/settings.hh>
-#include <moof/video.hh>
-
-#include "GameLayer.hh"
-
-
-void GameLayer::loadSceneLoader()
-{
- state_.script.import_safe_standard_libraries();
- moof::log::import(state_.script);
- moof::mesh::import(state_.script, "yoink");
- moof::sound::import(state_.script, "yoink");
-
- 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);
- }
-
- 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)
-{
- 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();
-
- table = state_.script.globals().push_field("Event");
- if (table.is_table())
- {
- table.push_field("Draw");
- state_.script.registry().set_field("Draw");
- }
- state_.script.pop();
- }
-}
-
-
-GameLayer::GameLayer()
-{
- music_.queue("NightFusionIntro");
- music_.loop(true);
- music_.queue("NightFusionLoop");
- music_.position(moof::vector3(10.0, 5.0, 0.0));
-
- punch_sound_.sample("Thump");
-
- state_.heroine = Heroine::alloc();
- state_.heroine->animation.startSequence("FlyDiagonallyUp");
-
- state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate);
-}
-
-
-void GameLayer::did_add_to_view()
-{
- bool isMute = false;
- settings().get("nomusic", isMute);
- music_.play();
-
- 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(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;
-
- state_.script.registry().push_field("Think");
- if (state_.script[-1].is_function()) state_.script.call();
- else state_.script.pop();
-
- moof::view::update(t, dt);
-}
-
-void GameLayer::rayTimer()
-{
- moof::ray2::contact meh;
- std::list<moof::ray2::contact> 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);
-
- float pos[] = {state_.heroine->state().position[0],
- state_.heroine->state().position[1], 0.0f, 1.0f};
- glLightfv(GL_LIGHT0, GL_POSITION, pos);
-
- // DRAW THE SCENE
- moof::image::reset_binding();
-
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-
- //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
-
- size_t meh = 23;
- state_.script.push(meh);
- state_.script.pop();
-
- state_.script.registry().push_field("Draw");
- if (state_.script[-1].is_function())
- {
- moof::script::status result = state_.script.call();
- if (result != moof::script::success)
- {
- std::string str;
- state_.script[-1].get(str);
- throw std::runtime_error("script error: " + str);
- }
- }
- else state_.script.pop();
-
- state_.heroine->draw(alpha);
-
- mRay.draw();
- mLine.draw();
- mCircle.draw();
-
- moof::view::draw(alpha);
-}
-
-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)
- {
- 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_PAGEDOWN)
- {
- mRay.direction = moof::rotate_vector_2D(mRay.direction,
- moof::rad(-10.0));
- return true;
- }
- 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)
- {
- 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:
- state_.camera.handle_event(event);
- return true;
-
- case SDL_VIDEORESIZE:
- projection(event.resize.w, event.resize.h);
- break;
- }
-
- return false;
-}
-
-
-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));
-}
-