broker
Utility library for game programming.
Currently works only with heaps.
Requires Haxe 4 (developed with v4.1.3).
See also [https://github.com/fal-works/broker-sample](https://github.com/fal-works/broker-sample) for a usage example.
Features
Entity
Provides a base class for game entities, from which you can generate AoSoA classes.
See also: AoSoA generator of banker library.
Collision
2D AABB collision detection.
Gamepad
Abstraction of gamepad input.
Reflects any kind of physical input device (gamepad, keyboard, touch... depending on your implementation)
and provides you an integrated virtual gamepad.
Object
Elements of scene trees.
Scene
- Basic classes for game scenes.
- Classes for managing transition between game scenes.
Image
Classes for loading image data.
Tile
Atlas
FrameTiles
- etc.
Draw
Classes for drawing graphics.
TileDraw
BatchDraw
,BatchSprite
DrawArea
Timer
Basic classes for pseudo-asynchronous processing.
Sound
Some classes for abstracting sound features from heaps.
Menu
Menu UI.
Color
Some abstract types for representing colors.
Caveats
Quite unstable!
Usage
Collision
Collision Space
First create your collision space by implementing broker.collision.CollisionSpace
and providing metadata.
This adds several static fields, such as partitionLevel
of getCellIndex()
.
@:broker.leftTop(0, 0) // Left-top coordinates of the entire space
@:broker.rightBottom(800, 600) // Light-bottom coordinates of the entire space
@:broker.partitionLevel(3) // Depth of quadtrees. The greater, the deeper/finer
class YourSpace implements broker.collision.CollisionSpace {}
The entire space will be recursively devided; the leaf (deepest/finest) level will have pow(4, partitionLevel)
cells.
Collision Detector / Quadtree
Then prepare your collision detector object by either:
createIntraGroup()
(detects within a single collider group) orcreateInterGroup()
(detects between two collider groups)
final yourDetector = broker.collision.CollisionDetector.createIntraGroup(
YourSpace.partitionLevel,
maxNumberOfColliders
);
A collision detector has quadtree(s) for left/right collider groups (stored at variables leftQuadtree
/rightQuadtree
).
Remarks:
- Quadtrees can also be provided externally when creating a detector.
- Left and right are the same if intra-group.
Load colliders and detect collision
Finally,
- Each of your collidable objects should be associated with a
Collider
instance. - Colliders should be loaded to your
Quadtree
before running collision detection process.
AQuadtree
consists of multipleCell
s. Use yourCollisionSpace
class for determining the cell index to load your collider. - After loading, use your
CollisionDetector
to run detection with anyonOverlap
callback function. - You should manually reset your
Quadtree
before re-loading colliders.
An example would be:
class YourCollidableObject {
final collider: broker.collision.Collider;
public function new() { /* ... */ }
public function updatePosition() { /* ... */ }
public function loadCollider(quadtree: broker.collision.Quadtree) {
// Get bounds (leftX, topY, rightX, bottomY) here somehow
this.collider.setBounds(leftX, topY, rightX, bottomY);
final cellIndex = YourSpace.getCellIndex(leftX, topY, rightX, bottomY);
quadtree.loadAt(cellIndex, this.collider);
}
}
class YourSystem {
final objects: Array<YourCollidableObjects>;
final collisionDetector: broker.collision.CollisionDetector;
final onOverlap: (a: Collider, b: Collider) -> Void;
public function new() { /* ... */ }
public function run() {
// update position of objects
for (object in objects) object.updatePosition();
// reset & reload quadtree
final quadtree = collisionDetector.leftQuadtree;
quadtree.reset();
for (object in objects) object.loadCollider(quadtree);
// run collision detection
collisionDetector.detect(onOverlap);
}
}
Compilation flags
library | flag | description |
---|---|---|
broker | broker_generic_disable | Disables @:generic meta. |
broker | broker_catch_disable | Disables try-catch in App class. |
Dependencies
- sinker v0.5.0 or compatible
- prayer v0.1.3 or compatible
- sneaker v0.11.0 or compatible
- ripper v0.4.0 or compatible
- banker v0.7.0 or compatible
See also: FAL Haxe libraries
(Optional)
- heaps v1.8.0 or compatible