heh, sorry for bringing bad news. but actually, if you are still in stage of prototyping and still experimenting with variations of gameplay, etc… i would advise to go ahead with whatever you can do to progress fast. the idea is that you don’t want to spend time making a nice code at this stage, you want to move on and experiment very quickly.
once you think you have a few levels which play well, and you have feedback from various people that they feel the game feels and plays ok, it would be a good time to refactory your code and make it more flexible, modular and easier to update later. you will lose some time redoing your code, but it will surely pay off later as you’ll save so much time debugging and updating.
i myself have made a complete reworking of my new game’s code 2 times, and it really paid off, because it made making updates and bug tracking soooooo easier and faster.
code separation really depends a lot on what kind of game you are making, and whether you’re making your levels in some level editor or hard coding them.
the way i do this, which is not necessarily the best way, is something like this:
levelParamters_NNN.xml
I personally use xml but any other text format is useful, especially JSON. This is the data that describes your level. It might be something exported from Level editor, or data you will enter yourself. I personally create my levels in Illustrator, and use my custom made exporter to export all data to SVG (which is a xml file).
In this file you want to specify whatever is specific to your game level.
In an AngryBirds-like game it would be a level layout, how many birds you have available and what kind of birds they are, etc…
Usually here I would also assign name of the functions to call on collision and similar.
Ie:
< entity=“yellowBird” count=“4” onTouch=“speedUp” onTouchArgs=“15,5” onCollision=“breakWood” onCollisionArgs=“1000,55” />
where for speedUp and breakWood will functions that will be called when an event occure and 15 and 5 are their respective arguments that will be passed to functions.
gameplay.lua
-
loads level paramters levelData_NNN.xml. Creates entities (ie. 4 yellow birds from example above).
-
creates game world according to level parameters ( creates spritesheets, spawns the initial view with backgrounds, platforms, player and non-player entities, etc…)
-
loads levelLogic_NNN.lua ( see below ) - this contains all level-based interactions
-
adds event listeners, according to xml file above. usually i will store all callbacks in one table, like:
local gameEventsCallbacks = {
gameOver = function ( self, event )
-- do something
end,
updateScore = function ( self, event )
-- do something
end,
playerCollision = function ( self, event )
-- do something that is common to all levels, and then
-- call level-specific call back in levelLogic lua, according to xml example above:
self.logic[event.onCollisionArgs] ( event.entity.onCollisionArgs )
end
}
and then later in the code i would do point to this event callbacks table like this:
function Gameplay:startGame()
------------------------------------------------
-- Listen to game events (callback below)
------------------------------------------------
Runtime:addEventListener ( "onGameEvent", self )
end
function Gameplay:onGameEvent ( event )
if gameEventsCallbacks[event.phase] then
gameEventsCallbacks[event.phase] ( self, event )
end
end
All other classes ( player, entity, whatever ) send messages to gameplay.lua using custom onGameEvent, where phase in this example can be “gameOver” or “playerCollision” or whatever…
- finally i would initialize main game loop here through onEnterFrame event.
levelLogic_NNN.lua
Level logic handles some level-specific code. For example, maybe you want in different levels to resolve collision in different way between player and some sort of enemies.
Level logic will be the file which will contain the “speedUp(args )” and breakWood(args) functions that I mention above in xml.
Now if on contrary, you have almost all levels the same, I wouldn’t copy all the time the same stuff into many lua files.
Instead I would make levelLogic_default.lua. Every time I load a level, gameplay.lua would first try to load levelLogic_005.lua ( for example, level 5 ). if the file is found, great, if not - load levelLogic_default.lua
Phewwww… sorry for making it probably even more confusing. Anyway, hope to give you some ideas, remember there are no unique solutions 
[import]uid: 80100 topic_id: 21802 reply_id: 86609[/import]