In the previous post, we have seen how to use the Box2D support into the AGE engine. Now I just want to show you how simple it is to add a new framework/engine support.
For example, we are going to add the Nape support.
For those of you who don't know, Nape is a Haxe/AS3 physics engine. Since Nape is really close to Box2D, it will be simple to understand how box2d has been added to AGE.
Since AGE is based on a behaviors system, we are just going to create a new behavior, that we will call NapeMovementBehavior :
Now we are going to initialize the data needed by Nape for the behavior's entity :
If you need more information on how Nape is working, go check the documentation.
So for now, we have a basic entity initialized for working into Nape.
We have to update the entity position into the game based on the Nape’s one :
There is some differences between c++ and flash: with the flash renderer, we must have the center point at the top left position (for now).
Now the destroy function (called after the behavior has been removed) :
If you have noticed, the world variable is static, because we have to update it on each frame. So to keep the behaviors system intact, we are going to use the BehaviorsManager :
Now the globalUpdate() function is going to be called on each frame :
How to use it ?
Now to use this behavior with an entity, just add the NapeMovementBehavior to the behaviors list :
Here is a quick demo (drag the blocks) :
You can see the NapeMovementBehavior class here and the NapeEntity class here.