GeomCache.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Script.ReloadScript("scripts/Utils/EntityUtils.lua")
  12. GeomCache =
  13. {
  14. Properties = {
  15. geomcacheFile = "EngineAssets/GeomCaches/defaultGeomCache.cax",
  16. bPlaying = 0,
  17. fStartTime = 0,
  18. bLooping = 0,
  19. objectStandIn = "",
  20. materialStandInMaterial = "",
  21. objectFirstFrameStandIn = "",
  22. materialFirstFrameStandInMaterial = "",
  23. objectLastFrameStandIn = "",
  24. materialLastFrameStandInMaterial = "",
  25. fStandInDistance = 0,
  26. fStreamInDistance = 0,
  27. Physics = {
  28. bPhysicalize = 0,
  29. }
  30. },
  31. Editor={
  32. Icon = "animobject.bmp",
  33. IconOnTop = 1,
  34. },
  35. bPlaying = 0,
  36. currentTime = 0,
  37. precacheTime = 0,
  38. bPrecachedOutputTriggered = false,
  39. }
  40. function GeomCache:OnLoad(table)
  41. self.currentTime = table.currentTime;
  42. end
  43. function GeomCache:OnSave(table)
  44. table.currentTime = self.currentTime;
  45. end
  46. function GeomCache:OnSpawn()
  47. self.currentTime = self.Properties.fStartTime;
  48. self:SetFromProperties();
  49. end
  50. function GeomCache:OnReset()
  51. self.currentTime = self.Properties.fStartTime;
  52. self.bPrecachedOutputTriggered = true;
  53. self:SetFromProperties();
  54. end
  55. function GeomCache:SetFromProperties()
  56. local Properties = self.Properties;
  57. if (Properties.geomcacheFile == "") then
  58. do return end;
  59. end
  60. self:LoadGeomCache(0, Properties.geomcacheFile);
  61. self.bPlaying = Properties.bPlaying;
  62. if (self.bPlaying == 0) then
  63. self.currentTime = Properties.fStartTime;
  64. end
  65. self:SetGeomCachePlaybackTime(self.currentTime);
  66. self:SetGeomCacheParams(Properties.bLooping, Properties.objectStandIn, Properties.materialStandInMaterial, Properties.objectFirstFrameStandIn,
  67. Properties.materialFirstFrameStandInMaterial, Properties.objectLastFrameStandIn, Properties.materialLastFrameStandInMaterial,
  68. Properties.fStandInDistance, Properties.fStreamInDistance);
  69. self:SetGeomCacheStreaming(false, 0);
  70. if (Properties.Physics.bPhysicalize == 1) then
  71. local tempPhysParams = EntityCommon.TempPhysParams;
  72. self:Physicalize(0, PE_ARTICULATED, tempPhysParams);
  73. end
  74. self:Activate(1);
  75. end
  76. function GeomCache:PhysicalizeThis()
  77. local Physics = self.Properties.Physics;
  78. EntityCommon.PhysicalizeRigid(self, 0, Physics, false);
  79. end
  80. function GeomCache:OnUpdate(dt)
  81. if (self.bPlaying == 1) then
  82. self:SetGeomCachePlaybackTime(self.currentTime);
  83. end
  84. if (self:IsGeomCacheStreaming() and not self.bPrecachedOutputTriggered) then
  85. local precachedTime = self:GetGeomCachePrecachedTime();
  86. if (precachedTime >= self.precacheTime) then
  87. self:ActivateOutput("Precached", true);
  88. self.bPrecachedOutputTriggered = true;
  89. end
  90. end
  91. if (self.bPlaying == 1) then
  92. self.currentTime = self.currentTime + dt;
  93. end
  94. end
  95. function GeomCache:OnPropertyChange()
  96. self:SetFromProperties();
  97. end
  98. function GeomCache:Event_Start(sender, val)
  99. self.bPlaying = 1;
  100. end
  101. function GeomCache:Event_Stop(sender, value)
  102. self.bPlaying = 0;
  103. end
  104. function GeomCache:Event_SetTime(sender, value)
  105. self.currentTime = value;
  106. end
  107. function GeomCache:Event_StartStreaming(sender, value)
  108. self.bPrecachedOutputTriggered = false;
  109. self:SetGeomCacheStreaming(true, self.currentTime);
  110. end
  111. function GeomCache:Event_StopStreaming(sender, value)
  112. self:SetGeomCacheStreaming(false, 0);
  113. end
  114. function GeomCache:Event_PrecacheTime(sender, value)
  115. self.precacheTime = value;
  116. end
  117. function GeomCache:Event_Hide(sender, value)
  118. self:Hide(1);
  119. end
  120. function GeomCache:Event_Unhide(sender, value)
  121. self:Hide(0);
  122. end
  123. function GeomCache:Event_StopDrawing(sender, value)
  124. self:SetGeomCacheDrawing(false);
  125. end
  126. function GeomCache:Event_StartDrawing(sender, value)
  127. self:SetGeomCacheDrawing(true);
  128. end
  129. GeomCache.FlowEvents =
  130. {
  131. Inputs =
  132. {
  133. Start = { GeomCache.Event_Start, "any" },
  134. Stop = { GeomCache.Event_Stop, "any" },
  135. SetTime = { GeomCache.Event_SetTime, "float" },
  136. StartStreaming = { GeomCache.Event_StartStreaming, "any" },
  137. StopStreaming = { GeomCache.Event_StopStreaming, "any" },
  138. PrecacheTime = { GeomCache.Event_PrecacheTime, "float" },
  139. Hide = { GeomCache.Event_Hide, "any" },
  140. Unhide = { GeomCache.Event_Unhide, "any" },
  141. StopDrawing = { GeomCache.Event_StopDrawing, "any" },
  142. StartDrawing = { GeomCache.Event_StartDrawing, "any" },
  143. },
  144. Outputs =
  145. {
  146. Precached = "bool",
  147. },
  148. }