Skip to content

Commit

Permalink
Player can grab weapons on the ground
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Sep 29, 2024
1 parent dbcab8c commit 71b9b10
Show file tree
Hide file tree
Showing 37 changed files with 528 additions and 92 deletions.
14 changes: 7 additions & 7 deletions Engine.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Root>
<Object class="EngineOptions">
<Property type="String" name="m_name" flags="NotVisible" value="EngineOptions"/>
<Property type="Uint32" name="m_uid" flags="Debug|Hexadecimal" value="1488493778"/>
<Property type="Uint32" name="m_originalUID" flags="Debug|Hexadecimal" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="Debug|Bitfield" value=""/>
<Property type="Uint32" name="m_uid" flags="" value="1488493778"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="String" name="m_gamePath" flags="IsFolder" value="projects/game"/>
<Property type="String" name="m_startWorld" flags="IsFile" value="data/Worlds/Game.world"/>
<Property type="Bool" name="m_showRigidBodies" flags="NotVisible" value="false"/>
<Property type="EnumFlagsU32" name="m_showRigidBodiesMask" flags="Bitfield|Optional" value="Sphere|Box|Capsule|Cylinder"/>
<Property type="EnumFlagsU32" name="m_showRigidBodiesMask" flags="" value="Sphere|Box|Capsule|Cylinder"/>
<Property type="Bool" name="m_mergeStaticBodies" value="true"/>
<Property type="Bool" name="m_useTimeScale" flags="NotVisible" value="false"/>
<Property type="Float" name="m_timeScale" flags="HasRange|Optional" value="1"/>
<Property type="Float" name="m_timeScale" flags="" value="1"/>
<Property type="Bool" name="m_useMaxDT" flags="NotVisible" value="true"/>
<Property type="Float" name="m_maxDT" flags="HasRange|Optional" value="16.666666"/>
<Property type="Float" name="m_maxDT" flags="" value="16.666666"/>
<Property type="Bool" name="m_useFixedDT" flags="NotVisible" value="false"/>
<Property type="Float" name="m_fixedDT" flags="HasRange|Optional" value="16.666666"/>
<Property type="Float" name="m_fixedDT" flags="" value="16.666666"/>
<Property type="Bool" name="m_animationJobs" value="true"/>
</Object>
</Root>
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ You will also need if you want to build the ARM64EC versions:

![Screenshot](doc/img/vsbuild.png)

You can change the Solution Platform in Visual Studio's command line to toggle between the DirectX12 and the Vulkan renderers.
Use the 'Configuration' combo to select the target and graphics API you want to use:

![Screenshot](doc/img/SolutionPlatformName2.png)

Also don't forget to set the *working directory* to **$(SolutionDir)**.

![Screenshot](doc/img/SolutionPlatformName2.png)

## SDKs

