Advertisement
JupiterSky

Snadboox

Dec 12th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.98 KB | None | 0 0
  1. -- title:  Tower Defence Sandbox
  2. -- author: JupiterSky
  3. -- desc:   TD, Sandbox, Survival
  4. -- script: lua
  5.  
  6.  
  7.  
  8.  
  9. local llr = 0
  10.  
  11. local updates = 0
  12. local renders = 0
  13.  
  14. local inventory = {}
  15.  
  16. local x = 96
  17. local y = 24
  18. local z = 2
  19. local vx = 0
  20. local vy = 0
  21. local vz = 0
  22. local onGround = false
  23. local facing = 0
  24. local playerState = 0
  25.  
  26. bl = {1, 2, 3, 4, 5, 6, 7, 17, 33, 34}
  27.  
  28. local placex = 0
  29. local placey = 0
  30. local placeID = 1
  31.  
  32. local camx = 0
  33. local camy = 0
  34.  
  35. world = {}
  36.  
  37. local wminwidth = 0
  38. local wminheight = 0
  39. local wmindepth = 0
  40.  
  41. local wwidth = 30
  42. local wheight = 17
  43. local wdepth = 5
  44.  
  45. local ttemp = {}
  46. ttemp.type = 0
  47. ttemp.state = 0
  48.  
  49. for i = 0, wwidth do
  50.  world[i] = {}
  51.  for k = 0, wheight do
  52.   world[i][k] = {}
  53.   for j = 0, wdepth do
  54.       world[i][k][j] = ttemp
  55.             end
  56.         end
  57.  end
  58.  
  59.  
  60. function TIC()
  61.  
  62. if btnp(4) then
  63.     z = z + 1
  64.     end
  65.  
  66. if btnp(6) then
  67.     placeID = placeID + 1
  68.     end
  69.  
  70. if placeID > #bl then
  71.  placeID = 1
  72.     end
  73.  
  74. if z > wdepth then
  75.     z = 0
  76.     end
  77.  
  78.  
  79. updates = 0
  80. renders = 0
  81.  
  82.  
  83. -- Updates --
  84. updatePlayer()
  85. updateWorld()
  86.  
  87. -- Drawing --
  88. cls(0)
  89. drawWorld()
  90. drawPlayer()
  91.  
  92. local og = 0
  93. if onGroud then
  94.  og = 1
  95. end
  96.  
  97. print("Layer: " .. z .. "\nOn Ground: " .. og .. "\nRenders: " .. renders .. "\nUpdates: " .. updates, 0, 0, 15)
  98.  
  99. end
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. function updatePlayer()
  107.  
  108.  
  109. if btn(0) then
  110.     vy = vy - .3
  111.     facing = 0
  112. end
  113.  
  114. if btn(1) then
  115.     vy = vy + .3
  116.     facing = 2
  117. end
  118.  
  119. if btn(2) then
  120.     vx = vx - .3
  121.     facing = 3
  122. end
  123.  
  124. if btn(3) then
  125.     vx = vx + .3
  126.     facing = 1
  127. end
  128.  
  129. if btnp(7) then
  130.  playerState = playerState + 1
  131.  if playerState > 2 then
  132.      playerState = 0
  133.         end
  134.     end
  135.  
  136.  
  137. if facing == 0 then
  138.  placex = 0
  139.     placey = -1
  140. elseif facing == 1 then
  141.  placex = 1
  142.     placey = 0
  143. elseif facing == 2 then
  144.  placex = 0
  145.     placey = 1
  146. elseif facing == 3 then
  147.  placex = -1
  148.     placey = 0
  149. end
  150.  
  151.  
  152. if btnp(5) and playerState == 1 then
  153.  local tfd = wget(x//8+placex, y//8+placey, z-1)
  154.  local tfu = wget(x//8+placex, y//8+placey, z)
  155.     if tfd.type == 0 then
  156.   createTile(x//8+placex, y//8+placey, z-1, bl[placeID], 0)
  157.  elseif tfu.type == 0 then
  158.   createTile(x//8+placex, y//8+placey, z, bl[placeID], 0)
  159.  end
  160. elseif btnp(5) and playerState == 2 then
  161.  local tfd = wget(x//8+placex, y//8+placey, z-1)
  162.  local tfu = wget(x//8+placex, y//8+placey, z)
  163.     if tfu.type ~= 0 then
  164.   createTile(x//8+placex, y//8+placey, z, 0, 0)
  165.  elseif tfd.type ~= 0 then
  166.   createTile(x//8+placex, y//8+placey, z-1, 0, 0)
  167.  end
  168. end
  169.  
  170.  
  171. if not onGround then
  172.  vz = vz - 0.03
  173. end
  174.  
  175. vx = vx * .7
  176. vy = vy * .7
  177. vz = vz * .98
  178.  
  179. x = x + vx
  180. y = y + vy
  181. z = z + vz
  182.  
  183.  
  184. end
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. function drawPlayer()
  192.  
  193. if playerState == 0 then
  194.  spr(256 + facing, x - 4, y - 4, 1)
  195. elseif playerState == 1 then
  196.  
  197.     rect((x//8+placex)*8, (y//8+placey)*8, 8, 8, 11)
  198.    
  199.  spr(256 + facing + 4, x - 4, y - 4, 1)
  200.    
  201.     spr(bl[placeID], x - 4 + placex*8, y - 4 + placey*8)
  202.    
  203.     line((x-4+placex*8), (y-4+placey*8)-1, (x-4+placex*8)+7, (y-4+placey*8)-1, 0)
  204.     line((x-4+placex*8), (y-4+placey*8)+8, (x-4+placex*8)+7, (y-4+placey*8)+8, 0)
  205.     line((x-4+placex*8)-1, (y-4+placey*8), (x-4+placex*8)-1, (y-4+placey*8)+7, 0)
  206.     line((x-4+placex*8)+8, (y-4+placey*8), (x-4+placex*8)+8, (y-4+placey*8)+7, 0)
  207.    
  208. elseif playerState == 2 then
  209.  
  210.     rect((x//8+placex)*8, (y//8+placey)*8, 8, 8, 6)
  211.    
  212.  spr(256 + facing + 8, x - 4, y - 4, 1)
  213.    
  214.  end
  215.  
  216. end
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. function updateWorld()
  224.  
  225.  
  226. for j = 0, z//1 do
  227.  
  228.  for i = 0, wwidth do
  229.      
  230.         for k = 0, wheight do
  231.      
  232.            
  233.          local t = world[i][k][j]
  234.            
  235.          if t == nil then
  236.                
  237.                 createTile(i, k, j, 0, 0)
  238.                
  239.             elseif t.type == 0 then
  240.                
  241.                 --no 'thang
  242.                
  243.             else
  244.            
  245.            
  246.             local above = wget(i, k, z+1)
  247.            
  248.            
  249.             if i*8 < x+16 and i*8 > x-16 and k*8 < y+16 and k*8 > y-16 and j >= z//1-1 then
  250.                
  251.                 local box1 = {}
  252.                 local box2 = {}
  253.                
  254.                 box1.x = i*8+4
  255.                 box1.y = k*8+4
  256.                
  257.                 box2.x = x
  258.                 box2.y = y
  259.                
  260.                 box1.width = 8
  261.                 box2.width = 6
  262.                
  263.                 local hw1 = box1.width*0.5
  264.                 local hw2 = box2.width*0.5
  265.                 local invjmpx = false
  266.                 local invjmpy = false
  267.                 local xjump = 0
  268.                 local yjump = 0
  269.                 local colx = false
  270.                 local coly = false
  271.                
  272.                 if box2.x >= box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
  273.                
  274.                 local length = box2.x - box1.x
  275.                 local gap = length - hw1 - hw2
  276.                 if gap < 0 then
  277.                  xjump = gap
  278.                  invjmpx = true
  279.                     colx = true
  280.                 end
  281.                
  282.                 elseif box2.x < box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
  283.                
  284.                 local length = box1.x - box2.x
  285.                 local gap = length - hw1 - hw2
  286.                
  287.                 if gap < 0 then
  288.                     xjump = gap
  289.                     invjmpx = false
  290.                     colx = true
  291.                 end
  292.                
  293.                 end
  294.                
  295.                
  296.                
  297.                
  298.                 if box2.y >= box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
  299.                
  300.                 local length = box2.y - box1.y
  301.                 local gap = length - hw1 - hw2
  302.                
  303.                 if gap < 0 then
  304.                     yjump = gap
  305.                     invjmpy = true
  306.                     coly = true
  307.                 end
  308.                
  309.                 elseif box2.y < box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
  310.                
  311.                 local length = box1.y - box2.y
  312.                 local gap = length - hw1 - hw2
  313.                
  314.                 if gap < 0 then
  315.                     yjump = gap
  316.                     invjmpy = false
  317.                     coly = true
  318.                 end
  319.                
  320.                 end
  321.                
  322.                
  323.                
  324.                 if j == z//1 and above.type ~= 0 then
  325.                
  326.                 if yjump > xjump and coly then
  327.                     if invjmpy then
  328.                      y = box2.y - yjump
  329.                     elseif not invjmpy then
  330.                      y = box2.y + yjump
  331.                     end
  332.                 elseif xjump > yjump and colx then
  333.                     if invjmpx then
  334.                      x = box2.x - xjump
  335.                     elseif not invjmpx then
  336.                         x = box2.x + xjump
  337.                     end
  338.                 end
  339.                
  340.                
  341.                 elseif j == z//1 and above.type == 0 then
  342.                     z = z + 1
  343.                
  344.                 elseif j == z//1-1 then
  345.                  if colx or coly then
  346.                      onGround = true
  347.                         z = j + 1
  348.                         vz = 0
  349.                     else
  350.                         onGround = false
  351.                     end
  352.                 end
  353.                
  354.                
  355.                 end
  356.                 end
  357.                
  358.                
  359.                 updates = updates + 1
  360.                
  361.             end
  362.         end
  363.     end
  364. end
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. function drawWorld()
  372.  
  373.  
  374. for j = 0, z//1 do
  375.  
  376.  for i = 0, wwidth do
  377.      
  378.         for k = 0, wheight do
  379.      
  380.            
  381.             if i*8-camx >= 0 and i*8-camx <= 240 and k*8-camy >= 0 and k*8-camy <= 136 then
  382.  
  383.  
  384.          local t = world[i][k][j]
  385.            
  386.             renders = renders + 1
  387.            
  388.             if t.type == 0 then
  389.            
  390.             --llr = j
  391.              
  392.             elseif t.type ~= 34 then
  393.                
  394.                
  395.                 local n = wget(i, k - 1, j)
  396.              local s = wget(i, k + 1, j)
  397.                 local e = wget(i + 1, k, j)
  398.                 local w = wget(i - 1, k, j)
  399.                
  400.                
  401.        spr(t.type, i*8, k*8)
  402.                
  403.                 if w.type == 0 or w.type == 34 then
  404.                  line(i*8-1, k*8, i*8-1, k*8+7, 0)
  405.                     end
  406.                 if e.type == 0 or e.type == 34 then
  407.                  line(i*8+8, k*8, i*8+8, k*8+7, 0)
  408.                     end
  409.                 if n.type == 0 or n.type == 34 then
  410.                  line(i*8, k*8-1, i*8+7, k*8-1, 0)
  411.                     end
  412.                 if s.type == 0 or s.type == 34 then
  413.                  line(i*8, k*8+8, i*8+7, k*8+8, 0)
  414.                     end
  415.                
  416.                 --print(j, i*8, k*8)
  417.                
  418.                 llr = j
  419.                
  420.                 elseif t.type == 34 then
  421.                
  422.                 local n = wget(i, k - 1, j)
  423.              local s = wget(i, k + 1, j)
  424.                 local e = wget(i + 1, k, j)
  425.                 local w = wget(i - 1, k, j)
  426.                
  427.        spr(t.type, i*8, k*8, 1)
  428.                
  429.                 if w.type ~= 0 then
  430.         spr(t.type + 4, i*8, k*8, 1)
  431.                     end
  432.                 if e.type ~= 0 then
  433.         spr(t.type + 2, i*8, k*8, 1)
  434.                     end
  435.                 if n.type ~= 0 then
  436.         spr(t.type + 1, i*8, k*8, 1)
  437.                     end
  438.                 if s.type ~= 0 then
  439.         spr(t.type + 3, i*8, k*8, 1)
  440.                     end
  441.                
  442.                 llr = j
  443.                
  444.                 end
  445.                
  446.              end
  447.            
  448.             end
  449.      
  450.         end
  451.    
  452.     end
  453.  
  454. end
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461. function wget(x, y, z)
  462.  
  463. local t = {}
  464.  
  465. if x >= wminwidth and y >= wminheight and wmindepth >= 0 and x <= wwidth and y <= wheight and z <= wdepth then
  466.     t = world[x][y][z]
  467.  end
  468.  
  469. if t == nil then
  470.     t = {}
  471.     t.type = 0
  472.     t.state = 0
  473.     end
  474.  
  475. return t
  476.  
  477. end
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484. function createTile(x, y, z, type, state)
  485.  
  486. local t = {}
  487.  
  488. t.type = type
  489. t.state = state
  490.  
  491. world[x][y][z] = t
  492.  
  493. end
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. function _INIT()
  501.  
  502. for i = 0, 30 do
  503.     for k = 0, 17 do
  504.        
  505.         createTile(i, k, 0, 1, 0)
  506.        
  507.     end
  508. end
  509.  
  510. end
  511.  
  512. _INIT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement