diff options
-rw-r--r-- | src/crepe/api/Script.cpp | 3 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 10 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 5 | ||||
-rw-r--r-- | src/example/game.cpp | 2 | ||||
-rw-r--r-- | src/test/CollisionTest.cpp | 1 | ||||
-rw-r--r-- | src/test/ScriptTest.cpp | 22 | ||||
-rw-r--r-- | src/test/ScriptTest.h | 7 |
7 files changed, 40 insertions, 10 deletions
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 583c04f..85016f5 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -26,6 +26,8 @@ void Script::set_next_scene(const string & name) { SaveManager & Script::get_save_manager() const { return this->mediator->save_manager; } +LoopTimerManager & Script::get_loop_timer() const { return this->mediator->loop_timer; } + const keyboard_state_t & Script::get_keyboard_state() const { SDLContext & sdl_context = this->mediator->sdl_context; return sdl_context.get_keyboard_state(); @@ -38,3 +40,4 @@ bool Script::get_key_state(Keycode key) const noexcept { return false; } } + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 65306cd..a87af4e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -4,6 +4,7 @@ #include "../api/KeyCodes.h" #include "../manager/EventManager.h" +#include "../manager/LoopTimerManager.h" #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" #include "../types.h" @@ -47,10 +48,12 @@ protected: /** * \brief Script update function (empty by default) * + * \param delta_time Time since last fixed update + * * This function is called during the ScriptSystem::update() routine if the \c BehaviorScript * component holding this script instance is active. */ - virtual void update() {} + virtual void update(duration_t delta_time) {} //! \} //! ScriptSystem calls \c init() and \c update() @@ -135,6 +138,10 @@ protected: //! Retrieve SaveManager reference SaveManager & get_save_manager() const; + + //! Retrieve LoopTimerManager reference + LoopTimerManager & get_loop_timer() const; + /** * \brief Utility function to retrieve the keyboard state * \see SDLContext::get_keyboard_state @@ -151,7 +158,6 @@ protected: * */ bool get_key_state(Keycode key) const noexcept; - //! \} private: /** diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..4afd2ba 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -11,6 +11,7 @@ void ScriptSystem::update() { dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; + LoopTimerManager & timer = this->mediator.loop_timer; RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); for (BehaviorScript & behavior_script : behavior_scripts) { @@ -23,6 +24,8 @@ void ScriptSystem::update() { script->init(); script->initialized = true; } - script->update(); + + duration_t delta_time = timer.get_delta_time(); + script->update(delta_time); } } diff --git a/src/example/game.cpp b/src/example/game.cpp index 22effd2..3975650 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -86,7 +86,7 @@ class MyScript1 : public Script { subscribe<KeyPressEvent>( [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } - void update() { + void update(duration_t) { Rigidbody & tf = this->get_component<Rigidbody>(); Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index ff9e7cc..baa95c1 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -54,6 +54,7 @@ public: ComponentManager mgr{m}; CollisionSystem collision_sys{m}; ScriptSystem script_sys{m}; + LoopTimerManager loop_timer{m}; GameObject world = mgr.new_object("world", "", {50, 50}); GameObject game_object1 = mgr.new_object("object1", "", {50, 50}); diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index acdae70..846e398 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -28,7 +28,7 @@ void ScriptTest::SetUp() { TEST_F(ScriptTest, Default) { MyScript & script = this->script; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(0); + EXPECT_CALL(script, update(_)).Times(0); } TEST_F(ScriptTest, UpdateOnce) { @@ -38,7 +38,7 @@ TEST_F(ScriptTest, UpdateOnce) { InSequence seq; EXPECT_CALL(script, init()).Times(1); - EXPECT_CALL(script, update()).Times(1); + EXPECT_CALL(script, update(_)).Times(1); system.update(); } @@ -46,7 +46,7 @@ TEST_F(ScriptTest, UpdateOnce) { InSequence seq; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(1); + EXPECT_CALL(script, update(_)).Times(1); system.update(); } } @@ -59,7 +59,7 @@ TEST_F(ScriptTest, UpdateInactive) { InSequence seq; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(0); + EXPECT_CALL(script, update(_)).Times(0); behaviorscript.active = false; system.update(); } @@ -68,8 +68,20 @@ TEST_F(ScriptTest, UpdateInactive) { InSequence seq; EXPECT_CALL(script, init()).Times(1); - EXPECT_CALL(script, update()).Times(1); + EXPECT_CALL(script, update(_)).Times(1); behaviorscript.active = true; system.update(); } } + +TEST_F(ScriptTest, SaveManager) { + MyScript & script = this->script; + + EXPECT_EQ(&script.get_save_manager(), &this->save_manager); +} + +TEST_F(ScriptTest, LoopTimerManager) { + MyScript & script = this->script; + + EXPECT_EQ(&script.get_loop_timer(), &this->loop_timer); +} diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 31fa7c9..f3dbda4 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -7,7 +7,10 @@ #include <crepe/api/Script.h> #include <crepe/manager/ComponentManager.h> #include <crepe/manager/EventManager.h> +#include <crepe/manager/LoopTimerManager.h> +#include <crepe/manager/SaveManager.h> #include <crepe/system/ScriptSystem.h> + class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; @@ -17,6 +20,8 @@ public: crepe::ComponentManager component_manager{mediator}; crepe::ScriptSystem system{mediator}; crepe::EventManager event_mgr{mediator}; + crepe::LoopTimerManager loop_timer{mediator}; + crepe::SaveManager save_manager{mediator}; crepe::GameObject entity = component_manager.new_object(OBJ_NAME); class MyScript : public crepe::Script { @@ -24,7 +29,7 @@ public: public: MOCK_METHOD(void, init, (), (override)); - MOCK_METHOD(void, update, (), (override)); + MOCK_METHOD(void, update, (crepe::duration_t), (override)); }; crepe::OptionalRef<crepe::BehaviorScript> behaviorscript; |