/** * A part of a doll, such as a head, torso, arm or leg. * DollParts are created by a [Doll] (which also joins the parts together). * Note, a [Doll] is invisible, and only the [DollPart]s are visible. */ class DollPart : AbstractDraggable, Scalable { @CostumeAttribute(order = 1) val offset = Vector2() @CostumeAttribute(order = 2) val fromAngle = Angle.degrees(-180.0) @CostumeAttribute(order = 3) val toAngle = Angle.degrees(180.0) var doll: Doll = null override fun tick() { super.tick() // Does nothing. JBox2d takes care of all movements } override fun mass() = doll.totalMass override fun scale( by : Vector2 ) { doll.scale( by ) } } /** * The only difference between MajorDollPart and [DollPart] is a test in tick(). * If the velocity changes a lot, (i.e. the doll has collided with something) then a sound is made. * Only the head, torso and abdomen use this class, so no sound when an arm or leg collide. * Note, there is a large change in speed when a Doll is launched by a [Launcher]. However, * [Doll.hit] ignores this. */ class MajorDollPart : DollPart() { /** * Used to test for then the doll part has hit something */ var oldSpeed = Vector2(0, 0) /** * Make a sound when the change in velocity is high (because it has hit something) */ override fun tick() { val newSpeed = actor.body.linearVelocity val dx = Math.abs(oldSpeed.x - newSpeed.x) val dy = Math.abs(oldSpeed.y - newSpeed.y) if (dx > 100.0 || dy > 100.0) { doll.hit(dx + dy) } oldSpeed.set(newSpeed) } }