AchievementsSystemComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AchievementsSystemComponent.h>
  9. #include <Achievements/AchievementNotificationBus.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. namespace Achievements
  14. {
  15. class AchievementNotificationBusBehaviorHandler
  16. : public AchievementNotificationBus::Handler
  17. , public AZ::BehaviorEBusHandler
  18. {
  19. public:
  20. ////////////////////////////////////////////////////////////////////////////////////////////
  21. AZ_EBUS_BEHAVIOR_BINDER(AchievementNotificationBusBehaviorHandler, "{33DFB6A3-434B-4341-B603-5F387D1CACFE}", AZ::SystemAllocator
  22. , OnAchievementUnlocked
  23. , OnAchievementDetailsQueried
  24. , OnAchievementUnlockRequested
  25. );
  26. ////////////////////////////////////////////////////////////////////////////////////////////
  27. void OnAchievementUnlocked(const AZStd::string& achievementId, const AzFramework::LocalUserId& localUserId) override
  28. {
  29. Call(FN_OnAchievementUnlocked, achievementId, localUserId);
  30. }
  31. ////////////////////////////////////////////////////////////////////////////////////////////
  32. void OnAchievementUnlockRequested(const AZStd::string& achievementId, const AzFramework::LocalUserId& localUserId) override
  33. {
  34. Call(FN_OnAchievementUnlockRequested, achievementId, localUserId);
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. void OnAchievementDetailsQueried(const AzFramework::LocalUserId& localUserId, const AchievementDetails& achievementDetails) override
  38. {
  39. Call(FN_OnAchievementDetailsQueried, localUserId, achievementDetails);
  40. }
  41. };
  42. ////////////////////////////////////////////////////////////////////////////////////////
  43. void AchievementDetails::Reflect(AZ::ReflectContext* context)
  44. {
  45. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  46. {
  47. serialize->Class<AchievementDetails>()
  48. ->Version(0);
  49. if (AZ::EditContext* ec = serialize->GetEditContext())
  50. {
  51. ec->Class<AchievementDetails>("AchievementDetails", "Struct to hold platform agnostic achievement details for query results")
  52. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  53. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  54. }
  55. }
  56. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  57. {
  58. behaviorContext->Class<AchievementDetails>()
  59. ->Constructor<const AchievementDetails&>()
  60. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  61. ->Property("id", BehaviorValueProperty(&AchievementDetails::id))
  62. ->Property("name", BehaviorValueProperty(&AchievementDetails::name))
  63. ->Property("desc", BehaviorValueProperty(&AchievementDetails::desc))
  64. ->Property("rewardValue", BehaviorValueProperty(&AchievementDetails::rewardValue))
  65. ->Property("secret", BehaviorValueProperty(&AchievementDetails::secret))
  66. ->Property("currentProgress", BehaviorValueProperty(&AchievementDetails::currentProgress))
  67. ->Property("unlocked", BehaviorValueProperty(&AchievementDetails::unlocked));
  68. }
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////
  71. AchievementDetails::AchievementDetails() :
  72. id(0),
  73. name(""),
  74. desc(""),
  75. rewardValue(0),
  76. currentProgress(0),
  77. unlocked(false),
  78. secret(false)
  79. {
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////////////
  82. void AchievementsSystemComponent::Reflect(AZ::ReflectContext* context)
  83. {
  84. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  85. {
  86. serialize->Class<AchievementsSystemComponent, AZ::Component>()
  87. ->Version(0);
  88. if (AZ::EditContext* ec = serialize->GetEditContext())
  89. {
  90. ec->Class<AchievementsSystemComponent>("Achievements", "Platform agnostic interface for retrieving achievement details and unlocking achievements")
  91. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  92. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  93. }
  94. }
  95. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  96. {
  97. behaviorContext->EBus<AchievementNotificationBus>("AchievementNotificationBus")
  98. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  99. ->Handler<AchievementNotificationBusBehaviorHandler>();
  100. using UnlockAchievementParams = AchievementRequests::UnlockAchievementParams;
  101. behaviorContext->Class<UnlockAchievementParams>()
  102. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  103. ->Property("achievementId", BehaviorValueProperty(&UnlockAchievementParams::achievementId))
  104. ->Property("localUserId", BehaviorValueProperty(&UnlockAchievementParams::localUserId))
  105. ->Property("percentage", BehaviorValueProperty(&UnlockAchievementParams::percentage));
  106. using QueryAchievementParams = AchievementRequests::QueryAchievementParams;
  107. behaviorContext->Class<QueryAchievementParams>()
  108. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  109. ->Property("achievementId", BehaviorValueProperty(&QueryAchievementParams::achievementId))
  110. ->Property("localUserId", BehaviorValueProperty(&QueryAchievementParams::localUserId));
  111. behaviorContext->EBus<AchievementRequestBus>("AchievementRequestBus")
  112. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  113. ->Attribute(AZ::Script::Attributes::Category, "Achievements")
  114. ->Event("UnlockAchievement", &AchievementRequestBus::Events::UnlockAchievement)
  115. ->Event("QueryAchievementDetails", &AchievementRequestBus::Events::QueryAchievementDetails);
  116. }
  117. AchievementDetails::Reflect(context);
  118. }
  119. ////////////////////////////////////////////////////////////////////////////////////////
  120. void AchievementsSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  121. {
  122. provided.push_back(AZ_CRC_CE("AchievementsService"));
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////////////
  125. void AchievementsSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  126. {
  127. incompatible.push_back(AZ_CRC_CE("AchievementsService"));
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////////////
  130. void AchievementsSystemComponent::Activate()
  131. {
  132. m_pimpl.reset(Implementation::Create(*this));
  133. AchievementRequestBus::Handler::BusConnect();
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////////////
  136. void AchievementsSystemComponent::Deactivate()
  137. {
  138. AchievementRequestBus::Handler::BusDisconnect();
  139. m_pimpl.reset();
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////////////
  142. void AchievementsSystemComponent::UnlockAchievement(const UnlockAchievementParams& params)
  143. {
  144. AchievementNotificationBus::Broadcast(&AchievementNotifications::OnAchievementUnlockRequested, params.achievementId, params.localUserId);
  145. AZ_Printf("Achievements", "Unlock Achievement request for localuserId %s, achievement ID %s", AzFramework::LocalUserIdToString(params.localUserId).c_str(), params.achievementId.c_str());
  146. if (m_pimpl)
  147. {
  148. m_pimpl->UnlockAchievement(params);
  149. }
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////////////
  152. void AchievementsSystemComponent::QueryAchievementDetails(const QueryAchievementParams& params)
  153. {
  154. AZ_Printf("Achievements", "Query Achievement request for localuserId %s, achievement ID %s", AzFramework::LocalUserIdToString(params.localUserId).c_str(), params.achievementId.c_str());
  155. if (m_pimpl)
  156. {
  157. m_pimpl->QueryAchievementDetails(params);
  158. }
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////////////////////
  161. AchievementsSystemComponent::Implementation::Implementation(AchievementsSystemComponent& achievementSystemComponent)
  162. : m_achievementsSystemComponent(achievementSystemComponent)
  163. {
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////////////////////
  166. AchievementsSystemComponent::Implementation::~Implementation()
  167. {
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////////////
  170. void AchievementsSystemComponent::Implementation::OnUnlockAchievementComplete(const UnlockAchievementParams& params)
  171. {
  172. AZ::TickBus::QueueFunction([params]()
  173. {
  174. if (params.OnAchievementUnlockCallback)
  175. {
  176. params.OnAchievementUnlockCallback(params.localUserId, params.achievementId);
  177. }
  178. AchievementNotificationBus::Broadcast(&AchievementNotifications::OnAchievementUnlocked, params.achievementId, params.localUserId);
  179. });
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////////////
  182. void AchievementsSystemComponent::Implementation::OnQueryAchievementDetailsComplete(const QueryAchievementParams& params, const AchievementDetails& details)
  183. {
  184. AZ::TickBus::QueueFunction([params, details]()
  185. {
  186. if (params.OnAchievementDetailsQueriedCallback)
  187. {
  188. params.OnAchievementDetailsQueriedCallback(params.localUserId, details);
  189. }
  190. AchievementNotificationBus::Broadcast(&AchievementNotifications::OnAchievementDetailsQueried, params.localUserId, details);
  191. });
  192. }
  193. }