RotateEntity.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 RotateEntity =
  12. {
  13. Properties =
  14. {
  15. IncomingGameplayEventName = "",
  16. AxisOfRotation = {default=Vector3(0,0,1), description="The axis to rotate around"},
  17. IsRelative = {default=true, description="When true, the entity's transform will transform the AxisOfRotation"},
  18. RotationSpeed = {default=60, description="Degrees per second."},
  19. },
  20. }
  21. function RotateEntity:OnActivate()
  22. local gameplayBusId = GameplayNotificationId(self.entityId, self.Properties.IncomingGameplayEventName, "float")
  23. self.gameplayBus = GameplayNotificationBus.Connect(self, gameplayBusId)
  24. end
  25. function RotateEntity:ApplyRotation(floatValue)
  26. local axisOfRotation = self.Properties.AxisOfRotation:GetNormalized()
  27. local localTM = TransformBus.Event.GetWorldTM(self.entityId)
  28. local translation = localTM:GetTranslation()
  29. localTM:SetTranslation(Vector3(0,0,0))
  30. local scale = localTM:ExtractScale()
  31. if not self.Properties.IsRelative then
  32. axisOfRotation = axisOfRotation * localTM
  33. end
  34. local dt = TickRequestBus.Broadcast.GetTickDeltaTime()
  35. local rotationAmount = floatValue * dt * Math.DegToRad(self.Properties.RotationSpeed)
  36. local finalTM = localTM * Transform.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(axisOfRotation, rotationAmount):GetNormalized())
  37. finalTM:SetPosition(translation)
  38. finalTM:MultiplyByScale(scale)
  39. TransformBus.Event.SetWorldTM(self.entityId, finalTM)
  40. end
  41. function RotateEntity:OnEventBegin(floatValue)
  42. self:ApplyRotation(floatValue)
  43. end
  44. function RotateEntity:OnEventUpdating(floatValue)
  45. self:ApplyRotation(floatValue)
  46. end
  47. function RotateEntity:OnEventEnd(floatValue)
  48. self:ApplyRotation(floatValue)
  49. end
  50. function RotateEntity:OnDeactivate()
  51. self.gameplayBus:Disconnect()
  52. end
  53. return RotateEntity