Simple Tiled Implementation - STI v1.2.3.0

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
megalukes
Citizen
Posts: 94
Joined: Fri Jun 27, 2014 11:29 pm
Location: Brazil

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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 4140 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.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post by Karai17 »

https://github.com/karai17/Simple-Tiled ... t.lua#L449

You can access all individual tile instances through here.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
megalukes
Citizen
Posts: 94
Joined: Fri Jun 27, 2014 11:29 pm
Location: Brazil

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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?
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
megalukes
Citizen
Posts: 94
Joined: Fri Jun 27, 2014 11:29 pm
Location: Brazil

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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 4071 times
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post by Karai17 »

Great job! Glad I could help :)
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
luaiscool
Prole
Posts: 34
Joined: Mon Apr 21, 2014 11:03 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post by luaiscool »

Hi, Is there a tutorial on collisions?
http://xkcd.com/979/

Code: Select all

if signature = true then
    print(signaturetext)
else
    print("Error: Signature Not Found")
end
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post by Karai17 »

You can look up tutorials on Box2D/love.physics or bump.lua. That is what STI uses.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
luaiscool
Prole
Posts: 34
Joined: Mon Apr 21, 2014 11:03 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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?
http://xkcd.com/979/

Code: Select all

if signature = true then
    print(signaturetext)
else
    print("Error: Signature Not Found")
end
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v0.16.0.3

Post 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.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest