-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntroScene.cs
159 lines (127 loc) · 6 KB
/
IntroScene.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using OpenTK.Graphics.OpenGL;
using System.Numerics;
using Walgelijk;
namespace MadnessMicroactive;
public static class IntroScene
{
public static Scene Create(Game game)
{
var scene = new Scene(game);
scene.ShouldBeDisposedOnSceneChange = true;
var d = new QOIDecoder();
var read = d.Decode(File.ReadAllBytes("resources/textures/intro.qoi"), true);
for (int i = 0; i < Math.Min(128, read.Width * read.Height); i++)
{
if (read.Colors[i].A < 0.5f)
continue;
var e = scene.CreateEntity();
scene.AttachComponent(e, new BatchedSpriteComponent(Texture.White) { });
}
scene.AddSystem(new IntroSystem(read));
scene.AddSystem(new BatchRendererSystem());
scene.AddSystem(new TransformSystem());
scene.AddSystem(new CameraSystem());
scene.AttachComponent(scene.CreateEntity(), new BatchRendererStorageComponent());
var camera = scene.CreateEntity();
scene.AttachComponent(camera, new TransformComponent()
{
Position = default
});
scene.AttachComponent(camera, new CameraComponent()
{
ClearColour = Colors.Black,
OrthographicSize = 1,
PixelsPerUnit = 1
});
return scene;
}
public class IntroSystem : Walgelijk.System
{
private readonly DecodedImage read;
private readonly Color accent = new Color("#FF0061");
private Vector2 mousePos;
private Vector2[] offsets;
public IntroSystem(DecodedImage read)
{
this.read = read;
offsets = new Vector2[read.Colors.Count(static r => r.R >= 0.5f)];
}
public override void Update()
{
if (Time.SecondsSinceLoad > 8)
{
Game.Scene = MenuScene.Create(Game);
return;
}
var sprites = Scene.GetAllComponentsOfType<BatchedSpriteComponent>().GetEnumerator();
int i = 0;
mousePos = Utilities.SmoothApproach(mousePos, Input.WorldMousePosition, 15, Time.DeltaTime);
int offsetIndex = 0;
for (int y = 0; y < read.Height; y++)
for (int x = 0; x < read.Width; x++)
{
if (read.Colors[i++].R < 0.5f)
continue;
if (!sprites.MoveNext())
return;
float time = ((Time.SecondsSinceLoad - Utilities.Hash(y * .0543f + x * 0.0643f) - 0.5f + y * 0.05f) / 6);
var openTime = Utilities.Clamp(Utilities.MapRange(0, 0.2f, 0, 1, time));
var closeTime = Utilities.Clamp(Utilities.MapRange(0.9f, 1, 0, 1, time));
bool isOpening = closeTime < float.Epsilon;
const float scale = 8;
var sprite = sprites.Current;
var position = new Vector2(x * (scale + 2), (y) * (scale + 2)) - new Vector2(read.Width * (scale + 2) / 2, read.Height * (scale + 2) / 2);
var origin = new Vector2(
(Utilities.Hash(x * .0543f - y * 0.0643f) * 2 - 1),
(Utilities.Hash(x * .0153f + y * 0.091f) * 2 - 1))
* 1024;
position = Utilities.Lerp(origin, position, Easings.Cubic.Out(openTime));
var th = Utilities.LerpAngle((Utilities.Hash(x * .0153f + y * 0.091f) * 2 - 1) * 45, 0, Easings.Expo.Out(openTime));
var skew = Utilities.Lerp((Utilities.Hash(y * .0153f + x * 0.091f) * 2 - 1) * MathF.PI * 0.5f, 0, Easings.Quad.Out(openTime));
if (!isOpening)
position.Y -= Easings.Expo.In(closeTime) * 50;
var s = Utilities.Lerp(4, 1, Easings.Cubic.Out(openTime));
var toMouse = mousePos - position;
var toMouseLgth = toMouse.Length();
if (toMouseLgth < 200)
{
var influence = 1 - (toMouseLgth / 200);
offsets[offsetIndex] = Utilities.SmoothApproach(
offsets[offsetIndex],
-toMouse * 20f / Math.Max(20, toMouseLgth) * influence,
8, Time.DeltaTime);
}
else
offsets[offsetIndex] = Utilities.SmoothApproach(
offsets[offsetIndex],
default,
8, Time.DeltaTime);
position += offsets[offsetIndex++];
var acc = Utilities.Lerp(accent, Colors.Red, Easings.Cubic.InOut(Utilities.Clamp(time * 2)));
sprite.Color = isOpening ? Utilities.Lerp(Colors.White.WithAlpha(0), acc, openTime) : Utilities.Lerp(acc, Colors.Gray.WithAlpha(0), closeTime);
sprite.Transform =
Matrix3x2.CreateRotation(th) *
Matrix3x2.CreateSkew(skew, 0) *
Matrix3x2.CreateScale(scale * s) *
Matrix3x2.CreateTranslation(position);
}
}
public static Vector2 Hash21(float p)
{
var inputVector = new Vector3(p);
var fractVector = new Vector3(.1031f, .1030f, .0973f);
var p3 = new Vector3(
inputVector.X * fractVector.X,
inputVector.Y * fractVector.Y,
inputVector.Z * fractVector.Z);
float dotProduct = Vector3.Dot(p3, new Vector3(p3.Y, p3.Z, p3.X) + new Vector3(33.33f));
p3 += new Vector3(dotProduct);
var fractPart = new Vector2(p3.X - (int)p3.X, p3.Y - (int)p3.Y);
var result = new Vector2(
fractPart.X + p3.Y * p3.Z - (int)(fractPart.X + p3.Y * p3.Z),
fractPart.Y + p3.Z * p3.Y - (int)(fractPart.Y + p3.Z * p3.Y)
);
return result;
}
}
}