Expand Down
Binary file modified data/Meshes/Baseball/Bat.fbx
Binary file not shown.
30 changes: 24 additions & 6 deletions data/Prefabs/Bat.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<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.5" Tw="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="0.99999797" Iy="0" Iz="0" Iw="0" Jx="0" Jy="0.99999529" Jz="0" Jw="0" Kx="0" Ky="0" Kz="0.99999779" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="ObjectPtrVector" name="m_components">
<Object class="PhysicsBodyComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New PhysicsBodyComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="1535866126"/>
<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="ObjectPtr" name="m_bodyDesc" flags="Flatten">
<Object class="PhysicsBodyDesc">
Expand All @@ -42,7 +42,7 @@
<Property type="EnumU8" name="m_motion" value="Dynamic"/>
<Property type="EnumU8" name="m_motionQuality" value="Continuous"/>
<Property type="Bool" name="m_overrideMass" flags="NotVisible" value="true"/>
<Property type="Float" name="m_mass" flags="Optional" value="10"/>
<Property type="Float" name="m_mass" flags="Optional" value="6.8722343"/>
<Property type="Float" name="m_friction" flags="HasRange" value="0.5"/>
<Property type="Float" name="m_restitution" flags="HasRange" value="0.1"/>
</Object>
Expand All @@ -62,17 +62,35 @@
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="Float3" name="m_rotation" flags="EulerAngle" x="0" y="0" z="0"/>
<Property type="Float3" name="m_offset" x="0" y="0" z="0.0099999998"/>
<Property type="Float3" name="m_offset" x="0" y="0" z="0.41"/>
<Property type="Float" name="m_radius" flags="HasRange" value="0.050000001"/>
<Property type="Float" name="m_height" flags="HasRange" value="1.175"/>
<Property type="Float" name="m_height" flags="HasRange" value="0.875"/>
</Object>
</Property>
</Object>
<Object class="WeaponBehaviour">
<Property type="String" name="m_name" flags="NotVisible" value="New WeaponBehaviour"/>
<Property type="Uint32" name="m_uid" flags="" value="684624813"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
</Object>
<Object class="AttachToNodeComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New AttachToNodeComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="30838399"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Bool" name="m_useTarget" flags="NotVisible" value="false"/>
<Property type="ObjectHandle" name="m_target" flags="Optional" value="0"/>
<Property type="String" name="m_boneName" value="mixamorig:RightHand"/>
<Property type="Float4x4" name="m_transform" flags="Flatten" Ix="-0.17364785" Iy="0.98480582" Iz="0" Iw="0" Jx="0.17100908" Jy="0.030153519" Jz="0.98480332" Jw="0" Kx="0.9698444" Ky="0.17100975" Kz="-0.17364761" Kw="0" Tx="-0.059999999" Ty="0.090000004" Tz="0.050000001" Tw="1"/>
</Object>
<Object class="MeshComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New MeshComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="4157073915"/>
<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="Bool" name="m_displayBones" value="false"/>
<Property type="Resource" name="m_meshResource" flags="Resource">
Expand Down
Binary file modified doc/img/SolutionPlatformName2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ class PlayerBehaviour final : public CharacterBehaviour
vg::core::u8 m_controllerIndex = 0;
vg::core::float4 m_customColor = vg::core::float4(1, 1, 1, 1);
vg::core::ObjectHandle m_UI;
ItemBehaviour * m_rightHandItem = nullptr;
};
73 changes: 63 additions & 10 deletions projects/game/src/Behaviour/Character/Player/PlayerBehaviour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include "PlayerBehaviour.h"
#include "Game.h"
#include "Behaviour/Item/Ball/BallBehaviour.h"
#include "Behaviour/Item/Weapon/WeaponBehaviour.h"
#include "engine/ICharacterControllerComponent.h"
#include "engine/IUITextComponent.h"
#include "engine/IPhysicsBodyComponent.h"
#include "engine/IPhysicsShapeComponent.h"
#include "engine/ISoundComponent.h"
#include "editor/Editor_Consts.h"
#include "core/GameObject/GameObject.h"
Expand Down Expand Up @@ -80,7 +82,8 @@ void PlayerBehaviour::FixedUpdate(const Context & _context)
if (_context.m_world->IsPlaying() && !_context.m_world->IsPaused()) // TODO: Use context intead?
{
IInput & input = Game::Input();
IAnimationComponent * animationComponent = GetGameObject()->GetComponentT<IAnimationComponent>();
IGameObject * playerGO = GetGameObject();
IAnimationComponent * animationComponent = playerGO->GetComponentT<IAnimationComponent>();

switch (m_state)
{
Expand Down Expand Up @@ -108,7 +111,7 @@ void PlayerBehaviour::FixedUpdate(const Context & _context)
float3 translation = float3(0, 0, 0);
const float joyDeadZone = 0.15f;

float runAmount = input.GetRightTrigger(joyID);
float runAmount = input.GetJoyRightTrigger(joyID);
if (runAmount > joyDeadZone)
runAmount = lerp(0.0f, 1.0f, saturate(runAmount - joyDeadZone) / (1.0f - joyDeadZone));
else
Expand Down Expand Up @@ -143,7 +146,7 @@ void PlayerBehaviour::FixedUpdate(const Context & _context)
m_state = CharacterState::Idle;
}

vg::engine::ICharacterControllerComponent * charaController = GetGameObject()->GetComponentT<vg::engine::ICharacterControllerComponent>();
vg::engine::ICharacterControllerComponent * charaController = playerGO->GetComponentT<vg::engine::ICharacterControllerComponent>();

bool jump = false;
if (input.IsJoyButtonJustPressed(joyID, JoyButton::A))
Expand All @@ -165,30 +168,80 @@ void PlayerBehaviour::FixedUpdate(const Context & _context)

if (input.IsJoyButtonJustPressed(joyID, JoyButton::X))
{
// kick
const float3 playerPos = playerGO->GetGlobalMatrix()[3].xyz;

const float pickupDist = 1.0f;

// Kick close balls
auto & balls = Game::get()->getBalls();
for (uint i = 0; i < balls.size(); ++i)
{
auto & ball = balls[i];
auto * ballBehaviour = balls[i];
auto * ballGO = ballBehaviour->GetGameObject();

float3 delta = ball->GetGameObject()->GetGlobalMatrix()[3].xyz - this->GetGameObject()->GetGlobalMatrix()[3].xyz;
float3 delta = ballGO->GetGlobalMatrix()[3].xyz - playerPos;
float dist = length(delta);
if (dist < 1)
if (dist < pickupDist)
{
float3 dir = normalize(delta);

if (auto * physicsBody = ball->GetGameObject()->GetComponentT<vg::engine::IPhysicsBodyComponent>())
ballBehaviour->SetOwner(ballGO);
if (auto * physicsBody = ballGO->GetComponentT<vg::engine::IPhysicsBodyComponent>())
{
//VG_INFO("[Player] Kick ball %u (dist = %.2f, dir = %.2f, %.2f, %.2f)", i, dist, dir.x, dir.y, dir.z);
float kickStrength = lerp(250.0f, 400.0f, runAmount);
physicsBody->AddImpulse(dir * kickStrength);

// Play sound
if (auto * soundComponent = ball->GetGameObject()->GetComponentT<vg::engine::ISoundComponent>())
if (auto * soundComponent = ballGO->GetComponentT<vg::engine::ISoundComponent>())
soundComponent->Play(0);
}
}
}

// Pick closest weapon
auto & weapons = Game::get()->getWeapons();
float closestDist = pickupDist+1;
IGameObject * closestWeaponGO = nullptr;
WeaponBehaviour * closestWeaponBehaviour = nullptr;
for (uint i = 0; i < weapons.size(); ++i)
{
auto * weaponBehaviour = weapons[i];
IGameObject * weaponGO = weaponBehaviour->GetGameObject();
float3 delta = weaponGO->GetGlobalMatrix()[3].xyz - playerPos;
float dist = length(delta);
if (dist < closestDist && dist < pickupDist && !weaponBehaviour->CanPick())
{
closestDist = dist;
closestWeaponBehaviour = weaponBehaviour;
closestWeaponGO = closestWeaponBehaviour->GetGameObject();
}
}

if (closestWeaponBehaviour)
{
closestWeaponBehaviour->SetOwner(playerGO);
m_rightHandItem = closestWeaponBehaviour;

if (auto * physicsShapeComponent = closestWeaponGO->GetComponentT<vg::engine::IPhysicsShapeComponent>())
physicsShapeComponent->SetComponentFlags(vg::core::ComponentFlags::Enabled, false);

}
}

if (input.GetJoyLeftTrigger(joyID) > joyDeadZone)
{
if (nullptr != m_rightHandItem)
{
// drop
m_rightHandItem->SetOwner(nullptr);

m_rightHandItem->GetGameObject()->SetGlobalMatrix(this->GetGameObject()->GetGlobalMatrix());

if (auto * physicsShapeComponent = m_rightHandItem->GetGameObject()->GetComponentT<vg::engine::IPhysicsShapeComponent>())
physicsShapeComponent->SetComponentFlags(vg::core::ComponentFlags::Enabled, true);

m_rightHandItem = nullptr;
}
}

if (charaController)
Expand Down
1 change: 1 addition & 0 deletions projects/game/src/Behaviour/Item/Ball/BallBehaviour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void BallBehaviour::OnCollisionEnter(vg::core::IGameObject * _other)
if (player->isActive())
{
m_lastOwnerCharacter = player;
SetOwner(player->GetGameObject());
GetGameObject()->SetColor(player->getCustomColor());
}
}
Expand Down
15 changes: 15 additions & 0 deletions projects/game/src/Behaviour/Item/ItemBehaviour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ItemBehaviour.h"

#include "Ball/BallBehaviour.hpp"
#include "Weapon/WeaponBehaviour.hpp"

VG_REGISTER_ABSTRACT_CLASS(ItemBehaviour, "ItemBehaviour");

Expand All @@ -24,6 +25,8 @@ bool ItemBehaviour::registerProperties(IClassDesc & _desc)
{
super::registerProperties(_desc);

registerPropertyEx(ItemBehaviour, m_owner, "Owner", vg::core::PropertyFlags::NotSaved);

return true;
}

Expand All @@ -39,4 +42,16 @@ void ItemBehaviour::OnStop()
{
Game::get()->removeItem(m_itemType, this);
super::OnStop();
}

//--------------------------------------------------------------------------------------
void ItemBehaviour::SetOwner(vg::core::IGameObject * _object)
{
m_owner.set(_object);
}

//--------------------------------------------------------------------------------------
bool ItemBehaviour::CanPick() const
{
return nullptr != m_owner.getObject();
}
11 changes: 8 additions & 3 deletions projects/game/src/Behaviour/Item/ItemBehaviour.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "game_consts.h"
#include "core/Object/ObjectHandle.h"
#include "core/Component/Behaviour/Behaviour.h"

class ItemBehaviour : public vg::core::Behaviour
Expand All @@ -10,9 +11,13 @@ class ItemBehaviour : public vg::core::Behaviour
ItemBehaviour(const vg::core::string & _name, vg::core::IObject * _parent, ItemType _itemType);
~ItemBehaviour();

