Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- title: Tower Defence Sandbox
- -- author: JupiterSky
- -- desc: TD, Sandbox, Survival
- -- script: lua
- local llr = 0
- local updates = 0
- local renders = 0
- local inventory = {}
- local x = 96
- local y = 24
- local z = 2
- local vx = 0
- local vy = 0
- local vz = 0
- local onGround = false
- local facing = 0
- local playerState = 0
- bl = {1, 2, 3, 4, 5, 6, 7, 17, 33, 34}
- local placex = 0
- local placey = 0
- local placeID = 1
- local camx = 0
- local camy = 0
- world = {}
- local wminwidth = 0
- local wminheight = 0
- local wmindepth = 0
- local wwidth = 30
- local wheight = 17
- local wdepth = 5
- local ttemp = {}
- ttemp.type = 0
- ttemp.state = 0
- for i = 0, wwidth do
- world[i] = {}
- for k = 0, wheight do
- world[i][k] = {}
- for j = 0, wdepth do
- world[i][k][j] = ttemp
- end
- end
- end
- function TIC()
- if btnp(4) then
- z = z + 1
- end
- if btnp(6) then
- placeID = placeID + 1
- end
- if placeID > #bl then
- placeID = 1
- end
- if z > wdepth then
- z = 0
- end
- updates = 0
- renders = 0
- -- Updates --
- updatePlayer()
- updateWorld()
- -- Drawing --
- cls(0)
- drawWorld()
- drawPlayer()
- local og = 0
- if onGroud then
- og = 1
- end
- print("Layer: " .. z .. "\nOn Ground: " .. og .. "\nRenders: " .. renders .. "\nUpdates: " .. updates, 0, 0, 15)
- end
- function updatePlayer()
- if btn(0) then
- vy = vy - .3
- facing = 0
- end
- if btn(1) then
- vy = vy + .3
- facing = 2
- end
- if btn(2) then
- vx = vx - .3
- facing = 3
- end
- if btn(3) then
- vx = vx + .3
- facing = 1
- end
- if btnp(7) then
- playerState = playerState + 1
- if playerState > 2 then
- playerState = 0
- end
- end
- if facing == 0 then
- placex = 0
- placey = -1
- elseif facing == 1 then
- placex = 1
- placey = 0
- elseif facing == 2 then
- placex = 0
- placey = 1
- elseif facing == 3 then
- placex = -1
- placey = 0
- end
- if btnp(5) and playerState == 1 then
- local tfd = wget(x//8+placex, y//8+placey, z-1)
- local tfu = wget(x//8+placex, y//8+placey, z)
- if tfd.type == 0 then
- createTile(x//8+placex, y//8+placey, z-1, bl[placeID], 0)
- elseif tfu.type == 0 then
- createTile(x//8+placex, y//8+placey, z, bl[placeID], 0)
- end
- elseif btnp(5) and playerState == 2 then
- local tfd = wget(x//8+placex, y//8+placey, z-1)
- local tfu = wget(x//8+placex, y//8+placey, z)
- if tfu.type ~= 0 then
- createTile(x//8+placex, y//8+placey, z, 0, 0)
- elseif tfd.type ~= 0 then
- createTile(x//8+placex, y//8+placey, z-1, 0, 0)
- end
- end
- if not onGround then
- vz = vz - 0.03
- end
- vx = vx * .7
- vy = vy * .7
- vz = vz * .98
- x = x + vx
- y = y + vy
- z = z + vz
- end
- function drawPlayer()
- if playerState == 0 then
- spr(256 + facing, x - 4, y - 4, 1)
- elseif playerState == 1 then
- rect((x//8+placex)*8, (y//8+placey)*8, 8, 8, 11)
- spr(256 + facing + 4, x - 4, y - 4, 1)
- spr(bl[placeID], x - 4 + placex*8, y - 4 + placey*8)
- line((x-4+placex*8), (y-4+placey*8)-1, (x-4+placex*8)+7, (y-4+placey*8)-1, 0)
- line((x-4+placex*8), (y-4+placey*8)+8, (x-4+placex*8)+7, (y-4+placey*8)+8, 0)
- line((x-4+placex*8)-1, (y-4+placey*8), (x-4+placex*8)-1, (y-4+placey*8)+7, 0)
- line((x-4+placex*8)+8, (y-4+placey*8), (x-4+placex*8)+8, (y-4+placey*8)+7, 0)
- elseif playerState == 2 then
- rect((x//8+placex)*8, (y//8+placey)*8, 8, 8, 6)
- spr(256 + facing + 8, x - 4, y - 4, 1)
- end
- end
- function updateWorld()
- for j = 0, z//1 do
- for i = 0, wwidth do
- for k = 0, wheight do
- local t = world[i][k][j]
- if t == nil then
- createTile(i, k, j, 0, 0)
- elseif t.type == 0 then
- --no 'thang
- else
- local above = wget(i, k, z+1)
- 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
- local box1 = {}
- local box2 = {}
- box1.x = i*8+4
- box1.y = k*8+4
- box2.x = x
- box2.y = y
- box1.width = 8
- box2.width = 6
- local hw1 = box1.width*0.5
- local hw2 = box2.width*0.5
- local invjmpx = false
- local invjmpy = false
- local xjump = 0
- local yjump = 0
- local colx = false
- local coly = false
- if box2.x >= box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
- local length = box2.x - box1.x
- local gap = length - hw1 - hw2
- if gap < 0 then
- xjump = gap
- invjmpx = true
- colx = true
- end
- elseif box2.x < box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
- local length = box1.x - box2.x
- local gap = length - hw1 - hw2
- if gap < 0 then
- xjump = gap
- invjmpx = false
- colx = true
- end
- end
- if box2.y >= box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
- local length = box2.y - box1.y
- local gap = length - hw1 - hw2
- if gap < 0 then
- yjump = gap
- invjmpy = true
- coly = true
- end
- elseif box2.y < box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
- local length = box1.y - box2.y
- local gap = length - hw1 - hw2
- if gap < 0 then
- yjump = gap
- invjmpy = false
- coly = true
- end
- end
- if j == z//1 and above.type ~= 0 then
- if yjump > xjump and coly then
- if invjmpy then
- y = box2.y - yjump
- elseif not invjmpy then
- y = box2.y + yjump
- end
- elseif xjump > yjump and colx then
- if invjmpx then
- x = box2.x - xjump
- elseif not invjmpx then
- x = box2.x + xjump
- end
- end
- elseif j == z//1 and above.type == 0 then
- z = z + 1
- elseif j == z//1-1 then
- if colx or coly then
- onGround = true
- z = j + 1
- vz = 0
- else
- onGround = false
- end
- end
- end
- end
- updates = updates + 1
- end
- end
- end
- end
- function drawWorld()
- for j = 0, z//1 do
- for i = 0, wwidth do
- for k = 0, wheight do
- if i*8-camx >= 0 and i*8-camx <= 240 and k*8-camy >= 0 and k*8-camy <= 136 then
- local t = world[i][k][j]
- renders = renders + 1
- if t.type == 0 then
- --llr = j
- elseif t.type ~= 34 then
- local n = wget(i, k - 1, j)
- local s = wget(i, k + 1, j)
- local e = wget(i + 1, k, j)
- local w = wget(i - 1, k, j)
- spr(t.type, i*8, k*8)
- if w.type == 0 or w.type == 34 then
- line(i*8-1, k*8, i*8-1, k*8+7, 0)
- end
- if e.type == 0 or e.type == 34 then
- line(i*8+8, k*8, i*8+8, k*8+7, 0)
- end
- if n.type == 0 or n.type == 34 then
- line(i*8, k*8-1, i*8+7, k*8-1, 0)
- end
- if s.type == 0 or s.type == 34 then
- line(i*8, k*8+8, i*8+7, k*8+8, 0)
- end
- --print(j, i*8, k*8)
- llr = j
- elseif t.type == 34 then
- local n = wget(i, k - 1, j)
- local s = wget(i, k + 1, j)
- local e = wget(i + 1, k, j)
- local w = wget(i - 1, k, j)
- spr(t.type, i*8, k*8, 1)
- if w.type ~= 0 then
- spr(t.type + 4, i*8, k*8, 1)
- end
- if e.type ~= 0 then
- spr(t.type + 2, i*8, k*8, 1)
- end
- if n.type ~= 0 then
- spr(t.type + 1, i*8, k*8, 1)
- end
- if s.type ~= 0 then
- spr(t.type + 3, i*8, k*8, 1)
- end
- llr = j
- end
- end
- end
- end
- end
- end
- function wget(x, y, z)
- local t = {}
- if x >= wminwidth and y >= wminheight and wmindepth >= 0 and x <= wwidth and y <= wheight and z <= wdepth then
- t = world[x][y][z]
- end
- if t == nil then
- t = {}
- t.type = 0
- t.state = 0
- end
- return t
- end
- function createTile(x, y, z, type, state)
- local t = {}
- t.type = type
- t.state = state
- world[x][y][z] = t
- end
- function _INIT()
- for i = 0, 30 do
- for k = 0, 17 do
- createTile(i, k, 0, 1, 0)
- end
- end
- end
- _INIT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement