2017-02-07 12:59:30 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-01 17:53:13 +08:00
|
|
|
|
|
|
|
|
|
using System.Linq;
|
2016-12-01 19:21:14 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2016-12-01 17:53:13 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.MathUtils;
|
|
|
|
|
using OpenTK;
|
2017-01-30 15:53:12 +08:00
|
|
|
|
using OpenTK.Graphics;
|
2017-02-04 21:09:44 +08:00
|
|
|
|
using System;
|
2016-12-01 17:53:13 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Backgrounds
|
|
|
|
|
{
|
2016-12-02 02:12:35 +08:00
|
|
|
|
public class Triangles : Container<Triangle>
|
2016-12-01 17:53:13 +08:00
|
|
|
|
{
|
2016-12-15 20:47:32 +08:00
|
|
|
|
public override bool HandleInput => false;
|
|
|
|
|
|
2017-01-30 15:53:12 +08:00
|
|
|
|
public Color4 ColourLight = Color4.White;
|
|
|
|
|
public Color4 ColourDark = Color4.Black;
|
2016-12-01 17:53:13 +08:00
|
|
|
|
|
2017-02-15 19:39:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether we want to expire triangles as they exit our draw area completely.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool ExpireOffScreenTriangles => true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether we should create new triangles as others expire.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool CreateNewTriangles => true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The amount of triangles we want compared to the default distribution.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual float SpawnRatio => 1;
|
|
|
|
|
|
2016-12-01 19:21:14 +08:00
|
|
|
|
private float triangleScale = 1;
|
|
|
|
|
|
2017-02-28 10:28:12 +08:00
|
|
|
|
/// <summary>
|
2017-03-01 02:01:02 +08:00
|
|
|
|
/// Whether we should drop-off alpha values of triangles more quickly to improve
|
|
|
|
|
/// the visual appearance of fading. This defaults to on as it is generally more
|
|
|
|
|
/// aesthetically pleasing, but should be turned off in <see cref="BufferedContainer{T}"/>s.
|
2017-02-28 10:28:12 +08:00
|
|
|
|
/// </summary>
|
2017-03-01 02:01:02 +08:00
|
|
|
|
public bool HideAlphaDiscrepancies = true;
|
2017-02-28 10:28:12 +08:00
|
|
|
|
|
2016-12-01 19:21:14 +08:00
|
|
|
|
public float TriangleScale
|
|
|
|
|
{
|
|
|
|
|
get { return triangleScale; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-02-15 19:39:10 +08:00
|
|
|
|
float change = value / triangleScale;
|
2016-12-01 19:21:14 +08:00
|
|
|
|
triangleScale = value;
|
|
|
|
|
|
2017-02-15 19:39:10 +08:00
|
|
|
|
if (change != 1)
|
|
|
|
|
Children.ForEach(t => t.Scale *= change);
|
2016-12-01 19:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 21:09:44 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2017-04-05 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
addTriangles(true);
|
2017-02-04 21:09:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 19:39:10 +08:00
|
|
|
|
private int aimTriangleCount => (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio);
|
2016-12-01 17:53:13 +08:00
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2017-03-01 02:01:02 +08:00
|
|
|
|
float adjustedAlpha = HideAlphaDiscrepancies ?
|
|
|
|
|
// Cubically scale alpha to make it drop off more sharply.
|
|
|
|
|
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
|
|
|
|
|
1;
|
2017-02-28 10:28:12 +08:00
|
|
|
|
|
2017-02-15 19:39:10 +08:00
|
|
|
|
foreach (var t in Children)
|
2016-12-01 17:53:13 +08:00
|
|
|
|
{
|
2017-02-28 10:28:12 +08:00
|
|
|
|
t.Alpha = adjustedAlpha;
|
2017-02-15 19:39:10 +08:00
|
|
|
|
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();
|
2016-12-01 17:53:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 19:00:22 +08:00
|
|
|
|
if (CreateNewTriangles)
|
|
|
|
|
addTriangles(false);
|
2016-12-01 17:53:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 02:12:35 +08:00
|
|
|
|
protected virtual Triangle CreateTriangle()
|
2016-12-01 17:53:13 +08:00
|
|
|
|
{
|
2017-03-23 12:52:38 +08:00
|
|
|
|
const float std_dev = 0.16f;
|
|
|
|
|
const float mean = 0.5f;
|
2017-02-04 21:09:44 +08:00
|
|
|
|
|
|
|
|
|
float u1 = 1 - RNG.NextSingle(); //uniform(0,1] random floats
|
|
|
|
|
float u2 = 1 - RNG.NextSingle();
|
|
|
|
|
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); //random normal(0,1)
|
2017-03-23 12:52:38 +08:00
|
|
|
|
var scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); //random normal(mean,stdDev^2)
|
2017-02-04 21:09:44 +08:00
|
|
|
|
|
2016-12-02 02:12:35 +08:00
|
|
|
|
const float size = 100;
|
2016-12-01 19:21:14 +08:00
|
|
|
|
|
2017-03-23 07:34:39 +08:00
|
|
|
|
return new EquilateralTriangle
|
2016-12-01 17:53:13 +08:00
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativePositionAxes = Axes.Both,
|
2017-03-23 07:34:39 +08:00
|
|
|
|
Size = new Vector2(size),
|
2016-12-01 19:21:14 +08:00
|
|
|
|
Scale = new Vector2(scale),
|
2017-02-07 10:15:37 +08:00
|
|
|
|
EdgeSmoothness = new Vector2(1),
|
2017-01-30 15:53:12 +08:00
|
|
|
|
Colour = GetTriangleShade(),
|
2016-12-01 19:21:14 +08:00
|
|
|
|
Depth = scale,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 15:53:12 +08:00
|
|
|
|
protected virtual Color4 GetTriangleShade() => Interpolation.ValueAt(RNG.NextSingle(), ColourDark, ColourLight, 0, 1);
|
|
|
|
|
|
2017-04-05 19:00:22 +08:00
|
|
|
|
private void addTriangles(bool randomY)
|
2016-12-01 19:21:14 +08:00
|
|
|
|
{
|
2017-04-05 19:00:22 +08:00
|
|
|
|
int addCount = aimTriangleCount - Children.Count();
|
|
|
|
|
for (int i = 0; i < addCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var sprite = CreateTriangle();
|
|
|
|
|
float triangleHeight = sprite.DrawHeight / DrawHeight;
|
|
|
|
|
sprite.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() * (1 + triangleHeight) - triangleHeight : 1);
|
|
|
|
|
Add(sprite);
|
|
|
|
|
}
|
2016-12-01 17:53:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|