-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
256 lines (231 loc) · 8.08 KB
/
MainWindow.xaml.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using EngineBase;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XAMLEngine;
namespace XAMLEngine
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// Framerate (targeted frames per second)
private readonly double FRAMERATE = 60d;
// Timer handles Update loop
private Timer mainLoop;
private double _currentTime = 0;
private bool Awoken = false;
private bool running = false;
public void ScreenLoaded(object sender, RoutedEventArgs e)
{
Manager.CallScreenEvent(this, "MainWindow");
MainStart();
mainLoop.Start();
}
public MainWindow()
{
InitializeComponent();
// Globalizing xaml objects
Manager.canvas = canvasArea;
Manager.t_end = EndBox;
Manager.t_high = HighScoreBox;
Manager.b_end = EndButton;
Manager.t_score = ScoreBox;
Persistents.Initialize();
Score.Initialize();
Application.Current.Exit += Persistents.OnQuit;
// Begin Game loop
mainLoop = new Timer();
mainLoop.Elapsed += new ElapsedEventHandler(Elapsed);
mainLoop.Interval = 1000d / FRAMERATE;
mainLoop.AutoReset = true;
MainAwake();
}
private void Elapsed(object sender, ElapsedEventArgs e)
{
MainUpdate(System.DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - _currentTime);
_currentTime = System.DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
private void MainAwake()
{
// Initialize Fields
Manager.entities = new List<Entity>();
Manager.instantiationQueue = new List<Entity>();
Manager.deletionQueue = new List<Entity>();
Manager.colliders = new List<Entity>();
Manager.random = new Random();
Input.keysPressed = new List<Key>();
Input.keysPressedThisFrame = new List<Key>();
Input.keysReleasedThisFrame = new List<Key>();
// Preloads time
_currentTime = System.DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
InitializeScripts();
Manager.entities.AddRange(Manager.instantiationQueue);
Manager.instantiationQueue.Clear();
Awoken = true;
}
private void MainStart()
{
this.Dispatcher.Invoke(() =>
{
foreach (Entity entity in Manager.entities)
{
if (entity.IsEnabled)
entity.Start();
}
});
}
private void MainUpdate(double deltaTime)
{
if (running)
{
Debug.WriteLine($@"Skipped frame at {Manager.runTime}");
return;
}
running = true;
Manager.entities.AddRange(Manager.instantiationQueue);
Manager.instantiationQueue.Clear();
foreach (Entity entity in Manager.deletionQueue)
Manager.entities.Remove(entity);
Manager.deletionQueue.Clear();
Manager.runTime += deltaTime / 1000d;
if (!Awoken)
return;
this.Dispatcher.Invoke(() =>
{
Manager.colliders.Clear();
// Creates current hitboxes every frame
foreach (Entity entity in Manager.entities)
{
if (entity.IsEnabled)
{
entity.Update(deltaTime);
if (entity.shape != null)
{
entity.hitbox = new Rect(Canvas.GetLeft(entity.shape), Canvas.GetTop(entity.shape), entity.shape.Width, entity.shape.Height);
Manager.colliders.Add(entity);
}
}
}
// Checks all entity collision status
foreach (Entity first in Manager.colliders)
{
foreach (Entity second in Manager.colliders)
{
if (second != first)
{
if (first.hitbox.IntersectsWith(second.hitbox))
{
if (!first.collidingWith.Contains(second))
{
first.collidingWith.Add(second);
first.OnCollisionEnter(second);
}
first.OnCollision(second);
}
else if (first.collidingWith.Contains(second))
{
first.collidingWith.Remove(second);
first.OnCollisionExit(second);
}
}
}
}
});
MainLateUpdate();
running = false;
}
private void MainLateUpdate()
{
this.Dispatcher.Invoke(() =>
{
foreach (Entity entity in Manager.entities)
{
if (entity.IsEnabled)
{
entity.LateUpdate();
}
}
Input.MouseClickedLeft = false;
Input.MouseReleasedLeft = false;
Input.MouseClickedRight = false;
Input.MouseReleasedRight = false;
Input.keysPressedThisFrame.Clear();
Input.keysReleasedThisFrame.Clear();
});
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
Input.MouseClickedLeft = true;
Input.MouseHeldLeft = true;
});
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
Input.MouseReleasedLeft = true;
Input.MouseHeldLeft = false;
});
}
private void OnMouseRightDown(object sender, MouseButtonEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
Input.MouseClickedRight = true;
Input.MouseHeldRight = true;
});
}
private void OnMouseRightUp(object sender, MouseButtonEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
Input.MouseReleasedRight = true;
Input.MouseHeldRight = false;
});
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (!Input.keysPressed.Contains(e.Key))
{
Input.keysPressedThisFrame.Add(e.Key);
Input.keysPressed.Add(e.Key);
}
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
if (Input.keysPressed.Contains(e.Key))
{
Input.keysPressed.Remove(e.Key);
Input.keysReleasedThisFrame.Add(e.Key);
}
}
private void ShutdownWindow(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void InitializeScripts()
{
}
}
}