Commit e749bea1 authored by Viktor Lidholt's avatar Viktor Lidholt

Adds constraint for aligning with another node in sprites

parent dbda2725
......@@ -70,3 +70,34 @@ class ConstraintRotationToNode extends Constraint {
node.rotation = _dampenRotation(node.rotation, target, dampening);
}
}
class ConstraintPositionToNode extends Constraint {
ConstraintPositionToNode(this.targetNode, {this.dampening, this.offset: Offset.zero});
final Node targetNode;
final Offset offset;
final double dampening;
void constrain(Node node, double dt) {
Point targetPosition;
if (targetNode.spriteBox != node.spriteBox || node.parent == null) {
// The target node is in another sprite box or has been removed
return;
}
if (targetNode.parent == node.parent) {
targetPosition = targetNode.position;
} else {
targetPosition = node.parent.convertPointFromNode(Point.origin, targetNode);
}
if (offset != null)
targetPosition += offset;
if (dampening == null)
node.position = targetPosition;
else
node.position = GameMath.filterPoint(node.position, targetPosition, dampening);
}
}
......@@ -101,4 +101,8 @@ class GameMath {
static double filter (double a, double b, double filterFactor) {
return (a * (1-filterFactor)) + b * filterFactor;
}
static Point filterPoint(Point a, Point b, double filterFactor) {
return new Point(filter(a.x, b.x, filterFactor), filter(a.y, b.y, filterFactor));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment