lualove2droguelike

How can I get values from multiple instances of a class?


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!


Solution

  • A simple idea is to store "who is here" information for every cell of the field and update this information on every move of every object.

     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
    

    Usage example:

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

    Then, in love.keypressed(key):

    if key == "kp8" and player1:can_move(0, -1) then
        player1:move(0, -1)
    end