aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-06 09:57:30 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-06 09:57:30 +0100
commit93893dbe710d864d5865f361f9a8a8f8f85b94f6 (patch)
treebd03b98d381f5a19d084dee71482a4fc73fb7337 /src
parentcbd7663b7f9cf7bf2b498f54b5d86c2ca37a5188 (diff)
Make format
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Rigidbody.h1
-rw-r--r--src/crepe/api/Script.h2
-rw-r--r--src/crepe/system/CollisionSystem.cpp40
-rw-r--r--src/crepe/system/CollisionSystem.h6
-rw-r--r--src/example/game.cpp98
-rw-r--r--src/test/CollisionTest.cpp6
-rw-r--r--src/test/Profiling.cpp6
7 files changed, 75 insertions, 84 deletions
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 <vector>
#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 <utility>
#include <variant>
+#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<CollisionInternal> all_colliders;
game_object_id_t id = 0;
ComponentManager & mgr = this->mediator.component_manager;
- RefVector<Rigidbody> rigidbodies
- = mgr.get_components_by_type<Rigidbody>();
+ RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>();
// 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<Transform>(id).front().get();
+ Transform & transform = mgr.get_components_by_id<Transform>(id).front().get();
// Check if the boxcollider is active and has the same id as the rigidbody.
- RefVector<BoxCollider> boxcolliders
- = mgr.get_components_by_type<BoxCollider>();
+ RefVector<BoxCollider> boxcolliders = mgr.get_components_by_type<BoxCollider>();
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<CollisionEvent>(
- data, info.this_collider.game_object_id);
+ emgr.trigger_event<CollisionEvent>(data, info.this_collider.game_object_id);
}
void CollisionSystem::static_collision_handler(CollisionInfo & info) {
@@ -389,14 +385,14 @@ CollisionSystem::gather_collisions(std::vector<CollisionInternal> & colliders) {
bool CollisionSystem::have_common_layer(const std::set<int> & layers1,
const std::set<int> & 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 <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"; }
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 <crepe/manager/ComponentManager.h>
-#include <crepe/manager/Mediator.h>
#include <crepe/api/Event.h>
-#include <crepe/manager/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/manager/Mediator.h>
#include <crepe/system/CollisionSystem.h>
#include <crepe/system/ScriptSystem.h>
#include <crepe/types.h>
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 <crepe/manager/ComponentManager.h>
#include <crepe/api/Event.h>
-#include <crepe/manager/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
#include <crepe/system/CollisionSystem.h>
#include <crepe/system/ScriptSystem.h>
#include <crepe/types.h>
@@ -162,7 +162,7 @@ TEST_F(Profiling, Profiling_2) {
.body_type = Rigidbody::BodyType::STATIC,
});
gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1});
-
+
gameobject.add_component<BehaviorScript>().set_script<TestScript>();
Color color(0, 0, 0, 0);
auto img = Texture("asset/texture/green_square.png");