Skip to content

Commit

Permalink
Graphics updates including new player image and cobblestone
Browse files Browse the repository at this point in the history
  • Loading branch information
Middlerun committed Aug 15, 2011
1 parent f08c411 commit 63aec46
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 88 deletions.
11 changes: 10 additions & 1 deletion chunk.lua
Expand Up @@ -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

Expand All @@ -190,23 +191,31 @@ 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
love.graphics.setRenderTarget()
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")
Expand Down
43 changes: 43 additions & 0 deletions 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
26 changes: 1 addition & 25 deletions generator.lua
Expand Up @@ -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
Expand Down
Binary file added gfx/cobblestone1.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/cobblestone10.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/cobblestone11.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/cobblestone12.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/cobblestone13.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/cobblestone14.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/cobblestone15.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/cobblestone16.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/cobblestone2.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/cobblestone3.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/cobblestone4.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/cobblestone5.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/cobblestone6.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/cobblestone7.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/cobblestone8.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/cobblestone9.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/rabbitStand.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions 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")
85 changes: 24 additions & 61 deletions main.lua
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion player.lua
Expand Up @@ -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
Expand Down

0 comments on commit 63aec46

Please sign in to comment.