package creatures.parts { import flash.display.Sprite; import flash.display.Graphics; public class Appendage extends Sprite { private var _plane:Plane; private var _segments:Array = new Array(); private var _animation:SegmentAnimation; private var _animationOffset:int = 0; private var _depth:Number = 0; public function get plane():Plane { return _plane; } public function set plane(p:Plane):void { _plane = p; } public function get animationOffset():int { return _animationOffset; } public function set animationOffset(n:int):void { _animationOffset = n; } public function get segments():Array { return _segments; } public function get animation():SegmentAnimation { return _animation; } public function get depth():Number { return _depth; } public function Appendage() {} public function clone():Appendage { var copy:Appendage = new Appendage(); var copySegments:Array = new Array(); var i:int = _segments.length; while (i--) { copySegments[i] = _segments[i].clone(); } copy.appendSegments(copySegments); copy.setAnimation(_animation, _animationOffset); return copy; } public override function toString():String { return "[Appendage]"; } public function appendSegments(segments:Array):void { // define hierarchy based on array order var i:int = segments.length - 1; if (i < 0) { return; } if (i > 0) { while (i--) { segments[i].addChild(segments[i + 1]); } } var last:int = _segments.length - 1; if (last >= 0){ _segments[last].addChild(segments[0]); } _segments = _segments.concat(segments); updateDepth(); } public function setAnimation(animation:SegmentAnimation, animationOffset:int = 0):void { _animation = animation; _animationOffset = animationOffset; } public function getSegmentAt(index:int):Segment { return (index >= 0 || index < _segments.length) ? _segments[index] : null; } public function updateTransform():void { if (_segments.length) { _segments[0].transform(_plane.transformMatrix); } } public function animate():void { if (_animation && _segments.length) { _animation.applyTo(_segments[0], _animationOffset); updateDepth(); } } public function draw():void { if (_segments.length) { graphics.clear(); _segments[0].drawIn(graphics); } } private function updateDepth():void { if (_segments.length) { _depth = _segments[0].x; }else{ _depth = 0; } } } }