This article initially We'll get down to the details further down, don't worry. To read along, you'll need basic understanding of Lua and Object-Oriented Programming.
///0) Lua. Link to the free version of PiL. Link to article explaining the differences between 5.0 and 5.1. The rest of the tutorial assumes basic notions of Lua and OOP.
Grokking the Moai programming model.
editWe will first review two core concepts around which the Moai SDK is built. This section brushes past a lot of details in order to give a broad perspective of the engine.
The Dependency Graph
editEach graphical element (a "prop" in Moai parlance), when moved, rotated or scaled, defines its own coordinate system, and other props can be added as children and share some or all of the new coordinates. It makes it very easy to compose elements. People familiar with 3D graphics would know this as affine transforms and UV coordinates.
For example, you can tie the position and scale of a text label to a sprite, but not its rotation, and tie the scale, rotation and position of the sprite to the background. You can then zoom everyting in one command, rotate the wole game world while keeping the label next to the sprite and rotate the whole scene or the sprite relative to the map while leaving the label straight. You don't have to track the relative angles, the engine does it for you.
Altogether, this system is refered to as the dependency graph. It is the first pillar of the Moai model, and extends to the properties of objects beyond props, (see the inheritance diagram of the MOAINode class for a complete list) .
The Action Tree
editThe second pillar is the action tree. Every object whose properties depends on time (commonly referred to as an "action" in local lingo, e.g.: animations, physics worlds, coroutines, ... ) is started/paused by being attached or detached to/from the root action (which provide a tick each frame). Alternatively you can attach actions to a parent action. By attaching/detaching the parent you (un)pause the whole sub tree.
A third pilar??
editBoth trees are defined declaratively in Lua, and implemented in C++, giving the best of both worlds: expressivity and speed.
Viewport, Coordinates and Layers
editProps, Decks, and Transforms
editAnimations
editLink to a coroutine article (or PiL chapter).
Moai Object model
edit(fairly standard if you know Lua). Illustrate with Lua pseudo-code. Now explain the mapping to C++.