Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- moving marker (global script version)
- created by Rodney
- ]]
- -- customize variables
- markerScale = 1
- markerColor = {1, 1, 1}
- markerRemainingTime = 1
- markerHeight = 1.5
- concurrentMarkerCount = 16
- frameInterval = 0.01
- rotationDegreePerFrame = 1
- -- global variables
- timerIdSerial = 0
- -- constants
- movingMarkerVersion = "2016092000g"
- hidePosition = {0, -300, 0}
- hideScale = {0.1, 0.1, 0.1}
- -- events
- function onLoad()
- pool = MarkerPool.new(concurrentMarkerCount)
- end
- function onObjectPickedUp(color, target)
- local markerData = pool:getMarkerData(target)
- if markerData then
- removeTimers(markerData)
- setMarker(target, markerData)
- end
- end
- function onObjectDropped(color, target)
- if pool:isReserved(target) then
- local markerData = pool:getMarkerData(target)
- fadeOutMarker(target, markerData)
- end
- end
- -- internal functions
- MarkerPool = {}
- function MarkerPool.new(count)
- local markers = {}
- local rotationTimerIds = {}
- local fadeOutTimerIds = {}
- local markerStack = {}
- for i = 1, count do
- markers[i] = createMarker()
- rotationTimerIds[i] = getID("rotation")
- fadeOutTimerIds[i] = getID("fadeOut")
- markerStack[i] = i
- end
- return {
- _markers = markers,
- _rotationTimerIds = rotationTimerIds,
- _fadeOutTimerIds = fadeOutTimerIds,
- _markerStack = markerStack,
- _reserved = {},
- getMarkerData = function(self, target)
- local index = self._reserved[target.guid]
- if not index then
- index = pop(self._markerStack)
- self._reserved[target.guid] = index
- end
- if index then
- return {
- marker = self._markers[index],
- rotationTimerId = self._rotationTimerIds[index],
- fadeOutTimerId = self._fadeOutTimerIds[index]
- }
- else
- return nil
- end
- end,
- removeMarkerData = function(self, target)
- if self._reserved[target.guid] then
- local index = self._reserved[target.guid]
- push(self._markerStack, index)
- self._reserved[target.guid] = nil
- end
- end,
- isReserved = function(self, target)
- return self._reserved[target.guid]
- end
- }
- end
- function push(a, v)
- a[#a+1] = v
- return a
- end
- function pop(a)
- local v = a[#a]
- a[#a] = nil
- return v
- end
- function getID(label)
- timerIdSerial = timerIdSerial + 1
- return string.format("_%s-%d-%d__%s__",
- os.date("%Y%m%d%H%M%S"), timerIdSerial, math.random(100), label)
- end
- function createMarker()
- local markerObject = spawnObject({
- type = "Custom_Model",
- position = hidePosition,
- rotation = {0, 0, 0},
- scale = hideScale
- })
- markerObject.setCustomObject({
- type = "Generic",
- mesh = "http://pastebin.com/raw/RBUFj0HE",
- collider = "http://pastebin.com/raw/bUwzJeWz"
- })
- markerObject.setColorTint(markerColor)
- markerObject.interactable = false
- markerObject.lock()
- return markerObject
- end
- function setMarker(target, markerData)
- local pos = target.getPosition()
- local scale = target.getScale()
- markerData.marker.setPosition({pos.x, markerHeight + 0.2, pos.z})
- markerData.marker.setScale({scale.x * markerScale, 10, scale.z * markerScale})
- if frameInterval <= 0 then
- frameInterval = 0.1
- end
- Timer.create({
- identifier = markerData.rotationTimerId,
- function_name = "rotateMarker",
- function_owner = self,
- parameters = {
- degree = rotationDegreePerFrame,
- target = target,
- markerData = markerData
- },
- delay = frameInterval,
- repetitions = 0
- })
- end
- function rotateMarker(param)
- param.markerData.marker.setRotation({0, param.markerData.marker.getRotation().y + param.degree, 0})
- end
- function fadeOutMarker(target, markerData)
- if markerRemainingTime <= 0 then
- markerRemainingTime = 0.1
- end
- Timer.create({
- identifier = markerData.fadeOutTimerId,
- function_name = "hideMarker",
- function_owner = self,
- parameters = {
- target = target,
- markerData = markerData
- },
- delay = markerRemainingTime,
- })
- end
- function hideMarker(param)
- Timer.destroy(param.markerData.rotationTimerId)
- param.markerData.marker.setPosition(hidePosition)
- param.markerData.marker.setScale(hideScale)
- pool:removeMarkerData(param.target)
- end
- function removeTimers(markerData)
- Timer.destroy(markerData.rotationTimerId)
- Timer.destroy(markerData.fadeOutTimerId)
- end
Advertisement
Add Comment
Please, Sign In to add comment