diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/AITest.cpp | 86 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/asset_manager.cpp | 36 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 44 |
4 files changed, 87 insertions, 82 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp new file mode 100644 index 0000000..f4efc9f --- /dev/null +++ b/src/example/AITest.cpp @@ -0,0 +1,86 @@ +#include <crepe/api/AI.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Texture.h> +#include <crepe/manager/Mediator.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +class Script1 : public Script { + bool shutdown(const ShutDownEvent & event) { + // Very dirty way of shutting down the game + throw "ShutDownEvent"; + return true; + } + + bool mousemove(const MouseMoveEvent & event) { + /*RefVector<AI> aivec = this->get_components<AI>(); + AI & ai = aivec.front().get(); + ai.flee_target + = vec2{static_cast<float>(event.mouse_x), static_cast<float>(event.mouse_y)};*/ + return true; + } + + void init() { + subscribe<ShutDownEvent>( + [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); + subscribe<MouseMoveEvent>( + [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); + } +}; + +class Scene1 : public Scene { +public: + void load_scene() override { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + + Texture img = Texture("asset/texture/test_ap43.png"); + game_object1.add_component<Sprite>(img, Sprite::Data{ + .color = Color::MAGENTA, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 1, + .order_in_layer = 1, + .size = {0, 195}, + }); + AI & ai = game_object1.add_component<AI>(3000); + // ai.arrive_on(); + // ai.flee_on(); + ai.path_follow_on(); + ai.make_oval_path(500, 1000, {0, -1000}, 1.5708, true); + ai.make_oval_path(1000, 500, {0, 500}, 4.7124, false); + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 0.1f, + .max_linear_velocity = {40, 40}, + }); + game_object1.add_component<BehaviorScript>().set_script<Script1>(); + + game_object2.add_component<Camera>(ivec2{1080, 720}, vec2{5000, 5000}, + Camera::Data{ + .bg_color = Color::WHITE, + .zoom = 1, + }); + } + + string get_name() const override { return "Scene1"; } +}; + +int main() { + LoopManager engine; + engine.add_scene<Scene1>(); + engine.start(); + + return 0; +} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8ef71bb..187ed46 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,8 +16,7 @@ function(add_example target_name) add_dependencies(examples ${target_name}) endfunction() -add_example(asset_manager) -add_example(savemgr) add_example(rendering_particle) add_example(game) add_example(button) +add_example(AITest) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp deleted file mode 100644 index 917b547..0000000 --- a/src/example/asset_manager.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include <crepe/api/AssetManager.h> -#include <crepe/api/Texture.h> -#include <crepe/facade/Sound.h> - -using namespace crepe; - -int main() { - - // this needs to be called before the asset manager otherwise the destructor of sdl is not in - // the right order - { Texture test("../asset/texture/img.png"); } - // FIXME: make it so the issue described by the above comment is not possible (i.e. the order - // in which internal classes are instantiated should not impact the way the engine works). - - auto & mgr = AssetManager::get_instance(); - - { - // TODO: [design] the Sound class can't be directly included by the user as it includes - // SoLoud headers. - auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache<Texture>("../asset/texture/img.png"); - auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); - } - - { - auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache<Texture>("../asset/texture/img.png"); - auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); - } -} diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp deleted file mode 100644 index 65c4a34..0000000 --- a/src/example/savemgr.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/** \file - * - * Standalone example for usage of the save manager - */ - -#include <cassert> -#include <crepe/api/Config.h> -#include <crepe/api/SaveManager.h> -#include <crepe/util/Log.h> -#include <crepe/util/Proxy.h> - -using namespace crepe; - -// unrelated setup code -int _ = []() { - // make sure all log messages get printed - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - const char * key = "mygame.test"; - - SaveManager & mgr = SaveManager::get_instance(); - - dbg_logf("has key = {}", mgr.has(key)); - ValueBroker<int> prop = mgr.get<int>(key, 0); - Proxy<int> val = mgr.get<int>(key, 0); - - dbg_logf("val = {}", mgr.get<int>(key).get()); - prop.set(1); - dbg_logf("val = {}", mgr.get<int>(key).get()); - val = 2; - dbg_logf("val = {}", mgr.get<int>(key).get()); - mgr.set<int>(key, 3); - dbg_logf("val = {}", mgr.get<int>(key).get()); - - dbg_logf("has key = {}", mgr.has(key)); - assert(true == mgr.has(key)); - - return 0; -} |