Domanda

I have this idea, where I want to use lua to create my levels, and the maps inside those levels. I want to simply and explicitly be able to manipulate data and add new levels as buy ins in the app store. How can I use lua to create maps and levels inside those maps? Does lua support OOP so I can make a base Map "class" and a base Level "class" or do I need to hardcode everything? I know for a fact that angry birds uses lua, so can I?

Any directions or samples are much appreciated. Thanks.

È stato utile?

Soluzione

Lua does not support classes directly (although there are some libraries which offer class-like functionality), instead it uses data structures called tables. Tables are very versatile because they can contain strings, numbers, functions and other tables.

The big benefit of Lua is that it is scripted, so it can be downloaded as a file and run while the compiled game is running. Here's a possible example of level stored in a .lua file:

return {
  name = 'Example level',
  score = 80,
  map = {
    height = 8,
    width = 8,
    data = 'sssssssssoooooossoooooossoooooossoooooossoooooossbooooesssssssss'
  },
  ondraw = function (self)
    drawmap(self.map)
    drawscore(self.score)
  end
}

Another feature which you may find useful are metatables. They sort-of allow inheritance to be used with tables. You can read more about implementing classes with metatables here: http://lua-users.org/wiki/SimpleLuaClasses

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top