From 96c4c55dc81b721860776190ea715b1565a006a7 Mon Sep 17 00:00:00 2001 From: Benualdo Date: Wed, 16 Oct 2024 22:37:22 +0200 Subject: [PATCH] Take damage with ball --- data/Prefabs/Football.prefab | 13 ++++++++-- .../Character/CharacterBehaviour.cpp | 26 +++++++++++++------ .../src/Behaviour/Item/Ball/BallBehaviour.hpp | 6 ++++- src/editor/ImGui/Window/ImGuiWindow.cpp | 9 ++++--- .../Object/Body/PhysicsBodyComponent.h | 1 + .../Object/Body/PhysicsBodyComponent.hpp | 7 +++++ src/physics/Body/Body.cpp | 6 +++++ src/physics/Body/Body.h | 3 ++- src/physics/IBody.h | 1 + 9 files changed, 57 insertions(+), 15 deletions(-) diff --git a/data/Prefabs/Football.prefab b/data/Prefabs/Football.prefab index b44d0d3ef..3dc71520d 100644 --- a/data/Prefabs/Football.prefab +++ b/data/Prefabs/Football.prefab @@ -13,6 +13,7 @@ + @@ -23,6 +24,7 @@ + @@ -30,6 +32,9 @@ + + + @@ -47,6 +52,7 @@ + @@ -71,14 +77,16 @@ - + + + - + @@ -95,6 +103,7 @@ + diff --git a/projects/game/src/Behaviour/Character/CharacterBehaviour.cpp b/projects/game/src/Behaviour/Character/CharacterBehaviour.cpp index 42340a033..0abc8c0d8 100644 --- a/projects/game/src/Behaviour/Character/CharacterBehaviour.cpp +++ b/projects/game/src/Behaviour/Character/CharacterBehaviour.cpp @@ -246,12 +246,25 @@ bool CharacterBehaviour::takeHit(CharacterBehaviour * _attacker, ItemBehaviour * return false; auto * go = GetGameObject(); - auto attackerGO = _attacker->GetGameObject(); + float3 hitDir; - if (_weapon) - VG_WARNING("[Enemy] \"%s\" was hit by \"%s\" with \"%s\"", go->GetName().c_str(), attackerGO->GetName().c_str(), _weapon->GetGameObject()->GetName().c_str()); + if (nullptr != _attacker) + { + auto attackerGO = _attacker->GetGameObject(); + hitDir = normalize(go->GetGlobalMatrix()[3].xyz - attackerGO->GetGlobalMatrix()[3].xyz); + + if (_weapon) + VG_INFO("[Character] \"%s\" was hit by \"%s\" with \"%s\"", go->GetName().c_str(), attackerGO->GetName().c_str(), _weapon->GetGameObject()->GetName().c_str()); + else + VG_INFO("[Character] \"%s\" was hit by \"%s\"", go->GetName().c_str(), attackerGO->GetName().c_str()); + } else - VG_WARNING("[Enemy] \"%s\" was hit by \"%s\"", go->GetName().c_str(), attackerGO->GetName().c_str()); + { + auto weaponGO = _weapon->GetGameObject(); + hitDir = normalize(go->GetGlobalMatrix()[3].xyz - weaponGO->GetGlobalMatrix()[3].xyz); + + VG_INFO("[Character] \"%s\" was hit by \"%s\"", go->GetName().c_str(), weaponGO->GetName().c_str()); + } float damage = _weapon ? _weapon->getDamage() : 0.0f; m_hp = max(0.0f, m_hp - damage); @@ -277,10 +290,7 @@ bool CharacterBehaviour::takeHit(CharacterBehaviour * _attacker, ItemBehaviour * if (push > 0) { if (auto * characterController = go->GetComponentT()) - { - float3 delta = normalize(go->GetGlobalMatrix()[3].xyz - attackerGO->GetGlobalMatrix()[3].xyz) * push; - characterController->SetVelocity(characterController->GetVelocity() + delta); - } + characterController->SetVelocity(characterController->GetVelocity() + hitDir * push); } } diff --git a/projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp b/projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp index 69585416c..74fe55c5b 100644 --- a/projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp +++ b/projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp @@ -1,6 +1,7 @@ #include "BallBehaviour.h" #include "Game.h" #include "Behaviour/Character/Player/PlayerBehaviour.h" +#include "Behaviour/Character/Enemy/EnemyBehaviour.h" using namespace vg::core; using namespace vg::engine; @@ -46,9 +47,12 @@ void BallBehaviour::OnCollisionEnter(vg::core::IGameObject * _other) { m_lastOwnerCharacter = player; SetOwner(player->GetGameObject()); - GetGameObject()->SetColor(player->getCustomColor()); } } + else if (auto * enemy = _other->GetComponentT()) + { + enemy->takeHit(nullptr, this); + } } //-------------------------------------------------------------------------------------- diff --git a/src/editor/ImGui/Window/ImGuiWindow.cpp b/src/editor/ImGui/Window/ImGuiWindow.cpp index 0d085e9de..d636a3a18 100644 --- a/src/editor/ImGui/Window/ImGuiWindow.cpp +++ b/src/editor/ImGui/Window/ImGuiWindow.cpp @@ -933,9 +933,12 @@ namespace vg::editor if (asBool(PropertyFlags::EulerAngle & flags)) return 5.0f; - //if (asBool(PropertyFlags::HasRange & _prop->getFlags())) - // return (_prop->getRange().y - _prop->getRange().x) / 1000.0f; - //else + if (asBool(PropertyFlags::HasRange & flags)) + { + const auto range = _prop->GetRange(); + return (range.y - range.x) / 1000.0f; + } + else return 0.05f; } diff --git a/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.h b/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.h index ba10c5008..36d499e91 100644 --- a/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.h +++ b/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.h @@ -56,5 +56,6 @@ namespace vg::engine private: physics::IBodyDesc * m_bodyDesc = nullptr; physics::IBody * m_body = nullptr; + core::float3 m_velocity = (core::float3)0.0f; }; } \ No newline at end of file diff --git a/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.hpp b/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.hpp index 732ce048a..c51f889f4 100644 --- a/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.hpp +++ b/src/engine/Component/Physics/Object/Body/PhysicsBodyComponent.hpp @@ -24,6 +24,8 @@ namespace vg::engine registerPropertyObjectPtrEx(PhysicsBodyComponent, m_bodyDesc, "Body", PropertyFlags::Flatten); + registerPropertyEx(PhysicsBodyComponent, m_velocity, "Velocity", PropertyFlags::Debug); + return true; } @@ -102,6 +104,11 @@ namespace vg::engine } } } + + if (m_body) + m_velocity = m_body->GetVelocity(); + else + m_velocity = (core::float3)0.0f; } //-------------------------------------------------------------------------------------- diff --git a/src/physics/Body/Body.cpp b/src/physics/Body/Body.cpp index 6dbd2bbc8..eb31ddb4c 100644 --- a/src/physics/Body/Body.cpp +++ b/src/physics/Body/Body.cpp @@ -156,4 +156,10 @@ namespace vg::physics { m_physicsWorld->getBodyInterface().AddImpulse(m_bodyID, getJoltVec3(_impulse)); } + + //-------------------------------------------------------------------------------------- + core::float3 Body::GetVelocity() const + { + return fromJoltVec3(m_physicsWorld->getBodyInterface().GetLinearVelocity(m_bodyID)); + } } \ No newline at end of file diff --git a/src/physics/Body/Body.h b/src/physics/Body/Body.h index b9b49366d..6c0973cdb 100644 --- a/src/physics/Body/Body.h +++ b/src/physics/Body/Body.h @@ -18,7 +18,7 @@ namespace vg::physics class Shape; class PhysicsWorld; - class Body : public IBody + class Body final : public IBody { public: VG_CLASS_DECL(Body, IBody); @@ -35,6 +35,7 @@ namespace vg::physics void SetMatrix (core::float4x4 _world) final override; void AddImpulse (const core::float3 & _impulse) final override; + core::float3 GetVelocity () const final override; private: void createBodyFromJoltShape (PhysicsWorld * _physicsWorld, const PhysicsBodyDesc * _bodyDesc, JPH::Shape * _joltShape, const core::float4x4 & _matrix, const core::string & _name, core::IObject * _parent); diff --git a/src/physics/IBody.h b/src/physics/IBody.h index 0be7efbbc..6d5cdb018 100644 --- a/src/physics/IBody.h +++ b/src/physics/IBody.h @@ -43,5 +43,6 @@ namespace vg::physics virtual void SetMatrix (core::float4x4 _world) = 0; virtual void AddImpulse (const core::float3 & _impulse) = 0; + virtual core::float3 GetVelocity () const = 0; }; } \ No newline at end of file