AttachPlayerWeaponComponent.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <AzCore/Asset/AssetSerializer.h>
  8. #include <AzCore/Math/Transform.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  12. #include <Components/AttachPlayerWeaponComponent.h>
  13. #include <LmbrCentral/Animation/AttachmentComponentBus.h>
  14. namespace MultiplayerSample
  15. {
  16. void AttachPlayerWeaponComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serializeContext->Class<AttachPlayerWeaponComponent, AZ::Component>()
  21. ->Field( "WeaponAsset", &AttachPlayerWeaponComponent::m_weaponAsset)
  22. ->Field( "Bone To Attach To", &AttachPlayerWeaponComponent::m_boneToAttachTo)
  23. ->Field( "Transform", &AttachPlayerWeaponComponent::m_attachmentTransform)
  24. ->Version(1);
  25. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  26. {
  27. using namespace AZ::Edit;
  28. editContext->Class<AttachPlayerWeaponComponent>("AttachPlayerWeaponComponent",
  29. "Spawns a non-network weapon prefab and attaches it to the player entity.")
  30. ->ClassElement(ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample")
  32. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  33. ->DataElement(AZ::Edit::UIHandlers::Default, &AttachPlayerWeaponComponent::m_weaponAsset, "WeaponAsset",
  34. "non-networked weapon prefab to spawn")
  35. ->DataElement(AZ::Edit::UIHandlers::Default, &AttachPlayerWeaponComponent::m_boneToAttachTo, "Bone To Attach To",
  36. "The bone on the player actor's skeleton to attach the weapon to")
  37. ->DataElement(AZ::Edit::UIHandlers::Default, &AttachPlayerWeaponComponent::m_attachmentTransform, "Transform",
  38. "Relative transform of the attachment to the bone")
  39. ;
  40. }
  41. }
  42. }
  43. void AttachPlayerWeaponComponent::Activate()
  44. {
  45. m_weaponTicket = AZStd::make_shared<AzFramework::EntitySpawnTicket>(m_weaponAsset);
  46. auto onSpawnedCallback = [this]([[maybe_unused]] AzFramework::EntitySpawnTicket::Id ticketId, AzFramework::SpawnableConstEntityContainerView view)
  47. {
  48. for (const AZ::Entity* entity : view)
  49. {
  50. LmbrCentral::AttachmentComponentRequestBus::Event(entity->GetId(),
  51. &LmbrCentral::AttachmentComponentRequestBus::Events::Attach, GetEntityId(), m_boneToAttachTo.c_str(), m_attachmentTransform);
  52. }
  53. };
  54. AZ_Assert(m_weaponTicket->IsValid(), "Unable to instantiate weapon's spawnable asset");
  55. if (m_weaponTicket->IsValid())
  56. {
  57. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  58. optionalArgs.m_completionCallback = AZStd::move(onSpawnedCallback);
  59. AzFramework::SpawnableEntitiesInterface::Get()->SpawnAllEntities(*m_weaponTicket, AZStd::move(optionalArgs));
  60. }
  61. }
  62. void AttachPlayerWeaponComponent::Deactivate()
  63. {
  64. m_weaponTicket.reset();
  65. }
  66. }