Page 44 of 92

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 8:49 pm
by TooMuchAbstraction
Yep, figured it out, thanks! Here's what I'm doing (powObject is the map object, powFixture is the associated fixture I created):

Code: Select all

        -- Delete the object from the map's object list.
        map.objects[powObject.id] = nil
        -- Delete the object from the layer in the map it is in.
        local newObjects = {}
        for i, obj in ipairs(powObject.layer.objects) do
            if obj ~= powObject then
                table.insert(newObjects, obj)
            end
        end
        powObject.layer.objects = newObjects
        -- Stop drawing the object.
        map:setObjectSpriteBatches(powObject.layer)
Next question: I want to have multiple maps that smoothly connect to each other -- each map is a separate "room", and I want to be able to assemble different arrangements of rooms on the fly, so I don't even know what their positions will be relative to each other until runtime. This requires me to offset the map and its collision data. I added this method to the Map class, which causes it to draw the map at a different location:

Code: Select all

function Map:offset(dx, dy)
    self.offsetx = self.offsetx + dx
    self.offsety = self.offsety + dy
    for _, layer in pairs(self.layers) do
        layer.x = layer.x + dx
        layer.y = layer.y + dy
    end 
end
and offset the collision detection with this:

Code: Select all

local body = map.box2d_collision.body
body.setPosition(body.getX() + dx, body.getY() + dy)
But bizarrely, I have to apply 2x the translation to the body that I do to the map. Like, if I move the collision body by (50, 50) then I have to call map:offset(25, 25). Any idea why that is?

More importantly, the map refuses to draw if it moves too far. I'm guessing there's some kind of automatic culling going on. What's the recommended way to deal with this? Am I barking up the wrong tree entirely?

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 8:52 pm
by Karai17
I already added this functionality a few weeks or so ago. Just pass in an offset_x and offset_y (in pixels) after the plugins table in STI.new and it Just Works.

Code: Select all

local sti = require "sti"
local map = sti.new("some_map.lua", {}, 500, 500)

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 9:17 pm
by TooMuchAbstraction
Awesome! But, some bad news: that doesn't affect the collision detection. This is easily fixed though! Just modify box2d_init so that its initialization of the "body" local looks like

Code: Select all

local body      = love.physics.newBody(world, map.offsetx, map.offsety)

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 9:28 pm
by Karai17
Hm, that's a point. I'll add that to box2d and bump within the next couple days or so, or feel free to send a PR.

Actually, can you test to see if it works with Bump? I am fairly certain I had collision working but I could be wrong.

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 9:35 pm
by TooMuchAbstraction
I have zero familiarity with bump, and my code is already somewhat heavily-tied to box2d so it's not trivial to test bump. However, a quick look at bump.lua shows no reference to map.offsetx or map.offsety, so I doubt that it works properly.

Re: Simple Tiled Implementation - STI v0.14.1.10

Posted: Fri Jan 01, 2016 10:19 pm
by bobbyjones
I'll test.

Re: Simple Tiled Implementation - STI v0.14.1.11

Posted: Fri Jan 01, 2016 11:54 pm
by Karai17
I've merged in a few pull requests that fix both plugins. hi5!

Re: Simple Tiled Implementation - STI v0.14.1.11

Posted: Wed Jan 06, 2016 12:32 pm
by MikaelS
I asked you earlier about conversion of hexagonal coordinates to orthogonal and vice versa. Have you had any time to look into this? I tried myself using the code in Map:setSpriteBatches() because as far as I can tell this is where the same conversion takes place for the tiles themselves, but I haven't been successful so far.

Re: Simple Tiled Implementation - STI v0.14.1.8

Posted: Thu Jan 07, 2016 12:11 am
by premek
premek wrote:

Code: Select all

        for k,v in ipairs(map.layers.objects.objects) do
          if v.id == item.id then
            table.remove(map.layers.objects.objects, k)
          end
        end
        table.remove(map.objects, item.id)
        map:setObjectSpriteBatches(map.layers.objects)
Hi, I have one more question regarding manipulating map objects.
When I want to move an object on a map, I use this code:

Code: Select all

  moveObjectByItem = function(map, item)
    for k, obj in ipairs(map.layers.objects.objects) do
      if obj.id == item.id then
        map.layers.objects.objects[k].x = item.x
        map.layers.objects.objects[k].y = item.y+item.height
      end
    end
    map.objects[item.id].x = item.x
    map.objects[item.id].y = item.y+item.height
    map:setObjectSpriteBatches(map.layers.objects) -- FIXME this resets all objects animation when any object moves
The problem is that objects animations on the same layer are reset every time I call setObjectSpriteBatches (which is every frame when some object is moving) and then it looks like animations stops when an object is moving.

Is there any way to do it better?
Thanks!

Re: Simple Tiled Implementation - STI v0.14.1.11

Posted: Thu Jan 07, 2016 4:00 am
by Karai17
I would not actively use object layers, I would transfer object data over to a custom layer if you plan to create dynamic objects.