Advertisement
billysback

Dodger3D

Dec 17th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. local tx,ty = term.getSize()
  2.  
  3. local function createWorld()
  4.     local world = {
  5.         sprites = {},
  6.         camera = {},
  7.         getDistance = function(x1, y1, z1, x2, y2, z2)
  8.             local mn = {x1, y1, z1}
  9.             local mx = {x2, y2, z2}
  10.             --[[for i=1,#mn do
  11.                 if mn[i] > mx[i] then
  12.                     local temp = mx[i]
  13.                     mx[i] = mn[i]
  14.                     mn[i] = temp
  15.                 end
  16.             end]]
  17.            
  18.             local dif = {mx[1]-mn[1], mx[2]-mn[2], mx[3]-mn[3]}
  19.             local dist = (dif[1]+dif[2]+dif[3])/2
  20.             return {dist, dif}
  21.         end,
  22.         addSprite = function(self, sprite)
  23.             local sprites = self.sprites
  24.             if sprites == nil then sprites = {} end
  25.             sprites[#sprites + 1] = sprite
  26.             self.sprites = sprites
  27.         end,
  28.         checkAll = function(self, s)
  29.             if self.sprites ~= nil then
  30.                 for i=1,#self.sprites do
  31.                     if check(s, self.sprites[i]) then return true end
  32.                 end
  33.             end
  34.             return false
  35.         end,
  36.         check = function(s1, s2)
  37.             for x=s1.x,s1.x+s1.width do
  38.                 for y=s1.y,s1.y+s1.height do
  39.                     for z=s1.z,s1.z+s1.depth do
  40.                        
  41.                         for x2=s2.x,s2.x+s2.width do
  42.                             for y2=s2.y,s2.y+s2.height do
  43.                                 for z2=s2.z,s2.z+s2.depth do
  44.                                     if x == x2 and y == y2 and z == z2 then return true end
  45.                                 end
  46.                             end
  47.                         end
  48.                     end
  49.                 end
  50.             end
  51.             return false
  52.         end,
  53.         draw = function(self)
  54.        
  55.             if self.sprites ~= nil and self.camera ~= nil then
  56.                 for i=1,#self.sprites do
  57.                     local sprite = self.sprites[i]
  58.                     self:drawSprite(sprite.x,
  59.                                     sprite.y,
  60.                                     sprite.z,
  61.                                     sprite.width,
  62.                                     sprite.height,
  63.                                     sprite.depth,
  64.                                     sprite.char,
  65.                                     sprite.cols,
  66.                                     self.getDistance(self.camera[4][1], self.camera[4][2], self.camera[4][3], sprite.x, sprite.y, sprite.z))
  67.                 end
  68.             end
  69.         end,
  70.         convertDifference = function(dir, dims)
  71.             local ndif = {0,0,0}
  72.             for i=1,dims do
  73.                 if dir[i] < 0 then ndif[i] = -1
  74.                 elseif dir[i] > 0 then ndif[i] = 1 end
  75.             end
  76.             return ndif
  77.         end,
  78.         checkViewpoint = function(self, dir, dif, dims)
  79.             dif = self.convertDifference(dir, dims)
  80.             for i=1,dims do
  81.                 if dir[i] ~= dif[i] then return false end
  82.             end
  83.             return true
  84.         end,
  85.         drawSprite = function(self, x, y, z, width, height, depth, char, cols, dist)
  86.             local scale = (dist[2][3]/2)
  87.             --local scale = dist[1]
  88.             local todo = 1
  89.             if scale < 0 then todo = -1 end
  90.             local scale = math.floor(scale + 0.5)
  91.             if self:checkViewpoint(self.camera, dist[2], 3) then
  92.                 --for sz=z,depth do
  93.                     local from = {x+self.camera[4][1],y+self.camera[4][2]}
  94.                     local to = {x+width+self.camera[4][1],y+height+self.camera[4][2]}
  95.                     local ft = true
  96.                     for i=1,scale do
  97.                         if ft == true then
  98.                             from[1] = from[1] - todo
  99.                             from[2] = from[2] - todo
  100.                             ft = false
  101.                         else
  102.                             to[1] = to[1] + todo
  103.                             to[2] = to[2] + todo
  104.                             ft = true
  105.                         end
  106.                     end
  107.                    
  108.                     for sx=from[1],to[1] do
  109.                         for sy=from[2],to[2] do
  110.                             if sx > 0 and sx <= tx and sy > 0 and sy <= ty then
  111.                                 term.setCursorPos(sx, sy)
  112.                                 term.setBackgroundColor(cols[1])
  113.                                 term.setTextColor(cols[2])
  114.                                 term.write(char)
  115.                             end
  116.                         end
  117.                     end
  118.                     --scale = scale + 0.1
  119.                 --end
  120.             end
  121.         end,
  122.     }
  123.     return world
  124. end
  125.  
  126. local function createSprite(sworld, sx, sy, sz, swidth, sheight, sdepth, sback_col, stext_col, schar)
  127.     local sprite = {
  128.         x = sx,
  129.         y = sy,
  130.         z = sz,
  131.         width = swidth,
  132.         height = sheight,
  133.         depth = sdepth,
  134.         cols = {sback_col, stext_col},
  135.         char = schar,
  136.         children = {},
  137.         world = sworld,
  138.         moveChildren = function(self, dx, dy, dz, clever)
  139.             if children ~= nil then
  140.                 for i=1,#self.children do self.children[i]:move(dx, dy, dz, clever) end
  141.             end
  142.         end,
  143.         move = function(self, dx, dy, dz, clever)
  144.             local old = {self.x, self.y, self.z}
  145.             self.x = self.x + dx
  146.             self.y = self.y + dy
  147.             self.z = self.z + sz
  148.             if clever then
  149.                 if self.world:checkAll(self) then
  150.                     self.x = old[1]
  151.                     self.y = old[2]
  152.                     self.z = old[3]
  153.                     return false
  154.                 end
  155.             end
  156.             moveChildren(dx, dy, dz, clever)
  157.             return true
  158.         end,
  159.     }
  160.     return sprite
  161. end
  162.  
  163. local function clear()
  164.     term.setBackgroundColor(colors.black)
  165.     term.setTextColor(colors.black)
  166.     term.clear()
  167.     term.setCursorPos(1,1)
  168. end
  169.  
  170. --sprite function:
  171. --world, x, y, z, width, height, depth, background_colour, text_colour, character
  172. local world = createWorld()
  173. local block = createSprite(world, 3, 3, 1, 1, 1, 1, colors.red, colors.green, "#")
  174. local b2 = createSprite(world, 17, 13, 1, 1, 1, 1, colors.blue, colors.yellow, "#")
  175.  
  176. world.camera = {1, 1, 1, {0,0,0}}
  177. world:addSprite(block)
  178. world:addSprite(b2)
  179.  
  180. term.clear()
  181. term.setCursorPos(1,1)
  182. world:draw()
  183.  
  184. local on = true
  185. local interval = 0.2
  186.  
  187. local timer = os.startTimer(0)
  188.  
  189. local function moveC(dx, dy, dz)
  190.     world.camera[4][1] = world.camera[4][1] + dx
  191.     world.camera[4][2] = world.camera[4][2] + dy
  192.     world.camera[4][3] = world.camera[4][3] + dz
  193. end
  194.  
  195. while on do
  196.     local event, p1 = os.pullEvent()
  197.     if event == "timer" and p1 == timer then
  198.         clear()
  199.         world:draw()
  200.         timer = os.startTimer(interval)
  201.     elseif event == "key" then
  202.         local key = p1
  203.         if key == 29 then
  204.             on = false
  205.         elseif key == 200 then --up
  206.             moveC(0, 0, 1)
  207.         elseif key == 203 then --left
  208.             moveC(-1, 0, 0)
  209.         elseif key == 208 then --down
  210.             moveC(0, 0, -1)
  211.         elseif key == 205 then --right 
  212.             moveC(1, 0, 0)
  213.         end
  214.     end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement