1

I am making a roguelike in Love2D as a hobby project. My approach is to try and use as much of the native capabilities of Lua and the Love2D (0.10.1) API as possible, without relying on fancy libraries like middleclass or HUMP, so as to learn more about the language.

After reading PiL's chapters on OOP and seeing the power there, I decided to set up a Mob class (using metamethods to emulate class functionality) that encompasses the players, monsters, and other NPCs (anything that can move). So, far, it's working beautifully, I can create all kinds of instances easily that share methods and all that stuff. But there's a lot of things I don't know how to do, yet, and one of them is holding my prototype up from further progress.

Setting up collision with the map itself wasn't too bad. My maps are tables full of tables full of integers, with 0 being the floor. The game draws "." and "#" and "+" and such to denote various inanimate objects, from each table. Player 1 moves using the numpad, and their position is tracked by dividing their raw pixel position by 32 to create a grid of 32x32 "tiles". Then, inside love.keypressed(key), I have lines like:

if key == "kp8" and currentmap[player1.grid_y - 1][player1.grid_x] == 0 then
        player1.grid_y = player1.grid_y - 1

and so on, with elseifs for each key the player can press. This prevents them from walking through anything that isn't an open floor tile in the map itself.

But, I'm trying to implement some kind of "collision detection" to prevent MOBs from walking through each other and to use in writing the rules for combat, and this is trickier. I had a method in place to calculate the distance between mobs, but I'm told this might eventually cause rounding errors, plus it had to be written for each combination of mobs I want to test, individually.

What I'd like to know is: Is there a known (preferably elegant) way to get all instances of a particular class to pass some number of values to a table?

What I'd like to do is "ask" every Mob on a given map where they are, and have them "report" self.grid_x and self.grid_y to another layer of map that's just for tracking mobs (1 if self.is_here is true, 0 if not, or similar), that gets updated every turn. Then, I could implement collision rules based on coordinates being equal, or maybe a foo.is_here flag or something.

I have only vague ideas about how to proceed, however. Any help would be appreciated, including (and maybe especially) feedback as to a better way to do what I'm trying to do. Thanks!

4

2 回答 2

0

有几种方法可以获得所有实例数据,但其中一种更简单的方法可能是在创建它们时将它们全部添加到表中。如果您为该实例添加整个表,则所有值都将在主表中更新,因为它就像一个指针集合。

function mob:new( x, y, type )
    self.x = 100
    self.y = 200
    self.type = type
    -- any other declarations you need
    table.insert(allMobs, self)
    return self
end

在这里,我们将所有的生物插入到表“allMobs”中。一旦我们有了它,我们就可以简单地迭代并获得我们所有的坐标。

for i, v in ipairs(allMobs) do
    local x, y = v.x, v.y
    -- Do whatever you need with the coordinates. Add them to another table, compare
    -- them to others, etc.
end

现在我们有一张桌子,里面有我们所有的小怪,还有一种访问他们每个位置的方法。如果您有任何进一步的疑问,请告诉我。

于 2016-09-04T08:31:56.833 回答
0

一个简单的想法是为该字段的每个单元格存储“谁在这里”信息,并在每个对象的每次移动时更新此信息。

 function create_game_field()
 -- initialize a table for storing "who is here" information
      who_is_here = {}
      for y = 1,24 do
        who_is_here[y] = {}
        for x = 1,38 do
            who_is_here[y][x] = 0
        end
      end
 end

function Mob:can_move(dx, dy)
     local u = currentmap[self.y + dy][self.x + dx]
     local v = who_is_here[self.y + dy][self.x + dx]
     if u == 0 and v == 0 then
          return true
     else
     end
end

function Mob:move(dx, dy)
-- update "who is here"
     who_is_here[self.y][self.x] = 0
     self.x, self.y = self.x + dx, self.y + dy
     who_is_here[self.y][self.x] = 1
end

function Mob:who_is_there(dx, dy) -- look who is standing on adjacent cell
    return who_is_here[self.y + dy][self.x + dx] -- return mob or nil
end

function Mob:roll_call()
    who_is_here[self.y][self.x] = 1
end

使用示例:

-- player1 spawns in at (6,9) on the grid coords
player1 = Mob:spawn(6,9)
-- player1 added to who_is_here
player1:roll_call()

然后,在 love.keypressed(key) 中:

if key == "kp8" and player1:can_move(0, -1) then
    player1:move(0, -1)
end
于 2016-09-04T07:58:06.053 回答