diff --git a/chunk.lua b/chunk.lua index f201f76..8fd5680 100644 --- a/chunk.lua +++ b/chunk.lua @@ -177,6 +177,7 @@ 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 return self.block[r][c] end @@ -190,16 +191,23 @@ function Chunk:isGenerated() 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 love.graphics.setRenderTarget(self.framebuffer) love.graphics.setColor(255, 255, 255, 255) + local num for r = 1, 32 do for c = 1, 32 do if self.block[r][c] ~= AIR and self.block[r][c] ~= UNGENERATED then - love.graphics.draw(images[self.block[r][c]], (c-1)*16, (r-1)*16) + num = 1 + if self:getBlock(r-1, c) == self.block[r][c] then num = num + 1 end + if self:getBlock(r, c+1) == self.block[r][c] then num = num + 2 end + if self:getBlock(r+1, c) == self.block[r][c] then num = num + 4 end + if self:getBlock(r, c-1) == self.block[r][c] then num = num + 8 end + love.graphics.draw(images[self.block[r][c]][num], (c-1)*16, (r-1)*16) end end end @@ -207,6 +215,7 @@ 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") diff --git a/common.lua b/common.lua new file mode 100644 index 0000000..d700a9e --- /dev/null +++ b/common.lua @@ -0,0 +1,43 @@ +-- Modes +MENU = 1 +GENERATING = 2 +PLAY = 3 + +-- Block codes +AIR = 0 +STONE = 1 +DIRT = 3 +COBBLESTONE = 4 +COAL_ORE = 16 +UNGENERATED = 255 + +durability = {} +durability[STONE] = 2 +durability[DIRT] = 1 +durability[COBBLESTONE] = 2 +durability[COAL_ORE] = 3 + +breakGive = {} +breakGive[STONE] = COBBLESTONE +breakGive[DIRT] = DIRT +breakGive[COBBLESTONE] = COBBLESTONE +breakGive[COAL_ORE] = COAL_ORE + +-- Random number engine +rand = {mySeed = 1, lastN = -1} +function rand:get(seed, n) + if n <= 0 then n = -2 * n + else n = 2 * n - 1 + end + + if seed ~= self.mySeed or self.lastN < 0 or n <= self.lastN then + self.mySeed = seed + math.randomseed(seed) + self.lastN = -1 + end + while self.lastN < n do + num = math.random() + self.lastN = self.lastN + 1 + end + return num - 0.5 +end diff --git a/generator.lua b/generator.lua index e8094da..50716e3 100644 --- a/generator.lua +++ b/generator.lua @@ -2,33 +2,9 @@ this_thread = love.thread.getThread() require("love.timer") require("love.filesystem") love.filesystem.load("TSerial.lua")() +love.filesystem.load("common.lua")() love.filesystem.load("chunk.lua")() --- Block codes -AIR = 0 -STONE = 1 -DIRT = 3 -COAL_ORE = 16 -UNGENERATED = 255 - -rand = {mySeed = 1, lastN = -1} -function rand:get(seed, n) - if n <= 0 then n = -2 * n - else n = 2 * n - 1 - end - - if seed ~= self.mySeed or self.lastN < 0 or n <= self.lastN then - self.mySeed = seed - math.randomseed(seed) - self.lastN = -1 - end - while self.lastN < n do - num = math.random() - self.lastN = self.lastN + 1 - end - return num - 0.5 -end - run = true while run do diff --git a/gfx/cobblestone1.png b/gfx/cobblestone1.png new file mode 100644 index 0000000..0470b22 Binary files /dev/null and b/gfx/cobblestone1.png differ diff --git a/gfx/cobblestone10.png b/gfx/cobblestone10.png new file mode 100644 index 0000000..fcbec9e Binary files /dev/null and b/gfx/cobblestone10.png differ diff --git a/gfx/cobblestone11.png b/gfx/cobblestone11.png new file mode 100644 index 0000000..40943be Binary files /dev/null and b/gfx/cobblestone11.png differ diff --git a/gfx/cobblestone12.png b/gfx/cobblestone12.png new file mode 100644 index 0000000..44c00fc Binary files /dev/null and b/gfx/cobblestone12.png differ diff --git a/gfx/cobblestone13.png b/gfx/cobblestone13.png new file mode 100644 index 0000000..44fd874 Binary files /dev/null and b/gfx/cobblestone13.png differ diff --git a/gfx/cobblestone14.png b/gfx/cobblestone14.png new file mode 100644 index 0000000..aab7b8a Binary files /dev/null and b/gfx/cobblestone14.png differ diff --git a/gfx/cobblestone15.png b/gfx/cobblestone15.png new file mode 100644 index 0000000..c7fbea4 Binary files /dev/null and b/gfx/cobblestone15.png differ diff --git a/gfx/cobblestone16.png b/gfx/cobblestone16.png new file mode 100644 index 0000000..cd6ef48 Binary files /dev/null and b/gfx/cobblestone16.png differ diff --git a/gfx/cobblestone2.png b/gfx/cobblestone2.png new file mode 100644 index 0000000..fc7157c Binary files /dev/null and b/gfx/cobblestone2.png differ diff --git a/gfx/cobblestone3.png b/gfx/cobblestone3.png new file mode 100644 index 0000000..f518b0a Binary files /dev/null and b/gfx/cobblestone3.png differ diff --git a/gfx/cobblestone4.png b/gfx/cobblestone4.png new file mode 100644 index 0000000..2edbdfe Binary files /dev/null and b/gfx/cobblestone4.png differ diff --git a/gfx/cobblestone5.png b/gfx/cobblestone5.png new file mode 100644 index 0000000..9ce1500 Binary files /dev/null and b/gfx/cobblestone5.png differ diff --git a/gfx/cobblestone6.png b/gfx/cobblestone6.png new file mode 100644 index 0000000..b9831e4 Binary files /dev/null and b/gfx/cobblestone6.png differ diff --git a/gfx/cobblestone7.png b/gfx/cobblestone7.png new file mode 100644 index 0000000..fc356c4 Binary files /dev/null and b/gfx/cobblestone7.png differ diff --git a/gfx/cobblestone8.png b/gfx/cobblestone8.png new file mode 100644 index 0000000..16889fc Binary files /dev/null and b/gfx/cobblestone8.png differ diff --git a/gfx/cobblestone9.png b/gfx/cobblestone9.png new file mode 100644 index 0000000..3d6df10 Binary files /dev/null and b/gfx/cobblestone9.png differ diff --git a/gfx/rabbitStand.png b/gfx/rabbitStand.png new file mode 100644 index 0000000..7afa588 Binary files /dev/null and b/gfx/rabbitStand.png differ diff --git a/loadgraphics.lua b/loadgraphics.lua new file mode 100644 index 0000000..42d54f9 --- /dev/null +++ b/loadgraphics.lua @@ -0,0 +1,36 @@ +images = {} + +images[STONE] = {} +images[STONE][1] = love.graphics.newImage("gfx/stone.png") +for i = 2, 16 do + images[STONE][i] = images[STONE][1] + images[STONE][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[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") +end + +breakImage = {} +for i = 1, 8 do + breakImage[i] = love.graphics.newImage("gfx/break" .. i .. ".png") + breakImage[i]:setFilter("linear", "nearest") +end +genChunk = love.graphics.newImage("gfx/genChunk.png") +genChunk:setFilter("linear", "nearest") diff --git a/main.lua b/main.lua index 53d53f8..e1ecd3c 100644 --- a/main.lua +++ b/main.lua @@ -3,37 +3,12 @@ love.filesystem.load("chunk.lua")() love.filesystem.load("terrain.lua")() love.filesystem.load("player.lua")() love.filesystem.load("collision.lua")() +love.filesystem.load("common.lua")() +love.filesystem.load("loadgraphics.lua")() love.filesystem.setIdentity("lovecraft") --- Modes -MENU = 1 -GENERATING = 2 -PLAY = 3 - --- Block codes -AIR = 0 -STONE = 1 -DIRT = 3 -COAL_ORE = 16 -UNGENERATED = 255 - -images = {} -images[STONE] = love.graphics.newImage("gfx/stone.png") -images[STONE]:setFilter("linear", "nearest") -images[DIRT] = love.graphics.newImage("gfx/dirt.png") -images[DIRT]:setFilter("linear", "nearest") -images[COAL_ORE] = love.graphics.newImage("gfx/coalOre.png") -images[COAL_ORE]:setFilter("linear", "nearest") -breakImage = {} -for i = 1, 8 do - breakImage[i] = love.graphics.newImage("gfx/break" .. i .. ".png") - breakImage[i]:setFilter("linear", "nearest") -end -genChunk = love.graphics.newImage("gfx/genChunk.png") -genChunk:setFilter("linear", "nearest") - -rand = {mySeed = 1, lastN = -1} view = {zoom = 32, x = 0, y = 0} +showPerlin = false oldMouse = {x = 0, y = 0} cursor = {x = 0, y = 0} cursorFade = false @@ -42,21 +17,16 @@ inreach = true selected = 1 mineBlock = {r = nil, c = nil} mineProgress = 0 -durability = {} -durability[DIRT] = 1 -durability[STONE] = 2 -durability[COAL_ORE] = 3 placeTime = 0 +entities = {} function love.load() generator = love.thread.newThread("generator", "generator.lua") generator:start() - showPerlin = false player = Player:new() - terrain = Terrain:new() terrain:generateInitial() player.x = 0.5 @@ -80,13 +50,21 @@ function love.update(dt) local oldy = player.y if not first and player.falling then player.vy = player.vy + 40 * dt - if love.keyboard.isDown("a") and not player.againstLeftWall then player.vx = math.max(-8, player.vx - 16 * dt) - elseif love.keyboard.isDown("d") and not player.againstRightWall then player.vx = math.min( 8, player.vx + 16 * dt) + if love.keyboard.isDown("a") and not player.againstLeftWall then + player.vx = math.max(-8, player.vx - 16 * dt) + player.direction = -1 + elseif love.keyboard.isDown("d") and not player.againstRightWall then + player.vx = math.min( 8, player.vx + 16 * dt) + player.direction = 1 end end if not first and not player.falling then - if love.keyboard.isDown("a") and not player.againstLeftWall then player.vx = math.max(-8, player.vx - 36 * dt) - elseif love.keyboard.isDown("d") and not player.againstRightWall then player.vx = math.min( 8, player.vx + 36 * dt) + if love.keyboard.isDown("a") and not player.againstLeftWall then + player.vx = math.max(-8, player.vx - 36 * dt) + player.direction = -1 + elseif love.keyboard.isDown("d") and not player.againstRightWall then + player.vx = math.min( 8, player.vx + 36 * dt) + player.direction = 1 elseif player.vx > 0 then player.vx = math.max(0, player.vx - 128 * dt) elseif player.vx < 0 then player.vx = math.min(0, player.vx + 128 * dt) end @@ -133,7 +111,7 @@ function love.update(dt) if math.ceil(cursor.x) == mineBlock.c and math.ceil(cursor.y) == mineBlock.r then mineProgress = mineProgress + dt / durability[block] if mineProgress >= 1 then - player:give(block) + player:give(breakGive[block]) terrain:setBlock(math.ceil(cursor.y), math.ceil(cursor.x), AIR) mineProgress = 0 mineBlock.r = nil @@ -149,7 +127,8 @@ function love.update(dt) -- Temporary hack, change later if selected == 1 then block = DIRT elseif selected == 2 then block = STONE - elseif selected == 3 then block = COAL_ORE + elseif selected == 3 then block = COBBLESTONE + elseif selected == 4 then block = COAL_ORE end -- end hack @@ -190,7 +169,7 @@ function love.draw() drawTerrain(terrain, view.zoom, view.x, view.y) end love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(player.image, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, view.zoom/64, view.zoom/64, player.image:getWidth()/2, player.image:getHeight()) + love.graphics.draw(player.image, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103) -- Can't remember what this was for: --love.graphics.line((player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y-1.5)*view.zoom+love.graphics.getHeight()/2, (cursor.x-view.x)*view.zoom + love.graphics.getWidth()/2, (cursor.y-view.y)*view.zoom+love.graphics.getHeight()/2) @@ -212,7 +191,10 @@ function love.draw() love.graphics.print("Stone: " .. player:checkInventory(STONE), 50, 80) love.graphics.setColor(0, 0, 0, 127) if selected == 3 then love.graphics.setColor(0, 0, 0, 255) end - love.graphics.print("Coal ore: " .. player:checkInventory(COAL_ORE), 50, 110) + love.graphics.print("Cobblestone: " .. player:checkInventory(COBBLESTONE), 50, 110) + 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) end @@ -251,25 +233,6 @@ end -function rand:get(seed, n) - if n <= 0 then n = -2 * n - else n = 2 * n - 1 - end - - if seed ~= self.mySeed or self.lastN < 0 or n <= self.lastN then - self.mySeed = seed - math.randomseed(seed) - self.lastN = -1 - end - while self.lastN < n do - num = math.random() - self.lastN = self.lastN + 1 - end - return num - 0.5 -end - - - function drawTerrain(terrain, zoom, x, y) local minR = math.max(terrain.rMin, math.floor((y - zoom * (love.graphics.getHeight() / 2)) / 32)) local maxR = math.min(terrain.rMax, math.floor((y + zoom * (love.graphics.getHeight() / 2)) / 32)) diff --git a/player.lua b/player.lua index 2509a0c..49f6af6 100644 --- a/player.lua +++ b/player.lua @@ -14,11 +14,12 @@ function Player:new(seed) o.againstLeftWall = false o.vx = 0 o.vy = 0 - o.image = love.graphics.newImage("gfx/happyman.png") + o.image = love.graphics.newImage("gfx/rabbitStand.png") o.image:setFilter("linear", "nearest") o.inventory = {} o.oldX = o.x o.oldY = o.y + o.direction = 1 return o end