Adds sprite physics groups

This commit is contained in:
Viktor Lidholt
2015-10-20 10:35:40 -07:00
parent 623b3ba06d
commit 4187bcf90c
4 changed files with 158 additions and 6 deletions

View File

@@ -132,9 +132,8 @@ class Node {
void set rotation(double rotation) {
assert(rotation != null);
if (_physicsBody != null && parent is PhysicsWorld) {
PhysicsWorld physicsNode = parent;
physicsNode._updateRotation(this.physicsBody, rotation);
if (_physicsBody != null && (parent is PhysicsWorld || parent is PhysicsGroup)) {
_updatePhysicsRotation(physicsBody, rotation, parent);
return;
}
@@ -142,6 +141,19 @@ class Node {
invalidateTransformMatrix();
}
void _updatePhysicsRotation(PhysicsBody body, double rotation, Node physicsParent) {
if (physicsParent == null) return;
if (physicsParent is PhysicsWorld) {
PhysicsWorld world = physicsParent;
world._updateRotation(body, rotation);
} else if (physicsParent is PhysicsGroup) {
_updatePhysicsRotation(body, rotation + physicsParent.rotation, physicsParent.parent);
} else {
assert(false);
}
}
void _setRotationFromPhysics(double rotation) {
assert(rotation != null);
_rotation = rotation;
@@ -166,9 +178,8 @@ class Node {
void set position(Point position) {
assert(position != null);
if (_physicsBody != null && parent is PhysicsWorld) {
PhysicsWorld physicsNode = parent;
physicsNode._updatePosition(this.physicsBody, position);
if (_physicsBody != null && (parent is PhysicsWorld || parent is PhysicsGroup)) {
_updatePhysicsPosition(this.physicsBody, position, parent);
return;
}
@@ -176,6 +187,20 @@ class Node {
invalidateTransformMatrix();
}
void _updatePhysicsPosition(PhysicsBody body, Point position, Node physicsParent) {
if (physicsParent == null) return;
if (physicsParent is PhysicsWorld) {
PhysicsWorld world = physicsParent;
world._updatePosition(body, position);
} else if (physicsParent is PhysicsGroup) {
Vector4 parentPos = physicsParent.transformMatrix.transform(new Vector4(position.x, position.y, 0.0, 1.0));
_updatePhysicsPosition(body, new Point(parentPos.x, parentPos.y), physicsParent.parent);
} else {
assert(false);
}
}
void _setPositionFromPhysics(Point position) {
assert(position != null);
_position = position;
@@ -310,6 +335,7 @@ class Node {
void addChild(Node child) {
assert(child != null);
assert(child._parent == null);
assert(!(child is PhysicsGroup) || this is PhysicsGroup || this is PhysicsWorld);
_childrenNeedSorting = true;
_children.add(child);
@@ -318,6 +344,10 @@ class Node {
_childrenLastAddedOrder += 1;
child._addedOrder = _childrenLastAddedOrder;
if (_spriteBox != null) _spriteBox._registerNode(child);
if (child is PhysicsGroup) {
child._attachGroup(child, child._world);
}
}
/// Removes a child from this node.
@@ -330,6 +360,10 @@ class Node {
child._spriteBox = null;
if (_spriteBox != null) _spriteBox._deregisterNode(child);
}
if (child is PhysicsGroup) {
child._detachGroup(child);
}
}
/// Removes this node from its parent node.