vectorized_combination.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. local gameplayMultiHandler = require('Scripts.Utils.Components.GameplayUtils')
  12. local vectorized_combination =
  13. {
  14. Properties =
  15. {
  16. XCoordEventName = "",
  17. YCoordEventName = "",
  18. ZCoordEventName = "",
  19. OutgoingGameplayEventName = "",
  20. DeadZoneLength = 0.2,
  21. },
  22. }
  23. function vectorized_combination:OnActivate()
  24. self.vectorizedValue = Vector3(0,0,0)
  25. self.active = false
  26. self.outgoingId = GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName, "Vector3")
  27. self.vectorizedHandlers = gameplayMultiHandler.ConnectMultiHandlers{
  28. [GameplayNotificationId(self.entityId, self.Properties.XCoordEventName, "float")] = {
  29. OnEventBegin = function(floatValue) self:UpdateX(floatValue) end,
  30. OnEventUpdating = function(floatValue) self:UpdateX(floatValue) end,
  31. OnEventEnd = function(floatValue) self:UpdateX(0) end,
  32. },
  33. [GameplayNotificationId(self.entityId, self.Properties.YCoordEventName, "float")] = {
  34. OnEventBegin = function(floatValue) self:UpdateY(floatValue) end,
  35. OnEventUpdating = function(floatValue) self:UpdateY(floatValue) end,
  36. OnEventEnd = function(floatValue) self:UpdateY(0) end,
  37. },
  38. [GameplayNotificationId(self.entityId, self.Properties.ZCoordEventName, "float")] = {
  39. OnEventBegin = function(floatValue) self:UpdateZ(floatValue) end,
  40. OnEventUpdating = function(floatValue) self:UpdateZ(floatValue) end,
  41. OnEventEnd = function(floatValue) self:UpdateZ(0) end,
  42. },
  43. }
  44. -- deprecated bus id's will be removed in 1.12
  45. -------------------------------------------------------------------------
  46. self.deprecatedOutgoingId = GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName)
  47. self.deprecatedVectorizedHandlers = gameplayMultiHandler.ConnectMultiHandlers{
  48. [GameplayNotificationId(self.entityId, self.Properties.XCoordEventName)] = {
  49. OnEventBegin = function(floatValue) self:UpdateX(floatValue) end,
  50. OnEventUpdating = function(floatValue) self:UpdateX(floatValue) end,
  51. OnEventEnd = function(floatValue) self:UpdateX(0) end,
  52. },
  53. [GameplayNotificationId(self.entityId, self.Properties.YCoordEventName)] = {
  54. OnEventBegin = function(floatValue) self:UpdateY(floatValue) end,
  55. OnEventUpdating = function(floatValue) self:UpdateY(floatValue) end,
  56. OnEventEnd = function(floatValue) self:UpdateY(0) end,
  57. },
  58. [GameplayNotificationId(self.entityId, self.Properties.ZCoordEventName)] = {
  59. OnEventBegin = function(floatValue) self:UpdateZ(floatValue) end,
  60. OnEventUpdating = function(floatValue) self:UpdateZ(floatValue) end,
  61. OnEventEnd = function(floatValue) self:UpdateZ(0) end,
  62. },
  63. }
  64. -------------------------------------------------------------------------
  65. end
  66. function vectorized_combination:OnDeactivate()
  67. self.vectorizedHandlers:Disconnect()
  68. self.deprecatedVectorizedHandlers:Disconnect()
  69. end
  70. function vectorized_combination:SendGameplayEvent()
  71. local shouldBeActive = self.vectorizedValue:GetLength() > self.Properties.DeadZoneLength
  72. if shouldBeActive and not self.active then
  73. GameplayNotificationBus.Event.OnEventBegin(self.outgoingId, self.vectorizedValue, "float")
  74. elseif shouldBeActive and self.active then
  75. GameplayNotificationBus.Event.OnEventUpdating(self.outgoingId, self.vectorizedValue, "float")
  76. elseif not shouldBeActive and self.active then
  77. GameplayNotificationBus.Event.OnEventEnd(self.outgoingId, self.vectorizedValue, "float")
  78. end
  79. self.active = shouldBeActive
  80. -- otherwise we were not active and shouldn't be active so do nothing
  81. end
  82. -- X coordinate handlers
  83. function vectorized_combination:UpdateX(floatValue)
  84. self.vectorizedValue.x = floatValue
  85. self:SendGameplayEvent()
  86. end
  87. -- Y coordinate handlers
  88. function vectorized_combination:UpdateY(floatValue)
  89. self.vectorizedValue.y = floatValue
  90. self:SendGameplayEvent()
  91. end
  92. -- Z coordinate handlers
  93. function vectorized_combination:UpdateZ(floatValue)
  94. self.vectorizedValue.z = floatValue
  95. self:SendGameplayEvent()
  96. end
  97. --deprecated methods will be removed in 1.12
  98. -------------------------------------------------------------------------
  99. function vectorized_combination:DeprecatedSendGameplayEvent()
  100. local shouldBeActive = self.vectorizedValue:GetLength() > self.Properties.DeadZoneLength
  101. if shouldBeActive and not self.active then
  102. GameplayNotificationBus.Event.OnEventBegin(self.deprecatedOutgoingId, self.vectorizedValue)
  103. elseif shouldBeActive and self.active then
  104. GameplayNotificationBus.Event.OnEventUpdating(self.deprecatedOutgoingId, self.vectorizedValue)
  105. elseif not shouldBeActive and self.active then
  106. GameplayNotificationBus.Event.OnEventEnd(self.deprecatedOutgoingId, self.vectorizedValue)
  107. end
  108. self.active = shouldBeActive
  109. -- otherwise we were not active and shouldn't be active so do nothing
  110. end
  111. -- X coordinate handlers
  112. function vectorized_combination:DeprecatedUpdateX(floatValue)
  113. self.vectorizedValue.x = floatValue
  114. self:DeprecatedSendGameplayEvent()
  115. end
  116. -- Y coordinate handlers
  117. function vectorized_combination:DeprecatedUpdateY(floatValue)
  118. self.vectorizedValue.y = floatValue
  119. self:DeprecatedSendGameplayEvent()
  120. end
  121. -- Z coordinate handlers
  122. function vectorized_combination:DeprecatedUpdateZ(floatValue)
  123. self.vectorizedValue.z = floatValue
  124. self:DeprecatedSendGameplayEvent()
  125. end
  126. -------------------------------------------------------------------------
  127. return vectorized_combination