Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tx,ty = term.getSize()
- local function createWorld()
- local world = {
- sprites = {},
- camera = {},
- getDistance = function(x1, y1, z1, x2, y2, z2)
- local mn = {x1, y1, z1}
- local mx = {x2, y2, z2}
- --[[for i=1,#mn do
- if mn[i] > mx[i] then
- local temp = mx[i]
- mx[i] = mn[i]
- mn[i] = temp
- end
- end]]
- local dif = {mx[1]-mn[1], mx[2]-mn[2], mx[3]-mn[3]}
- local dist = (dif[1]+dif[2]+dif[3])/2
- return {dist, dif}
- end,
- addSprite = function(self, sprite)
- local sprites = self.sprites
- if sprites == nil then sprites = {} end
- sprites[#sprites + 1] = sprite
- self.sprites = sprites
- end,
- checkAll = function(self, s)
- if self.sprites ~= nil then
- for i=1,#self.sprites do
- if check(s, self.sprites[i]) then return true end
- end
- end
- return false
- end,
- check = function(s1, s2)
- for x=s1.x,s1.x+s1.width do
- for y=s1.y,s1.y+s1.height do
- for z=s1.z,s1.z+s1.depth do
- for x2=s2.x,s2.x+s2.width do
- for y2=s2.y,s2.y+s2.height do
- for z2=s2.z,s2.z+s2.depth do
- if x == x2 and y == y2 and z == z2 then return true end
- end
- end
- end
- end
- end
- end
- return false
- end,
- draw = function(self)
- if self.sprites ~= nil and self.camera ~= nil then
- for i=1,#self.sprites do
- local sprite = self.sprites[i]
- self:drawSprite(sprite.x,
- sprite.y,
- sprite.z,
- sprite.width,
- sprite.height,
- sprite.depth,
- sprite.char,
- sprite.cols,
- self.getDistance(self.camera[4][1], self.camera[4][2], self.camera[4][3], sprite.x, sprite.y, sprite.z))
- end
- end
- end,
- convertDifference = function(dir, dims)
- local ndif = {0,0,0}
- for i=1,dims do
- if dir[i] < 0 then ndif[i] = -1
- elseif dir[i] > 0 then ndif[i] = 1 end
- end
- return ndif
- end,
- checkViewpoint = function(self, dir, dif, dims)
- dif = self.convertDifference(dir, dims)
- for i=1,dims do
- if dir[i] ~= dif[i] then return false end
- end
- return true
- end,
- drawSprite = function(self, x, y, z, width, height, depth, char, cols, dist)
- local scale = (dist[2][3]/2)
- --local scale = dist[1]
- local todo = 1
- if scale < 0 then todo = -1 end
- local scale = math.floor(scale + 0.5)
- if self:checkViewpoint(self.camera, dist[2], 3) then
- --for sz=z,depth do
- local from = {x+self.camera[4][1],y+self.camera[4][2]}
- local to = {x+width+self.camera[4][1],y+height+self.camera[4][2]}
- local ft = true
- for i=1,scale do
- if ft == true then
- from[1] = from[1] - todo
- from[2] = from[2] - todo
- ft = false
- else
- to[1] = to[1] + todo
- to[2] = to[2] + todo
- ft = true
- end
- end
- for sx=from[1],to[1] do
- for sy=from[2],to[2] do
- if sx > 0 and sx <= tx and sy > 0 and sy <= ty then
- term.setCursorPos(sx, sy)
- term.setBackgroundColor(cols[1])
- term.setTextColor(cols[2])
- term.write(char)
- end
- end
- end
- --scale = scale + 0.1
- --end
- end
- end,
- }
- return world
- end
- local function createSprite(sworld, sx, sy, sz, swidth, sheight, sdepth, sback_col, stext_col, schar)
- local sprite = {
- x = sx,
- y = sy,
- z = sz,
- width = swidth,
- height = sheight,
- depth = sdepth,
- cols = {sback_col, stext_col},
- char = schar,
- children = {},
- world = sworld,
- moveChildren = function(self, dx, dy, dz, clever)
- if children ~= nil then
- for i=1,#self.children do self.children[i]:move(dx, dy, dz, clever) end
- end
- end,
- move = function(self, dx, dy, dz, clever)
- local old = {self.x, self.y, self.z}
- self.x = self.x + dx
- self.y = self.y + dy
- self.z = self.z + sz
- if clever then
- if self.world:checkAll(self) then
- self.x = old[1]
- self.y = old[2]
- self.z = old[3]
- return false
- end
- end
- moveChildren(dx, dy, dz, clever)
- return true
- end,
- }
- return sprite
- end
- local function clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- --sprite function:
- --world, x, y, z, width, height, depth, background_colour, text_colour, character
- local world = createWorld()
- local block = createSprite(world, 3, 3, 1, 1, 1, 1, colors.red, colors.green, "#")
- local b2 = createSprite(world, 17, 13, 1, 1, 1, 1, colors.blue, colors.yellow, "#")
- world.camera = {1, 1, 1, {0,0,0}}
- world:addSprite(block)
- world:addSprite(b2)
- term.clear()
- term.setCursorPos(1,1)
- world:draw()
- local on = true
- local interval = 0.2
- local timer = os.startTimer(0)
- local function moveC(dx, dy, dz)
- world.camera[4][1] = world.camera[4][1] + dx
- world.camera[4][2] = world.camera[4][2] + dy
- world.camera[4][3] = world.camera[4][3] + dz
- end
- while on do
- local event, p1 = os.pullEvent()
- if event == "timer" and p1 == timer then
- clear()
- world:draw()
- timer = os.startTimer(interval)
- elseif event == "key" then
- local key = p1
- if key == 29 then
- on = false
- elseif key == 200 then --up
- moveC(0, 0, 1)
- elseif key == 203 then --left
- moveC(-1, 0, 0)
- elseif key == 208 then --down
- moveC(0, 0, -1)
- elseif key == 205 then --right
- moveC(1, 0, 0)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement