3
0

ordered_event_combination.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 ordered_event_combination =
  13. {
  14. Properties =
  15. {
  16. IncomingGameplayEventNames = {"",},
  17. OutgoingGameplayEventName = "",
  18. TimeToCompleteAllEvents = 1,
  19. },
  20. BusIds = {},
  21. DeprecatedBusIds = {},
  22. Handlers = {},
  23. DeprecatedHandlers = {},
  24. }
  25. function ordered_event_combination:OnActivate()
  26. for nameCount = 1, #self.Properties.IncomingGameplayEventNames do
  27. self.BusIds[nameCount] = GameplayNotificationId(self.entityId, self.Properties.IncomingGameplayEventNames[nameCount], "float")
  28. self.Handlers[nameCount] = gameplayMultiHandler.ConnectMultiHandlers{
  29. [self.BusIds[nameCount]] = {
  30. OnEventBegin = function(floatValue) self:OnEventBegin(floatValue) end,
  31. },
  32. }
  33. -- support for deprecated APIs. This will be removed in 1.12
  34. self.DeprecatedBusIds[nameCount] = GameplayNotificationId(self.entityId, self.Properties.IncomingGameplayEventNames[nameCount])
  35. self.DeprecatedHandlers[nameCount] = gameplayMultiHandler.ConnectMultiHandlers{
  36. [self.DeprecatedBusIds[nameCount]] = {
  37. OnEventBegin = function(floatValue) self:OnDeprecatedHandlerEventBegin(floatValue) end,
  38. },
  39. }
  40. end
  41. self.nextExpectedEventIndex = 1
  42. self.nextExpectedDeprecatedEventIndex = 1
  43. self.accumulatedTime = 0
  44. end
  45. function ordered_event_combination:OnDeactivate()
  46. for nameCount = 1, #self.Properties.IncomingGameplayEventNames do
  47. self.Handlers[nameCount]:Disconnect()
  48. self.DeprecatedHandlers[nameCount]:Disconnect()
  49. end
  50. self.TickHandler:Disconnect()
  51. end
  52. function ordered_event_combination:OnDeprecatedHandlerEventBegin(floatValue)
  53. self.TickHandler = TickBus.Connect(self, 0)
  54. if GameplayNotificationBus.GetCurrentBusId() == self.DeprecatedBusIds[self.nextExpectedDeprecatedEventIndex] then
  55. self.nextExpectedDeprecatedEventIndex = self.nextExpectedDeprecatedEventIndex + 1
  56. if (self.nextExpectedDeprecatedEventIndex > #self.Properties.IncomingGameplayEventNames) then
  57. self:Reset(true, floatValue)
  58. end
  59. elseif self.nextExpectedDeprecatedEventIndex > 1 then
  60. self:Reset(false, 0)
  61. end
  62. end
  63. function ordered_event_combination:OnEventBegin(floatValue)
  64. self.TickHandler = TickBus.Connect(self, 0)
  65. if GameplayNotificationBus.GetCurrentBusId() == self.BusIds[self.nextExpectedEventIndex] then
  66. self.nextExpectedEventIndex = self.nextExpectedEventIndex + 1
  67. if (self.nextExpectedEventIndex > #self.Properties.IncomingGameplayEventNames) then
  68. self:Reset(true, floatValue)
  69. end
  70. elseif self.nextExpectedEventIndex > 1 then
  71. self:Reset(false, 0)
  72. end
  73. end
  74. function ordered_event_combination:OnTick(dt, scriptTime)
  75. if self.nextExpectedEventIndex == 1 then
  76. self.TickHandler:Disconnect()
  77. return
  78. end
  79. self.accumulatedTime = self.accumulatedTime + dt
  80. if self.accumulatedTime > self.Properties.TimeToCompleteAllEvents then
  81. self:Reset(false, 0)
  82. end
  83. end
  84. function ordered_event_combination:Reset(success, floatValue)
  85. self.TickHandler:Disconnect()
  86. self.nextExpectedEventIndex = 1
  87. self.nextExpectedDeprecatedEventIndex = 1
  88. self.accumulatedTime = 0
  89. if success then
  90. GameplayNotificationBus.Event.OnEventBegin(GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName), floatValue)
  91. GameplayNotificationBus.Event.OnEventBegin(GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName, "float"), floatValue)
  92. else
  93. GameplayNotificationBus.Event.OnEventEnd(GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName), floatValue)
  94. GameplayNotificationBus.Event.OnEventEnd(GameplayNotificationId(self.entityId, self.Properties.OutgoingGameplayEventName, "float"), floatValue)
  95. end
  96. end
  97. return ordered_event_combination