I've managed to learn enough Crafty for some basic demos of what I want to do. For instance, here's a sample of some movable boxes with collision detection and Google maps-like zoom functionality:
http://jsfiddle.net/b9ozcm91/
Feel free to include the zoom function in Crafty if you think it would be useful. Also, please let me know if there are better ways of doing what I've done.
So I have two challenges to tackle next:
1. take the above example and make each side of the box "magnetic", so that if one box gets sufficiently close to another, they "snap" together, and dragging the mouse sufficiently far away pulls them apart.
2. make the boxes selectable by clicking or dragging the mouse on the background to encompass a set of boxes, like how you select multiple files in a desktop file manager.
What components should I be looking at to do the above? My current thoughts:
For #1, I'm not sure what would be a Crafty approach. I could register a Move event handler on the box, then use Crafty.findClosestEntityByComponent for each side of the box and call .move() if it meets the criteria. Would that be the best approach? To pull them apart, perhaps add the MouseDrag component for the Dragging event, and if the force of the drag is above a threshold, move them apart past the closeness threshold?
For #2, I can capture the 'Click' event for the Mouse component and add a custom component name to "select" a box, but it still fires even if I was dragging the box (not sure if this is a bug). I would like to know how to avoid it a click even on a drag.
Then I also want a click on the background to "deselect" any selected entities. I don't see how to capture a mouse click that's not on an entity though. I could obviously capture the DOM click event on the canvas element, but I figured there must be a Crafty-way I'm not seeing. Once captured, I can just do something like: Crafty(selectComponent).each(function(i) { this.removeComponent(selectComponent); });
Sandro