-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationBase.cs
119 lines (90 loc) · 3.04 KB
/
AnimationBase.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
using g2.Animation.Core.ParticleSystems;
using g2.Animation.Core.Timing;
using g2.Datastructures.Geometry;
using System.Diagnostics;
using System.Timers;
namespace g2.Animation.Core.AnimationSystems;
// ToDo: Add Boundary for canvas maybe move checking for boundaries in box like quadtree? or BoundaryCheckc class? efficiant boundary checks (k d tree?)
public class AnimationBase
{
private const int PARTICLESCOUNT = 500;
private readonly FPSCounter fpsCounter;
private readonly Quadrant quadrant;
private readonly Particle[] particles;
private bool updateRunning;
private bool fixedUpdateRunning;
public AnimationBase(FPSCounter fpsCounter, double width, double height)
{
this.fpsCounter = fpsCounter;
quadrant = new(0, 0, width, height);
particles = new Particle[PARTICLESCOUNT];
Random random = new();
for (int i = 0; i < PARTICLESCOUNT; i++)
{
double x = 275;// (random.NextDouble() * width);
double y = (random.NextDouble() * height);
Particle particle = new(x, y, 2, 2, quadrant)
{
//Velocity = new Vector2D((random.NextDouble() * 150) - 75, (random.NextDouble() * 150) - 75),
Velocity = new Vector2D(100, 0),
Acceleration = new Vector2D(0, 0)
};
particles[i] = particle;
}
}
public Particle[] Particles
{
get => particles;
}
Task a;
Task b;
public Task Loop()
{
return Task.Run(() =>
{
// ToDo: never in "running" status_
a = UpdateAsync();
b = FixedUpdateAsync();
});
}
private async Task UpdateAsync()
{
updateRunning = true;
await Task.Run(() =>
{
while (updateRunning)
{
Time.Delta();
fpsCounter.Update();
//Debug.WriteLine($"Update:\t Detlatatime: {Time.FixedDeltaTime:G65}");
_ = (UpdateComplete?.Invoke(null, EventArgs.Empty));
}
});
}
int fixedcount = 0;
private async Task FixedUpdateAsync()
{
fixedUpdateRunning = true;
Time.StarPeriodicTimer(1 / 50.0);
while (fixedUpdateRunning && Time.PeriodicTimer is not null && await Time.PeriodicTimer.WaitForNextTickAsync())
{
Time.FixedDelta();
fpsCounter.FixedUpdate();
for (int i = 0; i < particles.Length; i++)
{
particles[i].FixedUpdate();
particles[i].CheckBoundaries();
}
fixedcount++;
Debug.WriteLine($"FixedUpdate\t{fixedcount}:\t\t{Time.FixedDeltaTime:G65}");
_ = (FixedUpdateComplete?.Invoke("-" + fixedcount, EventArgs.Empty));
}
}
public void Pause()
{
updateRunning = false;
fixedUpdateRunning = false;
}
public event Func<object?, EventArgs, Task>? FixedUpdateComplete;
public event Func<object?, EventArgs, Task>? UpdateComplete;
}