From 1bfd582b7b7f762011f5f4b7f84e180cf20e9046 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 19:35:19 +0100 Subject: shielded mediator --- src/example/game.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/example') diff --git a/src/example/game.cpp b/src/example/game.cpp index 5361f3a..16fe18f 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -160,15 +160,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::Data{ .mass = 0, @@ -196,7 +194,7 @@ public: .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::Data{ .mass = 1, @@ -228,7 +226,7 @@ public: .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::Data{ .mass = 1, -- cgit v1.2.3 From e7d9c28c509588d627169568776d40fc5752698f Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:27:33 +0100 Subject: multiple bug/feature fixes --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/system/CollisionSystem.cpp | 59 +++++++++++++++++++++++++----------- src/example/game.cpp | 23 ++++++++------ 3 files changed, 56 insertions(+), 28 deletions(-) (limited to 'src/example') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index a76c167..1baa21d 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -35,9 +35,9 @@ void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { // TODO: retrieve EventManager from direct member after singleton refactor + this->get_system().update(); EventManager & ev = this->mediator.event_manager; ev.dispatch_events(); - this->get_system().update(); this->get_system().update(); this->get_system().update(); } diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 44a0431..5c49983 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -192,13 +192,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal resolution_direction = Direction::BOTH; } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; - if (data1.rigidbody.data.linear_velocity.y != 0) - resolution.y = data1.rigidbody.data.linear_velocity.y + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.y != 0 && data1.rigidbody.data.linear_velocity.x != 0) + resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; - if (data1.rigidbody.data.linear_velocity.x != 0) - resolution.x = data1.rigidbody.data.linear_velocity.x + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -314,28 +316,51 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Move object back using calculate move back value info.this_transform.position += info.resolution; - // If bounce is enabled mirror velocity - if (info.this_rigidbody.data.elastisity_coefficient > 0) { - if (info.resolution_direction == Direction::BOTH) { - info.this_rigidbody.data.linear_velocity.y + switch (info.resolution_direction) { + case Direction::BOTH: + //bounce + if(info.this_rigidbody.data.elastisity_coefficient > 0){ + info.this_rigidbody.data.linear_velocity.y = -info.this_rigidbody.data.linear_velocity.y * info.this_rigidbody.data.elastisity_coefficient; info.this_rigidbody.data.linear_velocity.x = -info.this_rigidbody.data.linear_velocity.x * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::Y_DIRECTION) { - info.this_rigidbody.data.linear_velocity.y + } + //stop movement + else { + info.this_rigidbody.data.linear_velocity = {0, 0}; + } + break; + case Direction::Y_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.y = -info.this_rigidbody.data.linear_velocity.y * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::X_DIRECTION) { - info.this_rigidbody.data.linear_velocity.x + } + // Stop movement + else{ + info.this_rigidbody.data.linear_velocity.y = 0; + info.this_transform.position.x -= info.resolution.x; + } + break; + case Direction::X_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.x = -info.this_rigidbody.data.linear_velocity.x * info.this_rigidbody.data.elastisity_coefficient; - } - } - // Stop movement if bounce is disabled - else { - info.this_rigidbody.data.linear_velocity = {0, 0}; + } + // Stop movement + else{ + info.this_rigidbody.data.linear_velocity.x = 0; + info.this_transform.position.y -= info.resolution.y; + } + break; + case Direction::NONE: + // Not possible + break; } } diff --git a/src/example/game.cpp b/src/example/game.cpp index 4239c15..af87647 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -29,23 +29,23 @@ class MyScript1 : public Script { Log::logf("Box script keypressed()"); switch (test.key) { case Keycode::A: { - Transform & tf = this->get_component(); - tf.position.x -= 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.x -= 1 ; break; } case Keycode::W: { - Transform & tf = this->get_component(); - tf.position.y -= 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.y -= 1 ; break; } case Keycode::S: { - Transform & tf = this->get_component(); - tf.position.y += 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.y += 1 ; break; } case Keycode::D: { - Transform & tf = this->get_component(); - tf.position.x += 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.x += 1 ; break; } case Keycode::E: { @@ -80,7 +80,10 @@ class MyScript1 : public Script { [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } void update() { - // Retrieve component from the same GameObject this script is on + Rigidbody & tf = this->get_component(); + Log::logf("linear_velocity.x {}",tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}",tf.data.linear_velocity.y); + // tf.data.linear_velocity = {0,0}; } }; @@ -194,7 +197,7 @@ public: .mass = 1, .gravity_scale = 0, .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {0, 0}, + .linear_velocity = {0, 1}, .constraints = {0, 0, 0}, .elastisity_coefficient = 1, .offset = {0, 0}, -- cgit v1.2.3 From fea0c2f0f96e093962c5faf3643176e856b1e8f6 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:42:44 +0100 Subject: make format --- src/crepe/system/CollisionSystem.cpp | 30 ++++++++++++++++-------------- src/example/game.cpp | 12 ++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) (limited to 'src/example') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 5c49983..ab9b1aa 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -193,13 +193,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.y != 0 && data1.rigidbody.data.linear_velocity.x != 0) + if (data1.rigidbody.data.linear_velocity.y != 0 + && data1.rigidbody.data.linear_velocity.x != 0) resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + if (data1.rigidbody.data.linear_velocity.x != 0 + && data1.rigidbody.data.linear_velocity.y != 0) resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -319,13 +321,13 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { switch (info.resolution_direction) { case Direction::BOTH: //bounce - if(info.this_rigidbody.data.elastisity_coefficient > 0){ + if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; - info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; + info.this_rigidbody.data.linear_velocity.x + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; } //stop movement else { @@ -336,11 +338,11 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; } // Stop movement - else{ + else { info.this_rigidbody.data.linear_velocity.y = 0; info.this_transform.position.x -= info.resolution.x; } @@ -349,11 +351,11 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; } // Stop movement - else{ + else { info.this_rigidbody.data.linear_velocity.x = 0; info.this_transform.position.y -= info.resolution.y; } diff --git a/src/example/game.cpp b/src/example/game.cpp index af87647..dd5b968 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -30,22 +30,22 @@ class MyScript1 : public Script { switch (test.key) { case Keycode::A: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.x -= 1 ; + tf.data.linear_velocity.x -= 1; break; } case Keycode::W: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.y -= 1 ; + tf.data.linear_velocity.y -= 1; break; } case Keycode::S: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.y += 1 ; + tf.data.linear_velocity.y += 1; break; } case Keycode::D: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.x += 1 ; + tf.data.linear_velocity.x += 1; break; } case Keycode::E: { @@ -81,8 +81,8 @@ class MyScript1 : public Script { } void update() { Rigidbody & tf = this->get_component(); - Log::logf("linear_velocity.x {}",tf.data.linear_velocity.x); - Log::logf("linear_velocity.y {}",tf.data.linear_velocity.y); + Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); // tf.data.linear_velocity = {0,0}; } }; -- cgit v1.2.3