Guest User

Untitled

a guest
Sep 22nd, 2016
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. --[[
  2.    moving marker (global script version)
  3.    created by Rodney
  4. ]]
  5.  
  6. -- customize variables
  7. markerScale = 1
  8. markerColor = {1, 1, 1}
  9. markerRemainingTime = 1
  10. markerHeight = 1.5
  11.  
  12. concurrentMarkerCount = 16
  13.  
  14. frameInterval = 0.01
  15. rotationDegreePerFrame = 1
  16.  
  17. -- global variables
  18.  
  19. timerIdSerial = 0
  20.  
  21. -- constants
  22. movingMarkerVersion = "2016092000g"
  23.  
  24. hidePosition = {0, -300, 0}
  25. hideScale = {0.1, 0.1, 0.1}
  26.  
  27. -- events
  28. function onLoad()
  29.    pool = MarkerPool.new(concurrentMarkerCount)
  30. end
  31.  
  32. function onObjectPickedUp(color, target)
  33.    local markerData = pool:getMarkerData(target)
  34.  
  35.    if markerData then
  36.       removeTimers(markerData)
  37.       setMarker(target, markerData)
  38.    end
  39. end
  40.  
  41. function onObjectDropped(color, target)
  42.    if pool:isReserved(target) then
  43.       local markerData = pool:getMarkerData(target)
  44.  
  45.       fadeOutMarker(target, markerData)
  46.    end
  47. end
  48.  
  49. -- internal functions
  50.  
  51. MarkerPool = {}
  52.  
  53. function MarkerPool.new(count)
  54.    local markers = {}
  55.    local rotationTimerIds = {}
  56.    local fadeOutTimerIds = {}
  57.    local markerStack = {}
  58.  
  59.    for i = 1, count do
  60.       markers[i] = createMarker()
  61.       rotationTimerIds[i] = getID("rotation")
  62.       fadeOutTimerIds[i] = getID("fadeOut")
  63.       markerStack[i] = i
  64.    end
  65.  
  66.    return {
  67.       _markers = markers,
  68.       _rotationTimerIds = rotationTimerIds,
  69.       _fadeOutTimerIds = fadeOutTimerIds,
  70.       _markerStack = markerStack,
  71.       _reserved = {},
  72.  
  73.       getMarkerData = function(self, target)
  74.          local index = self._reserved[target.guid]
  75.  
  76.          if not index then
  77.             index = pop(self._markerStack)
  78.             self._reserved[target.guid] = index
  79.          end
  80.  
  81.          if index then
  82.             return {
  83.                marker = self._markers[index],
  84.                rotationTimerId = self._rotationTimerIds[index],
  85.                fadeOutTimerId = self._fadeOutTimerIds[index]
  86.             }
  87.          else
  88.             return nil
  89.          end
  90.       end,
  91.  
  92.       removeMarkerData = function(self, target)
  93.          if self._reserved[target.guid] then
  94.             local index = self._reserved[target.guid]
  95.  
  96.             push(self._markerStack, index)
  97.             self._reserved[target.guid] = nil
  98.          end
  99.       end,
  100.  
  101.       isReserved = function(self, target)
  102.          return self._reserved[target.guid]
  103.       end
  104.    }
  105. end
  106.  
  107. function push(a, v)
  108.    a[#a+1] = v
  109.    return a
  110. end
  111.  
  112. function pop(a)
  113.    local v = a[#a]
  114.    a[#a] = nil
  115.    return v
  116. end
  117.  
  118. function getID(label)
  119.    timerIdSerial = timerIdSerial + 1
  120.    return string.format("_%s-%d-%d__%s__",
  121.                         os.date("%Y%m%d%H%M%S"), timerIdSerial, math.random(100), label)
  122. end
  123.  
  124. function createMarker()
  125.    local markerObject = spawnObject({
  126.          type = "Custom_Model",
  127.          position = hidePosition,
  128.          rotation = {0, 0, 0},
  129.          scale = hideScale
  130.    })
  131.  
  132.    markerObject.setCustomObject({
  133.          type = "Generic",
  134.          mesh = "http://pastebin.com/raw/RBUFj0HE",
  135.          collider = "http://pastebin.com/raw/bUwzJeWz"
  136.    })
  137.    
  138.    markerObject.setColorTint(markerColor)
  139.    markerObject.interactable = false
  140.    markerObject.lock()
  141.  
  142.    return markerObject
  143. end
  144.  
  145.  
  146. function setMarker(target, markerData)
  147.    local pos = target.getPosition()
  148.    local scale = target.getScale()
  149.  
  150.    markerData.marker.setPosition({pos.x, markerHeight + 0.2, pos.z})
  151.    markerData.marker.setScale({scale.x * markerScale, 10, scale.z * markerScale})
  152.    
  153.    if frameInterval <= 0 then
  154.       frameInterval = 0.1
  155.    end
  156.  
  157.    Timer.create({
  158.          identifier = markerData.rotationTimerId,
  159.          function_name = "rotateMarker",
  160.          function_owner = self,
  161.          parameters = {
  162.             degree = rotationDegreePerFrame,
  163.             target = target,
  164.             markerData = markerData
  165.          },
  166.          delay = frameInterval,
  167.          repetitions = 0
  168.    })
  169. end
  170.  
  171. function rotateMarker(param)
  172.    param.markerData.marker.setRotation({0, param.markerData.marker.getRotation().y + param.degree, 0})
  173. end
  174.  
  175. function fadeOutMarker(target, markerData)
  176.    if markerRemainingTime <= 0 then
  177.       markerRemainingTime = 0.1
  178.    end
  179.  
  180.    Timer.create({
  181.          identifier = markerData.fadeOutTimerId,
  182.          function_name = "hideMarker",
  183.          function_owner = self,
  184.          parameters = {
  185.             target = target,
  186.             markerData = markerData
  187.          },
  188.          delay = markerRemainingTime,
  189.    })
  190. end
  191.  
  192. function hideMarker(param)
  193.    Timer.destroy(param.markerData.rotationTimerId)
  194.    param.markerData.marker.setPosition(hidePosition)
  195.    param.markerData.marker.setScale(hideScale)
  196.  
  197.    pool:removeMarkerData(param.target)
  198. end
  199.  
  200. function removeTimers(markerData)
  201.    Timer.destroy(markerData.rotationTimerId)
  202.    Timer.destroy(markerData.fadeOutTimerId)
  203. end
Advertisement
Add Comment
Please, Sign In to add comment