这个lua代码可以更好地编写吗?

时间:2018-06-07 14:17:27

标签: lua lua-table

以下代码是否可以重构为更简洁或更清晰?我还附上了一张图片,以帮助说明我的想法。

    local playerAreaPos = {
    {x = playerPos.x, y = playerPos.y - 1, z = playerPos.z}, -- NORTH
    {x = playerPos.x, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH
    {x = playerPos.x + 1, y = playerPos.y, z = playerPos.z}, -- EAST
    {x = playerPos.x - 1, y = playerPos.y, z = playerPos.z}, -- WEST

    {x = playerPos.x - 1, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH-WEST
    {x = playerPos.x + 1, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH-EAST
    {x = playerPos.x - 1, y = playerPos.y - 1, z = playerPos.z}, -- NORTH-WEST
    {x = playerPos.x + 1, y = playerPos.y - 1, z = playerPos.z} -- NORTH-EAST
}

local posTable = {
    {x = playerPos.x, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH"},
    {x = playerPos.x, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH"},
    {x = playerPos.x, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH"},
    {x = playerPos.x, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH"},

    {x = playerPos.x + 2, y = playerPos.y, z = playerPos.z, dir = "EAST"},
    {x = playerPos.x + 3, y = playerPos.y, z = playerPos.z, dir = "EAST"},
    {x = playerPos.x - 2, y = playerPos.y, z = playerPos.z, dir = "WEST"},
    {x = playerPos.x - 3, y = playerPos.y, z = playerPos.z, dir = "WEST"},

    {x = playerPos.x - 2, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH_WEST"},
    {x = playerPos.x - 3, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH_WEST"},
    {x = playerPos.x + 2, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH_EAST"},
    {x = playerPos.x + 3, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH_EAST"},
    {x = playerPos.x - 2, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH_WEST"},
    {x = playerPos.x - 3, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH_WEST"},
    {x = playerPos.x + 2, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH_EAST"},
    {x = playerPos.x + 3, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH_EAST"}
}

for i = 1, #posTable do
    if targetPos == Position(posTable[i]) then
        if posTable[i].dir == "NORTH_EAST" then
            print("TELEPORT TO: ", playerAreaPos[8].x, playerAreaPos[8].y)
        elseif posTable[i].dir == "NORTH_WEST" then
            print("TELEPORT TO: ", playerAreaPos[7].x, playerAreaPos[7].y)
        elseif posTable[i].dir == "NORTH" then
            print("TELEPORT TO: ", playerAreaPos[1].x, playerAreaPos[1].y)
        elseif posTable[i].dir == "SOUTH_WEST" then
            print("TELEPORT TO: ", playerAreaPos[5].x, playerAreaPos[5].y)
        elseif posTable[i].dir == "SOUTH_EAST" then
            print("TELEPORT TO: ", playerAreaPos[6].x, playerAreaPos[6].y)
        elseif posTable[i].dir == "SOUTH" then
            print("TELEPORT TO: ", playerAreaPos[2].x, playerAreaPos[2].y)
        elseif posTable[i].dir == "EAST" then
            print("TELEPORT TO: ", playerAreaPos[3].x, playerAreaPos[3].y)
        elseif posTable[i].dir == "WEST" then
            print("TELEPORT TO: ", playerAreaPos[4].x, playerAreaPos[4].y)
        end
    end
end

此功能的目的是将敌人从posTable传送到playerAreaPos,同时确保他们在相应的行内传送,这意味着如果他们从主角向北3个方格,他们将被传送到主角北面的1个方格

2 个答案:

答案 0 :(得分:0)

local enemyPos = {x = 11, y = 22, z = 33}
local playerPos = {x = 10, y = 20, z = 30}
local beam_length = 3

if enemyPos.z == playerPos.z then
   local dx = enemyPos.x - playerPos.x
   local dy = enemyPos.y - playerPos.y
   local ax, ay = math.abs(dx), math.abs(dy)
   local len_max, len_min = math.max(ax, ay), math.min(ax, ay)
   if len_max >= 2 and len_max <= beam_length and len_min % len_max == 0 then
      print("TELEPORT TO: ", playerPos.x + dx / len_max, playerPos.y + dy / len_max)
   end
end

答案 1 :(得分:0)

有很多方法可以实现你的问题,但是我会尝试使用一些数学,然后我会对它们做一些考虑。

看看下一个diagram我们的英雄与敌人之间的最小距离,当敌人落入射程时,它将被传送到英雄的半径,之后,敌人无法传送,除非我们的英雄将它扔到半径范围内。

考虑到这些因素,我做了这个实现

<span v-for="(val, key, index) of person">key: {{key}}, val: {{val}}<separator :items="person" :index="index">, </separator></span>

之前的实现需要大量计算,如果你在场景中放置了很多敌人,我的建议是使用C API或Box2D最后在发生碰撞时使用sensor fixture并且对此非常有用排序情况。目前有许多具有这些功能的Lua SDK,因此您可以启动。