Advertisement
Guest User

TAMods v0.4 fancy lol damage numbers, heavily commented

a guest
Jul 8th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.02 KB | None | 0 0
  1. -- The text string that will be printed, this is what will be used to store the "lolol"
  2. local damage_str = ""
  3. -- The moving speed of the "lolol" when moving towards the next hit location
  4. local speed = 0.1
  5. -- The next hit location
  6. local target_loc
  7.  
  8. -- List of values used to cycle through rainbow colors
  9. local color_list = {rgb(255, 0, 0), rgb(255, 127, 0), rgb(255, 255, 0), rgb(127, 255, 0),
  10.                     rgb(0, 255, 0), rgb(0, 255, 127), rgb(0, 255, 255), rgb(0, 127, 255),
  11.                     rgb(0, 0, 255), rgb(127, 0, 255), rgb(255, 0, 255), rgb(255, 0, 127)}
  12. -- The current index in the rainbow color
  13. local color_idx = 0
  14. -- The time before the numbers disappear
  15. local max_time = 1.70
  16. -- The distance that the damage numbers moving in a random direction will travel
  17. local max_dist = 100
  18.  
  19. -- The array containing the random direction of the damage numbers
  20. local custom_array = {}
  21.  
  22. -- Utility function of linear interpolation, used to smoothly move the text from one hit to another
  23. function lerp(a, b, x)
  24.    return a + (b - a) * x
  25. end
  26.  
  27. -- Same as the lerp function, but interpolating vectors
  28. function vectorLerp(start, target, x)
  29.    local v = Vector4(0, 0, 0, 0)
  30.    v.x = lerp(start.x, target.x, x)
  31.    v.y = lerp(start.y, target.y, x)
  32.    v.z = lerp(start.z, target.z, x)
  33.    v.w = lerp(start.w, target.w, x)
  34.    return v
  35. end
  36.  
  37. -- Called when a damage number should be created
  38. function onDamageNumberCreate(dam_nums, number, loc, is_shield)
  39.    local num
  40.    -- Update the hit location
  41.    target_loc = loc
  42.    -- Cycle through the rainbow colors
  43.    color_idx = color_idx + 1
  44.    -- The next block is executed if there's at least 1 damage number already existing
  45.    -- This means that we should add "lol" to the existing string instead of creating a new one
  46.    if dam_nums:size() > 0 then
  47.       -- Retrieve the main damage number corresponding to the "lolol"
  48.       num = dam_nums:get(0)
  49.       -- Reset its timer so that it doesn't fade away as long as you hit
  50.       num.time = max_time
  51.       -- Change its color to the new rainbow color
  52.       -- Note the "+ 1" at the end, it's because lua arrays are indexed starting at 1 and not 0
  53.       num.color = color_list[color_idx % 12 + 1]
  54.       -- We add another "ol" to the string so that it grows on each hit
  55.       damage_str = damage_str .. "ol"
  56.    -- This happen if no damage number exists
  57.    else
  58.       -- Create a new damage number for the "lolol"
  59.       -- The value doesn't matter because we'll print a bunch of "lol"s, so we can just put 0 or anything
  60.       num = DamageNumber(0, max_time, loc, is_shield)
  61.       -- Set its color to the new rainbow color
  62.       num.color = color_list[color_idx % 12 + 1]
  63.       -- Add the damage number to the list
  64.       dam_nums:add(num)
  65.       -- Set the initial string to "lol"
  66.       damage_str = "lol"
  67.    end
  68.    -- This is the added damage numbers that goes into random directions
  69.    num = DamageNumber(number, max_time, loc, is_shield)
  70.    -- Set the rainbow color
  71.    num.color = color_list[color_idx % 12 + 1]
  72.    -- Set its transparency to half
  73.    num.color.a = 127
  74.    -- Add a new random rotation to our custom list
  75.    table.insert(custom_array, 1, math.random() * math.pi * 2)
  76.    -- Add the damage number to the list of damage numbers
  77.    dam_nums:add(num)
  78. end
  79.  
  80. -- Called each frame to update and draw the damage numbers
  81. function onDamageNumberUpdate(dam_nums, hud, delta)
  82.    local i = 0
  83.    -- Cycle through the existing damage numbers
  84.    -- We use a "while" instead of a "for" because "for" has a fixed range, and in our case we might need to remove some values and change the range
  85.    while i < dam_nums:size() do
  86.       -- Get the current damage number
  87.       local curr = dam_nums:get(i)
  88.       -- Update the timer of the number
  89.       curr.time = curr.time - delta
  90.       -- If the timer is finished
  91.       if curr.time <= 0 then
  92.          -- Remove the damage number from the list
  93.          dam_nums:remove(i)
  94.          -- And remove the random rotation if it's a damage number and not the one corresponding to the "lol"s
  95.          -- This one is always at the index 0 because it's the first that we add in onDamageNumberCreate
  96.          if i > 0 then
  97.             table.remove(custom_array, 1)
  98.          end
  99.          -- Decrease the index counter so that we don't miss a damage number
  100.          i = i - 1
  101.  
  102.       -- This happens if the timer is not finished
  103.       else
  104.          -- If we are currently updating the string and not the random numbers
  105.          -- Interpolate its location to smoothly go to the next hit location
  106.          if i == 0 then
  107.             curr.location = vectorLerp(curr.location, target_loc, speed)
  108.          end
  109.          -- Create a Vector for the number location
  110.          -- We have to do this because "isOnScreen" takes a Vector but curr.location is a Vector4, with depth information
  111.          local loc = Vector(curr.location.x, curr.location.y, curr.location.z)
  112.          -- Check if the number is not behind the player where it would be useless to draw it
  113.          if isOnScreen(hud, loc) then
  114.             -- Project the location to get the coordinates on the screen of the number
  115.             loc = project(hud, loc)
  116.             -- In case we're handling the random numbers and not the string
  117.             if i > 0 then
  118.                -- Move the number in the defined random direction
  119.                local dist = max_dist * (max_time - curr.time) / max_time
  120.                loc.x = loc.x + math.cos(custom_array[i]) * dist
  121.                loc.y = loc.y + math.sin(custom_array[i]) * dist
  122.             end
  123.             -- Set the 2D screen-location depth to the 3D world-location depth
  124.             loc.z = curr.location.w
  125.             -- If we're handling the string, draw the string
  126.             if i == 0 then
  127.                drawDamageNumber(hud, damage_str, curr.color, loc, 1, 1)
  128.             -- Otherwise we're dealing with numbers, so draw the actual number value
  129.             else
  130.                drawDamageNumber(hud, curr.number, curr.color, loc, 1, 1)
  131.             end
  132.          end
  133.       end
  134.       i = i + 1
  135.    end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement