Skaruts

rl.lua

Jul 13th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. -- title:  game title
  2. -- author: game developer
  3. -- desc:   short description
  4. -- script: lua
  5. -- input: keyboard
  6.  
  7. local key_repeat          = true
  8. local key_cooldown        = 0.10
  9. local key_1st_cooldown    = 0.25
  10. local key_timer           = 0
  11. local max_key_timer       = key_1st_cooldown
  12.  
  13. local k_left  = false     -- key flags
  14. local k_right = false
  15. local k_up    = false
  16. local k_down  = false
  17.  
  18. local moving_left     = false -- movement flags for no-key-repeat
  19. local moving_right    = false
  20. local moving_up       = false
  21. local moving_down     = false
  22. local moving          = false
  23.  
  24. local UP, DOWN, LEFT, RIGHT = 58, 59, 60, 61
  25. local SS = 8 -- sprite size
  26. local t_old = 0
  27.  
  28. local x, y = 8*14, 8*8
  29.  
  30. function TIC()
  31.     local t_now = time()/1000
  32.     local dt = t_now-t_old
  33.     t_old = t_now
  34.  
  35.     took_turn, k_left, k_right, k_up, k_down = false, false, false, false, false
  36.     cls(2)
  37.  
  38.     if key(UP)    then k_up    = true end    -- also works with   "keyp(UP,    0, 0)"
  39.     if key(DOWN)  then k_down  = true end    -- also works with   "keyp(DOWN,  0, 0)"
  40.     if key(LEFT)  then k_left  = true end    -- also works with   "keyp(LEFT,  0, 0)"
  41.     if key(RIGHT) then k_right = true end    -- also works with   "keyp(RIGHT, 0, 0)"
  42.  
  43.     if key_repeat then move_repeat(dt)
  44.     else               move_once() end
  45.  
  46.     spr(64, x, y, 14)
  47.     print("Took turn", 8, 8, took_turn and 14 or 7)
  48. end
  49.  
  50. function move_once()
  51.     if k_left and not moving_left and not k_right then
  52.         took_turn = move( -1, 0 )
  53.         moving_left = true
  54.     elseif not k_left and moving_left then
  55.         moving_left = false
  56.     end
  57.  
  58.     if k_right and not moving_right and not k_left then
  59.         took_turn = move( 1, 0 )
  60.         moving_right = true
  61.     elseif not k_right and moving_right then
  62.         moving_right = false
  63.     end
  64.  
  65.     if k_up and not moving_up and not k_down then
  66.         took_turn = move( 0, -1 )
  67.         moving_up = true
  68.     elseif not k_up and moving_up then
  69.         moving_up = false
  70.     end
  71.  
  72.     if k_down and not moving_down and not k_up then
  73.         took_turn = move( 0, 1 )
  74.         moving_down = true
  75.     elseif not k_down and moving_down then
  76.         moving_down = false
  77.     end
  78. end
  79.  
  80. function move_repeat(dt)
  81.     -- if already moving since last frame, make the cooldown shorter
  82.     -- else, make it longer for the first key press
  83.     if moving then max_key_timer = key_cooldown
  84.     else           max_key_timer = key_1st_cooldown end
  85.  
  86.     -- check if any movement key is being pressed
  87.     moving = k_left or k_right or k_up or k_down
  88.  
  89.     -- if none is pressed, reset the timer so the player is free to move
  90.     if not moving then key_timer = 0 end
  91.  
  92.     -- if no cooldown and a key was pressed
  93.     if key_timer <= 0 and moving then
  94.         if k_left and not k_right then
  95.             if move(-1,  0) then took_turn = true end end
  96.         if k_right and not k_left then
  97.             if move( 1,  0) then took_turn = true end end
  98.         if k_up and not k_down then
  99.             if move( 0, -1) then took_turn = true end end
  100.         if k_down and not k_up then
  101.             if move( 0,  1) then took_turn = true end end
  102.  
  103.         if took_turn then
  104.             key_timer = max_key_timer end  -- set the cooldown
  105.  
  106.     -- else if still cooling down, just reduce timer by dt
  107.     else
  108.         key_timer = key_timer-dt
  109.     end
  110. end
  111.  
  112. function move(dx, dy)
  113.     -- check against obstacles/mobs if player can_move()
  114.     x = x + dx*SS
  115.     y = y + dy*SS
  116.     return true
  117. end
Advertisement
Add Comment
Please, Sign In to add comment