PlayerCoinCollectorComponent.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <GameplayEffectsNotificationBus.h>
  8. #include <PlayerCoinCollectorBus.h>
  9. #include <UiCoinCountBus.h>
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzFramework/Physics/PhysicsScene.h>
  13. #include <AzFramework/Physics/Collision/CollisionEvents.h>
  14. #include <Components/Multiplayer/GemComponent.h>
  15. #include <Source/Components/Multiplayer/PlayerCoinCollectorComponent.h>
  16. namespace MultiplayerSample
  17. {
  18. PlayerCoinCollectorComponentController::PlayerCoinCollectorComponentController(PlayerCoinCollectorComponent& parent)
  19. : PlayerCoinCollectorComponentControllerBase(parent)
  20. {
  21. }
  22. void PlayerCoinCollectorComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  23. {
  24. if (IsNetEntityRoleAuthority())
  25. {
  26. #if AZ_TRAIT_SERVER
  27. if (AzPhysics::SceneInterface* si = AZ::Interface<AzPhysics::SceneInterface>::Get())
  28. {
  29. const AzPhysics::SceneHandle sh = si->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  30. si->RegisterSceneTriggersEventHandler(sh, m_trigger);
  31. }
  32. PlayerCoinCollectorNotificationBus::Broadcast(&PlayerCoinCollectorNotifications::OnPlayerCollectorActivated, GetNetEntityId());
  33. #endif
  34. }
  35. if (IsNetEntityRoleAutonomous())
  36. {
  37. CoinsCollectedAddEvent(m_coinCountChangedHandler);
  38. UiCoinCountNotificationBus::Broadcast(&UiCoinCountNotifications::OnCoinCountChanged, GetCoinsCollected());
  39. }
  40. }
  41. void PlayerCoinCollectorComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  42. {
  43. #if AZ_TRAIT_SERVER
  44. if (IsNetEntityRoleAuthority())
  45. {
  46. PlayerCoinCollectorNotificationBus::Broadcast(&PlayerCoinCollectorNotifications::OnPlayerCollectorDeactivated, GetNetEntityId());
  47. }
  48. m_trigger.Disconnect();
  49. #endif
  50. m_coinCountChangedHandler.Disconnect();
  51. }
  52. #if AZ_TRAIT_SERVER
  53. void PlayerCoinCollectorComponentController::OnTriggerEvents(const AzPhysics::TriggerEventList& tel)
  54. {
  55. for (const AzPhysics::TriggerEvent& te : tel)
  56. {
  57. if (te.m_otherBody && te.m_otherBody->GetEntityId() == GetEntityId())
  58. {
  59. if (te.m_type == AzPhysics::TriggerEvent::Type::Enter)
  60. {
  61. AZ::Entity* coinEntity = nullptr;
  62. AZ::ComponentApplicationBus::BroadcastResult(coinEntity,
  63. &AZ::ComponentApplicationBus::Events::FindEntity, te.m_triggerBody->GetEntityId());
  64. if (coinEntity)
  65. {
  66. if (GemComponent* gem = coinEntity->FindComponent<GemComponent>())
  67. {
  68. gem->RPC_CollectedByPlayer();
  69. ModifyCoinsCollected() += gem->GetGemScoreValue();
  70. PlayerCoinCollectorNotificationBus::Broadcast(&PlayerCoinCollectorNotifications::OnPlayerCollectedCoinCountChanged,
  71. GetNetEntityId(), GetCoinsCollected());
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. #endif
  79. void PlayerCoinCollectorComponentController::OnCoinsChanged(uint16_t coins)
  80. {
  81. UiCoinCountNotificationBus::Broadcast(&UiCoinCountNotifications::OnCoinCountChanged, coins);
  82. #if AZ_TRAIT_CLIENT
  83. GameplayEffectsNotificationBus::Broadcast(&GameplayEffectsNotificationBus::Events::OnPositionalEffect,
  84. SoundEffect::GemPickup, GetEntity()->GetTransform()->GetWorldTranslation());
  85. #endif
  86. }
  87. }