1
1
// Copyright (c) Shane Woolcock. Licensed under the MIT Licence.
2
2
// See the LICENCE file in the repository root for full licence text.
3
3
4
+ using System ;
5
+ using System . Linq ;
4
6
using osu . Framework . Allocation ;
5
7
using osu . Framework . Graphics ;
6
8
using osu . Framework . Graphics . Containers ;
9
+ using osu . Framework . Graphics . Shapes ;
7
10
using osu . Framework . Graphics . Sprites ;
8
11
using osu . Framework . Graphics . Textures ;
12
+ using osu . Framework . Utils ;
9
13
using osuTK ;
10
14
using osuTK . Graphics ;
11
15
@@ -18,13 +22,12 @@ public class DefaultHitExplosion : CompositeDrawable
18
22
19
23
public override bool RemoveWhenNotAlive => true ;
20
24
21
- public DefaultHitExplosion ( Color4 explosionColour )
25
+ public DefaultHitExplosion ( Color4 explosionColour , int sparkCount = 10 , Color4 ? sparkColour = null )
22
26
{
23
27
Origin = Anchor . Centre ;
24
28
25
29
InternalChildren = new Drawable [ ]
26
30
{
27
- // TODO: flashbang
28
31
colouredExplosion = new Sprite
29
32
{
30
33
Anchor = Anchor . Centre ,
@@ -38,7 +41,14 @@ public DefaultHitExplosion(Color4 explosionColour)
38
41
Origin = Anchor . Centre ,
39
42
Scale = new Vector2 ( 0.75f )
40
43
} ,
41
- // TODO: small particles
44
+ new Sparks ( sparkCount )
45
+ {
46
+ Anchor = Anchor . Centre ,
47
+ Origin = Anchor . Centre ,
48
+ RelativeSizeAxes = Axes . Both ,
49
+ Size = new Vector2 ( 2f ) ,
50
+ Colour = sparkColour ?? Color4 . White
51
+ }
42
52
// TODO: stars
43
53
} ;
44
54
}
@@ -49,5 +59,53 @@ private void load(TextureStore store)
49
59
colouredExplosion . Texture = store . Get ( "exp" ) ;
50
60
whiteExplosion . Texture = store . Get ( "exp" ) ;
51
61
}
62
+
63
+ protected class Sparks : CompositeDrawable
64
+ {
65
+ private const double average_duration = 1500f ;
66
+
67
+ private readonly Random random = new Random ( ) ;
68
+ private readonly Triangle [ ] triangles ;
69
+
70
+ private double randomDirection ( int index , int max )
71
+ {
72
+ var offset = random . NextDouble ( ) * 2f / max ;
73
+ return ( double ) index / max + offset ;
74
+ }
75
+
76
+ public Sparks ( int sparkCount )
77
+ {
78
+ Origin = Anchor . Centre ;
79
+ Anchor = Anchor . Centre ;
80
+ RelativeSizeAxes = Axes . Both ;
81
+
82
+ triangles = Enumerable . Range ( 0 , sparkCount ) . Select ( i => new Triangle
83
+ {
84
+ Origin = Anchor . Centre ,
85
+ Anchor = Anchor . Centre ,
86
+ Size = new Vector2 ( 5f , 10f ) ,
87
+ Rotation = ( float ) ( randomDirection ( i , sparkCount ) * 360 ) ,
88
+ } ) . ToArray ( ) ;
89
+
90
+ InternalChildren = triangles ;
91
+ }
92
+
93
+ protected override void LoadComplete ( )
94
+ {
95
+ base . LoadComplete ( ) ;
96
+
97
+ foreach ( var triangle in triangles )
98
+ {
99
+ var scale = 0.8f + random . NextDouble ( ) * 0.2f ;
100
+ var duration = average_duration * ( 0.8f + random . NextDouble ( ) * 0.4f ) ;
101
+ var radians = MathUtils . DegreesToRadians ( triangle . Rotation + 90 ) ;
102
+ var distance = DrawWidth * ( 0.8f + random . NextDouble ( ) * 0.2f ) ;
103
+ var target = new Vector2 ( MathF . Cos ( radians ) , MathF . Sin ( radians ) ) * ( float ) distance ;
104
+ triangle . Scale = new Vector2 ( ( float ) scale ) ;
105
+ triangle . MoveTo ( target , duration , Easing . OutExpo ) ;
106
+ triangle . FadeOutFromOne ( duration , Easing . InExpo ) ;
107
+ }
108
+ }
109
+ }
52
110
}
53
111
}
0 commit comments