Add easing function

This commit is contained in:
shylie 2025-07-19 12:12:15 -04:00
parent 832f9a5c9a
commit d02bc5f0fd
3 changed files with 41 additions and 33 deletions

BIN
atlas.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 B

After

Width:  |  Height:  |  Size: 321 B

3
conf.lua Normal file
View File

@ -0,0 +1,3 @@
function love.conf(t)
t.window.resizable = true
end

View File

@ -3,8 +3,8 @@ local zprite = require("zprite")
local tex = love.graphics.newImage("atlas.png") local tex = love.graphics.newImage("atlas.png")
tex:setFilter("nearest", "nearest") tex:setFilter("nearest", "nearest")
local x = 0 local gx = 0
local y = 0 local gy = 0
local angle = 0 local angle = 0
local scale = 1 local scale = 1
local dist = 4 local dist = 4
@ -26,10 +26,10 @@ function colors.sand(layer)
return layer / 16 + 0.2, layer / 16 + 0.15, 0.4 return layer / 16 + 0.2, layer / 16 + 0.15, 0.4
end end
function colors.dirt(layer) function colors.dirt(layer)
if layer < 20 then if layer < 16 then
return layer / 40, layer / 80, 0.1 return layer / 40, layer / 80, 0.1
else else
return 0.1, layer / 32 + 0.3, 0.1 return 0.1, layer / 50 + 0.1, 0.1
end end
end end
function colors.stone(layer) function colors.stone(layer)
@ -37,19 +37,19 @@ function colors.stone(layer)
return value / 1.1, value, value * 1.1 return value / 1.1, value, value * 1.1
end end
function colors.snow(layer) function colors.snow(layer)
local value = layer / 48 + 0.2 local value = layer / 48 + 0.25
return value / 1.1, value, value * 1.1 return value / 1.1, value, value * 1.1
end end
local z = zprite.zchunk.new(tex, 16 * 8, 32, dist * 2 + 1) local z = zprite.zchunk.new(tex, 16 * 8, 40, dist * 2 + 1)
local function layered_noise(x, y, layers, persistence) local function layered_noise(x, y, layers, persistence)
local freq = 1 local freq = 1
local ampl = 1 local ampl = 1
local maxval = 0 local maxval = 0
local val = 0 local val = 0
for i = 1, layers do for _ = 1, layers do
val = val + love.math.perlinNoise(x * freq, y * freq) * ampl val = val + love.math.simplexNoise(x * freq, y * freq) * ampl
maxval = maxval + ampl maxval = maxval + ampl
@ -60,8 +60,12 @@ local function layered_noise(x, y, layers, persistence)
return val / maxval return val / maxval
end end
local function ease(n)
return math.min((1.1 * n + 1 - math.cos(n * math.pi / 2)) / 2, 1)
end
local function heightmap(x, y) local function heightmap(x, y)
return layered_noise(x * 0.003, y * 0.003, 1, 0.1) return ease(layered_noise(x * 0.0005, y * 0.0005, 4, 0.8))
end end
local function dirtgrass_sprite(layer) local function dirtgrass_sprite(layer)
@ -72,7 +76,7 @@ local function dirtgrass_sprite(layer)
end end
local function pick_from_atlas(height) local function pick_from_atlas(height)
if height < 0.2 then if height < 0.25 then
return atlas.water return atlas.water
end end
if height < 0.3 then if height < 0.3 then
@ -94,10 +98,10 @@ local function pick_color(height)
if height < 0.3 then if height < 0.3 then
return colors.sand return colors.sand
end end
if height < 0.7 then if height < 0.67 then
return colors.dirt return colors.dirt
end end
if height < 0.9 then if height < 0.85 then
return colors.stone return colors.stone
end end
return colors.snow return colors.snow
@ -105,7 +109,7 @@ end
local function generate(x, y) local function generate(x, y)
local height = heightmap(x, y) local height = heightmap(x, y)
z:put(x, y, pick_from_atlas(height), math.ceil(height * 32), pick_color(height)) z:put(x, y, atlas.stone, math.ceil(height * 40), pick_color(height))
end end
local generated = {} local generated = {}
@ -128,29 +132,30 @@ local function generate_chunk(cx, cy)
end end
function love.update(dt) function love.update(dt)
local speed = 512
if love.keyboard.isDown("d") then if love.keyboard.isDown("d") then
local dx = 256 * math.cos(-angle) local dx = speed * math.cos(-angle)
local dy = 256 * math.sin(-angle) local dy = speed * math.sin(-angle)
x = x - dx * dt gx = gx - dx * dt
y = y - dy * dt gy = gy - dy * dt
end end
if love.keyboard.isDown("a") then if love.keyboard.isDown("a") then
local dx = 256 * math.cos(-angle) local dx = speed * math.cos(-angle)
local dy = 256 * math.sin(-angle) local dy = speed * math.sin(-angle)
x = x + dx * dt gx = gx + dx * dt
y = y + dy * dt gy = gy + dy * dt
end end
if love.keyboard.isDown("s") then if love.keyboard.isDown("s") then
local dx = 256 * math.cos(-angle + math.pi / 2) local dx = speed * math.cos(-angle + math.pi / 2)
local dy = 256 * math.sin(-angle + math.pi / 2) local dy = speed * math.sin(-angle + math.pi / 2)
x = x - dx * dt gx = gx - dx * dt
y = y - dy * dt gy = gy - dy * dt
end end
if love.keyboard.isDown("w") then if love.keyboard.isDown("w") then
local dx = 256 * math.cos(-angle + math.pi / 2) local dx = speed * math.cos(-angle + math.pi / 2)
local dy = 256 * math.sin(-angle + math.pi / 2) local dy = speed * math.sin(-angle + math.pi / 2)
x = x + dx * dt gx = gx + dx * dt
y = y + dy * dt gy = gy + dy * dt
end end
if love.keyboard.isDown("q") then if love.keyboard.isDown("q") then
angle = angle + dt angle = angle + dt
@ -171,8 +176,8 @@ function love.update(dt)
end end
end end
local center_cx = -math.floor(x / z._chunk_size) local center_cx = -math.floor(gx / z._chunk_size)
local center_cy = -math.floor(y / z._chunk_size) local center_cy = -math.floor(gy / z._chunk_size)
for key, gen in pairs(generated) do for key, gen in pairs(generated) do
if math.abs(gen[1] - center_cx) > dist or math.abs(gen[2] - center_cy) > dist then if math.abs(gen[1] - center_cx) > dist or math.abs(gen[2] - center_cy) > dist then
@ -190,13 +195,13 @@ local t = love.math.newTransform()
function love.draw() function love.draw()
for _, chunk in pairs(z._chunks) do for _, chunk in pairs(z._chunks) do
chunk.z._height_scale = 12 * scale chunk.z._height_scale = 18 * scale
end end
t:reset() t:reset()
t:translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2) t:translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)
t:scale(scale, scale) t:scale(scale, scale)
t:rotate(angle) t:rotate(angle)
t:translate(x - z._chunk_size, y - z._chunk_size / 2) t:translate(gx - z._chunk_size, gy - z._chunk_size / 2)
z:draw(t) z:draw(t)
end end