3
0

AddPhysicsImpulse.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 AddPhysicsImpulse =
  12. {
  13. Properties =
  14. {
  15. IncomingGameplayEventName = "",
  16. IsRelative = {default=true, description="When true, the impulse will be applied relative to the entity's transform"},
  17. ImpulseScale = {default=1.0, description="This will scale the vector before sending it"},
  18. },
  19. }
  20. function AddPhysicsImpulse:OnActivate()
  21. local gameplayBusId = GameplayNotificationId(self.entityId, self.Properties.IncomingGameplayEventName, "Vector3")
  22. self.gameplayBus = GameplayNotificationBus.Connect(self, gameplayBusId)
  23. end
  24. function AddPhysicsImpulse:AddImpulse(vectorValue)
  25. if self.Properties.IsRelative then
  26. local localTM = TransformBus.Event.GetWorldTM(self.entityId)
  27. localTM:SetTranslation(Vector3(0,0,0))
  28. localTM:ExtractScale()
  29. PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, localTM * (vectorValue*self.Properties.ImpulseScale))
  30. else
  31. PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, vectorValue*self.Properties.ImpulseScale)
  32. end
  33. end
  34. function AddPhysicsImpulse:OnEventBegin(vectorValue)
  35. self:AddImpulse(vectorValue)
  36. end
  37. function AddPhysicsImpulse:OnEventUpdating(vectorValue)
  38. self:AddImpulse(vectorValue)
  39. end
  40. function AddPhysicsImpulse:OnEventEnd(vectorValue)
  41. self:AddImpulse(vectorValue)
  42. end
  43. function AddPhysicsImpulse:OnDeactivate()
  44. self.gameplayBus:Disconnect()
  45. end
  46. return AddPhysicsImpulse