Page 80 of 85

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Tue Feb 04, 2020 3:00 pm
by pauljessup
Here's a silly question- is there a way I can do parallax scrolling? ie: draw each individual layers at different relative x/y to the main layer? Usually I would keep each one on a separate speed, and modify the x/y based on this speed (further back=slower, after the player/closer to the viewer=faster), but I don't see any way to draw each layer separately on different x/y without completely overriding the layer's draw function. Am I missing something? Should I just used Tiled's save to image and just load these as Love image's and do it manually like that?

That's the probably the route I'm going to do...

Can't have a good platformer without some nice plax scrolling :)

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Tue Feb 04, 2020 4:07 pm
by Karai17
The map.layers table is set up to both be an array and a hashmap, so you could override map.draw or create your own map.parallax_draw function and just loop through the layers table and draw them based on their parallax speed.

Code: Select all

local map = sti("maps/01.lua")
map.custom_draw = function(self)
   for _, layer in ipairs(self.layers) do
      if layer.visible and layer.opacity > 0 then
         local r,g,b,a = lg.getColor()
         local px, py = layer.properties.speed_x, layer.properties.speed_y  -- do your parallax calc here
         lg.push()
         lg.setColor(r, g, b, a * layer.opacity)
         lg.translate(math.floor(px or 0), math.floor(py or 0))
         layer:draw()
         lg.pop()
      end
   end
end

function love.draw()
   map:custom_draw()
end
This may be a good candidate for a simple parallax plugin that reads a layer's speed property or some value with a similarly meaningful name that offsets a layer's draw position.

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Tue Feb 04, 2020 4:47 pm
by pauljessup
Oh! Right, that makes perfect sense.

When I get time to code it I'll add that plugin to the repo, if that's cool

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Tue Feb 04, 2020 8:32 pm
by pauljessup
Update: added some code modifications. Latest Love2d's alpha channel/opacity is no longer 0-255, but rather 0-1 (floating), which means it can just use Tiled's opacity for each layer just fine. I also corrected local px, py, and other issues I had no clue over, since I didn't want to pore through STI's code to see what was what.

I also added two auto scroll properties, so that you can have it scrolling x/y, etc.

In case anyone's interested, the actual function I got working is (based on the code above):

Code: Select all

	map.plax_draw = function(self, tx, ty)
		for _, layer in ipairs(self.layers) do
		   if layer.visible and layer.opacity > 0 then
			  local r,g,b,a = love.graphics.getColor()
			  local px, py=0, 0
			  
			  if(layer.properties["speed_x"]~=nil)then px=tx*layer.properties["speed_x"] end
			  if(layer.properties["speed_y"]~=nil) then py=ty*layer.properties["speed_y"] end
			  if(layer.properties["autoscroll_x"]~=nil)then px=tx+(layer.properties["autoscroll_x"]*layer.scrollx) end
			  if(layer.properties["autoscroll_y"]~=nil) then py=ty+(layer.properties["autoscroll_y"]*layer.scrolly) end
			  			  
			  love.graphics.push()
			  love.graphics.setColor(r, g, b, layer.opacity)
			  love.graphics.translate(math.floor(px or 0), math.floor(py or 0))
			  layer:draw()
			  love.graphics.pop()
		   end
		end
	 end
speed_x and speed_y being the layer attributes, if none are found it just follows the regular draw routine. Values over 1 go faster, values over 0 go slower, and a value of 0 is static (with 1 following the player normally...since this is a float, slower is 0.25, etc, while faster is 1.5, etc)

And here's how you call it

Code: Select all

	map:plax_draw(-tx, -ty)
so it's easy to just put into any existing projects that uses the example code in the documentation

I'll work on turning this into a plugin this weekend, just wanted to get it working in game

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Tue Feb 04, 2020 11:14 pm
by Karai17
hi5! I noticed that you have sx and sy as args but you don't actually use them, and that your px and py values are actually globals. You may want to tidy that up a bit before submitting a plugin. ;)

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Wed Feb 05, 2020 2:40 am
by pauljessup
Aha, I didn't know that the px and py where globals. That makes that a lot easier. Yeah, was not going to just submit this code as a plugin, this was me messing around and getting stuff to work, not really clean code yet.

I'm still in the experimental testing stuff out phase in all of this platformer, def not anywhere near as clean as I want it at the moment. But it's getting to the part where the experimentation is near done, and I can actually create proper clean code that uses reusable stuff, etc.

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Wed Feb 05, 2020 7:56 am
by Karai17
What I meant was that they *shouldn't* be globals, but the way you've defined them, they are. Globals are bad except in extremely specific cases and you should do your best to avoid them at almost all costs.

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Thu Feb 06, 2020 4:18 pm
by pauljessup
I do know what globals are, I was just going off of your code above, so I was assuming that you meant that they were global to the STI library, which didn't make any sense to me yet, but I see what you're saying. I've already fixed the code, but I might not do a plugin just yet. I don't know enough about the STI library, and I've barely used it.

Also, been a few years since I used Love and Lua, so it's taking me a bit to get back into after coming from C

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Thu Feb 13, 2020 9:49 pm
by OtherCannon
pauljessup wrote:
Fri Jan 31, 2020 10:36 pm
Sounds good, will do! I was like giving back to the tools I use, when I can
yeah I would like that too considering how short the tutorial is

Re: Simple Tiled Implementation - STI v1.2.3.0

Posted: Sat Feb 15, 2020 3:28 pm
by OtherCannon
Aight so I was trying to figure out this problem
In my code I have a layer called walls, which has bool named collidable, which has a checkmark. When I load into the project, however, not only does the player not collide with the walls, but the frame that shows that collision is on is split.
Here's my bool showing collidable is on:
Image
This is what it looks like in game:
Image