void OnPlay() override;
void OnStop() override;
void OnPlay() override;
void OnStop() override;

virtual void SetOwner(vg::core::IGameObject * _object);
virtual bool CanPick() const;

private:
ItemType m_itemType;
ItemType m_itemType;
vg::core::ObjectHandle m_owner;
};
17 changes: 17 additions & 0 deletions projects/game/src/Behaviour/Item/Weapon/WeaponBehaviour.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "Behaviour/Item/ItemBehaviour.h"

class CharacterBehaviour;

class WeaponBehaviour final : public ItemBehaviour
{
public:
VG_CLASS_DECL(WeaponBehaviour, ItemBehaviour);

WeaponBehaviour(const vg::core::string & _name, vg::core::IObject * _parent);
~WeaponBehaviour();

void OnPlay() final override;
void Update(const Context & _context) override;
void SetOwner(vg::core::IGameObject * _object) final override;
};
57 changes: 57 additions & 0 deletions projects/game/src/Behaviour/Item/Weapon/WeaponBehaviour.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "WeaponBehaviour.h"
#include "editor/Editor_Consts.h"
#include "Game.h"
#include "Behaviour/Character/Player/PlayerBehaviour.h"
#include "engine/IAttachToNodeComponent.h"

using namespace vg::core;
using namespace vg::engine;

VG_REGISTER_COMPONENT_CLASS(WeaponBehaviour, "Weapon", "Game", "A weapon the player can pick", vg::editor::style::icon::Script, 0);

//--------------------------------------------------------------------------------------
WeaponBehaviour::WeaponBehaviour(const string & _name, IObject * _parent) :
super(_name, _parent, ItemType::Weapon)
{

}

//--------------------------------------------------------------------------------------
WeaponBehaviour::~WeaponBehaviour()
{

}

//--------------------------------------------------------------------------------------
bool WeaponBehaviour::registerProperties(IClassDesc & _desc)
{
super::registerProperties(_desc);

return true;
}

//--------------------------------------------------------------------------------------
void WeaponBehaviour::OnPlay()
{
super::OnPlay();
}

//--------------------------------------------------------------------------------------
void WeaponBehaviour::Update(const Context & _context)
{

}

//--------------------------------------------------------------------------------------
void WeaponBehaviour::SetOwner(vg::core::IGameObject * _object)
{
super::SetOwner(_object);

if (auto * attachToNode = GetGameObject()->GetComponentT<vg::engine::IAttachToNodeComponent>())
{
attachToNode->SetUseTarget(true);
attachToNode->SetTarget(_object);
}
}


Loading

0 comments on commit 71b9b10

Please sign in to comment.