Advertisement
LDDestroier

dude.lua

Aug 3rd, 2020
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. scr_x, scr_y = term.getSize()
  2.  
  3. local dude = {
  4.     x = scr_x / 2,
  5.     y = scr_y / 2,
  6.     oldx = scr_x / 2,
  7.     oldy = scr_y / 2,
  8.     xvel = 0,
  9.     yvel = 0,
  10.     speed = 0.6,
  11.     friction = 1.5,
  12.  
  13.     bullets = {},
  14.     bulletSpeed = 1
  15. }
  16.  
  17. local tick = function(dude)
  18.     dude.oldx = dude.x
  19.     dude.oldy = dude.y
  20.     dude.x = dude.x + dude.xvel
  21.     dude.y = dude.y + dude.yvel
  22.     dude.xvel = dude.xvel / dude.friction
  23.     dude.yvel = dude.yvel / dude.friction
  24.     local rList = {}
  25.     for i = 1, #dude.bullets do
  26.         if not (inRange(dude.bullets[i].x, 1, scr_x + 1) and inRange(dude.bullets[i].y, 1, scr_y + 1)) then
  27.             if dude.bullets[i].offScreen == false then
  28.                 rList[#rList + 1] = i
  29.             end
  30.         else
  31.             dude.bullets[i].offScreen = false
  32.         end
  33.         dude.bullets[i].oldx = dude.bullets[i].x
  34.         dude.bullets[i].oldy = dude.bullets[i].y
  35.         dude.bullets[i].x = dude.bullets[i].x + dude.bullets[i].xvel
  36.         dude.bullets[i].y = dude.bullets[i].y + dude.bullets[i].yvel
  37.         dude.bullets[i].xvel = dude.bullets[i].xvel / dude.bullets[i].friction
  38.         dude.bullets[i].yvel = dude.bullets[i].yvel / dude.bullets[i].friction
  39.     end
  40.     for i = #rList, 1, -1 do
  41.         table.remove(dude.bullets, rList[i])
  42.     end
  43. end
  44.  
  45. local render = function()
  46.     if (math.floor(dude.oldx) ~= math.floor(dude.x)) or (math.floor(dude.oldy) ~= math.floor(dude.y)) then
  47.         term.setCursorPos(dude.oldx, dude.oldy)
  48.         term.write(" ")
  49.     end
  50.     term.setCursorPos(dude.x, dude.y)
  51.     term.write("O")
  52.     for i = 1, #dude.bullets do
  53.         if (math.floor(dude.bullets[i].oldx) ~= math.floor(dude.bullets[i].x)) or (math.floor(dude.bullets[i].oldy) ~= math.floor(dude.bullets[i].y)) then
  54.             term.setCursorPos(dude.bullets[i].oldx, dude.bullets[i].oldy)
  55.             term.write(" ")
  56.         end
  57.         term.setCursorPos(dude.bullets[i].x, dude.bullets[i].y)
  58.         term.write("x")
  59.     end
  60. end
  61.  
  62. local spawnBullet = function(dude, xMult, yMult)
  63.     table.insert(dude.bullets, {
  64.         x = dude.x,
  65.         y = dude.y,
  66.         oldx = dude.oldx,
  67.         oldy = dude.oldy,
  68.         xvel = dude.bulletSpeed * xMult,
  69.         yvel = dude.bulletSpeed * yMult,
  70.         offScreen = not (inRange(dude.x, 1, scr_x + 1) and inRange(dude.y, 1, scr_y + 1)),
  71.         friction = 1
  72.     })
  73. end
  74.  
  75. inRange = function(num, min, max)
  76.     return num >= min and num <= max
  77. end
  78.  
  79. local tickTimer = os.startTimer(0.05)
  80. local keysDown = {}
  81. local evt
  82. term.clear()
  83. while true do
  84.     evt = {os.pullEvent()}
  85.     if evt[1] == "key" and not evt[3] then
  86.         keysDown[evt[2]] = 0
  87.     elseif evt[1] == "key_up" then
  88.         keysDown[evt[2]] = nil
  89.     elseif evt[1] == "timer" and evt[2] == tickTimer then
  90.         tick(dude)
  91.         tickTimer = os.startTimer(0.05)
  92.  
  93.         if keysDown[keys.w] then
  94.             dude.yvel = dude.yvel - dude.speed
  95.         end
  96.         if keysDown[keys.s] then
  97.             dude.yvel = dude.yvel + dude.speed
  98.         end
  99.         if keysDown[keys.a] then
  100.             dude.xvel = dude.xvel - dude.speed
  101.         end
  102.         if keysDown[keys.d] then
  103.             dude.xvel = dude.xvel + dude.speed
  104.         end
  105.  
  106.         if keysDown[keys.up] == 0 then
  107.             spawnBullet(dude, 0, -1)
  108.         end
  109.         if keysDown[keys.down] == 0 then
  110.             spawnBullet(dude, 0, 1)
  111.         end
  112.         if keysDown[keys.left] == 0 then
  113.             spawnBullet(dude, -1, 0)
  114.         end
  115.         if keysDown[keys.right] == 0 then
  116.             spawnBullet(dude, 1, 0)
  117.         end
  118.  
  119.         for k,v in pairs(keysDown) do
  120.             keysDown[k] = v + 1
  121.         end
  122.  
  123.         render()
  124.     end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement