Skip to content

Commit

Permalink
Take damage with ball
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Oct 16, 2024
1 parent b4e8992 commit 96c4c55
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 15 deletions.
13 changes: 11 additions & 2 deletions data/Prefabs/Football.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="EnumFlagsU64" name="m_tags" flags="Bitfield" value=""/>
<Property type="ObjectPtrVector" name="m_components"/>
<Property type="ObjectPtrVector" name="m_children" flags="NotVisible">
<Object class="GameObject">
Expand All @@ -23,13 +24,17 @@
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="EnumFlagsU64" name="m_tags" flags="Bitfield" value=""/>
<Property type="ObjectPtrVector" name="m_components">
<Object class="PhysicsBodyComponent">
<Property type="String" name="m_name" flags="NotVisible" value="RigidBodyComponent #1"/>
<Property type="Uint32" name="m_uid" flags="" value="202547355"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="EnumU8" name="m_category" value="Default"/>
<Property type="Bool" name="m_useCollisionMask" flags="NotVisible" value="false"/>
<Property type="EnumFlagsU64" name="m_collisionMask" flags="" value=""/>
<Property type="ObjectPtr" name="m_bodyDesc" flags="Flatten">
<Object class="PhysicsBodyDesc">
<Property type="String" name="m_name" flags="NotVisible" value="RigidBodyDesc #4"/>
Expand All @@ -47,6 +52,7 @@
<Property type="Float" name="m_restitution" flags="HasRange" value="0.75"/>
</Object>
</Property>
<Property type="Float3" name="m_velocity" flags="Debug" x="0" y="0" z="0"/>
</Object>
<Object class="PhysicsShapeComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New PhysicsShapeComponent"/>
Expand All @@ -71,14 +77,16 @@
<Property type="String" name="m_name" flags="NotVisible" value="New BallBehaviour"/>
<Property type="Uint32" name="m_uid" flags="" value="853441235"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float" name="m_damage" flags="HasRange" value="5"/>
<Property type="Float" name="m_push" flags="HasRange" value="1"/>
</Object>
<Object class="SoundComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New SoundComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="540222898"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Object" name="m_sounds">
<Object class="SoundResourceList">
Expand All @@ -95,6 +103,7 @@
<Property type="String" name="m_resourcePath" flags="NotVisible" value="data/Sounds/Soccer/shoot.wav"/>
<Property type="EnumFlagsU32" name="m_soundSettings.m_flags" flags="Bitfield" value=""/>
<Property type="Float" name="m_soundSettings.m_volume" flags="HasRange" value="1"/>
<Property type="Float" name="m_soundSettings.m_delay" flags="HasRange" value="0"/>
</Object>
</Property>
</Object>
Expand Down
26 changes: 18 additions & 8 deletions projects/game/src/Behaviour/Character/CharacterBehaviour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -277,10 +290,7 @@ bool CharacterBehaviour::takeHit(CharacterBehaviour * _attacker, ItemBehaviour *
if (push > 0)
{
if (auto * characterController = go->GetComponentT<ICharacterControllerComponent>())
{
float3 delta = normalize(go->GetGlobalMatrix()[3].xyz - attackerGO->GetGlobalMatrix()[3].xyz) * push;
characterController->SetVelocity(characterController->GetVelocity() + delta);
}
characterController->SetVelocity(characterController->GetVelocity() + hitDir * push);
}
}

Expand Down
6 changes: 5 additions & 1 deletion projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<EnemyBehaviour>())
{
enemy->takeHit(nullptr, this);
}
}

//--------------------------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions src/editor/ImGui/Window/ImGuiWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace vg::engine

registerPropertyObjectPtrEx(PhysicsBodyComponent, m_bodyDesc, "Body", PropertyFlags::Flatten);

registerPropertyEx(PhysicsBodyComponent, m_velocity, "Velocity", PropertyFlags::Debug);

return true;
}

Expand Down Expand Up @@ -102,6 +104,11 @@ namespace vg::engine
}
}
}

if (m_body)
m_velocity = m_body->GetVelocity();
else
m_velocity = (core::float3)0.0f;
}

//--------------------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/physics/Body/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
3 changes: 2 additions & 1 deletion src/physics/Body/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/physics/IBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

0 comments on commit 96c4c55

Please sign in to comment.