aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-14 11:40:33 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-14 11:40:33 +0100
commit061b2b8ab9aba67c1467cdd163fb7a04de95f4f1 (patch)
treee1960a83461e0d83a7027a88404af1a7c70f6dcd /src/example
parentfd2ebb54d0c2b269c15fc84a4ac77993efc917d7 (diff)
parentb9fc66f6922b1f40f2dbe14e8dfc4caa469654bc (diff)
Merge branch 'master' of github.com:lonkaars/crepe into jaro/collision-system
Diffstat (limited to 'src/example')
-rw-r--r--src/example/AITest.cpp90
-rw-r--r--src/example/CMakeLists.txt3
-rw-r--r--src/example/asset_manager.cpp36
-rw-r--r--src/example/game.cpp53
-rw-r--r--src/example/rendering_particle.cpp100
-rw-r--r--src/example/savemgr.cpp44
6 files changed, 181 insertions, 145 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp
new file mode 100644
index 0000000..93ba500
--- /dev/null
+++ b/src/example/AITest.cpp
@@ -0,0 +1,90 @@
+#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/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);
+
+ Asset img{"asset/texture/test_ap43.png"};
+
+ Sprite & test_sprite = game_object1.add_component<Sprite>(
+ img, Sprite::Data{
+ .color = Color::MAGENTA,
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 2,
+ .order_in_layer = 2,
+ .size = {0, 100},
+ .angle_offset = 0,
+ .position_offset = {0, 0},
+ });
+
+ 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,
+ });
+ 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/game.cpp b/src/example/game.cpp
index dd5b968..8ea50ea 100644
--- a/src/example/game.cpp
+++ b/src/example/game.cpp
@@ -2,6 +2,7 @@
#include "api/Scene.h"
#include "manager/ComponentManager.h"
#include "manager/Mediator.h"
+#include "types.h"
#include <crepe/api/BoxCollider.h>
#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
@@ -11,7 +12,6 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
#include <crepe/api/Sprite.h>
-#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
@@ -66,6 +66,11 @@ class MyScript1 : public Script {
//add collider switch
break;
}
+ case Keycode::Q: {
+ Rigidbody & rg = this->get_component<Rigidbody>();
+ rg.data.angular_velocity = 1;
+ break;
+ }
default:
break;
}
@@ -158,15 +163,13 @@ public:
void load_scene() {
- Mediator & m = this->mediator;
- ComponentManager & mgr = m.component_manager;
Color color(0, 0, 0, 255);
float screen_size_width = 320;
float screen_size_height = 240;
float world_collider = 1000;
//define playable world
- GameObject world = mgr.new_object(
+ GameObject world = new_object(
"Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
world.add_component<Rigidbody>(Rigidbody::Data{
.mass = 0,
@@ -187,15 +190,18 @@ public:
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);
+ vec2{screen_size_width, screen_size_height},
+ Camera::Data{
+ .bg_color = Color::WHITE,
+ .zoom = 1,
+ });
- GameObject game_object1 = mgr.new_object(
+ GameObject game_object1 = new_object(
"Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
game_object1.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
- .gravity_scale = 0,
+ .gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
.linear_velocity = {0, 1},
.constraints = {0, 0, 0},
@@ -206,19 +212,24 @@ public:
// add box with boxcollider
game_object1.add_component<BoxCollider>(vec2{0, 0}, vec2{20, 20});
game_object1.add_component<BehaviorScript>().set_script<MyScript1>();
- auto img1 = Texture("asset/texture/square.png");
- game_object1.add_component<Sprite>(img1, color, Sprite::FlipSettings{false, false}, 1,
- 1, 20);
+
+ Asset img1{"asset/texture/square.png"};
+ game_object1.add_component<Sprite>(img1, Sprite::Data{
+ .size = {20, 20},
+ });
//add circle with cirlcecollider deactiveated
game_object1.add_component<CircleCollider>(vec2{0, 0}, 10).active = false;
- auto img2 = Texture("asset/texture/circle.png");
+ Asset img2{"asset/texture/circle.png"};
game_object1
- .add_component<Sprite>(img2, color, Sprite::FlipSettings{false, false}, 1, 1, 20)
+ .add_component<Sprite>(img2,
+ Sprite::Data{
+ .size = {20, 20},
+ })
.active
= false;
- GameObject game_object2 = mgr.new_object(
+ GameObject game_object2 = new_object(
"Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
game_object2.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
@@ -233,15 +244,19 @@ public:
// add box with boxcollider
game_object2.add_component<BoxCollider>(vec2{0, 0}, vec2{20, 20});
game_object2.add_component<BehaviorScript>().set_script<MyScript2>();
- auto img3 = Texture("asset/texture/square.png");
- game_object2.add_component<Sprite>(img3, color, Sprite::FlipSettings{false, false}, 1,
- 1, 20);
+
+ game_object2.add_component<Sprite>(img1, Sprite::Data{
+ .size = {20, 20},
+ });
//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)
+ .add_component<Sprite>(img2,
+ Sprite::Data{
+ .size = {20, 20},
+ })
.active
= false;
}
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index 349d11e..13e625f 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -1,43 +1,24 @@
-#include "api/Animator.h"
-#include "api/Camera.h"
-#include "system/AnimatorSystem.h"
-#include "system/ParticleSystem.h"
-#include <SDL2/SDL_timer.h>
-#include <crepe/ComponentManager.h>
-
+#include "api/Asset.h"
#include <crepe/Component.h>
+#include <crepe/api/Animator.h>
+#include <crepe/api/Button.h>
+#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
#include <crepe/api/GameObject.h>
+#include <crepe/api/LoopManager.hpp>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
-#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
-#include <crepe/system/RenderSystem.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/Mediator.h>
#include <crepe/types.h>
-
-#include <chrono>
+#include <iostream>
using namespace crepe;
using namespace std;
-int main(int argc, char * argv[]) {
- ComponentManager mgr;
- GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1);
- RenderSystem sys{mgr};
- ParticleSystem psys{mgr};
- AnimatorSystem asys{mgr};
-
- Color color(255, 255, 255, 100);
-
- auto img = Texture("asset/texture/test_ap43.png");
- Sprite & test_sprite = game_object.add_component<Sprite>(
- img, color, Sprite::FlipSettings{true, true}, 1, 1, 500);
-
- //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true;
- game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true;
-
- /*
+/*
auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
.position = {0, 0},
.max_particles = 10,
@@ -59,25 +40,56 @@ int main(int argc, char * argv[]) {
});
*/
- auto & cam = game_object.add_component<Camera>(Color::RED, ivec2{1080, 720},
- vec2{2000, 2000}, 1.0f);
+class TestScene : public Scene {
+public:
+ void load_scene() {
- /*
- game_object
- .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
- .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
- FlipSettings{false, false})
- .order_in_layer
- = 6;
- */
+ cout << "TestScene" << endl;
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1);
- auto start = std::chrono::steady_clock::now();
- while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
- psys.update();
- asys.update();
- sys.update();
- SDL_Delay(10);
+ Color color(255, 255, 255, 255);
+
+ Asset img{"asset/spritesheet/spritesheet_test.png"};
+
+ Sprite & test_sprite = game_object.add_component<Sprite>(
+ img, Sprite::Data{
+ .color = color,
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 2,
+ .order_in_layer = 2,
+ .size = {0, 100},
+ .angle_offset = 0,
+ .position_offset = {0, 0},
+ });
+
+ //auto & anim = game_object.add_component<Animator>(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{});
+ //anim.set_anim(0);
+
+ auto & cam = game_object.add_component<Camera>(ivec2{720, 1280}, vec2{400, 400},
+ Camera::Data{
+ .bg_color = Color::WHITE,
+ });
+
+ function<void()> on_click = [&]() { cout << "button clicked" << std::endl; };
+ function<void()> on_enter = [&]() { cout << "enter" << std::endl; };
+ function<void()> on_exit = [&]() { cout << "exit" << std::endl; };
+
+ auto & button
+ = game_object.add_component<Button>(vec2{200, 200}, vec2{0, 0}, on_click, false);
+ button.on_mouse_enter = on_enter;
+ button.on_mouse_exit = on_exit;
+ button.is_toggle = true;
+ button.active = true;
}
+ string get_name() const { return "TestScene"; };
+};
+
+int main(int argc, char * argv[]) {
+ LoopManager engine;
+ engine.add_scene<TestScene>();
+ engine.start();
return 0;
}
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;
-}