Advertisement
Jummit

Lua Computercraft Rotation Demo

Apr 16th, 2018
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local image = {
  2.   {
  3.     0,
  4.     0,
  5.     0,
  6.     128,
  7.   },
  8.   {
  9.     0,
  10.     0,
  11.     128,
  12.     128,
  13.     128,
  14.   },
  15.   {
  16.     0,
  17.     0,
  18.     128,
  19.     128,
  20.     128,
  21.   },
  22.   {
  23.     0,
  24.     256,
  25.     128,
  26.     128,
  27.     128,
  28.     256,
  29.   },
  30.   {
  31.     256,
  32.     256,
  33.     128,
  34.     128,
  35.     128,
  36.     256,
  37.     256,
  38.   },
  39.   {
  40.     0,
  41.     0,
  42.     0,
  43.     16384,
  44.   },
  45.   {
  46.     0,
  47.     0,
  48.     0,
  49.     2,
  50.     16384,
  51.   },
  52.   {
  53.     0,
  54.     0,
  55.     16384,
  56.     2,
  57.     16,
  58.     16384,
  59.   },
  60.   {
  61.     0,
  62.     0,
  63.     0,
  64.     16384,
  65.     2,
  66.   },
  67.   {
  68.     0,
  69.     0,
  70.     0,
  71.     2,
  72.     16384,
  73.   },
  74. }
  75. if fs.exists("rocket.nfp") then
  76.    image = paintutils.loadImage("rocket.nfp")
  77. end
  78. local w, h = term.getSize()
  79. local buffer = window.create(term.current(), 1, 1, w, h)
  80. local oldTerm = term.redirect(buffer)
  81. local rotation = 0
  82. local rocketX, rocketY = 10, 10
  83. local imageH, imageW = #image, 0
  84. for y = 1, #image do
  85.   if #image[y] > imageW then
  86.     imageW = #image[y]
  87.   end
  88. end
  89. local keyboard = {}
  90. local particles = {}
  91.  
  92. function newParticle()
  93.   table.insert(particles, {
  94.     x = rocketX, y = rocketY, mx = math.sin(rotation+math.random(-10, 10)/10), my = math.cos(rotation+math.random(-10, 10)/10), age = 0, color = colors.orange
  95.   })
  96. end
  97. function drawParticles()
  98.   local toRemoveParticles = {}
  99.   for i = 1, #particles do
  100.     local particle = particles[i]
  101.     paintutils.drawPixel(particle.x, particle.y, particle.color)
  102.     if particle.color == colors.yellow then
  103.       particle.color = colors.orange
  104.     else
  105.       particle.color = colors.yellow
  106.     end
  107.  
  108.     particle.x = particle.x+particle.mx
  109.     particle.y = particle.y+particle.my
  110.  
  111.     particle.age = particle.age + 1
  112.     if particle.age > 10 then
  113.       table.insert(toRemoveParticles, i)
  114.     end
  115.   end
  116.   for i = 1, #toRemoveParticles do
  117.     table.remove(particles, toRemoveParticles[i])
  118.   end
  119. end
  120.  
  121. function drawRocket()
  122.   for y = 1, #image do
  123.     for x = 1, #image[y] do
  124.       if image[y][x] ~= 0 then
  125.         local drawx = (x-imageW/2)*math.cos(rotation)+(y-imageH/2)*math.sin(rotation)
  126.         local drawy = -(x-imageW/2)*math.sin(rotation)+(y-imageH/2)*math.cos(rotation)
  127.         paintutils.drawPixel(drawx+rocketX, drawy+rocketY, image[y][x])
  128.       end
  129.     end
  130.   end
  131. end
  132.  
  133. while true do
  134.   buffer.setVisible(false)
  135.  
  136.   term.setBackgroundColor(colors.black)
  137.   term.clear()
  138.  
  139.   drawRocket()
  140.   drawParticles()
  141.  
  142.   buffer.setVisible(true)
  143.   buffer.redraw()
  144.  
  145.   local timer = os.startTimer(0.01)
  146.   local event, key = os.pullEventRaw()
  147.   if event == "terminate" then break end
  148.   os.cancelTimer(timer)
  149.  
  150.   if event == "key_up" then
  151.     keyboard[keys.getName(key)] = false
  152.   elseif event == "key" then
  153.     keyboard[keys.getName(key)] = true
  154.   end
  155.  
  156.   if keyboard.right then
  157.     rotation = rotation - 0.1
  158.   elseif keyboard.left then
  159.     rotation = rotation + 0.1
  160.   end
  161.   if keyboard.up then
  162.     rocketX = rocketX-math.sin(rotation)/2
  163.     rocketY = rocketY-math.cos(rotation)/2
  164.     newParticle()
  165.   end
  166.  
  167.   if rocketX > w then rocketX = 1 end
  168.   if rocketY > h then rocketY = 1 end
  169.   if rocketX < 1 then rocketX = w end
  170.   if rocketY < 1 then rocketY = h end
  171. end
  172.  
  173. term.redirect(oldTerm)
  174. term.setBackgroundColor(colors.black)
  175. term.clear()
  176. term.setCursorPos(1, h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement