Page 63 of 92

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sat Nov 26, 2016 2:11 am
by megalukes
Well, I'm having some trouble here. Currently I'm controlling how each layer is drawn. I do something like this:

Code: Select all

draw_all_bottom_layers
draw_sprites
draw_all_top_layers
This works just fine, but since I'm working in a topdown rpg I need something else. I'd like to control how each tile in a layer is drawn for certain situations. I'd need to change each tile's z depending on where my sprite is. I know how to do it, all I need is the access of each tile's information, so I can sort them with my table of sprites in love.draw(). This is what happens at the moment (my sprite gets this odd haircut :ultrahappy: ):
megagif.gif.gif
megagif.gif.gif (37.04 KiB) Viewed 4089 times
Is there a way I can do this? Before using STI I was thinking about writing my own tiled lib exactly because of this, but STI saved me a lot of time. Now I'm wondering what should I do. Thank you in advance.

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sat Nov 26, 2016 6:23 am
by Karai17
https://github.com/karai17/Simple-Tiled ... t.lua#L449

You can access all individual tile instances through here.

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sun Nov 27, 2016 12:20 am
by megalukes
Karai17 wrote:https://github.com/karai17/Simple-Tiled ... t.lua#L449

You can access all individual tile instances through here.
Well, I tried this. I modified STI first:

Code: Select all

table.insert(self.tileInstances[tile.gid], {
					layer = layer,
					batch = batch,
					id    = id,
					gid   = tile.gid,
					x     = tileX,
					y     = tileY,
					r     = tile.r,
					oy    = 0,
					quad  = tile.quad, -- I add this
					tileset = self.tilesets[tile.tileset].image, -- and this
So I went to the drawing process:

Code: Select all

			for i,v in pairs(map.tileInstances) do
				for k, j in ipairs(v) do
					if j.layer.name == "bot2" then
						love.graphics.draw(j.tileset, j.quad, j.x, j.y, j.r)
					end
				end
			end
It worked but I didn't even try to sort the tiles because my fps went from 60 to 30. I'm trying to find the best solution for that, even though I'm starting to get hopeless Any tips?

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sun Nov 27, 2016 12:24 am
by Karai17
The issue is that you are now doing thousands of draw calls. Sti uses sprite batches to reduce the draw calls from thousands to, like, 4~

You could instead create objects out of those tables and then sort the objects to draw by their Y value.

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sun Nov 27, 2016 3:37 am
by megalukes
Oh, thank you so much. That's how I did it:

Change in STI's :setSpriteBatches (I created the layer.tiles table in the function's beginning):

Code: Select all

		table.insert(layer.tiles, {
					tileset = self.tilesets[tile.tileset].image,
					gid   = tile.gid,
					x     = tileX,
					y     = tileY,
					z     = tileY,
					r     = tile.r,
					oy    = 0,
					quad  = tile.quad,
					kind  = "tile",
				})
My drawing method:

Code: Select all

function sceneMap:drawMidLayer(map)
	-- merging and sorting
	local tileLayer = nil
	local drawingTable = nil
	for i,v in ipairs(map.layers) do
		if v.name == "mid" then
			tileLayer = table.copy(v.tiles)
		end
	end
	if tileLayer ~= nil then
		drawingTable = table.merge(tileLayer,self.events)
	else
		drawingTable = self.events
	end
	table.sort(drawingTable,sortZ)
	for i,v in ipairs(drawingTable) do
		if v.kind == "tile" then 
			love.graphics.setColor(255, 255, 255, 255)
			love.graphics.draw(v.tileset, v.quad, v.x, v.y, v.r)
		else
			v:draw()
		end
	end
end
I was kind of worried because I had to keep copying and merging the same tables all the time, but I didn't notice any performance issue. Also, since the "mid" layer will be set only when it's necessary, I don't think I'll have more than 20~80 objects to draw for map. Performance looks fine so far.

Thanks you so much for the help. And here's a gif of the final result :awesome:
z teste.gif
z teste.gif (105.01 KiB) Viewed 4020 times

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Sun Nov 27, 2016 3:40 am
by Karai17
Great job! Glad I could help :)

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Wed Nov 30, 2016 12:38 am
by luaiscool
Hi, Is there a tutorial on collisions?

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Wed Nov 30, 2016 1:14 am
by Karai17
You can look up tutorials on Box2D/love.physics or bump.lua. That is what STI uses.

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Wed Nov 30, 2016 9:04 pm
by luaiscool
Karai17 wrote:You can look up tutorials on Box2D/love.physics or bump.lua. That is what STI uses.
I'll look at them, is there an example using any of these for easy set up?

Re: Simple Tiled Implementation - STI v0.16.0.3

Posted: Wed Nov 30, 2016 9:06 pm
by Karai17
The readme on github shows how you might set up box2d (love.physics), but I haven't really put much time into writing tutotrials for collision.