diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/AITest.cpp | 63 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/game.cpp | 98 |
3 files changed, 111 insertions, 51 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp new file mode 100644 index 0000000..2b6a4d6 --- /dev/null +++ b/src/example/AITest.cpp @@ -0,0 +1,63 @@ +#include <SDL2/SDL_timer.h> +#include <chrono> +#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> + +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; + } + + void init() { + subscribe<ShutDownEvent>( + [this](const ShutDownEvent & ev) -> bool { return this->shutdown(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{250, 250}, 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, Color::MAGENTA, + Sprite::FlipSettings{false, false}, 1, 1, 195); + game_object1.add_component<AI>(200).seek_on(); + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1.0f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 + }); + + game_object2.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, + 1.0f); + game_object2.add_component<BehaviorScript>().set_script<Script1>(); + } + + 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..149c412 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -21,3 +21,4 @@ add_example(savemgr) add_example(rendering_particle) add_example(game) add_example(button) +add_example(AITest) diff --git a/src/example/game.cpp b/src/example/game.cpp index be756bd..2b4e46f 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,6 +1,6 @@ #include "api/CircleCollider.h" -#include "manager/ComponentManager.h" #include "api/Scene.h" +#include "manager/ComponentManager.h" #include "manager/Mediator.h" #include <crepe/api/BoxCollider.h> #include <crepe/api/Camera.h> @@ -28,66 +28,64 @@ class MyScript1 : public Script { bool keypressed(const KeyPressEvent & test) { Log::logf("Box script keypressed()"); switch (test.key) { - case Keycode::A: - { + case Keycode::A: { Transform & tf = this->get_component<Transform>(); tf.position.x -= 1; break; } - case Keycode::W: - { + case Keycode::W: { Transform & tf = this->get_component<Transform>(); tf.position.y -= 1; break; } - case Keycode::S: - { + case Keycode::S: { Transform & tf = this->get_component<Transform>(); tf.position.y += 1; break; } - case Keycode::D: - { + case Keycode::D: { Transform & tf = this->get_component<Transform>(); tf.position.x += 1; break; } - case Keycode::E: - { - if(flip){ + case Keycode::E: { + if (flip) { flip = false; this->get_component<BoxCollider>().active = true; this->get_components<Sprite>()[0].get().active = true; this->get_component<CircleCollider>().active = false; this->get_components<Sprite>()[1].get().active = false; - } - else { + } else { flip = true; this->get_component<BoxCollider>().active = false; this->get_components<Sprite>()[0].get().active = false; this->get_component<CircleCollider>().active = true; this->get_components<Sprite>()[1].get().active = true; } - - + //add collider switch break; } + case Keycode::Q: { + throw "Test"; + break; + } default: - break; + break; } return false; - } + } void init() { Log::logf("init"); - subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + subscribe<CollisionEvent>( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe<KeyPressEvent>( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } void update() { // Retrieve component from the same GameObject this script is on } - }; class MyScript2 : public Script { @@ -99,74 +97,68 @@ class MyScript2 : public Script { bool keypressed(const KeyPressEvent & test) { Log::logf("Box script keypressed()"); switch (test.key) { - case Keycode::LEFT: - { + case Keycode::LEFT: { Transform & tf = this->get_component<Transform>(); tf.position.x -= 1; break; } - case Keycode::UP: - { + case Keycode::UP: { Transform & tf = this->get_component<Transform>(); tf.position.y -= 1; break; } - case Keycode::DOWN: - { + case Keycode::DOWN: { Transform & tf = this->get_component<Transform>(); tf.position.y += 1; break; } - case Keycode::RIGHT: - { + case Keycode::RIGHT: { Transform & tf = this->get_component<Transform>(); tf.position.x += 1; break; } - case Keycode::PAUSE: - { - if(flip){ + case Keycode::PAUSE: { + if (flip) { flip = false; this->get_component<BoxCollider>().active = true; this->get_components<Sprite>()[0].get().active = true; this->get_component<CircleCollider>().active = false; this->get_components<Sprite>()[1].get().active = false; - } - else { + } else { flip = true; this->get_component<BoxCollider>().active = false; this->get_components<Sprite>()[0].get().active = false; this->get_component<CircleCollider>().active = true; this->get_components<Sprite>()[1].get().active = true; } - - + //add collider switch break; } default: - break; + break; } return false; - } + } void init() { Log::logf("init"); - subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + subscribe<CollisionEvent>( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe<KeyPressEvent>( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } void update() { // Retrieve component from the same GameObject this script is on } - }; class ConcreteScene1 : public Scene { public: using Scene::Scene; - + void load_scene() { - + Mediator & m = this->mediator; ComponentManager & mgr = m.component_manager; Color color(0, 0, 0, 255); @@ -195,7 +187,10 @@ public: vec2{world_collider, world_collider}); // Left world.add_component<BoxCollider>(vec2{screen_size_width / 2 + world_collider / 2, 0}, vec2{world_collider, world_collider}); // right - world.add_component<Camera>(Color::WHITE, ivec2{static_cast<int>(screen_size_width), static_cast<int>(screen_size_height)}, vec2{screen_size_width, screen_size_height}, 1.0f); + world.add_component<Camera>( + Color::WHITE, + ivec2{static_cast<int>(screen_size_width), static_cast<int>(screen_size_height)}, + vec2{screen_size_width, screen_size_height}, 1.0f); GameObject game_object1 = mgr.new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); @@ -219,10 +214,10 @@ public: //add circle with cirlcecollider deactiveated game_object1.add_component<CircleCollider>(vec2{0, 0}, 10).active = false; auto img2 = Texture("asset/texture/circle.png"); - game_object1.add_component<Sprite>(img2, color, Sprite::FlipSettings{false, false}, 1, - 1, 20).active = false; - - + game_object1 + .add_component<Sprite>(img2, color, Sprite::FlipSettings{false, false}, 1, 1, 20) + .active + = false; GameObject game_object2 = mgr.new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); @@ -246,9 +241,10 @@ public: //add circle with cirlcecollider deactiveated game_object2.add_component<CircleCollider>(vec2{0, 0}, 10).active = false; auto img4 = Texture("asset/texture/circle.png"); - game_object2.add_component<Sprite>(img4, color, Sprite::FlipSettings{false, false}, 1, - 1, 20).active = false; - + game_object2 + .add_component<Sprite>(img4, color, Sprite::FlipSettings{false, false}, 1, 1, 20) + .active + = false; } string get_name() const { return "scene1"; } |