Skip to content

Commit

Permalink
smooth tile patterns, tile debug, tile id lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Jan 25, 2019
1 parent d49a4f1 commit 0733a45
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 90 deletions.
8 changes: 4 additions & 4 deletions entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function entities.server.update(dt)
if choice ~= 'none' then
local x = (cx*entities.chunkSize + math.random()*entities.chunkSize)*15
local y = (cy*entities.chunkSize + math.random()*entities.chunkSize)*15
-- if not in spawn area
if not (x^2 + y^2 < 192^2) then
-- if not in spawn area or wall
if x^2 + y^2 > 192^2 and serverRealm.world:getTile(x, y) ~= tile2id['wall'] then
entities.server.defs[choice]:new{x=x, y=y}:spawn()
end
end
Expand All @@ -67,7 +67,7 @@ function entities.server.update(dt)
local x = (cx*entities.chunkSize + math.random()*entities.chunkSize)*15
local y = (cy*entities.chunkSize + math.random()*entities.chunkSize)*15
-- if on grass
if serverRealm.world:getTile(x, y) == 1 then
if serverRealm.world:getTile(x, y) == tile2id['grass'] then
entities.server.defs.tree:new{x=x, y=y}:spawn()
end
end
Expand All @@ -76,7 +76,7 @@ function entities.server.update(dt)
for j=1, entities.chunkSize do
local x = (cx*entities.chunkSize + (i-1))*15
local y = (cy*entities.chunkSize + (j-1))*15
if serverRealm.world:getTile(x, y) == 8 then
if serverRealm.world:getTile(x, y) == tile2id['wall'] then
entities.server.defs.wall:new{x=x, y=y}:spawn()
end
end
Expand Down
8 changes: 4 additions & 4 deletions entityDefs/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ for _, sc in ipairs{'server', 'client'} do
dd = math.sqrt(dx^2 + dy^2)
local spd = self.spd*(self.inputState.keyboard.lshift and 2.5 or 1)
local tile = self.realm.world:getTile(self.x, self.y)
if tile == 5 or tile == 6 then spd = spd * 1.5 end -- platform, path
if tile == 4 then spd = spd / 2 end -- water
if tile == tile2id['platform'] or tile == tile2id['path'] then spd = spd * 1.5 end
if tile == tile2id['water'] then spd = spd / 2 end
local attackItem = self.items.getItem(self.inventory.items[2])
if dd ~= 0 then
self.body:applyForce(dx/dd*spd, dy/dd*spd)
Expand Down Expand Up @@ -284,7 +284,7 @@ function player.client:draw()

-- offset if on platform or path
local tile = clientRealm.world:getTile(self.x, self.y)
if tile == 5 or tile == 6 then
if tile == tile2id['platform'] or tile == tile2id['path'] then
love.graphics.translate(0, -2)
end

Expand All @@ -305,7 +305,7 @@ function player.client:draw()
love.graphics.clear()

-- offset/clip feet if in water
if tile == 4 then
if tile == tile2id['water'] then
love.graphics.translate(0, 4)
love.graphics.stencil(function()
love.graphics.rectangle('fill', self.x - 50, self.y - 4, 100, 100)
Expand Down
Binary file added gfx/tiles/all_tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed gfx/tiles/platformSheet.png
Binary file not shown.
Binary file added gfx/tiles/smooth_tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed gfx/tiles/tilesheet1.png
Binary file not shown.
Binary file removed gfx/tiles/tilesheet2.png
Binary file not shown.
Binary file added gfx/tiles/tilesheet2_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 17 additions & 24 deletions loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ gfx = {
}
},
tiles = {
tileSheet1 = love.graphics.newImage('gfx/tiles/tilesheet1.png'),
platformSheet = love.graphics.newImage('gfx/tiles/platformSheet.png'),
tileSheet2 = love.graphics.newImage('gfx/tiles/tilesheet2.png')
allTiles = love.graphics.newImage('gfx/tiles/all_tiles.png'),
smoothTiles = love.graphics.newImage('gfx/tiles/smooth_tiles.png')
},
environment = {
tree = love.graphics.newImage('gfx/environment/tree.png'),
Expand Down Expand Up @@ -193,14 +192,18 @@ function newTileSheet(sheet, w, h, pad, num, names)
local x = (i-1)*(w + pad*2) + 1
local y = 1
local sw, sh = sheet:getDimensions()
t.quads[names[i] or i] = love.graphics.newQuad(x, y, w, h, sw, sh)
local quad = love.graphics.newQuad(x, y, w, h, sw, sh)
t.quads[i] = quad
if names[i] then
t.quads[names[i]] = quad
end
end
return t
end

tileSheets.ts1 = newTileSheet(gfx.tiles.tileSheet1, 15, 15, 1, 4, {'grass', 'sand', 'rock', 'water'})
tileSheets.platform = newTileSheet(gfx.tiles.platformSheet, 15, 15, 1, 2)
tileSheets.ts2 = newTileSheet(gfx.tiles.tileSheet2, 15, 15, 1, 3, {'path', 'floor', 'wall'})
tileSheets.allTiles = newTileSheet(gfx.tiles.allTiles, 15, 15, 1, 9,
{'water', 'sand', 'grass', 'rock', 'path', 'floor', 'wall', 'platform', 'platform2'})
tileSheets.smoothTiles = newTileSheet(gfx.tiles.smoothTiles, 15, 15, 1, 16)

fonts = {
f10 = love.graphics.newFont(10),
Expand Down Expand Up @@ -233,33 +236,23 @@ love.graphics.clear(0, 0, 0)
love.graphics.setCanvas()
-- black tile
table.insert(tileImgs, love.graphics.newImage(tileCanv:newImageData()))
for _, v in ipairs{'grass', 'sand', 'rock', 'water'} do
for i=1, 9 do
love.graphics.setCanvas(tileCanv)
love.graphics.clear()
love.graphics.draw(tileSheets.ts1.sheet, tileSheets.ts1.quads[v], 0, 0)
love.graphics.draw(tileSheets.allTiles.sheet, tileSheets.allTiles.quads[i], 0, 0)
love.graphics.setCanvas()
table.insert(tileImgs, love.graphics.newImage(tileCanv:newImageData()))
end
shaders.mapRender:send('tiles', unpack(tileImgs))

local platformFrames = {}
for _, quad in ipairs(tileSheets.platform.quads) do
smoothTileImgs = {}
for i=1, 16 do
love.graphics.setCanvas(tileCanv)
love.graphics.clear()
love.graphics.draw(tileSheets.platform.sheet, quad, 0, 0)
love.graphics.draw(tileSheets.smoothTiles.sheet, tileSheets.smoothTiles.quads[i], 0, 0)
love.graphics.setCanvas()
table.insert(platformFrames, love.graphics.newImage(tileCanv:newImageData()))
table.insert(smoothTileImgs, love.graphics.newImage(tileCanv:newImageData()))
end
shaders.mapRender:send('platformFrames', unpack(platformFrames))

local tileImgs2 = {}
for _, v in ipairs{'path', 'floor', 'wall'} do
love.graphics.setCanvas(tileCanv)
love.graphics.clear()
love.graphics.draw(tileSheets.ts2.sheet, tileSheets.ts2.quads[v], 0, 0)
love.graphics.setCanvas()
table.insert(tileImgs2, love.graphics.newImage(tileCanv:newImageData()))
end
shaders.mapRender:send('tiles2', unpack(tileImgs2))
shaders.mapRender:send('smoothTiles', unpack(smoothTileImgs))

shaders.lifemana:send('lifemanaEmpty', gfx.hud.lifemanaEmpty)
6 changes: 6 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ require 'chat'

-- todo: better sword check
isSword = {['sword0']=true, ['sword1']=true, ['sword2']=true, ['sword3']=true, ['sword4']=true}
tile2id = {}
for i, v in ipairs{'water', 'sand', 'grass', 'rock', 'path', 'floor', 'wall', 'platform', 'platform2'} do
tile2id[v] = i
end

function love.load()
camera = Camera{ssx=gsx, ssy=gsy}
Expand Down Expand Up @@ -106,6 +110,8 @@ function love.update(dt)
manual_gc(1e-3, 64)
prof.pop('update gc')
prof.pop('update')

shaders.mapRender:send('drawDebug', drawDebug)
end

function love.mousepressed(x, y, btn, isTouch, presses)
Expand Down
29 changes: 21 additions & 8 deletions shaders/mapGen.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ float snoise(vec2 v)
}


// 1: water, 2: sand, 3: grass, 4: rock, 5: path, 6: floor, 7: wall, 8: platform

vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{
vec2 uv = screen_coords;
uv += camPos;
vec2 p = floor(uv);

// grass/sand/rock/water
int tileChoices[3] = int[](1, 2, 4);
// grass/sand/water
int tileChoices[3] = int[](3, 2, 1);
float r1 = snoise(p/32.0)*0.5 + 0.5;
int choice = 0;
for (int i=0; i < 3; i++) {
Expand All @@ -98,9 +100,10 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
break;
}
}
// rock
float r2 = snoise(1000.0 + p/64.0)*0.5 + 0.5;
if (r1 < 0.2 && r2 < 0.5) {
choice = 3;
choice = 4;
}

// buildings
Expand All @@ -109,31 +112,41 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
vec4 vpts = voronoi(vp, 0.5);
vec2 vd = vec2(floor(abs(vp.x - vpts.x)*freq), floor(abs(vp.y - vpts.y)*freq));
// inside
// floor
if (vd.x <= 4.0 && vd.y <= 6.0) {
choice = 7;
choice = 6;
}
// path
if (distance(vp, vpts.xy)*freq < 2.0) {
choice = 6;
choice = 5;
}
// walls
if ((vd.x == 4.0 && vd.y <= 6.0 || vd.y == 6.0 && vd.x <= 4.0)
&& vd.x > 1.0 && vd.y > 1.0) {
choice = 8;
choice = 7;
}

// paths
float d = length(p);
float angle = atan(p.y, p.x);
angle += snoise(2000.0 + p/64.0)/d*4.0;
if (distance(mod(angle/(2.0*M_PI)*8.0, 1.0), 0.5) < 0.3/(d/8.0)) {
choice = 6;
choice = 5;
}

// platform
if (length(p) < 8.0) {
choice = 5;
choice = 8;
}

// test
if (p == vec2(0.0) || p == vec2(1.0, 0.0) || p == vec2(0.0, 1.0) || p == vec2(1.0, 2.0)) {
choice = 5;
}
if (p == vec2(0.0)+vec2(18.0, 0.0) || p == vec2(1.0, 0.0)+vec2(18.0, 0.0) || p == vec2(0.0, 1.0)+vec2(18.0, 0.0) || p == vec2(1.0, 2.0)+vec2(18.0, 0.0)) {
choice = 5;
}


return vec4(choice/255.0, 0.0, 0.0, 1.0);
}
Loading

0 comments on commit 0733a45

Please sign in to comment.