Advertisement
Yirktos

yk_fightHealth.lua

May 30th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.53 KB | None | 0 0
  1. require "base/internal/ui/reflexcore";
  2.  
  3. yk_fightHealth =
  4. {
  5.     healthTimer = 0;
  6.     flashTimer = 0;
  7. };
  8. registerWidget("yk_fightHealth");
  9.  
  10. ---------------
  11. -- CONSTANTS --
  12. ---------------
  13. local healthAlarm = 1.2;
  14.  
  15. local barWidth = 480;
  16. local barHeight = 40;
  17. local barHealthOffsetX = 0;
  18. local barHealthOffsetY = 0;
  19. local frameBorder = 5;
  20. -- I want to use these colors for the truehealth/stack/arenamode bar
  21. --local colorHealth = Color(255, 160, 0);
  22. --local colorOverHealth = Color(0, 224, 255);
  23. local colorHealth = Color(112, 255, 255);
  24. local colorOverHealth = Color(0, 96, 255);
  25. local colorHealthDamage = Color(255, 0, 64);
  26. local colorHealthDamage = Color(255, 0, 64);
  27. local colorHealthPickup = Color(255, 255, 255);
  28.  
  29. local colorFrame = Color(4, 2, 0, 160);
  30. local colorTrim = Color(255, 255, 255);
  31.  
  32. local drainSpeed = 225; --228
  33.  
  34. local flashDirection = 1;
  35. local flashLength = 0.15;
  36.  
  37. ---------------
  38. -- VARIABLES --
  39. ---------------
  40. local oldHealth = 0;
  41. local healthDamage = 0;
  42. local healthPickup = 0;
  43. local lostHealthDamage = 0;
  44. local lostHealthPickup = 0;
  45.  
  46. local drainHealthDamage = false;
  47. local drainHealthPickup = false;
  48.  
  49. colorHealthFinal = Color(
  50.     lerp(colorHealth.r, colorOverHealth.r, 0.5),
  51.     lerp(colorHealth.g, colorOverHealth.g, 0.5),
  52.     lerp(colorHealth.b, colorOverHealth.b, 0.5)
  53. );
  54.  
  55. ---------------
  56. function yk_fightHealth:draw()
  57.     -- PLAYER, FIRST OUT
  58.     local player = getPlayer();
  59.    
  60.     if player == nil or
  61.         player.state == PLAYER_STATE_EDITOR or
  62.         player.state == PLAYER_STATE_SPECTATOR or
  63.         world.gameState == GAME_STATE_GAMEOVER or
  64.         isInMenu()
  65.     then return false end;
  66.  
  67.     if not player.connected then return end;
  68.    
  69.     -- IF YOU DIE, CLEAR EVERYTHING
  70.     if player.health <= 0 then
  71.         self.healthTimer = 0;
  72.         oldHealth = 0;
  73.         healthDamage = 0;
  74.         healthPickup = 0;
  75.         lostHealthDamage = 0;
  76.         lostHealthPickup = 0;
  77.         drainHealthDamage = false;
  78.         drainHealthPickup = false;
  79.     end
  80.    
  81.     -- SECOND OUT
  82.     if not shouldShowHUD() then return end;
  83.    
  84.     -- FLASH TIMER
  85.     if self.flashTimer >= flashLength then
  86.         flashDirection = -1;
  87.     elseif self.flashTimer <= 0 then
  88.         flashDirection = 1;
  89.     end
  90.     self.flashTimer = self.flashTimer + (flashDirection * deltaTime);
  91.    
  92.     -- CALCULATE DAMAGE COLOR
  93.     local colorHealthDamageFinal = Color(colorHealthDamage.r, colorHealthDamage.g, colorHealthDamage.b,
  94.         lerp(colorHealthDamage.a, colorHealthDamage.a / 3, self.flashTimer/flashLength)
  95.     );
  96.    
  97.     -- HEALTH REMAINED CONSTANT WHILE DAMAGE OR PICKUP WAS BUFFERED, INCREMENT TIMER
  98.     if (player.health == oldHealth) and ((healthPickup ~= 0) or (healthDamage ~= 0)) then
  99.         self.healthTimer = math.min(healthAlarm, self.healthTimer + deltaTime);
  100.     end
  101.    
  102.     -- HEALTH DAMAGE, RESET TIMER, ADJUST BUFFERS
  103.     if player.health < oldHealth then
  104.         local d = (oldHealth - player.health);
  105.         healthDamage = healthDamage + d;
  106.        
  107.         if d > 1 then
  108.             drainHealthDamage = false;
  109.             drainHealthPickup = false;
  110.             self.healthTimer = 0;
  111.         end
  112.        
  113.         -- IF PICKUP BUFFERED, DECREASE PICKUP FIRST, KEEP TRACK OF PICKUP LOST THIS WAY
  114.         if healthPickup > 0 then
  115.             if healthPickup >= d then
  116.                 lostHealthPickup = lostHealthPickup + d;
  117.                 healthPickup = healthPickup - d;
  118.             elseif healthPickup < d then
  119.                 lostHealthPickup = lostHealthPickup + healthPickup;
  120.                 healthPickup = 0;
  121.             end
  122.         end
  123.        
  124.         oldHealth = player.health;
  125.        
  126.     -- HEALTH PICKUP, RESET TIMER, ADJUST BUFFERS
  127.     elseif player.health > oldHealth then
  128.         local p = (player.health - oldHealth);
  129.         healthPickup = healthPickup + p;
  130.        
  131.         if p > 1 then
  132.             drainHealthDamage = false;
  133.             drainHealthPickup = false;
  134.             self.healthTimer = 0;
  135.         end
  136.        
  137.         -- IF DAMAGE BUFFERED, DECREASE DAMAGE FIRST, KEEP TRACK OF DAMAGE LOST THIS WAY
  138.         if healthDamage > 0 then
  139.             if healthDamage >= p then
  140.                 lostHealthDamage = lostHealthDamage + p;
  141.                 healthDamage = healthDamage - p;
  142.             elseif healthDamage < p then
  143.                 lostHealthDamage = lostHealthDamage + healthDamage;
  144.                 healthDamage = 0;
  145.             end
  146.         end
  147.        
  148.         oldHealth = player.health;
  149.     end
  150.    
  151.     -- AT SET TIME, RESET TIMER, BEGIN DRAINING BUFFERS
  152.     if self.healthTimer >= healthAlarm then
  153.         self.healthTimer = 0;
  154.         drainHealthDamage = true;      
  155.         drainHealthPickup = true;
  156.     end
  157.     -- DRAIN DAMAGE BUFFERS, CALCULATE HEALTH COLOR
  158.     if drainHealthDamage then
  159.         if (healthDamage > 0) or (lostHealthDamage > 0) then           
  160.             healthDamage = math.max(0, healthDamage - (drainSpeed * deltaTime));
  161.             lostHealthDamage = math.max(0, lostHealthDamage - (drainSpeed * deltaTime));
  162.            
  163.         else
  164.             drainHealthDamage = false;
  165.         end
  166.        
  167.         colorHealthFinal = Color(
  168.             lerp(colorHealth.r, colorOverHealth.r, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200)),
  169.             lerp(colorHealth.g, colorOverHealth.g, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200)),
  170.             lerp(colorHealth.b, colorOverHealth.b, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200))
  171.         );
  172.     end
  173.     -- DRAIN PICKUP BUFFERS, CALCULATE HEALTH COLOR
  174.     if drainHealthPickup then
  175.         if (healthPickup > 0) or (lostHealthPickup > 0) then   
  176.             healthPickup = math.max(0, healthPickup - (drainSpeed * deltaTime));
  177.             lostHealthPickup = math.max(0, lostHealthPickup - (drainSpeed * deltaTime));
  178.            
  179.         else
  180.             drainHealthPickup = false;
  181.         end
  182.        
  183.         colorHealthFinal = Color(
  184.             lerp(colorHealth.r, colorOverHealth.r, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200)),
  185.             lerp(colorHealth.g, colorOverHealth.g, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200)),
  186.             lerp(colorHealth.b, colorOverHealth.b, ((player.health - (healthPickup - lostHealthDamage) + (healthDamage - lostHealthPickup)) / 200))
  187.         );
  188.     end
  189.  
  190.     -- PREPARE TO DRAW
  191.     local xPos = barHealthOffsetX;
  192.     local yPos = barHealthOffsetY;
  193.    
  194.     local barDamage = healthDamage * barWidth / 200; -- 200 max health
  195.     local barPickup = healthPickup * barWidth / 200;
  196.     local barFull = player.health * barWidth / 200;
  197.     local barEmpty = barWidth - barFull - barDamage;
  198.    
  199.     -- DRAW FRAME
  200.     nvgBeginPath();
  201.     nvgRect(xPos, yPos, barWidth + (2 * frameBorder), barHeight + (2 * frameBorder));
  202.     nvgClosePath();
  203.     nvgFillColor(colorFrame);
  204.     nvgFill();
  205.    
  206.     xPos = xPos + frameBorder;
  207.     yPos = yPos + frameBorder;
  208.    
  209.     -- DRAW HEALTH
  210.     nvgBeginPath();
  211.     nvgRect(xPos, yPos, barFull, barHeight);
  212.     nvgClosePath();
  213.     nvgFillColor(colorHealthFinal);
  214.     nvgFill();
  215.    
  216.     xPos = xPos + barFull;
  217. --  yPos = yPos;
  218.  
  219.     -- DRAW PICKUP
  220.     xPos = xPos - barPickup;
  221.     if healthPickup > 0 then
  222.         nvgBeginPath();
  223.         nvgRect(xPos, yPos, barPickup, barHeight);
  224.         nvgClosePath();
  225.         nvgFillColor(colorHealthPickup);
  226.         nvgFill();
  227.     end
  228.     xPos = xPos + barPickup;
  229.    
  230.     -- DRAW DAMAGE
  231.     if healthDamage > 0 then
  232.         yPos = yPos + (barHeight / 16);
  233.         nvgBeginPath();
  234.         nvgRect(xPos, yPos, barDamage, barHeight - (barHeight / 8 ));
  235.         nvgClosePath();
  236.         nvgFillLinearGradient(xPos, yPos, xPos + barDamage, yPos, colorHealthDamageFinal, Color(0, 0, 0, 0));
  237.         nvgFill();
  238.         yPos = yPos - (barHeight / 16);
  239.     end
  240.    
  241.     -- DRAW DELTA-MARK
  242.     if (healthDamage > 0) or (healthPickup > 0) then
  243.         nvgBeginPath();
  244.         nvgRect(xPos - (barWidth/150), yPos - (barHeight/20), barWidth/75, barHeight + (barHeight/10));
  245.         nvgClosePath();
  246.         nvgFillColor(colorTrim);
  247.         nvgFill();
  248.     end
  249.     xPos = barHealthOffsetX;
  250.     yPos = barHealthOffsetY;
  251.    
  252.     nvgFillColor(colorTrim);
  253.     nvgFontFace(FONT_HUD);
  254.     nvgFontSize(36);
  255.     nvgTextAlign(NVG_ALIGN_RIGHT, NVG_ALIGN_MIDDLE);
  256.     nvgText(xPos - 6, yPos + (2 * barHeight / 3), player.health);
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement