From: Matthias Braun Date: Sun, 22 Jan 2006 15:18:42 +0000 (+0000) Subject: camera can move now X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ca12c25190d1349c69fe26093bd7f85186399b4f;p=supertux.git camera can move now SVN-Revision: 3018 --- diff --git a/data/levels/test/default.nut b/data/levels/test/default.nut index dbafc8eb2..6d917b570 100644 --- a/data/levels/test/default.nut +++ b/data/levels/test/default.nut @@ -2,11 +2,16 @@ function intro() { - DisplayEffect.sixteen_to_nine(); - PENNY.set_action("stand"); - Text.set_centered(true); - Text.set_text(translate("SuperTux\nMilestone 2")); - Text.fade_in(2); - wait(4); - Text.fade_out(1); + Tux.deactivate(); + Tux.set_visible(false); + DisplayEffect.sixteen_to_nine(0); + DisplayEffect.fade_in(2); + Camera.scroll_to(0, 945, 15); + // Sound.play_music("music/tux_intro.ogg"); + wait(2); + + wait(20); + + Level.finish(); } + diff --git a/data/levels/test/intro2.stl b/data/levels/test/intro2.stl index 4c25ffbb1..2ec27e828 100644 --- a/data/levels/test/intro2.stl +++ b/data/levels/test/intro2.stl @@ -5,7 +5,8 @@ (author "No Author") (sector (name "main") - (init-script "") + (init-script "intro()") + (music "tux_intro.ogg") (gravity 10.000000) (tilemap (layer "background") diff --git a/src/game_session.cpp b/src/game_session.cpp index 14a69d649..b0e61b20e 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -393,6 +393,11 @@ GameSession::try_cheats() exit_status = ES_LEVEL_FINISHED; // don't add points to stats though... } + if(main_controller->check_cheatcode("camera")) { + std::cout << "Camera is at " + << Sector::current()->camera->get_translation().x << "," + << Sector::current()->camera->get_translation().y << "\n"; + } } void diff --git a/src/object/camera.cpp b/src/object/camera.cpp index 216a3b2ae..f33215a9f 100644 --- a/src/object/camera.cpp +++ b/src/object/camera.cpp @@ -132,7 +132,7 @@ Camera::reset(const Vector& tuxpos) translation.y = tuxpos.y - SCREEN_HEIGHT/2; shakespeed = 0; shaketimer.stop(); - keep_in_bounds(); + keep_in_bounds(translation); } void @@ -144,20 +144,41 @@ Camera::shake(float time, float x, float y) shakespeed = M_PI/2 / time; } +void +Camera::scroll_to(const Vector& goal, float scrolltime) +{ + scroll_from = translation; + scroll_goal = goal; + keep_in_bounds(scroll_goal); + + scroll_to_pos = 0; + scrollspeed = 1.0 / scrolltime; + mode = SCROLLTO; +} + static const float EPSILON = .00001; static const float max_speed_y = 140; void Camera::update(float elapsed_time) { - if(mode == NORMAL) - scroll_normal(elapsed_time); - else if(mode == AUTOSCROLL) - scroll_autoscroll(elapsed_time); + switch(mode) { + case NORMAL: + update_scroll_normal(elapsed_time); + break; + case AUTOSCROLL: + update_scroll_autoscroll(elapsed_time); + break; + case SCROLLTO: + update_scroll_to(elapsed_time); + break; + default: + break; + } } void -Camera::keep_in_bounds() +Camera::keep_in_bounds(Vector& translation) { float width = sector->solids->get_width() * 32; float height = sector->solids->get_height() * 32; @@ -183,7 +204,7 @@ Camera::shake() } void -Camera::scroll_normal(float elapsed_time) +Camera::update_scroll_normal(float elapsed_time) { assert(sector != 0); Player* player = sector->player; @@ -269,12 +290,12 @@ Camera::scroll_normal(float elapsed_time) // apply scrolling translation.x -= speed_x * elapsed_time; - keep_in_bounds(); + keep_in_bounds(translation); shake(); } void -Camera::scroll_autoscroll(float elapsed_time) +Camera::update_scroll_autoscroll(float elapsed_time) { Player* player = sector->player; @@ -292,7 +313,7 @@ Camera::scroll_autoscroll(float elapsed_time) // construct path for next point if(auto_idx+1 >= scrollpoints.size()) { - keep_in_bounds(); + keep_in_bounds(translation); return; } Vector distance = scrollpoints[auto_idx+1].position @@ -306,7 +327,20 @@ Camera::scroll_autoscroll(float elapsed_time) auto_idx++; } - keep_in_bounds(); + keep_in_bounds(translation); shake(); } +void +Camera::update_scroll_to(float elapsed_time) +{ + scroll_to_pos += elapsed_time * scrollspeed; + if(scroll_to_pos >= 1.0) { + mode = MANUAL; + translation = scroll_goal; + return; + } + + translation = (scroll_goal - scroll_from) * scroll_to_pos; +} + diff --git a/src/object/camera.hpp b/src/object/camera.hpp index 5ce5db470..fa4728a80 100644 --- a/src/object/camera.hpp +++ b/src/object/camera.hpp @@ -66,23 +66,30 @@ public: translation.y = scroll_y; } + /** + * scroll the upper left edge of the camera in scrolltime seconds + * to the position goal + */ + void scroll_to(const Vector& goal, float scrolltime); + enum CameraMode { - NORMAL, AUTOSCROLL, MANUAL + NORMAL, AUTOSCROLL, SCROLLTO, MANUAL }; CameraMode mode; private: - void scroll_normal(float elapsed_time); - void scroll_autoscroll(float elapsed_time); - void keep_in_bounds(); + void update_scroll_normal(float elapsed_time); + void update_scroll_autoscroll(float elapsed_time); + void update_scroll_to(float elapsed_time); + void keep_in_bounds(Vector& vector); void shake(); enum LeftRightScrollChange { NONE, LEFT, RIGHT }; - + Vector translation; Sector* sector; @@ -107,6 +114,12 @@ private: float shakespeed; float shakedepth_x; float shakedepth_y; + + // scrollto mode + Vector scroll_from; + Vector scroll_goal; + float scroll_to_pos; + float scrollspeed; }; #endif /*SUPERTUX_CAMERA_H*/ diff --git a/src/scripting/camera.cpp b/src/scripting/camera.cpp index 6f6173b1b..d96b5a468 100644 --- a/src/scripting/camera.cpp +++ b/src/scripting/camera.cpp @@ -2,13 +2,16 @@ #include #include -#include "camera.hpp" +#include "object/camera.hpp" +#include "scripting/camera.hpp" +#include "math/vector.hpp" #define NOIMPL printf("%s not implemented.\n", __PRETTY_FUNCTION__); namespace Scripting { - Camera::Camera() + Camera::Camera(::Camera* camera) + : camera(camera) { } Camera::~Camera() @@ -30,5 +33,11 @@ namespace Scripting Camera::set_mode(const std::string& ) { NOIMPL; - } + } + + void + Camera::scroll_to(float x, float y, float scrolltime) + { + camera->scroll_to(Vector(x, y), scrolltime); + } } diff --git a/src/scripting/camera.hpp b/src/scripting/camera.hpp index add5e5f06..c94777a08 100644 --- a/src/scripting/camera.hpp +++ b/src/scripting/camera.hpp @@ -1,6 +1,11 @@ #ifndef __CAMERA_H__ #define __CAMERA_H__ +#ifndef SCRIPTING_API +class Camera; +typedef Camera _Camera; +#endif + namespace Scripting { @@ -8,16 +13,22 @@ class Camera { public: #ifndef SCRIPTING_API - Camera(); - ~Camera(); + Camera(_Camera* camera); + ~Camera(); #endif - /** Shake the camera */ - void shake(float speed, float x, float y); - /** Set camera to a specific coordinate */ - void set_pos(float x, float y); - /** Set camera to a specific mode, can be "normal", "manual" */ - void set_mode(const std::string& mode); + /** Shake the camera */ + void shake(float speed, float x, float y); + /** Set camera to a specific coordinate */ + void set_pos(float x, float y); + /** Set camera to a specific mode, can be "normal", "manual" */ + void set_mode(const std::string& mode); + /** Scroll camera to position x,y in scrolltime seconds */ + void scroll_to(float x, float y, float scrolltime); + +#ifndef SCRIPTING_API + _Camera* camera; +#endif }; } diff --git a/src/scripting/script_interpreter.cpp b/src/scripting/script_interpreter.cpp index 1047ed7f7..6c3fce570 100644 --- a/src/scripting/script_interpreter.cpp +++ b/src/scripting/script_interpreter.cpp @@ -40,7 +40,7 @@ static void printfunc(HSQUIRRELVM, const char* str, ...) ScriptInterpreter* ScriptInterpreter::_current = 0; ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory) - : working_directory(new_working_directory), sound(0), level(0) + : working_directory(new_working_directory), sound(0), level(0), camera(0) { v = sq_open(1024); if(v == 0) @@ -101,6 +101,9 @@ ScriptInterpreter::register_sector(Sector* sector) Scripting::DisplayEffect* display_effect_api = static_cast (display_effect); expose_object(display_effect_api, "DisplayEffect"); + + Scripting::Camera* camera = new Scripting::Camera(sector->camera); + expose_object(camera, "Camera"); } ScriptInterpreter::~ScriptInterpreter() @@ -108,6 +111,7 @@ ScriptInterpreter::~ScriptInterpreter() sq_close(v); delete sound; delete level; + delete camera; } static SQInteger squirrel_read_char(SQUserPointer file) diff --git a/src/scripting/script_interpreter.hpp b/src/scripting/script_interpreter.hpp index 6e233e9fe..de86f876d 100644 --- a/src/scripting/script_interpreter.hpp +++ b/src/scripting/script_interpreter.hpp @@ -12,6 +12,7 @@ #include "scripting/sound.hpp" #include "scripting/level.hpp" #include "scripting/squirrel_error.hpp" +#include "scripting/camera.hpp" class Sector; @@ -76,6 +77,7 @@ private: std::string working_directory; Scripting::Sound* sound; Scripting::Level* level; + Scripting::Camera* camera; }; #endif diff --git a/src/scripting/sound.cpp b/src/scripting/sound.cpp index 6b2ac531f..37ee32e51 100644 --- a/src/scripting/sound.cpp +++ b/src/scripting/sound.cpp @@ -16,11 +16,8 @@ namespace Scripting {} void - Sound::play_music(const std::string& name) + Sound::play_music(const std::string& filename) { - std::string filename = "music/"; - filename += name; - filename += ".ogg"; sound_manager->play_music(filename); } diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index ab1a764e5..2dd0c8f3c 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -145,6 +145,22 @@ static int Camera_set_mode_wrapper(HSQUIRRELVM v) return 0; } +static int Camera_scroll_to_wrapper(HSQUIRRELVM v) +{ + Scripting::Camera* _this; + sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0); + float arg0; + sq_getfloat(v, 2, &arg0); + float arg1; + sq_getfloat(v, 3, &arg1); + float arg2; + sq_getfloat(v, 4, &arg2); + + _this->scroll_to(arg0, arg1, arg2); + + return 0; +} + static int Level_release_hook(SQUserPointer ptr, int ) { Scripting::Level* _this = reinterpret_cast (ptr); @@ -931,6 +947,14 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, msg.str()); } + sq_pushstring(v, "scroll_to", -1); + sq_newclosure(v, &Camera_scroll_to_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + std::ostringstream msg; + msg << "Couldn't register function'scroll_to'"; + throw SquirrelError(v, msg.str()); + } + if(SQ_FAILED(sq_createslot(v, -3))) { std::ostringstream msg; msg << "Couldn't register class'Camera'";