From f6111b6df65047557239c827100454c188979c43 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 4 Dec 2024 11:45:50 +0100 Subject: Setup test for AI --- src/example/AITest.cpp | 32 ++++++++++++++++++++++++++++++++ src/example/CMakeLists.txt | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/example/AITest.cpp (limited to 'src/example') diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp new file mode 100644 index 0000000..ccf5a85 --- /dev/null +++ b/src/example/AITest.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace crepe; + +int main() { + ComponentManager mgr; + 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(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); + + game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); + + RenderSystem sys{mgr}; + + auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + sys.update(); + SDL_Delay(10); + } + + return 0; +} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 560e2bc..ef770ae 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -20,4 +20,4 @@ add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) add_example(gameloop) - +add_example(AITest) -- cgit v1.2.3 From 16444f19ae2c7c71a2be53ce6fd2e4d671aa8765 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 4 Dec 2024 12:15:05 +0100 Subject: Modified test and setup of AISystem --- src/crepe/api/LoopManager.cpp | 14 ++++++++++++- src/crepe/system/AISystem.cpp | 6 ++++++ src/crepe/system/AISystem.h | 14 +++++++++++++ src/crepe/system/CMakeLists.txt | 2 ++ src/example/AITest.cpp | 45 ++++++++++++++++++++++++----------------- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/crepe/system/AISystem.cpp create mode 100644 src/crepe/system/AISystem.h (limited to 'src/example') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 7edf4d1..cd602d8 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,5 +1,6 @@ #include "../facade/SDLContext.h" +#include "../system/AISystem.h" #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" #include "../system/ParticleSystem.h" @@ -20,6 +21,7 @@ LoopManager::LoopManager() { this->load_system(); this->load_system(); this->load_system(); + this->load_system(); } void LoopManager::process_input() { @@ -51,6 +53,11 @@ void LoopManager::loop() { this->render(); timer.enforce_frame_rate(); + + // Stop the game after 5 seconds, for testing purposes + if (timer.get_current_time() > 5) { + this->game_running = false; + } } } @@ -58,6 +65,7 @@ void LoopManager::setup() { this->game_running = true; LoopTimer::get_instance().start(); LoopTimer::get_instance().set_fps(200); + this->scene_manager.load_next_scene(); } void LoopManager::render() { @@ -66,4 +74,8 @@ void LoopManager::render() { } } -void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); } +void LoopManager::update() { + LoopTimer & timer = LoopTimer::get_instance(); + this->get_system().update(); + this->get_system().update(); +} diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp new file mode 100644 index 0000000..012f4fa --- /dev/null +++ b/src/crepe/system/AISystem.cpp @@ -0,0 +1,6 @@ +#include "AISystem.h" +#include + +using namespace crepe; + +void AISystem::update() { std::cout << "AI System update" << std::endl; } diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h new file mode 100644 index 0000000..4138e01 --- /dev/null +++ b/src/crepe/system/AISystem.h @@ -0,0 +1,14 @@ +#pragma once + +#include "System.h" + +namespace crepe { + +class AISystem : public System { +public: + using System::System; + + void update() override; +}; + +} // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index d658b25..ca89d4d 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(crepe PUBLIC CollisionSystem.cpp RenderSystem.cpp AnimatorSystem.cpp + AISystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,4 +16,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES CollisionSystem.h RenderSystem.h AnimatorSystem.h + AISystem.h ) diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index ccf5a85..3998ff4 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,32 +1,41 @@ +#include +#include +#include #include #include -#include -#include -#include #include +#include +#include +#include +#include #include -#include -#include using namespace crepe; +using namespace std; -int main() { - ComponentManager mgr; - 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(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); +class Scene1 : public Scene { +public: + void load_scene() override { + ComponentManager & mgr = this->component_manager; - game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); + GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1); - RenderSystem sys{mgr}; + Texture img = Texture("asset/texture/test_ap43.png"); + game_object1.add_component(img, Color::MAGENTA, + Sprite::FlipSettings{false, false}, 1, 1, 195); - auto start = std::chrono::steady_clock::now(); - while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - sys.update(); - SDL_Delay(10); + game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, + 1.0f); } + string get_name() const override { return "Scene1"; } +}; + +int main() { + LoopManager engine; + engine.add_scene(); + engine.start(); + return 0; } -- cgit v1.2.3 From ac87bfad20e1bcf1fd066a4eda231608fe12f504 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 4 Dec 2024 13:17:08 +0100 Subject: Added AI component --- src/crepe/api/AI.cpp | 11 +++++++++++ src/crepe/api/AI.h | 18 ++++++++++++++++++ src/crepe/api/CMakeLists.txt | 2 ++ src/example/AITest.cpp | 3 ++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/crepe/api/AI.cpp create mode 100644 src/crepe/api/AI.h (limited to 'src/example') diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp new file mode 100644 index 0000000..6b63216 --- /dev/null +++ b/src/crepe/api/AI.cpp @@ -0,0 +1,11 @@ +#include "AI.h" + +namespace crepe { + +AI::AI(game_object_id_t id, double mass, double max_speed, double max_force) + : Component(id), + mass(mass), + max_speed(max_speed), + max_force(max_force) {} + +} // namespace crepe diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h new file mode 100644 index 0000000..b755439 --- /dev/null +++ b/src/crepe/api/AI.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Component.h" +#include "types.h" + +namespace crepe { + +class AI : public Component { +public: + AI(game_object_id_t id, double mass, double max_speed, double max_force); + +public: + double mass; + double max_speed; + double max_force; +}; + +} // namespace crepe diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 50c51ed..d42b459 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -23,6 +23,7 @@ target_sources(crepe PUBLIC Asset.cpp EventHandler.cpp Script.cpp + AI.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -58,4 +59,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES LoopManager.h LoopTimer.h Asset.h + AI.h ) diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 3998ff4..1c4633f 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -8,7 +9,6 @@ #include #include #include -#include using namespace crepe; using namespace std; @@ -24,6 +24,7 @@ public: Texture img = Texture("asset/texture/test_ap43.png"); game_object1.add_component(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); + game_object1.add_component(1, 1, 1); game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); -- cgit v1.2.3 From 121387ba92a23d6f17b36331d25757abc899f7d2 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 4 Dec 2024 17:27:51 +0100 Subject: Implemeted seek behavior --- src/crepe/api/AI.h | 3 +++ src/crepe/system/AISystem.cpp | 45 +++++++++++++++++++++++++++++++++++++++++-- src/crepe/system/AISystem.h | 3 +++ src/example/AITest.cpp | 4 ++-- 4 files changed, 51 insertions(+), 4 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index faeeba5..242ff89 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -51,6 +51,9 @@ public: float arrive_deceleration = 2.0f; private: + vec2 velocity; + friend class AISystem; + int flags = 0; }; diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 12da3d9..9029f32 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,4 +1,7 @@ #include "../ComponentManager.h" +#include "api/LoopTimer.h" +#include "api/Transform.h" +#include "types.h" #include "AISystem.h" @@ -8,9 +11,19 @@ void AISystem::update() { ComponentManager & mgr = this->component_manager; RefVector ai_components = mgr.get_components_by_type(); + double dt = LoopTimer::get_instance().get_delta_time(); + for (AI & ai : ai_components) { vec2 force = this->calculate(ai); - //... + vec2 acceleration = force / ai.mass; + ai.velocity += acceleration * dt; + ai.velocity.truncate(ai.max_speed); + + // Update the position + RefVector transforms + = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + transform.position += ai.velocity * dt; } } @@ -18,7 +31,11 @@ vec2 AISystem::calculate(AI & ai) { vec2 force; if (ai.on(AI::BehaviorType::SEEK)) { - // Seek the target + vec2 force_to_add = this->seek(ai); + + if (!this->accumulate_force(force, force_to_add)) { + return force; + } } if (ai.on(AI::BehaviorType::FLEE)) { // Flee from the target @@ -32,3 +49,27 @@ vec2 AISystem::calculate(AI & ai) { return force; } + +bool AISystem::accumulate_force(vec2 & running_total, vec2 force_to_add) { + double magnitude_remaining = running_total.length(); + double magnitude_to_add = force_to_add.length(); + + if (magnitude_remaining + magnitude_to_add > 0) { + running_total += force_to_add; + return true; + } + + return false; +} + +vec2 AISystem::seek(const AI & ai) { + ComponentManager & mgr = this->component_manager; + RefVector transforms = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + + vec2 desired_velocity = ai.seek_target - transform.position; + desired_velocity.normalize(); + desired_velocity *= ai.max_speed; + + return desired_velocity - ai.velocity; +} diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h index eb8d08c..5e94ccb 100644 --- a/src/crepe/system/AISystem.h +++ b/src/crepe/system/AISystem.h @@ -15,6 +15,9 @@ public: private: vec2 calculate(AI & ai); + bool accumulate_force(vec2 & running_total, vec2 force_to_add); + + vec2 seek(const AI & ai); }; } // namespace crepe diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 1c4633f..341e1de 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -18,13 +18,13 @@ public: void load_scene() override { ComponentManager & mgr = this->component_manager; - GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + 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(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - game_object1.add_component(1, 1, 1); + game_object1.add_component(1, 200, 200).seek_on(); game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); -- cgit v1.2.3 From e617b4f002638e37dbe4d2ce13849728e7e82c78 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 5 Dec 2024 17:26:41 +0100 Subject: Used Mediator --- src/crepe/system/AISystem.cpp | 9 ++++++--- src/example/AITest.cpp | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 9029f32..c67d5ea 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,6 +1,7 @@ -#include "../ComponentManager.h" +#include "manager/ComponentManager.h" #include "api/LoopTimer.h" #include "api/Transform.h" +#include "manager/Mediator.h" #include "types.h" #include "AISystem.h" @@ -8,7 +9,8 @@ using namespace crepe; void AISystem::update() { - ComponentManager & mgr = this->component_manager; + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; RefVector ai_components = mgr.get_components_by_type(); double dt = LoopTimer::get_instance().get_delta_time(); @@ -63,7 +65,8 @@ bool AISystem::accumulate_force(vec2 & running_total, vec2 force_to_add) { } vec2 AISystem::seek(const AI & ai) { - ComponentManager & mgr = this->component_manager; + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; RefVector transforms = mgr.get_components_by_id(ai.game_object_id); Transform & transform = transforms.front().get(); diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 341e1de..71aacb2 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,6 +1,7 @@ +#include #include #include -#include +#include #include #include #include @@ -16,7 +17,8 @@ using namespace std; class Scene1 : public Scene { public: void load_scene() override { - ComponentManager & mgr = this->component_manager; + 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); -- cgit v1.2.3 From 6b45759e570bcaafc167e74ac46c8ffe05efa66e Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 5 Dec 2024 18:28:15 +0100 Subject: Make format --- src/crepe/system/AISystem.cpp | 2 +- src/example/AITest.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index c67d5ea..3c61c78 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,6 +1,6 @@ -#include "manager/ComponentManager.h" #include "api/LoopTimer.h" #include "api/Transform.h" +#include "manager/ComponentManager.h" #include "manager/Mediator.h" #include "types.h" diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 71aacb2..841b195 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,7 +1,5 @@ -#include #include #include -#include #include #include #include @@ -10,6 +8,8 @@ #include #include #include +#include +#include using namespace crepe; using namespace std; -- cgit v1.2.3 From 93893dbe710d864d5865f361f9a8a8f8f85b94f6 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 09:57:30 +0100 Subject: Make format --- mwe/events/include/event.h | 2 +- src/crepe/api/Rigidbody.h | 1 - src/crepe/api/Script.h | 2 +- src/crepe/system/CollisionSystem.cpp | 40 +++++++-------- src/crepe/system/CollisionSystem.h | 6 +-- src/example/game.cpp | 98 +++++++++++++++++------------------- src/test/CollisionTest.cpp | 6 +-- src/test/Profiling.cpp | 6 +-- 8 files changed, 76 insertions(+), 85 deletions(-) (limited to 'src/example') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index ee1bf52..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index b0a24f7..722a665 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -7,7 +7,6 @@ #include "types.h" - namespace crepe { /** diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 1474a09..fa83152 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -3,10 +3,10 @@ #include #include "../manager/EventManager.h" -#include "system/CollisionSystem.h" #include "../manager/Mediator.h" #include "../types.h" #include "../util/OptionalRef.h" +#include "system/CollisionSystem.h" namespace crepe { diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index f75d0ad..da9e3af 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -6,6 +6,8 @@ #include #include +#include "../manager/ComponentManager.h" +#include "../manager/EventManager.h" #include "api/BoxCollider.h" #include "api/CircleCollider.h" #include "api/Event.h" @@ -13,8 +15,6 @@ #include "api/Rigidbody.h" #include "api/Transform.h" #include "api/Vector2.h" -#include "../manager/ComponentManager.h" -#include "../manager/EventManager.h" #include "Collider.h" #include "CollisionSystem.h" @@ -27,17 +27,14 @@ void CollisionSystem::update() { std::vector all_colliders; game_object_id_t id = 0; ComponentManager & mgr = this->mediator.component_manager; - RefVector rigidbodies - = mgr.get_components_by_type(); + RefVector rigidbodies = mgr.get_components_by_type(); // Collisions can only happen on object with a rigidbody for (Rigidbody & rigidbody : rigidbodies) { if (!rigidbody.active) continue; id = rigidbody.game_object_id; - Transform & transform - = mgr.get_components_by_id(id).front().get(); + Transform & transform = mgr.get_components_by_id(id).front().get(); // Check if the boxcollider is active and has the same id as the rigidbody. - RefVector boxcolliders - = mgr.get_components_by_type(); + RefVector boxcolliders = mgr.get_components_by_type(); for (BoxCollider & boxcollider : boxcolliders) { if (boxcollider.game_object_id != id) continue; if (!boxcollider.active) continue; @@ -159,7 +156,7 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform, data2.rigidbody); resolution = this->get_circle_box_resolution(collider2, collider1, collider_pos2, - collider_pos1,true); + collider_pos1, true); break; } case CollisionInternalType::CIRCLE_CIRCLE: { @@ -185,7 +182,7 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform, data2.rigidbody); resolution = this->get_circle_box_resolution(collider1, collider2, collider_pos1, - collider_pos2,false); + collider_pos2, false); break; } } @@ -261,7 +258,6 @@ vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider & circle // Normalize the delta vector to get the collision direction vec2 collision_normal = delta / distance; - // Compute the resolution vector vec2 resolution = -collision_normal * penetration_depth; @@ -272,7 +268,8 @@ vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider & circle vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_collider, const BoxCollider & box_collider, const vec2 & circle_position, - const vec2 & box_position,bool inverse) const { + const vec2 & box_position, + bool inverse) const { vec2 delta = circle_position - box_position; // Compute half-dimensions of the box @@ -294,7 +291,7 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; - if(inverse) collision_normal = -collision_normal; + if (inverse) collision_normal = -collision_normal; // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; @@ -311,8 +308,7 @@ void CollisionSystem::determine_collision_handler(CollisionInfo & info) { // Call collision event for user CollisionEvent data(info); EventManager & emgr = this->mediator.event_manager; - emgr.trigger_event( - data, info.this_collider.game_object_id); + emgr.trigger_event(data, info.this_collider.game_object_id); } void CollisionSystem::static_collision_handler(CollisionInfo & info) { @@ -389,14 +385,14 @@ CollisionSystem::gather_collisions(std::vector & colliders) { bool CollisionSystem::have_common_layer(const std::set & layers1, const std::set & layers2) { - + // Check if any number is equal in the layers for (int num : layers1) { - if (layers2.contains(num)) { - // Common layer found - return true; - break; - } + if (layers2.contains(num)) { + // Common layer found + return true; + break; + } } // No common layer found return false; @@ -512,7 +508,7 @@ bool CollisionSystem::get_box_circle_collision(const BoxCollider & box1, float distance_squared = distance_x * distance_x + distance_y * distance_y; // Compare distance squared with the square of the circle's radius - return distance_squared <= circle2.radius * circle2.radius-1; + return distance_squared <= circle2.radius * circle2.radius - 1; } bool CollisionSystem::get_circle_circle_collision(const CircleCollider & circle1, diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index b978dbb..eee582b 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -6,11 +6,11 @@ #include "api/BoxCollider.h" #include "api/CircleCollider.h" +#include "api/Event.h" #include "api/Metadata.h" #include "api/Rigidbody.h" #include "api/Transform.h" #include "api/Vector2.h" -#include "api/Event.h" #include "Collider.h" #include "System.h" @@ -183,8 +183,8 @@ private: */ vec2 get_circle_box_resolution(const CircleCollider & circle_collider, const BoxCollider & box_collider, - const vec2 & circle_position, - const vec2 & box_position,bool inverse) const; + const vec2 & circle_position, const vec2 & box_position, + bool inverse) const; /** * \brief Determines the appropriate collision handler for a collision. 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 #include @@ -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(); tf.position.x -= 1; break; } - case Keycode::W: - { + case Keycode::W: { Transform & tf = this->get_component(); tf.position.y -= 1; break; } - case Keycode::S: - { + case Keycode::S: { Transform & tf = this->get_component(); tf.position.y += 1; break; } - case Keycode::D: - { + case Keycode::D: { Transform & tf = this->get_component(); tf.position.x += 1; break; } - case Keycode::E: - { - if(flip){ + case Keycode::E: { + if (flip) { flip = false; this->get_component().active = true; this->get_components()[0].get().active = true; this->get_component().active = false; this->get_components()[1].get().active = false; - } - else { + } else { flip = true; this->get_component().active = false; this->get_components()[0].get().active = false; this->get_component().active = true; this->get_components()[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([this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe([this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + subscribe( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe( + [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(); tf.position.x -= 1; break; } - case Keycode::UP: - { + case Keycode::UP: { Transform & tf = this->get_component(); tf.position.y -= 1; break; } - case Keycode::DOWN: - { + case Keycode::DOWN: { Transform & tf = this->get_component(); tf.position.y += 1; break; } - case Keycode::RIGHT: - { + case Keycode::RIGHT: { Transform & tf = this->get_component(); tf.position.x += 1; break; } - case Keycode::PAUSE: - { - if(flip){ + case Keycode::PAUSE: { + if (flip) { flip = false; this->get_component().active = true; this->get_components()[0].get().active = true; this->get_component().active = false; this->get_components()[1].get().active = false; - } - else { + } else { flip = true; this->get_component().active = false; this->get_components()[0].get().active = false; this->get_component().active = true; this->get_components()[1].get().active = true; } - - + //add collider switch break; } default: - break; + break; } return false; - } + } void init() { Log::logf("init"); - subscribe([this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe([this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + subscribe( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe( + [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(vec2{screen_size_width / 2 + world_collider / 2, 0}, vec2{world_collider, world_collider}); // right - world.add_component(Color::WHITE, ivec2{static_cast(screen_size_width), static_cast(screen_size_height)}, vec2{screen_size_width, screen_size_height}, 1.0f); + world.add_component( + Color::WHITE, + ivec2{static_cast(screen_size_width), static_cast(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(vec2{0, 0}, 10).active = false; auto img2 = Texture("asset/texture/circle.png"); - game_object1.add_component(img2, color, Sprite::FlipSettings{false, false}, 1, - 1, 20).active = false; - - + game_object1 + .add_component(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(vec2{0, 0}, 10).active = false; auto img4 = Texture("asset/texture/circle.png"); - game_object2.add_component(img4, color, Sprite::FlipSettings{false, false}, 1, - 1, 20).active = false; - + game_object2 + .add_component(img4, color, Sprite::FlipSettings{false, false}, 1, 1, 20) + .active + = false; } string get_name() const { return "scene1"; } diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index a683b1f..dd45eb6 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -7,14 +7,14 @@ #define private public #define protected public -#include -#include #include -#include #include #include #include #include +#include +#include +#include #include #include #include diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 91be769..f091d9d 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -9,14 +9,14 @@ #define private public #define protected public -#include #include -#include #include #include #include #include #include +#include +#include #include #include #include @@ -162,7 +162,7 @@ TEST_F(Profiling, Profiling_2) { .body_type = Rigidbody::BodyType::STATIC, }); gameobject.add_component(vec2{0, 0}, vec2{1, 1}); - + gameobject.add_component().set_script(); Color color(0, 0, 0, 0); auto img = Texture("asset/texture/green_square.png"); -- cgit v1.2.3 From 42ecf9c1d0e3ed1f37f99a24f31241b68f978b28 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 10:14:33 +0100 Subject: Added script to shutdown game (by throwing an exception and not catching it) --- src/example/AITest.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/example') diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 841b195..91a529c 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,19 +1,33 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include -#include #include 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( + [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); + } +}; + class Scene1 : public Scene { public: void load_scene() override { @@ -30,6 +44,7 @@ public: game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); + game_object2.add_component().set_script(); } string get_name() const override { return "Scene1"; } -- cgit v1.2.3 From 9c2cafd85ed7aa9a860ba25fbe2bd3ccc2439f29 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 10:39:18 +0100 Subject: Using Rigidbody from now on --- src/crepe/api/AI.cpp | 6 +----- src/crepe/api/AI.h | 7 +------ src/crepe/system/AISystem.cpp | 22 +++++++++++----------- src/example/AITest.cpp | 6 +++++- 4 files changed, 18 insertions(+), 23 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp index 7f820a8..d785bb5 100644 --- a/src/crepe/api/AI.cpp +++ b/src/crepe/api/AI.cpp @@ -2,10 +2,6 @@ namespace crepe { -AI::AI(game_object_id_t id, float mass, float max_speed, float max_force) - : Component(id), - mass(mass), - max_speed(max_speed), - max_force(max_force) {} +AI::AI(game_object_id_t id, float max_force) : Component(id), max_force(max_force) {} } // namespace crepe diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index 242ff89..046426d 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -16,7 +16,7 @@ public: }; public: - AI(game_object_id_t id, float mass, float max_speed, float max_force); + AI(game_object_id_t id, float max_force); bool on(BehaviorType behavior) const { return (flags & behavior) == behavior; } void seek_on() { flags |= SEEK; } @@ -37,8 +37,6 @@ public: } public: - float mass; - float max_speed; float max_force; // The target to seek or arrive at @@ -51,9 +49,6 @@ public: float arrive_deceleration = 2.0f; private: - vec2 velocity; - friend class AISystem; - int flags = 0; }; diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 3c61c78..d496e12 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,4 +1,5 @@ #include "api/LoopTimer.h" +#include "api/Rigidbody.h" #include "api/Transform.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" @@ -16,16 +17,13 @@ void AISystem::update() { double dt = LoopTimer::get_instance().get_delta_time(); for (AI & ai : ai_components) { + RefVector rigidbodies + = mgr.get_components_by_id(ai.game_object_id); + Rigidbody & rigidbody = rigidbodies.front().get(); + vec2 force = this->calculate(ai); - vec2 acceleration = force / ai.mass; - ai.velocity += acceleration * dt; - ai.velocity.truncate(ai.max_speed); - - // Update the position - RefVector transforms - = mgr.get_components_by_id(ai.game_object_id); - Transform & transform = transforms.front().get(); - transform.position += ai.velocity * dt; + vec2 acceleration = force / rigidbody.data.mass; + rigidbody.data.linear_velocity += acceleration * dt; } } @@ -69,10 +67,12 @@ vec2 AISystem::seek(const AI & ai) { ComponentManager & mgr = mediator.component_manager; RefVector transforms = mgr.get_components_by_id(ai.game_object_id); Transform & transform = transforms.front().get(); + RefVector rigidbodies = mgr.get_components_by_id(ai.game_object_id); + Rigidbody & rigidbody = rigidbodies.front().get(); vec2 desired_velocity = ai.seek_target - transform.position; desired_velocity.normalize(); - desired_velocity *= ai.max_speed; + desired_velocity *= rigidbody.data.max_linear_velocity; - return desired_velocity - ai.velocity; + return desired_velocity - rigidbody.data.linear_velocity; } diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 91a529c..2b6a4d6 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,10 @@ public: Texture img = Texture("asset/texture/test_ap43.png"); game_object1.add_component(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - game_object1.add_component(1, 200, 200).seek_on(); + game_object1.add_component(200).seek_on(); + game_object1.add_component(Rigidbody::Data{ + .mass = 1.0f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 + }); game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); -- cgit v1.2.3 From 9eac8d31b25c234a21b1d188df17e77e71f48088 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 16:21:40 +0100 Subject: Improved example --- src/crepe/system/CollisionSystem.cpp | 2 -- src/example/AITest.cpp | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 2a487fd..1282f7a 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -550,5 +550,3 @@ vec2 CollisionSystem::get_current_position(const vec2 & collider_offset, // Final positions considering scaling and rotation return (transform.position + vec2(rotated_total_offset_x1, rotated_total_offset_y1)); } - - diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 2b6a4d6..144aef3 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace crepe; using namespace std; @@ -23,9 +24,19 @@ class Script1 : public Script { return true; } + bool mousemove(const MouseMoveEvent & event) { + RefVector aivec = this->get_components(); + AI & ai = aivec.front().get(); + ai.seek_target + = vec2{static_cast(event.mouse_x), static_cast(event.mouse_y)}; + return true; + } + void init() { subscribe( [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); + subscribe( + [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); } }; @@ -41,14 +52,14 @@ public: Texture img = Texture("asset/texture/test_ap43.png"); game_object1.add_component(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - game_object1.add_component(200).seek_on(); + game_object1.add_component(30).seek_on(); game_object1.add_component(Rigidbody::Data{ - .mass = 1.0f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 + .mass = 0.5f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 }); + game_object1.add_component().set_script(); game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, 1.0f); - game_object2.add_component().set_script(); } string get_name() const override { return "Scene1"; } -- cgit v1.2.3 From 0d0943d23364d7110f0232e3564f4ea63af13db2 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 16:41:51 +0100 Subject: Implemented flee and arrive behaviors --- src/crepe/api/AI.h | 4 +-- src/crepe/system/AISystem.cpp | 71 ++++++++++++++++++++++++++++++++++++++----- src/example/AITest.cpp | 6 ++-- 3 files changed, 69 insertions(+), 12 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index 046426d..d4bd9d3 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -44,9 +44,9 @@ public: // The target to flee from vec2 flee_target; // The distance at which the entity will start to flee from the target - float square_flee_panic_distance = 100.0f * 100.0f; + float square_flee_panic_distance = 200.0f * 200.0f; // The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot)) - float arrive_deceleration = 2.0f; + float arrive_deceleration = 40.0f; private: int flags = 0; diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 4858000..ce3147f 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -1,11 +1,14 @@ +#include +#include + #include "api/LoopTimer.h" #include "api/Rigidbody.h" #include "api/Transform.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" -#include "types.h" #include "AISystem.h" +#include "types.h" using namespace crepe; @@ -30,21 +33,33 @@ void AISystem::update() { vec2 AISystem::calculate(AI & ai) { vec2 force; - if (ai.on(AI::BehaviorType::SEEK)) { - vec2 force_to_add = this->seek(ai); + if (ai.on(AI::BehaviorType::FLEE)) { + vec2 force_to_add = this->flee(ai); if (!this->accumulate_force(ai, force, force_to_add)) { return force; } } - if (ai.on(AI::BehaviorType::FLEE)) { - // Flee from the target - } if (ai.on(AI::BehaviorType::ARRIVE)) { - // Arrive at the target + vec2 force_to_add = this->arrive(ai); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } + } + if (ai.on(AI::BehaviorType::SEEK)) { + vec2 force_to_add = this->seek(ai); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + } } if (ai.on(AI::BehaviorType::PATH_FOLLOW)) { - // Follow the path + /*vec2 force_to_add = this->path_follow(ai); + + if (!this->accumulate_force(ai, force, force_to_add)) { + return force; + }*/ } return force; @@ -83,3 +98,43 @@ vec2 AISystem::seek(const AI & ai) { return desired_velocity - rigidbody.data.linear_velocity; } + +vec2 AISystem::flee(const AI & ai) { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + RefVector transforms = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + RefVector rigidbodies = mgr.get_components_by_id(ai.game_object_id); + Rigidbody & rigidbody = rigidbodies.front().get(); + + vec2 desired_velocity = transform.position - ai.flee_target; + if (desired_velocity.length_squared() > ai.square_flee_panic_distance) { + return vec2{0, 0}; + } + + desired_velocity.normalize(); + desired_velocity *= rigidbody.data.max_linear_velocity; + + return desired_velocity - rigidbody.data.linear_velocity; +} + +vec2 AISystem::arrive(const AI & ai) { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + RefVector transforms = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + RefVector rigidbodies = mgr.get_components_by_id(ai.game_object_id); + Rigidbody & rigidbody = rigidbodies.front().get(); + + vec2 to_target = ai.seek_target - transform.position; + float distance = to_target.length(); + if (distance > 0.0f) { + float speed = distance / ai.arrive_deceleration; + speed = std::min(speed, rigidbody.data.max_linear_velocity.length()); + vec2 desired_velocity = to_target * (speed / distance); + + return desired_velocity - rigidbody.data.linear_velocity; + } + + return vec2{0, 0}; +} diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 144aef3..319d0fe 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -27,7 +27,7 @@ class Script1 : public Script { bool mousemove(const MouseMoveEvent & event) { RefVector aivec = this->get_components(); AI & ai = aivec.front().get(); - ai.seek_target + ai.flee_target = vec2{static_cast(event.mouse_x), static_cast(event.mouse_y)}; return true; } @@ -52,7 +52,9 @@ public: Texture img = Texture("asset/texture/test_ap43.png"); game_object1.add_component(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - game_object1.add_component(30).seek_on(); + AI & ai = game_object1.add_component(30); + ai.arrive_on(); + ai.flee_on(); game_object1.add_component(Rigidbody::Data{ .mass = 0.5f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 }); -- cgit v1.2.3 From 33a072db28d71ba65e59f9491abd42dbf9695fc4 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 6 Dec 2024 17:14:00 +0100 Subject: Implemented path_follow --- src/crepe/api/AI.h | 14 ++++++++++++++ src/crepe/system/AISystem.cpp | 34 ++++++++++++++++++++++++++++++++-- src/crepe/system/AISystem.h | 2 +- src/example/AITest.cpp | 21 +++++++++++++-------- 4 files changed, 60 insertions(+), 11 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index d4bd9d3..35b8998 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -36,6 +36,8 @@ public: if (on(PATH_FOLLOW)) flags ^= PATH_FOLLOW; } + void add_path_node(vec2 node) { path.push_back(node); } + public: float max_force; @@ -47,9 +49,21 @@ public: float square_flee_panic_distance = 200.0f * 200.0f; // The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot)) float arrive_deceleration = 40.0f; + // The path to follow + std::vector path; + // The distance from the path node at which the entity will move to the next node + float path_node_distance = 400.0f; + // Looping behavior for the path + bool path_loop = true; private: + // The flags for the behaviors int flags = 0; + // The current path index + size_t path_index = 0; + + // The AISystem is the only class that can access the private members of AI + friend class AISystem; }; } // namespace crepe diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index ce3147f..55dc14c 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -55,11 +55,11 @@ vec2 AISystem::calculate(AI & ai) { } } if (ai.on(AI::BehaviorType::PATH_FOLLOW)) { - /*vec2 force_to_add = this->path_follow(ai); + vec2 force_to_add = this->path_follow(ai); if (!this->accumulate_force(ai, force, force_to_add)) { return force; - }*/ + } } return force; @@ -138,3 +138,33 @@ vec2 AISystem::arrive(const AI & ai) { return vec2{0, 0}; } + +vec2 AISystem::path_follow(AI & ai) { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + RefVector transforms = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + RefVector rigidbodies = mgr.get_components_by_id(ai.game_object_id); + Rigidbody & rigidbody = rigidbodies.front().get(); + + if (ai.path.empty()) { + return vec2{0, 0}; + } + + vec2 to_target = ai.path.at(ai.path_index) - transform.position; + if (to_target.length_squared() > ai.path_node_distance * ai.path_node_distance) { + ai.seek_target = ai.path.at(ai.path_index); + } else { + ai.path_index++; + if (ai.path_index >= ai.path.size()) { + if (ai.path_loop) { + ai.path_index = 0; + } else { + ai.path_index = ai.path.size() - 1; + return this->arrive(ai); + } + } + } + + return this->seek(ai); +} diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h index 18f1c61..27861d9 100644 --- a/src/crepe/system/AISystem.h +++ b/src/crepe/system/AISystem.h @@ -20,7 +20,7 @@ private: vec2 seek(const AI & ai); vec2 flee(const AI & ai); vec2 arrive(const AI & ai); - vec2 path_follow(const AI & ai); + vec2 path_follow(AI & ai); }; } // namespace crepe diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 319d0fe..d12a99a 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -25,10 +25,10 @@ class Script1 : public Script { } bool mousemove(const MouseMoveEvent & event) { - RefVector aivec = this->get_components(); + /*RefVector aivec = this->get_components(); AI & ai = aivec.front().get(); ai.flee_target - = vec2{static_cast(event.mouse_x), static_cast(event.mouse_y)}; + = vec2{static_cast(event.mouse_x), static_cast(event.mouse_y)};*/ return true; } @@ -46,21 +46,26 @@ public: Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; - GameObject game_object1 = mgr.new_object("", "", vec2{250, 250}, 0, 1); + 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(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - AI & ai = game_object1.add_component(30); - ai.arrive_on(); - ai.flee_on(); + AI & ai = game_object1.add_component(300); + // ai.arrive_on(); + // ai.flee_on(); + ai.path_follow_on(); + ai.add_path_node(vec2{1200, 1200}); + ai.add_path_node(vec2{-1200, 1200}); + ai.add_path_node(vec2{1200, -1200}); + ai.add_path_node(vec2{-1200, -1200}); game_object1.add_component(Rigidbody::Data{ - .mass = 0.5f, .max_linear_velocity = {21, 21}, // sqrt(21^2 + 21^2) = 30 + .mass = 0.5f, .max_linear_velocity = {50, 50}, // sqrt(21^2 + 21^2) = 30 }); game_object1.add_component().set_script(); - game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780}, + game_object2.add_component(Color::WHITE, ivec2{1080, 720}, vec2{5000, 5000}, 1.0f); } -- cgit v1.2.3 From f19f37ae3eff84161f86e62a26fbd8b68f8f91a9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 15:29:33 +0100 Subject: make SaveManager no longer a singleton --- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/SaveManager.cpp | 32 +++++++++++++--------------- src/crepe/manager/SaveManager.h | 27 ++++++++++-------------- src/example/CMakeLists.txt | 2 -- src/example/asset_manager.cpp | 36 -------------------------------- src/example/savemgr.cpp | 44 --------------------------------------- src/test/CMakeLists.txt | 1 + src/test/SaveManagerTest.cpp | 41 ++++++++++++++++++++++++++++++++++++ 8 files changed, 69 insertions(+), 118 deletions(-) delete mode 100644 src/example/asset_manager.cpp delete mode 100644 src/example/savemgr.cpp create mode 100644 src/test/SaveManagerTest.cpp (limited to 'src/example') diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 8094d80..6507a74 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,13 +5,13 @@ // TODO: remove these singletons: #include "../facade/SDLContext.h" #include "EventManager.h" -#include "SaveManager.h" #include "api/LoopTimer.h" namespace crepe { class ComponentManager; class SceneManager; +class SaveManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -28,7 +28,7 @@ class SceneManager; struct Mediator { OptionalRef component_manager; OptionalRef scene_manager; - OptionalRef save_manager = SaveManager::get_instance(); + OptionalRef save_manager; OptionalRef event_manager = EventManager::get_instance(); OptionalRef sdl_context = SDLContext::get_instance(); OptionalRef timer = LoopTimer::get_instance(); diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index d4ed1c1..292e8fd 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -1,13 +1,24 @@ #include "../ValueBroker.h" #include "../api/Config.h" #include "../facade/DB.h" -#include "../util/Log.h" #include "SaveManager.h" using namespace std; using namespace crepe; +SaveManager::SaveManager(Mediator & mediator) : Manager(mediator) { + mediator.save_manager = *this; +} + +DB & SaveManager::get_db() { + if (this->db == nullptr) { + Config & cfg = Config::get_instance(); + this->db = make_unique(cfg.savemgr.location); + } + return *this->db; +} + template <> string SaveManager::serialize(const string & value) const noexcept { return value; @@ -90,22 +101,6 @@ int32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -SaveManager::SaveManager() { dbg_trace(); } - -SaveManager & SaveManager::get_instance() { - dbg_trace(); - static SaveManager instance; - return instance; -} - -DB & SaveManager::get_db() { - Config & cfg = Config::get_instance(); - // TODO: make this path relative to XDG_DATA_HOME on Linux and whatever the - // default equivalent is on Windows using some third party library - static DB db(cfg.savemgr.location); - return db; -} - bool SaveManager::has(const string & key) { DB & db = this->get_db(); return db.has(key); @@ -155,7 +150,8 @@ ValueBroker SaveManager::get(const string & key) { return { [this, key](const T & target) { this->set(key, target); }, [this, key, value]() mutable -> const T & { - value = this->deserialize(this->get_db().get(key)); + DB & db = this->get_db(); + value = this->deserialize(db.get(key)); return value; }, }; diff --git a/src/crepe/manager/SaveManager.h b/src/crepe/manager/SaveManager.h index 3d8c852..d13a97a 100644 --- a/src/crepe/manager/SaveManager.h +++ b/src/crepe/manager/SaveManager.h @@ -4,6 +4,8 @@ #include "../ValueBroker.h" +#include "Manager.h" + namespace crepe { class DB; @@ -18,7 +20,7 @@ class DB; * * The underlying database is a key-value store. */ -class SaveManager { +class SaveManager : public Manager { public: /** * \brief Get a read/write reference to a value and initialize it if it does not yet exist @@ -63,8 +65,8 @@ public: */ bool has(const std::string & key); -private: - SaveManager(); +public: + SaveManager(Mediator & mediator); virtual ~SaveManager() = default; private: @@ -90,25 +92,18 @@ private: T deserialize(const std::string & value) const noexcept; public: - // singleton - static SaveManager & get_instance(); SaveManager(const SaveManager &) = delete; SaveManager(SaveManager &&) = delete; SaveManager & operator=(const SaveManager &) = delete; SaveManager & operator=(SaveManager &&) = delete; +protected: + //! Create or return DB + virtual DB & get_db(); + private: - /** - * \brief Create an instance of DB and return its reference - * - * \returns DB instance - * - * This function exists because DB is a facade class, which can't directly be used in the API - * without workarounds - * - * TODO: better solution - */ - static DB & get_db(); + //! Database + std::unique_ptr db = nullptr; }; } // namespace crepe diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8ef71bb..5a93b1f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,8 +16,6 @@ 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) 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 -#include -#include - -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("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache("../asset/texture/img.png"); - auto img1 = mgr.cache("../asset/texture/second.png"); - } - - { - auto bgm = mgr.cache("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache("../asset/texture/img.png"); - auto img1 = mgr.cache("../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 -#include -#include -#include -#include - -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 prop = mgr.get(key, 0); - Proxy val = mgr.get(key, 0); - - dbg_logf("val = {}", mgr.get(key).get()); - prop.set(1); - dbg_logf("val = {}", mgr.get(key).get()); - val = 2; - dbg_logf("val = {}", mgr.get(key).get()); - mgr.set(key, 3); - dbg_logf("val = {}", mgr.get(key).get()); - - dbg_logf("has key = {}", mgr.has(key)); - assert(true == mgr.has(key)); - - return 0; -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c9cbac5..734e3ee 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -17,4 +17,5 @@ target_sources(test_main PUBLIC ScriptEventTest.cpp ScriptSceneTest.cpp Profiling.cpp + SaveManagerTest.cpp ) diff --git a/src/test/SaveManagerTest.cpp b/src/test/SaveManagerTest.cpp new file mode 100644 index 0000000..a1efc33 --- /dev/null +++ b/src/test/SaveManagerTest.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class SaveManagerTest : public Test { + Mediator m; + class TestSaveManager : public SaveManager { + using SaveManager::SaveManager; + + // in-memory database for testing + DB db{}; + virtual DB & get_db() override { return this->db; } + }; + +public: + TestSaveManager mgr{m}; +}; + +TEST_F(SaveManagerTest, ReadWrite) { + ASSERT_FALSE(mgr.has("foo")); + mgr.set("foo", "bar"); + ASSERT_TRUE(mgr.has("foo")); + + ValueBroker value = mgr.get("foo"); + EXPECT_EQ(value.get(), "bar"); +} + +TEST_F(SaveManagerTest, DefaultValue) { + ValueBroker value = mgr.get("foo", 3); + + ASSERT_EQ(value.get(), 3); + value.set(5); + ASSERT_EQ(value.get(), 5); +} + -- cgit v1.2.3 From 6cee1cff083fc50eeedf88537965d3c79e7b790a Mon Sep 17 00:00:00 2001 From: max-001 Date: Mon, 9 Dec 2024 11:28:06 +0100 Subject: Added Doxygen --- src/crepe/api/AI.cpp | 23 ++++++++++++++++++++ src/crepe/api/AI.h | 50 ++++++++++++++++++++++++++++++++++--------- src/crepe/system/AISystem.cpp | 1 + src/crepe/system/AISystem.h | 44 +++++++++++++++++++++++++++++++++++++ src/example/AITest.cpp | 10 ++++----- 5 files changed, 112 insertions(+), 16 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp index d785bb5..49f6b92 100644 --- a/src/crepe/api/AI.cpp +++ b/src/crepe/api/AI.cpp @@ -4,4 +4,27 @@ namespace crepe { AI::AI(game_object_id_t id, float max_force) : Component(id), max_force(max_force) {} +void AI::make_circle_path(float radius, vec2 center, float start_angle, bool clockwise) { + // The step size is determined by the radius (step size is in radians) + float step = 400.0f / radius; + // Force at least 16 steps (in case of a small radius) + if (step > 2 * M_PI / 16) { + step = 2 * M_PI / 16; + } + // The path node distance is determined by the step size and the radius + path_node_distance = radius * step * 0.75f; + + if (clockwise) { + for (float i = start_angle; i < 2 * M_PI + start_angle; i += step) { + path.push_back(vec2{static_cast(center.x + radius * cos(i)), + static_cast(center.y + radius * sin(i))}); + } + } else { + for (float i = start_angle; i > start_angle - 2 * M_PI; i -= step) { + path.push_back(vec2{static_cast(center.x + radius * cos(i)), + static_cast(center.y + radius * sin(i))}); + } + } +} + } // namespace crepe diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index 35b8998..9f5c0a8 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -5,8 +5,15 @@ namespace crepe { +/** + * \brief The AI component is used to control the movement of an entity using AI. + * + * The AI component can be used to control the movement of an entity. The AI component can be used + * to implement different behaviors such as seeking, fleeing, arriving, and path following. + */ class AI : public Component { public: + //! The different types of behaviors that can be used enum BehaviorType { NONE = 0x00000, SEEK = 0x00002, @@ -16,53 +23,76 @@ public: }; public: + /** + * \param id The id of the game object + * \param max_force The maximum force that can be applied to the entity + */ AI(game_object_id_t id, float max_force); + /** + * \brief Check if a behavior is on/activated + * + * \param behavior The behavior to check + * \return true if the behavior is on, false otherwise + */ bool on(BehaviorType behavior) const { return (flags & behavior) == behavior; } + //! Turn on the seek behavior void seek_on() { flags |= SEEK; } + //! Turn off the seek behavior void seek_off() { if (on(SEEK)) flags ^= SEEK; } + //! Turn on the flee behavior void flee_on() { flags |= FLEE; } + //! Turn off the flee behavior void flee_off() { if (on(FLEE)) flags ^= FLEE; } + //! Turn on the arrive behavior void arrive_on() { flags |= ARRIVE; } + //! Turn off the arrive behavior void arrive_off() { if (on(ARRIVE)) flags ^= ARRIVE; } + //! Turn on the path follow behavior void path_follow_on() { flags |= PATH_FOLLOW; } + //! Turn off the path follow behavior void path_follow_off() { if (on(PATH_FOLLOW)) flags ^= PATH_FOLLOW; } + //! Add a path node to the path void add_path_node(vec2 node) { path.push_back(node); } + //! Create a circle path + void make_circle_path(float radius, vec2 center = {0, 0}, float start_angle = 0, + bool clockwise = true); public: + //! The maximum force that can be applied to the entity (higher values will make the entity adjust faster) float max_force; - // The target to seek or arrive at + //! The target to seek or arrive at vec2 seek_target; - // The target to flee from + //! The target to flee from vec2 flee_target; - // The distance at which the entity will start to flee from the target + //! The distance at which the entity will start to flee from the target float square_flee_panic_distance = 200.0f * 200.0f; - // The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot)) + //! The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot)) float arrive_deceleration = 40.0f; - // The path to follow + //! The path to follow std::vector path; - // The distance from the path node at which the entity will move to the next node + //! The distance from the path node at which the entity will move to the next node float path_node_distance = 400.0f; - // Looping behavior for the path + //! Looping behavior for the path bool path_loop = true; private: - // The flags for the behaviors + //! The flags for the behaviors int flags = 0; - // The current path index + //! The current path index size_t path_index = 0; - // The AISystem is the only class that can access the private members of AI + //! The AISystem is the only class that should access the flags and path_index variables friend class AISystem; }; diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 55dc14c..7b801c3 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -17,6 +17,7 @@ void AISystem::update() { ComponentManager & mgr = mediator.component_manager; RefVector ai_components = mgr.get_components_by_type(); + //TODO: Use fixed loop dt (this is not available at master at the moment) double dt = LoopTimer::get_instance().get_delta_time(); for (AI & ai : ai_components) { diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h index 27861d9..670d20d 100644 --- a/src/crepe/system/AISystem.h +++ b/src/crepe/system/AISystem.h @@ -7,19 +7,63 @@ namespace crepe { +/** + * \brief The AISystem is used to control the movement of entities using AI. + * + * The AISystem is used to control the movement of entities using AI. The AISystem can be used to + * implement different behaviors such as seeking, fleeing, arriving, and path following. + */ class AISystem : public System { public: using System::System; + //! Update the AI system void update() override; private: + /** + * \brief Calculate the total force to apply to the entity + * + * \param ai The AI component + */ vec2 calculate(AI & ai); + /** + * \brief Accumulate the force to apply to the entity + * + * \param ai The AI component + * \param running_total The running total of the force + * \param force_to_add The force to add + * \return true if the force was added, false otherwise + */ bool accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add); + /** + * \brief Calculate the seek force + * + * \param ai The AI component + * \return The seek force + */ vec2 seek(const AI & ai); + /** + * \brief Calculate the flee force + * + * \param ai The AI component + * \return The flee force + */ vec2 flee(const AI & ai); + /** + * \brief Calculate the arrive force + * + * \param ai The AI component + * \return The arrive force + */ vec2 arrive(const AI & ai); + /** + * \brief Calculate the path follow force + * + * \param ai The AI component + * \return The path follow force + */ vec2 path_follow(AI & ai); }; diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index d12a99a..72e06cf 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -52,16 +52,14 @@ public: Texture img = Texture("asset/texture/test_ap43.png"); game_object1.add_component(img, Color::MAGENTA, Sprite::FlipSettings{false, false}, 1, 1, 195); - AI & ai = game_object1.add_component(300); + AI & ai = game_object1.add_component(3000); // ai.arrive_on(); // ai.flee_on(); ai.path_follow_on(); - ai.add_path_node(vec2{1200, 1200}); - ai.add_path_node(vec2{-1200, 1200}); - ai.add_path_node(vec2{1200, -1200}); - ai.add_path_node(vec2{-1200, -1200}); + ai.make_circle_path(1000, {0, -1000}, 1.5707, true); + ai.make_circle_path(1000, {0, 1000}, 4.7124, false); game_object1.add_component(Rigidbody::Data{ - .mass = 0.5f, .max_linear_velocity = {50, 50}, // sqrt(21^2 + 21^2) = 30 + .mass = 0.5f, .max_linear_velocity = {40, 40}, // sqrt(21^2 + 21^2) = 30 }); game_object1.add_component().set_script(); -- cgit v1.2.3 From 54d93519e90569a6f127da301e6e612ef89f2fc3 Mon Sep 17 00:00:00 2001 From: max-001 Date: Mon, 9 Dec 2024 11:29:11 +0100 Subject: Deleted unnecessary header --- src/example/AITest.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/example') diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 72e06cf..857eb94 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -1,5 +1,3 @@ -#include -#include #include #include #include -- cgit v1.2.3 From a4d76993e51ac4486fc313e868285eb7b4cb89f2 Mon Sep 17 00:00:00 2001 From: max-001 Date: Mon, 9 Dec 2024 12:04:26 +0100 Subject: Make format --- src/example/AITest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/example') diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 857eb94..0920fb2 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -57,7 +57,8 @@ public: ai.make_circle_path(1000, {0, -1000}, 1.5707, true); ai.make_circle_path(1000, {0, 1000}, 4.7124, false); game_object1.add_component(Rigidbody::Data{ - .mass = 0.5f, .max_linear_velocity = {40, 40}, // sqrt(21^2 + 21^2) = 30 + .mass = 0.5f, + .max_linear_velocity = {40, 40}, }); game_object1.add_component().set_script(); -- cgit v1.2.3 From a12986c0855e68ac027c2686223813c112960165 Mon Sep 17 00:00:00 2001 From: max-001 Date: Mon, 9 Dec 2024 13:32:18 +0100 Subject: Fixed merge issue --- src/crepe/api/Script.h | 1 - src/example/game.cpp | 4 ---- 2 files changed, 5 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index e351e6a..d99ab0e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -7,7 +7,6 @@ #include "../system/CollisionSystem.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "system/CollisionSystem.h" namespace crepe { diff --git a/src/example/game.cpp b/src/example/game.cpp index 2b4e46f..4239c15 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -66,10 +66,6 @@ class MyScript1 : public Script { //add collider switch break; } - case Keycode::Q: { - throw "Test"; - break; - } default: break; } -- cgit v1.2.3 From 857e408d6d3a8631b61ebd283b337819783f092c Mon Sep 17 00:00:00 2001 From: max-001 Date: Mon, 9 Dec 2024 14:14:03 +0100 Subject: Implemented oval path --- src/crepe/api/AI.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/crepe/api/AI.h | 14 ++++++++++++++ src/example/AITest.cpp | 4 ++-- 3 files changed, 62 insertions(+), 2 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp index 49f6b92..00e5b37 100644 --- a/src/crepe/api/AI.cpp +++ b/src/crepe/api/AI.cpp @@ -27,4 +27,50 @@ void AI::make_circle_path(float radius, vec2 center, float start_angle, bool clo } } +void AI::make_oval_path(float radius_x, float radius_y, vec2 center, float start_angle, + bool clockwise, float rotation) { + float max_radius = std::max(radius_x, radius_y); + // The step size is determined by the radius (step size is in radians) + float step = 400.0f / max_radius; + // Force at least 16 steps (in case of a small radius) + if (step > 2 * M_PI / 16) { + step = 2 * M_PI / 16; + } + // The path node distance is determined by the step size and the radius + path_node_distance = max_radius * step * 0.75f; + + auto rotate_point = [rotation](vec2 point, vec2 center) { + float s = sin(rotation); + float c = cos(rotation); + + // Translate point back to origin + point.x -= center.x; + point.y -= center.y; + + // Rotate point + float xnew = point.x * c - point.y * s; + float ynew = point.x * s + point.y * c; + + // Translate point back + point.x = xnew + center.x; + point.y = ynew + center.y; + + return point; + }; + + if (clockwise) { + for (float i = start_angle; i < 2 * M_PI + start_angle; i += step) { + vec2 point = {static_cast(center.x + radius_x * cos(i)), + static_cast(center.y + radius_y * sin(i))}; + path.push_back(rotate_point(point, center)); + } + } else { + for (float i = start_angle; i > start_angle - 2 * M_PI; i -= step) { + vec2 point = {static_cast(center.x + radius_x * cos(i)), + static_cast(center.y + radius_y * sin(i))}; + path.push_back(rotate_point(point, center)); + } + } +} + } // namespace crepe diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h index 0dccd5f..c95924d 100644 --- a/src/crepe/api/AI.h +++ b/src/crepe/api/AI.h @@ -81,6 +81,20 @@ public: */ void make_circle_path(float radius, vec2 center = {0, 0}, float start_angle = 0, bool clockwise = true); + /** + * \brief Make an oval path (for the path following behavior) + * + * \note The path is not relative to the entity's position (it is an absolute path) + * + * \param radius_x The x radius of the oval (in game units) + * \param radius_y The y radius of the oval (in game units) + * \param center The center of the oval (in game units) + * \param start_angle The start angle of the oval (in radians) + * \param clockwise The direction of the oval + * \param rotation The rotation of the oval (in radians) + */ + void make_oval_path(float radius_x, float radius_y, vec2 center = {0, 0}, + float start_angle = 0, bool clockwise = true, float rotation = 0); public: //! The maximum force that can be applied to the entity (higher values will make the entity adjust faster) diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 28537ed..f4efc9f 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -59,8 +59,8 @@ public: // ai.arrive_on(); // ai.flee_on(); ai.path_follow_on(); - ai.make_circle_path(1000, {0, -1000}, 1.5707, true); - ai.make_circle_path(1000, {0, 1000}, 4.7124, false); + 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::Data{ .mass = 0.1f, .max_linear_velocity = {40, 40}, -- cgit v1.2.3