AttachPlayerWeaponComponent.cpp 3.4 KB

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