对多个列表进行排序 lua

Sort multiple lists lua

如何对 2 个列表 y 位置(地图图块和人物)进行排序并根据 y 的顺序绘制它们。我要使用的 2 个列表:

map = {}
map.y = {60,10,40,80}
map.t = {0,0,1,1} -- type

people = {}
people.y = {0,100}
people.t = {0,1} -- type

我目前可以排序和绘制单个英雄和盒子列表。 排序/抽奖代码:

box1 = love.graphics.newImage("box1.png")
box2 = love.graphics.newImage("box2.png")
box3 = love.graphics.newImage("box3.png")
hero = love.graphics.newImage("hero.png")

object = {
    x = {0, 50,100,200},
    y = {0,200, 50,100},
    g = {0,1,2,3}
}
function sortIndex(item)
    local i
    local id = {}       -- id list
    for i = 1, #item.x do   -- Fill id list (1 to length)
        id[i] = i
    end
--  print( unpack(id) ) -- Check before
    table.sort(id,sortY)-- Sort list
--  print( unpack(id) ) -- Check after
    item.sort = id      -- List added to object.sort
--  Sort id, using item values
    function sortY(a,b)
        return item.y[a] < item.y[b]
    end
end
function drawObject()
    local i,v, g,x,y
    for i = 1, #object.x do
        v = object.sort[i] -- Draw in order
        x = object.x[v]
        y = object.y[v]
        g = object.g[v]
        if      g == 0 then g = hero -- set to an image value
        elseif  g == 1 then g = box1
        elseif  g == 2 then g = box2
        elseif  g == 3 then g = box3
        end
        love.graphics.draw(g,x,y,0,7,7)
    end
end

更新排序:

sortIndex(object)

我的函数对 id 列表进行排序,比较 y 位置列表。 id 用于根据 y 的顺序绘制对象。我如何将 2 个 id 列表排序在一起比较 2 个 y 位置列表,然后按顺序绘制它们?

也许在画图的时候,从地图瓦片切换到依赖人y,但我不知道如何。

可能与您之前的问题有很大关系:

我假设如果您的身高可以是 1,2 和 3(1 在顶部),您首先要渲染 Y1 处的所有图块,然后是 Y1 处的所有人,然后是 Y2 和 Y3。为此,您必须制作一个组合列表并对其进行排序:

map = {}
map.y = {60,10,40,80}
map.t = {0,0,1,1} -- type

people = {}
people.y = {0,100}
people.t = {0,1} -- type

local all = {}
local map_y = map.y
local offset = #map_y
local people_y = people.y
-- Fill the list with map tiles
for i=1,offset do
    all[i] = {1,i,map_y[i]} --{type,index,y}
end
-- Fill the list with people
for i=1,#people_y do
    all[i+offset] = {2,i,people_y[i]}
end
-- Do the sorting
-- It works a bit like your previous question:
-- 'all' contains "references":
--    They tell us is it's from map/people + the index
-- We sort the references using the third element in it:
--    The 'y' variable we put there during the first 2 loops
table.sort(all,function(a,b)
    return a[3] < b[3]
end)
-- Printing example
-- The references are sorted using the 'y' field of your objects
--    With v[1] we know if it's from map/people
--    The v[2] tells us the index in that ^ table
--    The v[3] is the 'y'-field. No real need to remove it
for k,v in pairs(all) do
    print(v[1] == 1 and "Map" or "Person",v[2],"with y being",v[3])
end

输出:

Person 1 with y being 0
Map 2    with y being 10
Map 3    with y being 40
Map 1    with y being 60
Map 4    with y being 80
Person 2 with y being 100

我想补充两点,与我回答的问题没有任何关系:

  • 如果每个元素都有一个 table,也许会更容易。 您的人员将是 {0,0} 和 {100,1},这可能更容易操纵。
  • 如果您希望您的东西总是有序的,您可能想要使用这个:Sorted List。如果您保留所有对象的排序列表,则不必在每次 add/remove 元素或更糟的是每次渲染时都对列表进行排序。 (取决于人们是否移动) 如果您计划拥有大量 map/people 对象,这可能有助于提高性能。 (排序列表可能对您当前的数据系统有用,但对 {y=1,t=1} 也有用)
function sortIndex(...)
  sorted = {}  -- global
  local arrays_order = {}
  for arr_index, array in ipairs{...} do
    arrays_order[array] = arr_index
    for index = 1, #array.y do
      table.insert(sorted, {array = array, index = index})
    end
  end
  table.sort(sorted,
    function (a,b)
      local arr1, arr2 = a.array, b.array
      local ind1, ind2 = a.index, b.index
      return arr1.y[ind1] < arr2.y[ind2] or
        arr1.y[ind1] == arr2.y[ind2] and arrays_order[arr1] < arrays_order[arr2]
    end)
end

function drawAll()
  for _, elem_info in ipairs(sorted) do
    local array = elem_info.array
    local index = elem_info.index
    local x = array.x[index]
    local y = array.y[index]
    if array == map then
      -- draw a map tile with love.graphics.draw()
    elseif array == people then
      -- draw a human with love.graphics.draw()
    end
  end
end

sortIndex(map, people)  -- to draw map tiles before people for the same y