From 12e265a3e2d0cac98b49aff080f6396000df8970 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 15 Feb 2017 20:39:10 +0900 Subject: [PATCH] Combine triangle system implementations. --- .../Objects/Drawables/Pieces/CirclePiece.cs | 8 ++-- .../Objects/Drawables/Pieces/ExplodePiece.cs | 5 +- .../Objects/Drawables/Pieces/Triangles.cs | 46 ------------------- .../Drawables/Pieces/TrianglesPiece.cs | 26 +++++++++++ osu.Game.Modes.Osu/osu.Game.Modes.Osu.csproj | 2 +- osu.Game/Graphics/Backgrounds/Triangles.cs | 39 +++++++++++----- 6 files changed, 61 insertions(+), 65 deletions(-) delete mode 100644 osu.Game.Modes.Osu/Objects/Drawables/Pieces/Triangles.cs create mode 100644 osu.Game.Modes.Osu/Objects/Drawables/Pieces/TrianglesPiece.cs diff --git a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/CirclePiece.cs b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/CirclePiece.cs index f7477fb6f0..23f878d491 100644 --- a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/CirclePiece.cs +++ b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/CirclePiece.cs @@ -14,9 +14,8 @@ namespace osu.Game.Modes.Osu.Objects.Drawables.Pieces { public class CirclePiece : Container { - private Sprite disc; - private Triangles triangles; + public Func Hit; @@ -36,10 +35,11 @@ public CirclePiece() Anchor = Anchor.Centre, Origin = Anchor.Centre }, - triangles = new Triangles + new TrianglesPiece { + RelativeSizeAxes = Axes.Both, BlendingMode = BlendingMode.Additive, - RelativeSizeAxes = Axes.Both + Alpha = 0.5f, } }; } diff --git a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/ExplodePiece.cs b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/ExplodePiece.cs index ae382519df..08c7423c23 100644 --- a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/ExplodePiece.cs +++ b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/ExplodePiece.cs @@ -21,10 +21,11 @@ public ExplodePiece() Children = new Drawable[] { - new Triangles + new TrianglesPiece { BlendingMode = BlendingMode.Additive, - RelativeSizeAxes = Axes.Both + RelativeSizeAxes = Axes.Both, + Alpha = 0.1f, } }; } diff --git a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/Triangles.cs b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/Triangles.cs deleted file mode 100644 index 42b53606ef..0000000000 --- a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/Triangles.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using osu.Framework.Allocation; -using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Sprites; -using osu.Framework.Graphics.Textures; -using osu.Framework.MathUtils; -using OpenTK; - -namespace osu.Game.Modes.Osu.Objects.Drawables.Pieces -{ - public class Triangles : Container - { - public override bool HandleInput => false; - - protected override void LoadComplete() - { - base.LoadComplete(); - - const float size = 100; - for (int i = 0; i < 10; i++) - { - Add(new Triangle - { - Origin = Anchor.Centre, - RelativePositionAxes = Axes.Both, - Position = new Vector2(RNG.NextSingle(), RNG.NextSingle()), - Scale = new Vector2(RNG.NextSingle() * 0.4f + 0.2f), - // Scaling height by 0.866 results in equiangular triangles (== 60° and equal side length) - Size = new Vector2(size, 0.866f * size), - Alpha = RNG.NextSingle() * 0.3f, - }); - } - } - - protected override void Update() - { - base.Update(); - - foreach (Drawable d in Children) - d.Position -= new Vector2(0, (float)(d.Scale.X * (Time.Elapsed / 2880))); - } - } -} \ No newline at end of file diff --git a/osu.Game.Modes.Osu/Objects/Drawables/Pieces/TrianglesPiece.cs b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/TrianglesPiece.cs new file mode 100644 index 0000000000..57e00be1cf --- /dev/null +++ b/osu.Game.Modes.Osu/Objects/Drawables/Pieces/TrianglesPiece.cs @@ -0,0 +1,26 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using osu.Game.Graphics.Backgrounds; + +namespace osu.Game.Modes.Osu.Objects.Drawables.Pieces +{ + public class TrianglesPiece : Triangles + { + protected override bool ExpireOffScreenTriangles => false; + protected override bool CreateNewTriangles => false; + protected override float SpawnRatio => 0.5f; + + public TrianglesPiece() + { + TriangleScale = 1.2f; + } + + protected override void Update() + { + if (IsPresent) + base.Update(); + } + } +} \ No newline at end of file diff --git a/osu.Game.Modes.Osu/osu.Game.Modes.Osu.csproj b/osu.Game.Modes.Osu/osu.Game.Modes.Osu.csproj index 4845deb167..82c5537597 100644 --- a/osu.Game.Modes.Osu/osu.Game.Modes.Osu.csproj +++ b/osu.Game.Modes.Osu/osu.Game.Modes.Osu.csproj @@ -62,7 +62,7 @@ - + diff --git a/osu.Game/Graphics/Backgrounds/Triangles.cs b/osu.Game/Graphics/Backgrounds/Triangles.cs index 2dd630efae..219af9bd38 100644 --- a/osu.Game/Graphics/Backgrounds/Triangles.cs +++ b/osu.Game/Graphics/Backgrounds/Triangles.cs @@ -2,12 +2,10 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System.Linq; -using osu.Framework.Allocation; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; -using osu.Framework.Graphics.Textures; using osu.Framework.MathUtils; using OpenTK; using OpenTK.Graphics; @@ -22,6 +20,21 @@ public class Triangles : Container public Color4 ColourLight = Color4.White; public Color4 ColourDark = Color4.Black; + /// + /// Whether we want to expire triangles as they exit our draw area completely. + /// + protected virtual bool ExpireOffScreenTriangles => true; + + /// + /// Whether we should create new triangles as others expire. + /// + protected virtual bool CreateNewTriangles => true; + + /// + /// The amount of triangles we want compared to the default distribution. + /// + protected virtual float SpawnRatio => 1; + private float triangleScale = 1; public float TriangleScale @@ -29,9 +42,11 @@ public float TriangleScale get { return triangleScale; } set { + float change = value / triangleScale; triangleScale = value; - Children.ForEach(t => t.ScaleTo(triangleScale)); + if (change != 1) + Children.ForEach(t => t.Scale *= change); } } @@ -42,20 +57,20 @@ protected override void LoadComplete() addTriangle(true); } - private int aimTriangleCount => (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale)); + private int aimTriangleCount => (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio); protected override void Update() { base.Update(); - foreach (Drawable d in Children) + foreach (var t in Children) { - d.Position -= new Vector2(0, (float)(d.Scale.X * (50 / DrawHeight) * (Time.Elapsed / 950)) / triangleScale); - if (d.DrawPosition.Y + d.DrawSize.Y * d.Scale.Y < 0) - d.Expire(); + t.Position -= new Vector2(0, (float)(t.Scale.X * (50 / DrawHeight) * (Time.Elapsed / 950)) / triangleScale); + if (ExpireOffScreenTriangles && t.DrawPosition.Y + t.DrawSize.Y * t.Scale.Y < 0) + t.Expire(); } - while (Children.Count() < aimTriangleCount) + while (CreateNewTriangles && Children.Count() < aimTriangleCount) addTriangle(false); } @@ -77,8 +92,8 @@ protected virtual Triangle CreateTriangle() RelativePositionAxes = Axes.Both, Scale = new Vector2(scale), EdgeSmoothness = new Vector2(1), - // Scaling height by 0.866 results in equiangular triangles (== 60° and equal side length) Colour = GetTriangleShade(), + // Scaling height by 0.866 results in equiangular triangles (== 60° and equal side length) Size = new Vector2(size, 0.866f * size), Depth = scale, }; @@ -89,8 +104,8 @@ protected virtual Triangle CreateTriangle() private void addTriangle(bool randomY) { var sprite = CreateTriangle(); - var triangleHeight = sprite.DrawHeight / DrawHeight; - sprite.Position = new Vector2(RNG.NextSingle(), randomY ? (RNG.NextSingle() * (1 + triangleHeight) - triangleHeight) : 1); + float triangleHeight = (sprite.DrawHeight / DrawHeight); + sprite.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() * (1 + triangleHeight) - triangleHeight : 1); Add(sprite); } }