MatchPlayerCoinsComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <AzCore/Serialization/SerializeContext.h>
  8. #include <Source/Components/Multiplayer/MatchPlayerCoinsComponent.h>
  9. namespace MultiplayerSample
  10. {
  11. void MatchPlayerCoinsComponent::Reflect(AZ::ReflectContext* context)
  12. {
  13. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  14. if (serializeContext)
  15. {
  16. serializeContext->Class<MatchPlayerCoinsComponent, MatchPlayerCoinsComponentBase>()
  17. ->Version(1);
  18. }
  19. MatchPlayerCoinsComponentBase::Reflect(context);
  20. }
  21. void MatchPlayerCoinsComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  22. {
  23. #if AZ_TRAIT_CLIENT
  24. AZ::Interface<MatchPlayerCoinsComponent>::Register(this);
  25. #endif
  26. }
  27. void MatchPlayerCoinsComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  28. {
  29. #if AZ_TRAIT_CLIENT
  30. AZ::Interface<MatchPlayerCoinsComponent>::Unregister(this);
  31. #endif
  32. }
  33. AZStd::vector<PlayerCoinState> MatchPlayerCoinsComponent::GetPlayerCoinCounts() const
  34. {
  35. AZStd::vector<PlayerCoinState> out;
  36. // We can return AZStd::array<PlayerCoinState, N> but only if the property is not rewindable, this will support either option.
  37. const auto& coins = GetCoinsPerPlayerArray();
  38. for (const auto& state : coins)
  39. {
  40. out.push_back(PlayerCoinState(state));
  41. }
  42. return out;
  43. }
  44. MatchPlayerCoinsComponentController::MatchPlayerCoinsComponentController(MatchPlayerCoinsComponent& parent)
  45. : MatchPlayerCoinsComponentControllerBase(parent)
  46. {
  47. }
  48. void MatchPlayerCoinsComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  49. {
  50. PlayerCoinCollectorNotificationBus::Handler::BusConnect();
  51. }
  52. void MatchPlayerCoinsComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  53. {
  54. PlayerCoinCollectorNotificationBus::Handler::BusDisconnect();
  55. }
  56. #if AZ_TRAIT_SERVER
  57. void MatchPlayerCoinsComponentController::ResetAllCoins()
  58. {
  59. for (int i = 0; i < MultiplayerSample::MaxSupportedPlayers; ++i)
  60. {
  61. ModifyCoinsPerPlayer(i).m_coins = 0;
  62. }
  63. }
  64. void MatchPlayerCoinsComponentController::OnPlayerCollectedCoinCountChanged(Multiplayer::NetEntityId playerEntity,
  65. uint16_t coinsCollected)
  66. {
  67. const int stateIndex = GetCoinStateIndex(playerEntity);
  68. if (stateIndex >= 0)
  69. {
  70. ModifyCoinsPerPlayer(stateIndex).m_playerId = playerEntity;
  71. ModifyCoinsPerPlayer(stateIndex).m_coins = coinsCollected;
  72. }
  73. }
  74. void MatchPlayerCoinsComponentController::OnPlayerCollectorActivated(Multiplayer::NetEntityId playerEntity)
  75. {
  76. // Find an empty slot to store this player's state in.
  77. int32_t stateIndex = 0;
  78. const int32_t stateCount = aznumeric_cast<int32_t>(GetCoinsPerPlayerArray().size());
  79. for (; stateIndex < stateCount; ++stateIndex)
  80. {
  81. if (GetCoinsPerPlayer(stateIndex).m_playerId == Multiplayer::InvalidNetEntityId)
  82. {
  83. break;
  84. }
  85. }
  86. if (stateIndex >= 0 && stateIndex < stateCount)
  87. {
  88. ModifyCoinsPerPlayer(stateIndex).m_playerId = playerEntity;
  89. ModifyCoinsPerPlayer(stateIndex).m_coins = 0;
  90. }
  91. }
  92. void MatchPlayerCoinsComponentController::OnPlayerCollectorDeactivated(Multiplayer::NetEntityId playerEntity)
  93. {
  94. const int stateIndex = GetCoinStateIndex(playerEntity);
  95. if (stateIndex >= 0)
  96. {
  97. ModifyCoinsPerPlayer(stateIndex).m_playerId = Multiplayer::InvalidNetEntityId;
  98. ModifyCoinsPerPlayer(stateIndex).m_coins = 0;
  99. }
  100. }
  101. #endif
  102. int MatchPlayerCoinsComponentController::GetCoinStateIndex(Multiplayer::NetEntityId playerEntity) const
  103. {
  104. int32_t stateIndex = 0;
  105. const int32_t stateCount = aznumeric_cast<int32_t>(GetCoinsPerPlayerArray().size());
  106. for (; stateIndex < stateCount; ++stateIndex)
  107. {
  108. if (GetCoinsPerPlayer(stateIndex).m_playerId == playerEntity)
  109. {
  110. break;
  111. }
  112. }
  113. if (stateIndex >= 0 && stateIndex < stateCount)
  114. {
  115. return stateIndex;
  116. }
  117. return -1;
  118. }
  119. }