From 558b2622a74e90ef7899b4d1d8549dba62e53ae3 Mon Sep 17 00:00:00 2001 From: tgi74000 Date: Thu, 5 Jul 2018 15:48:54 +0200 Subject: [PATCH] Move the Shake logic to a new ShakeContainer --- .../Objects/Drawables/DrawableOsuHitObject.cs | 23 +++++++++++------- .../Objects/Drawables/DrawableSlider.cs | 2 +- .../Objects/Drawables/Pieces/SliderBall.cs | 6 +++-- .../Graphics/Containers/ShakeContainer.cs | 24 +++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 osu.Game/Graphics/Containers/ShakeContainer.cs diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableOsuHitObject.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableOsuHitObject.cs index f25ddc4d82..6b12fc029e 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableOsuHitObject.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableOsuHitObject.cs @@ -8,6 +8,7 @@ using System.Linq; using osu.Game.Rulesets.Objects.Types; using osu.Game.Skinning; using OpenTK.Graphics; +using osu.Game.Graphics.Containers; namespace osu.Game.Rulesets.Osu.Objects.Drawables { @@ -15,12 +16,24 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables { public override bool IsPresent => base.IsPresent || State.Value == ArmedState.Idle && Time.Current >= HitObject.StartTime - HitObject.TimePreempt; + private readonly ShakeContainer shakeContainer; + protected DrawableOsuHitObject(OsuHitObject hitObject) : base(hitObject) { + shakeContainer = new ShakeContainer + { + RelativeSizeAxes = Axes.Both, + }; + base.AddInternal(shakeContainer); Alpha = 0; } + // Forward all internal management to shakeContainer + protected override void AddInternal(Drawable drawable) => shakeContainer.Add(drawable); + protected override void ClearInternal(bool disposeChildren = true) => shakeContainer.Clear(disposeChildren); + protected override bool RemoveInternal(Drawable drawable) => shakeContainer.Remove(drawable); + protected sealed override void UpdateState(ArmedState state) { double transformTime = HitObject.StartTime - HitObject.TimePreempt; @@ -61,15 +74,7 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables protected void Shake() { - const int shake_amount = 8; - const int shake_duration = 20; - - this.MoveToX(Position.X + shake_amount, shake_duration).Then() - .MoveToX(Position.X - shake_amount, shake_duration).Then() - .MoveToX(Position.X + shake_amount, shake_duration).Then() - .MoveToX(Position.X - shake_amount, shake_duration).Then() - .MoveToX(Position.X + shake_amount, shake_duration).Then() - .MoveToX(Position.X, shake_duration); + shakeContainer.Shake(); } } diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs index c8544d9cc1..d48419ba53 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs @@ -45,7 +45,7 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables }, ticks = new Container { RelativeSizeAxes = Axes.Both }, repeatPoints = new Container { RelativeSizeAxes = Axes.Both }, - Ball = new SliderBall(s) + Ball = new SliderBall(s, this) { BypassAutoSizeAxes = Axes.Both, Scale = new Vector2(s.Scale), diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBall.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBall.cs index 894d972e47..278bb0dcb1 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBall.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBall.cs @@ -34,9 +34,11 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces private readonly Slider slider; public readonly Box FollowCircle; private readonly Box ball; + private readonly DrawableSlider drawableSlider; - public SliderBall(Slider slider) + public SliderBall(Slider slider, DrawableSlider drawableSlider = null) { + this.drawableSlider = drawableSlider; this.slider = slider; Masking = true; AutoSizeAxes = Axes.Both; @@ -136,7 +138,7 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces Tracking = canCurrentlyTrack && lastState != null && base.ReceiveMouseInputAt(lastState.Mouse.NativeState.Position) - && ((Parent as DrawableSlider)?.OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false); + && (drawableSlider?.OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false); } } diff --git a/osu.Game/Graphics/Containers/ShakeContainer.cs b/osu.Game/Graphics/Containers/ShakeContainer.cs new file mode 100644 index 0000000000..33fba390d6 --- /dev/null +++ b/osu.Game/Graphics/Containers/ShakeContainer.cs @@ -0,0 +1,24 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; + +namespace osu.Game.Graphics.Containers +{ + public class ShakeContainer : Container + { + public void Shake() + { + const int shake_amount = 8; + const int shake_duration = 20; + + this.MoveToX(shake_amount, shake_duration).Then() + .MoveToX(-shake_amount, shake_duration).Then() + .MoveToX(shake_amount, shake_duration).Then() + .MoveToX(-shake_amount, shake_duration).Then() + .MoveToX(shake_amount, shake_duration).Then() + .MoveToX(0, shake_duration); + } + } +}