Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. require "/scripts/rails.lua"
  2.  
  3. function init()
  4.   self.active = false
  5.  
  6.   self.connectionOffset = config.getParameter("connectionOffset", {0, 0})
  7.   self.activeArmAngle = config.getParameter("activeArmAngle", 0)
  8.   self.inactiveArmAngle = config.getParameter("inactiveArmAngle", 0)
  9.  
  10.   local railConfig = config.getParameter("railConfig", {})
  11.   railConfig.speed = config.getParameter("speed")
  12.   railConfig.onEngage = function() animator.playSound("engage") end
  13.  
  14.   self.railRider = Rails.createRider(railConfig)
  15.   self.railRider:init()
  16.  
  17.   self.onRail = false
  18.   self.volumeAdjustTimer = 0.0
  19.   self.volumeAdjustTime = 0.1
  20. end
  21.  
  22. function uninit()
  23.   status.clearPersistentEffects("railhook")
  24. end
  25.  
  26. function update(dt, fireMode, shiftHeld)
  27.   local edgeTrigger = fireMode ~= self.lastFireMode
  28.   if fireMode == "primary" then
  29.     if edgeTrigger and self.railRider:onRail() then
  30.       disengageHook()
  31.     elseif edgeTrigger and not status.statPositive("activeMovementAbilities") and not mcontroller.onGround() then
  32.       engageHook()
  33.     end
  34.   else
  35.     if edgeTrigger and not self.railRider:onRail() then
  36.       disengageHook()
  37.     end
  38.   end
  39.   self.lastFireMode = fireMode
  40.  
  41.   if self.active then
  42.     if mcontroller.isColliding() then
  43.       disengageHook()
  44.     else
  45.       activeItem.setArmAngle(self.activeArmAngle)
  46.       self.railRider:updateConnectionOffset(activeItem.handPosition(self.connectionOffset))
  47.  
  48.       self.railRider:update(dt)
  49.  
  50.       if self.railRider:onRail() then
  51.         -- TODO: allow jumping off of rails
  52.         mcontroller.controlModifiers({jumpingSuppressed = true})
  53.         mcontroller.controlParameters({airForce = 0})
  54.       end
  55.     end
  56.   else
  57.     activeItem.setArmAngle(self.inactiveArmAngle)
  58.   end
  59.  
  60.   if self.railRider:onRail() then
  61.     status.setPersistentEffects("railhook", {{stat = "activeMovementAbilities", amount = 1}})
  62.     animator.setAnimationState("railState", "on")
  63.   else
  64.     status.clearPersistentEffects("railhook")
  65.     animator.setAnimationState("railState", "off")
  66.   end
  67.  
  68.   local onRail = self.railRider:onRail() and self.railRider.moving and self.railRider.speed > 0.01
  69.   if onRail then
  70.     animator.setParticleEmitterActive("sparks", true)
  71.     animator.setParticleEmitterEmissionRate("sparks", math.floor(self.railRider.speed) * 2)
  72.  
  73.     local volumeAdjustment = math.max(0.5, math.min(1.0, self.railRider.speed / 20))
  74.  
  75.     if not self.onRail then
  76.       self.onRail = true
  77.       animator.playSound("grind", -1)
  78.       animator.setSoundVolume("grind", volumeAdjustment, 0)
  79.     end
  80.  
  81.     self.volumeAdjustTimer = math.max(0, self.volumeAdjustTimer - dt)
  82.     if self.volumeAdjustTimer == 0 then
  83.       animator.setSoundVolume("grind", volumeAdjustment, self.volumeAdjustTime)
  84.       self.volumeAdjustTimer = self.volumeAdjustTime
  85.     end
  86.   else
  87.     animator.setParticleEmitterActive("sparks", false)
  88.  
  89.     self.onRail = false
  90.     self.volumeAdjustTimer = self.volumeAdjustTime
  91.     animator.stopAllSounds("grind")
  92.   end
  93. end
  94.  
  95. function engageHook()
  96.   activeItem.callOtherHandScript("disengageHook")
  97.   self.active = true
  98.   self.railRider:reset()
  99.   activeItem.setArmAngle(self.activeArmAngle)
  100.   self.railRider.connectionOffset = activeItem.handPosition(self.connectionOffset)
  101. end
  102.  
  103. function disengageHook()
  104.   self.active = false
  105.   self.onRail = false
  106.   self.railRider:reset()
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement