Advertisement
Guest User

Untitled

a guest
Dec 16th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. -- Button state management - Ever want an easy way to know whether a button's been tapped, held, released, for how long? Well, here you go. player.buttonstate[blah] returns x frames being held, or -x frames being released
  2. local dupeprevent = false
  3.  
  4. addHook("MobjThinker", function(mo)
  5.     local player = mo.player
  6.     if player.buttonstate and not dupeprevent then return end -- Avoid processing buttonstate multiple times per frame if multiple loaded addons have this code snippet in them!
  7.     dupeprevent = true
  8.    
  9.     if not player.buttonstate then player.buttonstate = {} end
  10.     local bs = player.buttonstate
  11.    
  12.     local function state(cond, key)
  13.         if cond then
  14.             bs[key] = max(($1 or 0)+1, 1)
  15.         else
  16.             bs[key] = min(($1 or 0)-1, -1)
  17.         end
  18.     end
  19.    
  20.     -- Handle normal buttons - get these with player.buttonstate[BT_WHATEVER]
  21.     for _,v in ipairs({
  22.         BT_JUMP, BT_USE, BT_ATTACK, BT_FIRENORMAL,
  23.         BT_CAMLEFT, BT_CAMRIGHT,
  24.         BT_WEAPONNEXT, BT_WEAPONPREV, BT_TOSSFLAG,
  25.         BT_CUSTOM1, BT_CUSTOM2, BT_CUSTOM3
  26.     }) do
  27.         state(player.cmd.buttons & v, v)
  28.     end
  29.    
  30.     -- Handle weapon quick select buttons - get these with player.buttonstate[weaponnum] (can probably use WEP_WHATEVER+1?)
  31.     for i = 1,BT_WEAPONMASK-1 do
  32.         state(player.cmd.buttons & BT_WEAPONMASK == i, i)
  33.     end
  34.    
  35.     -- Finally, handle directional taps (no analog support, soz) - get these with player.buttonstate["direction"] (up, down, left, right)
  36.     for k,v in pairs({
  37.         up = function(p) return p.cmd.forwardmove > 0 end,
  38.         down = function(p) return p.cmd.forwardmove < 0 end,
  39.         left = function(p) return p.cmd.sidemove < 0 end,
  40.         right = function(p) return p.cmd.sidemove > 0 end
  41.     }) do
  42.         state(v(player), k)
  43.     end
  44. end, MT_PLAYER)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement