From ca724ae151d45057fab0db5ca984133a20ace136 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 20 Oct 2015 10:47:58 -0700 Subject: [PATCH] Adds support for scaling sprite physics groups --- examples/game/test_physics.dart | 4 +++- packages/flutter_sprites/lib/src/node.dart | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/game/test_physics.dart b/examples/game/test_physics.dart index b645d365160..add66dd23a4 100644 --- a/examples/game/test_physics.dart +++ b/examples/game/test_physics.dart @@ -51,6 +51,8 @@ class TestBed extends NodeWithSize { TestBed() : super(new Size(1024.0, 1024.0)) { _physicsNode = new PhysicsWorld(new Offset(0.0, 100.0)); + PhysicsGroup group = new PhysicsGroup(); + _physicsNode.addChild(group); _obstacle = new Sprite(_spriteSheet["ship.png"]); _obstacle.position = new Point(512.0, 800.0); @@ -62,7 +64,7 @@ class TestBed extends NodeWithSize { friction: 0.5, tag: "obstacle" ); - _physicsNode.addChild(_obstacle); + group.addChild(_obstacle); _physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin); // Animate obstacle diff --git a/packages/flutter_sprites/lib/src/node.dart b/packages/flutter_sprites/lib/src/node.dart index 7b9e0e04642..67982bf59cd 100644 --- a/packages/flutter_sprites/lib/src/node.dart +++ b/packages/flutter_sprites/lib/src/node.dart @@ -277,15 +277,27 @@ class Node { void set scale(double scale) { assert(scale != null); - if (_physicsBody != null && parent is PhysicsWorld) { - PhysicsWorld physicsNode = parent; - physicsNode._updateScale(this.physicsBody, scale); + if (_physicsBody != null && (parent is PhysicsWorld || parent is PhysicsGroup)) { + _updatePhysicsScale(physicsBody, scale, parent); } _scaleX = _scaleY = scale; invalidateTransformMatrix(); } + void _updatePhysicsScale(PhysicsBody body, double scale, Node physicsParent) { + if (physicsParent == null) return; + + if (physicsParent is PhysicsWorld) { + PhysicsWorld world = physicsParent; + world._updateScale(body, scale); + } else if (physicsParent is PhysicsGroup) { + _updatePhysicsScale(body, scale * physicsParent.scale, physicsParent.parent); + } else { + assert(false); + } + } + /// The horizontal scale of this node relative its parent. /// /// myNode.scaleX = 5.0;