Skip to content

Commit

Permalink
wip: removing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
brennop committed Mar 17, 2023
1 parent 22d11c9 commit 1989c25
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 35 deletions.
2 changes: 1 addition & 1 deletion block.lua → blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ blockTypes = {
-- 1: cobblestone
cube({ { 8, 1 }, { 8, 1 }, { 8, 1 }, { 8, 1 }, { 8, 1 }, { 8, 1 } }),
-- 2: grass
cube({ { 3, 30 }, { 3, 16 }, { 3, 30 }, { 3, 31 }, { 3, 30 }, { 3, 30 } }),
cube({ { 3, 30 }, { 3, 17 }, { 3, 30 }, { 3, 31 }, { 3, 30 }, { 3, 30 } }),
-- 3: dirt
cube({ { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 } }),
}
Expand Down
37 changes: 34 additions & 3 deletions camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ local cos, sin = math.cos, math.sin

local w, h = 400, 300

function Camera:new()
self.position = Vector(0, 18, 0)
function Camera:new(world)
self.position = Vector(0, CHUNK_HEIGHT + 2, 0)
self.world = world

self.view = Matrix()
self.projection = Matrix()
Expand Down Expand Up @@ -108,7 +109,37 @@ function Camera:update()
self.position = self.position + dir * 0.1

self:updateView()

local block, x, y, z = self:getHit()

debug("block", block, x, y, z)
if block > 0 and love.mouse.isDown(1) then
self.world:setBlock(x, y, z, 0)
end
end

return Camera
function Camera:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.circle("fill", w, h, 2)
end

local DOF = 16
function Camera:getHit()
local x, y, z = self.position:unpack()
local dx, dy, dz = (-self.forward):unpack()

for dof = 1, DOF do
local fx, fy, fz = math.ceil(x-0.5), math.ceil(y-0.5), math.ceil(z-0.5)
local block = self.world:getBlock(fx, fy, fz)

if block > 0 then
return block, fx, fy, fz
end

x, y, z = x + dx, y + dy, z + dz
end

return 0, 0, 0, 0
end

return Camera
55 changes: 38 additions & 17 deletions chunk.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

local Chunk = Object:extend()

local blockTypes = require "block"
local blockTypes = require "blocks"
local Vector = require "vector"

local format = {
{ "VertexPosition", "float", 3 },
{ "VertexTexCoord", "float", 2 },
{ "VertexNormal", "float", 3 },
{ "VertexColor", "byte", 4 },
}

function Chunk:new(x, y, z, world)
Expand All @@ -15,28 +16,31 @@ function Chunk:new(x, y, z, world)

self.blocks = {}

local _overworld = { 0, 2 }
local _terrain = { 0, 3 }
local _caves = { 0, 0, 1 }

local _dirt = { 0, 0, 3 }
local _caves = { 0, 0, 0, 1 }

for i = 1, CHUNK_SIZE do
self.blocks[i] = {}
for j = 1, CHUNK_HEIGHT do
self.blocks[i][j] = {}
for k = 1, CHUNK_SIZE do
if j == 16 then
self.blocks[i][j][k] = _overworld[math.random(1, #_overworld)]
elseif j > 13 then
self.blocks[i][j][k] = _terrain[math.random(1, #_terrain)]
local h = CHUNK_HEIGHT - math.floor(love.math.noise(i/8, k/8, 0) * 8)
if j == h then
self.blocks[i][j][k] = 2
elseif j < h and j > 16 then
self.blocks[i][j][k] = _dirt[love.math.random(1, #_dirt)]
elseif j < h and j <= 16 then
self.blocks[i][j][k] = _caves[love.math.random(1, #_caves)]
else
self.blocks[i][j][k] = _caves[math.random(1, #_caves)]
self.blocks[i][j][k] = 0
end
end
end
end

self.mesh = nil
maxVertices = CHUNK_SIZE * CHUNK_HEIGHT * CHUNK_SIZE * 6 * 6
self.mesh = love.graphics.newMesh(format, maxVertices, "triangles")
self.mesh:setTexture(tileset)
end

function Chunk:updateMesh()
Expand All @@ -45,10 +49,12 @@ function Chunk:updateMesh()
local function addFace(index, mesh, x, y, z)
for i = 1, 6 do
local vertex = mesh[index*6+i]
local vx, vy, vz, u, v = unpack(vertex)
local vx, vy, vz, u, v, s = unpack(vertex)
table.insert(vertices, {
vx + x, vy + y, vz + z,
u, v
u, v,
0, 0, 0,
s, s, s, 255
})
end
end
Expand All @@ -73,8 +79,7 @@ function Chunk:updateMesh()
end
end

self.mesh = love.graphics.newMesh(format, vertices, "triangles")
self.mesh:setTexture(tileset)
self.mesh:setVertices(vertices)
end

function Chunk:getBlock(x, y, z)
Expand All @@ -88,8 +93,24 @@ function Chunk:getBlock(x, y, z)
return self.blocks[x][y][z]
end

function Chunk:setBlock(x, y, z, block)
if y < 1 or y > CHUNK_HEIGHT then return end

-- translate to local coordinates
x = x - self.position.x
y = y - self.position.y
z = z - self.position.z

if self.blocks[x][y][z] == block then return end

self.blocks[x][y][z] = block
self:updateMesh()
end

function Chunk:draw()
love.graphics.draw(self.mesh)
if self.mesh then
love.graphics.draw(self.mesh)
end
end

return Chunk
26 changes: 14 additions & 12 deletions cube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ local uvs = {

-- vertex index / uv index
local faces = {
{ 1, 1, 2, 2, 3, 3 }, -- front
{ 3, 3, 2, 2, 4, 4 }, -- front
{ 3, 1, 4, 2, 5, 3 }, -- top
{ 5, 3, 4, 2, 6, 4 }, -- top
{ 5, 4, 6, 3, 7, 2 }, -- back
{ 7, 2, 6, 3, 8, 1 }, -- back
{ 7, 1, 8, 2, 1, 3 }, -- bottom
{ 1, 3, 8, 2, 2, 4 }, -- botoom
{ 2, 1, 8, 2, 4, 3 }, -- right
{ 4, 3, 8, 2, 6, 4 }, -- right
{ 7, 1, 1, 2, 5, 3 }, -- left
{ 5, 3, 1, 2, 3, 4 }, -- left
{ 1, 1, 2, 2, 3, 3, 0.8 }, -- front
{ 3, 3, 2, 2, 4, 4, 0.8 }, -- front
{ 3, 1, 4, 2, 5, 3, 1.0 }, -- top
{ 5, 3, 4, 2, 6, 4, 1.0 }, -- top
{ 5, 4, 6, 3, 7, 2, 0.6 }, -- back
{ 7, 2, 6, 3, 8, 1, 0.6 }, -- back
{ 7, 1, 8, 2, 1, 3, 0.4 }, -- bottom
{ 1, 3, 8, 2, 2, 4, 0.4 }, -- botoom
{ 2, 1, 8, 2, 4, 3, 0.8 }, -- right
{ 4, 3, 8, 2, 6, 4, 0.8 }, -- right
{ 7, 1, 1, 2, 5, 3, 0.6 }, -- left
{ 5, 3, 1, 2, 3, 4, 0.6 }, -- left
}

return function(textures)
Expand All @@ -53,13 +53,15 @@ return function(textures)

local vertex = face[j]
local uv = face[j+1]
local shade = face[7]

cube[#cube + 1] = {
vertices[vertex][1] * 0.5,
vertices[vertex][2] * 0.5,
vertices[vertex][3] * 0.5,
u + uvs[uv][1] * step,
v + uvs[uv][2] * step,
shade,
}
end
end
Expand Down
8 changes: 6 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Object = require "lib.classic"

CHUNK_SIZE = 16
CHUNK_HEIGHT = 16
CHUNK_HEIGHT = 32

local Matrix = require "matrix"
local Camera = require "camera"
Expand Down Expand Up @@ -44,11 +44,13 @@ function love.load()

tileset = love.graphics.newImage("tileset.png")

love.graphics.setBackgroundColor(0.65, 0.6, 0.95)

local World = require "world"

world = World()

camera = Camera()
camera = Camera(world)
end

function love.update(dt)
Expand All @@ -66,6 +68,8 @@ function love.draw()

love.graphics.setShader()

camera:draw()

debug("FPS:", love.timer.getFPS())

for i, v in ipairs(_debug) do
Expand Down
8 changes: 8 additions & 0 deletions world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function World:getBlock(x, y, z)
return nil
end

function World:setBlock(x, y, z, block)
local chunk = self:getChunk(x, z)

if chunk then
chunk:setBlock(x, y, z, block)
end
end

function World:draw()
for i, v in pairs(self.chunks) do
for j, chunk in pairs(v) do
Expand Down

0 comments on commit 1989c25

Please sign in to comment.