Guest User

3d

a guest
Aug 8th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. -- 3D cube/prism created by MagnusCaligo
  2. -- Coding style by Nitrogen Fingers
  3. l, h = term.getSize()
  4. d = 10
  5. running = true
  6.  
  7. -- Allows the points to have a "refrence" point for "smooth" animation
  8. -- through the 3rd dimension
  9. local cube = {
  10.   xpos = l/2;
  11.   ypos = h/2;
  12.   zpos = d/2;
  13.   points = {};
  14. }
  15.  
  16. -- Spawns a point in as a sprite in the points table above ^
  17. function spawnPoint(x, y, z)
  18.   local point = {
  19.     xpos = x;
  20.     ypos = y;
  21.     zpos = z;
  22.     col = 1;
  23.    
  24.     update = function(self, p1)
  25.       if p1 == keys.a then
  26.         self.xpos = self.xpos - (self.zpos/d)
  27.       elseif p1 == keys.d then
  28.         self.xpos = self.xpos + (self.zpos/d)
  29.       elseif p1 == keys.s then
  30.         self.ypos = self.ypos + (self.zpos/d)
  31.       elseif p1 == keys.w then
  32.         self.ypos = self.ypos - (self.zpos/d)
  33.       elseif p1 == keys.q then
  34.         self.zpos = self.zpos - (self.zpos/d)
  35.         self:zUpdate(1)
  36.       elseif p1 == keys.e then
  37.         self.zpos = self.zpos + (self.zpos/d)
  38.         self:zUpdate(2)
  39.       end
  40.     end;
  41.      
  42.     zUpdate = function(self, p1)
  43.       if p1 == 1 then
  44.         if self.xpos > cube.xpos then
  45.           self.xpos = self.xpos - 1
  46.         elseif self.xpos < cube.xpos then
  47.           self.xpos = self.xpos + 1
  48.         end
  49.         if self.ypos > cube.ypos then
  50.           self.ypos = self.ypos - 1
  51.         elseif self.ypos < cube.ypos then
  52.           self.ypos = self.ypos + 1
  53.         end
  54.       elseif p1 == 2 then
  55.         if self.xpos > cube.xpos then
  56.           self.xpos = self.xpos + 1
  57.         elseif self.xpos < cube.xpos then
  58.           self.xpos = self.xpos - 1
  59.         end
  60.         if self.ypos > cube.ypos then
  61.           self.ypos = self.ypos + 1
  62.         elseif self.ypos < cube.ypos then
  63.           self.ypos = self.ypos - 1
  64.         end
  65.       end
  66.     end;
  67.    
  68.     draw = function(self)
  69.       term.setTextColour(2^self.col)
  70.       term.setCursorPos(self.xpos, self.ypos)
  71.       if self.zpos/d <= .4 then
  72.         term.setBackgroundColour(colours.black)
  73.         term.write("*")
  74.       elseif self.zpos/d < .5 then
  75.         term.setBackgroundColour(colours.black)
  76.         term.write("&")
  77.       elseif self.zpos/d < .8 then
  78.         term.setBackgroundColour(2^self.col)
  79.         term.write(" ")
  80.       elseif self.zpos/d <= 1 then
  81.         term.setBackgroundColour(2^self.col)
  82.         term.write("  ")
  83.         term.setCursorPos(self.xpos, self.ypos + 1)
  84.         term.write("  ")
  85.       elseif self.zpos/d > 1 then
  86.         term.setBackgroundColour(2^self.col)
  87.         for x = 0, 2 do
  88.           for y = 0, 1 do
  89.             term.setCursorPos(self.xpos + x, self.ypos + y)
  90.             term.write(" ")
  91.           end
  92.         end
  93.       end
  94.     end;
  95.   }
  96.   return point
  97. end
  98.  
  99. -- Just inserts all the points, I never really considered a way to optimize this XD
  100. table.insert(cube.points, spawnPoint(l/2, h/2, d - 5))
  101. table.insert(cube.points, spawnPoint(l/2 + 5, h/2, d -5))
  102. table.insert(cube.points, spawnPoint(l/2 + 5, h/2 + 5, d - 5))
  103. table.insert(cube.points, spawnPoint(l/2, h/2 + 5, d - 5))
  104. table.insert(cube.points, spawnPoint(l/2, h/2, d))
  105. table.insert(cube.points, spawnPoint(l/2 + 5, h/2, d))
  106. table.insert(cube.points, spawnPoint(l/2 + 5, h/2 + 5, d))
  107. table.insert(cube.points, spawnPoint(l/2, h/2 + 5, d))
  108.  
  109.  
  110. -- The rest of the game essentials (I learned this from Nitrogen Finers)
  111. function drawGame()
  112.   term.setBackgroundColour(colours.black)
  113.   term.clear()
  114.   for i,v in pairs(cube.points) do
  115.     v:draw()
  116.   end
  117. end
  118.  
  119. function updateGame()
  120.   id, p1, p2 = os.pullEvent()
  121.   if id == "key" then
  122.     if p1 == keys.t then
  123.       running = false
  124.     else
  125.       for i,v in pairs(cube.points) do
  126.         v:update(p1)
  127.       end
  128.     end
  129.   end
  130. end
  131.  
  132. function gameLoop()
  133.   while running do
  134.     drawGame()
  135.     updateGame()
  136.   end
  137. end
  138.  
  139. gameLoop()
  140. term.setBackgroundColour(colours.black)
  141. shell.run("clear")
  142. sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment