Skip to content

Commit

Permalink
Added grass and trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Middlerun committed Aug 16, 2011
1 parent 5645448 commit 80cf5c5
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 24 deletions.
72 changes: 68 additions & 4 deletions chunk.lua
Expand Up @@ -6,8 +6,10 @@ function Chunk:new()
self.__index = self

o.generated = false
o.treesGenerated = false
o.r = nil
o.c = nil
o.terrain = nil
o.block = {}
o.perlin = {}
o.coalNoise = {}
Expand All @@ -16,7 +18,7 @@ function Chunk:new()
o.perlin[r] = {}
o.coalNoise[r] = {}
for c = 1, 32 do
o.block[r][c] = 255
o.block[r][c] = UNGENERATED
o.perlin[r][c] = 0
end
end
Expand Down Expand Up @@ -177,25 +179,38 @@ function Chunk:interpolate2D(values, chunkR, chunkC, N)
end

function Chunk:getBlock(r, c)
if r < 1 or r > 32 or c < 1 or c > 32 then return AIR end
if not self.generated then return UNGENERATED end
if r < 1 or r > 32 or c < 1 or c > 32 then
return self.terrain:getBlock(self.r * 32 + r, self.c * 32 + c)
end
return self.block[r][c]
end

function Chunk:setBlock(r, c, block)
if not self.generated then return end
if r < 1 or r > 32 or c < 1 or c > 32 then
self.terrain:setBlock(self.r * 32 + r, self.c * 32 + c, block)
return
elseif block ~= AIR and self:getBlock(r+1, c) == GRASS then self:setBlock(r+1, c, DIRT)
end
self.block[r][c] = block
self:render()
if r == 1 and self.terrain:hasChunk(self.r-1, self.c) then self.terrain:getChunk(self.r-1, self.c):render() end
if r == 32 and self.terrain:hasChunk(self.r+1, self.c) then self.terrain:getChunk(self.r+1, self.c):render() end
if c == 1 and self.terrain:hasChunk(self.r, self.c-1) then self.terrain:getChunk(self.r, self.c-1):render() end
if c == 32 and self.terrain:hasChunk(self.r, self.c+1) then self.terrain:getChunk(self.r, self.c+1):render() end
end

function Chunk:isGenerated()
return self.generated
end

function Chunk:render()
--if not self.generated then return end
if self.framebuffer == nil then
self.framebuffer = love.graphics.newFramebuffer(512, 512)
self.framebuffer:setFilter("linear", "nearest")
end
if not self.generated then return end
love.graphics.setRenderTarget(self.framebuffer)
love.graphics.setColor(255, 255, 255, 255)
local num
Expand All @@ -215,11 +230,11 @@ function Chunk:render()
end

function Chunk:renderPerlin()
--if not self.generated then return end
if self.framebufferPerlin == nil then
self.framebufferPerlin = love.graphics.newFramebuffer(512, 512)
self.framebufferPerlin:setFilter("linear", "nearest")
end
if not self.generated then return end
love.graphics.setRenderTarget(self.framebufferPerlin)
love.graphics.setColor(255, 255, 255, 255)
for r = 1, 32 do
Expand All @@ -232,3 +247,52 @@ function Chunk:renderPerlin()
end
love.graphics.setRenderTarget()
end

function Chunk:generateTrees()
if self.treesGenerated then return end
local canGenerate = true
for r = -1, 0 do
for c = -1, 1 do
if not self.terrain:hasChunk(self.r + r, self.c + c) or not self.terrain:getChunk(self.r + r, self.c + c).generated then
canGenerate = false
end
end
end
if canGenerate then
local canPlantTree
local height, maxHeight
local radius
math.randomseed(self.terrain:getSeed() + 13669 * self.r + self.c)
for r = 1, 32 do
for c = 1, 32 do
if self:getBlock(r, c) == DIRT then
if self:getBlock(r-1, c) == AIR or self:getBlock(r-1, c) == LEAVES then
if self.r * 32 + r < math.random() * 32 then self:setBlock(r, c, GRASS) end
end
if math.random() < 0.1 then
canPlantTree = true
for i = 1, 5 do
if self:getBlock(r-i, c) ~= AIR and self:getBlock(r-i, c) ~= LEAVES then canPlantTree = false end
end
if canPlantTree then
-- Plant tree
maxHeight = 5 + math.floor(6 * math.random())
for i = 1, maxHeight do
if self:getBlock(r-i-1, c) ~= AIR and self:getBlock(r-i-1, c) ~= LEAVES then break end
self:setBlock(r-i, c, WOOD)
height = i
end
radius = math.floor(height/3) + 1
for r2 = -radius, radius do
for c2 = -radius, radius do
if pythag(r2, c2) <= radius + 0.5 and self:getBlock(r-height+r2, c+c2) == AIR then self:setBlock(r-height+r2, c+c2, LEAVES) end
end
end
end
end
end
end
end
self.treesGenerated = true
end
end
9 changes: 9 additions & 0 deletions common.lua
Expand Up @@ -6,22 +6,31 @@ PLAY = 3
-- Block codes
AIR = 0
STONE = 1
GRASS = 2
DIRT = 3
COBBLESTONE = 4
COAL_ORE = 16
WOOD = 17
LEAVES = 18
UNGENERATED = 255

durability = {}
durability[STONE] = 2
durability[GRASS] = 1
durability[DIRT] = 1
durability[COBBLESTONE] = 2
durability[COAL_ORE] = 3
durability[WOOD] = 1.5
durability[LEAVES] = 0.3

breakGive = {}
breakGive[STONE] = COBBLESTONE
breakGive[GRASS] = DIRT
breakGive[DIRT] = DIRT
breakGive[COBBLESTONE] = COBBLESTONE
breakGive[COAL_ORE] = COAL_ORE
breakGive[WOOD] = WOOD
breakGive[LEAVES] = nil

-- Random number engine
rand = {mySeed = 1, lastN = -1}
Expand Down
Binary file added gfx/grass.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/leaves.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/wood.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 25 additions & 4 deletions loadgraphics.lua
Expand Up @@ -7,24 +7,45 @@ for i = 2, 16 do
images[STONE][i]:setFilter("linear", "nearest")
end

images[GRASS] = {}
images[GRASS][1] = love.graphics.newImage("gfx/grass.png")
for i = 2, 16 do
images[GRASS][i] = images[GRASS][1]
images[GRASS][i]:setFilter("linear", "nearest")
end

images[DIRT] = {}
images[DIRT][1] = love.graphics.newImage("gfx/dirt.png")
for i = 2, 16 do
images[DIRT][i] = images[DIRT][1]
images[DIRT][i]:setFilter("linear", "nearest")
end

images[COBBLESTONE] = {}
for i = 1, 16 do
images[COBBLESTONE][i] = love.graphics.newImage("gfx/cobblestone" .. i .. ".png")
images[COBBLESTONE][i]:setFilter("linear", "nearest")
end

images[COAL_ORE] = {}
images[COAL_ORE][1] = love.graphics.newImage("gfx/coalOre.png")
for i = 2, 16 do
images[COAL_ORE][i] = images[COAL_ORE][1]
images[COAL_ORE][i]:setFilter("linear", "nearest")
end

images[COBBLESTONE] = {}
for i = 1, 16 do
images[COBBLESTONE][i] = love.graphics.newImage("gfx/cobblestone" .. i .. ".png")
images[COBBLESTONE][i]:setFilter("linear", "nearest")
images[WOOD] = {}
images[WOOD][1] = love.graphics.newImage("gfx/wood.png")
for i = 2, 16 do
images[WOOD][i] = images[WOOD][1]
images[WOOD][i]:setFilter("linear", "nearest")
end

images[LEAVES] = {}
images[LEAVES][1] = love.graphics.newImage("gfx/leaves.png")
for i = 2, 16 do
images[LEAVES][i] = images[LEAVES][1]
images[LEAVES][i]:setFilter("linear", "nearest")
end

breakImage = {}
Expand Down
28 changes: 23 additions & 5 deletions main.lua
Expand Up @@ -21,6 +21,8 @@ mineProgress = 0
placeTime = 0
landTime = 0
entities = {}
instamine = false
debug = false



Expand Down Expand Up @@ -48,6 +50,7 @@ end


function love.update(dt)
if dt > 0.2 then dt = 0.2 end
local oldx = player.x
local oldy = player.y
if not first and player.falling then
Expand Down Expand Up @@ -123,7 +126,7 @@ function love.update(dt)
if love.mouse.isDown("l") and block ~= AIR and block ~= UNGENERATED then
if math.ceil(cursor.x) == mineBlock.c and math.ceil(cursor.y) == mineBlock.r then
mineProgress = mineProgress + dt / durability[block]
if mineProgress >= 1 then
if mineProgress >= 1 or instamine then
player:give(breakGive[block])
terrain:setBlock(math.ceil(cursor.y), math.ceil(cursor.x), AIR)
mineProgress = 0
Expand All @@ -142,6 +145,7 @@ function love.update(dt)
elseif selected == 2 then block = STONE
elseif selected == 3 then block = COBBLESTONE
elseif selected == 4 then block = COAL_ORE
elseif selected == 5 then block = WOOD
end
-- end hack

Expand All @@ -165,8 +169,8 @@ function love.update(dt)
player.walk:update(dt)

-- Generate new chunks
for r = math.floor((player.y - 48) / 32), math.floor((player.y + 48) / 32) do
for c = math.floor((player.x - 48) / 32), math.floor((player.x + 48) / 32) do
for r = math.floor((player.y - 80) / 32), math.floor((player.y + 80) / 32) do
for c = math.floor((player.x - 80) / 32), math.floor((player.x + 80) / 32) do
terrain:generate(r, c)
end
end
Expand Down Expand Up @@ -220,6 +224,14 @@ function love.draw()
love.graphics.setColor(0, 0, 0, 127)
if selected == 4 then love.graphics.setColor(0, 0, 0, 255) end
love.graphics.print("Coal ore: " .. player:checkInventory(COAL_ORE), 50, 140)
love.graphics.setColor(0, 0, 0, 127)
if selected == 5 then love.graphics.setColor(0, 0, 0, 255) end
love.graphics.print("Wood: " .. player:checkInventory(WOOD), 50, 170)

if debug then
love.graphics.setColor(0, 0, 0, 255)
love.graphics.print(love.timer.getFPS() .. " fps", love.graphics.getWidth() - 150, 50)
end
end


Expand All @@ -235,6 +247,8 @@ function love.keypressed(k, u)
if view.zoom > 1 then view.zoom = view.zoom / 2 end
elseif k == "]" then
if view.zoom < 256 then view.zoom = view.zoom * 2 end
elseif k == "f3" then
debug = not debug
end
end

Expand All @@ -249,10 +263,10 @@ end
function love.mousepressed(x, y, button)
if button == "wd" then
selected = selected + 1
if selected == 5 then selected = 1 end
if selected == 6 then selected = 1 end
elseif button == "wu" then
selected = selected - 1
if selected == 0 then selected = 4 end
if selected == 0 then selected = 5 end
end
end

Expand Down Expand Up @@ -295,6 +309,10 @@ end


function pythag(x1, y1, x2, y2)
if x2 == nil and y2 == nil then
x2 = 0
y2 = 0
end
return math.sqrt((x1-x2)^2 + (y1-y2)^2)
end

23 changes: 13 additions & 10 deletions player.lua
Expand Up @@ -33,25 +33,28 @@ function Player:new(seed)
return o
end

function Player:give(blocktype)
if self.inventory[blocktype] == nil then self.inventory[blocktype] = 1
else self.inventory[blocktype] = self.inventory[blocktype] + 1
function Player:give(block)
if block == nil then return end
if self.inventory[block] == nil then self.inventory[block] = 1
else self.inventory[block] = self.inventory[block] + 1
end
end

function Player:take(blocktype)
if self.inventory[blocktype] == nil then return false end
if self.inventory[blocktype] > 0 then
self.inventory[blocktype] = self.inventory[blocktype] - 1
function Player:take(block)
if block == nil then return false end
if self.inventory[block] == nil then return false end
if self.inventory[block] > 0 then
self.inventory[block] = self.inventory[block] - 1
return true
else
return false
end
end

function Player:checkInventory(blocktype)
if self.inventory[blocktype] == nil then return 0
else return self.inventory[blocktype]
function Player:checkInventory(block)
if block == nil then return 0 end
if self.inventory[block] == nil then return 0
else return self.inventory[block]
end
end

20 changes: 19 additions & 1 deletion terrain.lua
Expand Up @@ -25,6 +25,9 @@ function Terrain:addChunk(chunk, r, c)
if c > self.cMax then self.cMax = c end
if self.chunk[r] == nil then self.chunk[r] = {} end
self.chunk[r][c] = chunk
self.chunk[r][c].terrain = self
self.chunk[r][c].r = r
self.chunk[r][c].c = c
end

function Terrain:getChunk(r, c)
Expand Down Expand Up @@ -55,7 +58,7 @@ function Terrain:getBlock(r, c)
if self:hasChunk(chunkR, chunkC) then
return self:getChunk(chunkR, chunkC):getBlock(relR, relC)
else
return 255
return UNGENERATED
end
end

Expand All @@ -69,6 +72,13 @@ function Terrain:generateInitial()
chunk = Chunk:new()
chunk:generate(self:getSeed(), r, c)
terrain:addChunk(chunk, r, c)
for r = 0, 1 do
for c = -1, 1 do
if self:hasChunk(chunk.r + r, chunk.c + c) then
self:getChunk(chunk.r + r, chunk.c + c):generateTrees()
end
end
end
end
end
end
Expand All @@ -93,8 +103,16 @@ function Terrain:checkGenerator()
chunk.coalNoise[r][c] = chunkNew.coalNoise[r][c]
end
end
chunk.generated = true
chunk:render()
chunk:renderPerlin()
for r = 0, 1 do
for c = -1, 1 do
if self:hasChunk(chunk.r + r, chunk.c + c) then
self:getChunk(chunk.r + r, chunk.c + c):generateTrees()
end
end
end
end
if generator:peek("ready") then
local chunkRC = table.remove(self.generationQueue, 1)
Expand Down

0 comments on commit 80cf5c5

Please sign in to comment.