-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.cs
More file actions
310 lines (245 loc) · 7.17 KB
/
GameScreen.cs
File metadata and controls
310 lines (245 loc) · 7.17 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Engine;
using Engine.Core;
using Engine.Meshes;
public class Screen : Form
{
private bool isRunning = true;
private Camera cam = null!;
private Graphics g = null!;
private PictureBox pb = new()
{
Dock = DockStyle.Fill
};
public Screen()
{
renderTimes.Enqueue(DateTime.Now);
Cursor.Hide();
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.Controls.Add(pb);
this.Load += delegate
{
cam = new Camera(new Point3D(-200, 0, 0), new Vector3(1, 1E-10f, 0), new Vector3(0, 1, 0), pb.Width, pb.Height, 1000f, 1000);
var bmp = new Bitmap(pb.Width, pb.Height);
g = Graphics.FromImage(bmp);
pb.Image = bmp;
CenterScreen = new Point(pb.Width / 2, pb.Height / 2);
};
this.KeyUp += KeyBindUp;
this.KeyDown += KeyBindDown;
this.pb.MouseMove += MouseControl;
}
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern bool SetCursorPos(int X, int Y);
private Point CenterScreen;
private void MouseControl(object? o, MouseEventArgs e)
{
var sensibility = 1000f;
var thetaZ = -(CenterScreen.X - e.X) / sensibility;
var thetaY = -(CenterScreen.Y - e.Y) / sensibility;
cam?.RotateQuaternionZ(thetaZ);
cam?.RotateQuaternionY(thetaY);
// cam?.RotateGimbalLock(cosY, sinY, cosZ, sinZ);
SetCursorPos(CenterScreen.X, CenterScreen.Y);
}
private float xVel = 0;
private float yVel = 0;
private float zVel = 0;
private float velocity = 2f;
private float wVelX = 0;
private float sVelX = 0;
private float aVelX = 0;
private float dVelX = 0;
private float wVelY = 0;
private float sVelY = 0;
private float aVelY = 0;
private float dVelY = 0;
private bool isW = false;
private bool isS = false;
private bool isA = false;
private bool isD = false;
private void KeyBindDown(object? o, KeyEventArgs e)
{
var key = e.KeyCode;
var unitNormal = Vector3.Normalize(cam.Normal);
var unitHorizontal = Vector3.Normalize(cam.Horizontal);
if (key == Keys.Escape)
{
isRunning = false;
Application.Exit();
}
if (key == Keys.W)
{
if(isW)
{
xVel -= wVelX;
yVel -= wVelY;
}
isW = true;
wVelX = unitNormal.X * velocity;
xVel += wVelX;
wVelY = unitNormal.Y * velocity;
yVel += wVelY;
}
else if (key == Keys.S)
{
if(isS)
{
xVel += sVelX;
yVel += sVelY;
}
isS = true;
sVelX = unitNormal.X * velocity;
xVel -= sVelX;
sVelY = unitNormal.Y * velocity;
yVel -= sVelY;
}
if (key == Keys.A)
{
if(isA)
{
xVel += aVelX;
yVel += aVelY;
}
isA = true;
aVelX = unitHorizontal.X * velocity;
xVel -= aVelX;
aVelY = unitHorizontal.Y * velocity;
yVel -= aVelY;
}
else if (key == Keys.D)
{
if(isD)
{
xVel -= dVelX;
yVel -= dVelY;
}
isD = true;
dVelX = unitHorizontal.X * velocity;
xVel += dVelX;
dVelY = unitHorizontal.Y * velocity;
yVel += dVelY;
}
if (key == Keys.Space)
Jump();
}
private void KeyBindUp(object? o, KeyEventArgs e)
{
var key = e.KeyCode;
if (key == Keys.W)
{
isW = false;
xVel -= wVelX;
yVel -= wVelY;
}
if (key == Keys.S)
{
isS = false;
xVel += sVelX;
yVel += sVelY;
}
if (key == Keys.A)
{
isA = false;
xVel += aVelX;
yVel += aVelY;
}
if (key == Keys.D)
{
isD = false;
xVel -= dVelX;
yVel -= dVelY;
}
}
private float minJumpZ = 0;
private void checkMovement()
{
// Vector2 movementVector = new(xVel, yVel);
// Vector2 movementVectorNormalized = new(0, 0);
// if (movementVector != Vector2.Zero)
// movementVectorNormalized = Vector2.Normalize(movementVector);
// cam.Translate(movementVectorNormalized.X, movementVectorNormalized.Y, zVel);
cam.Translate(xVel, yVel, zVel);
foreach (var mesh in Scene.Current.Meshes)
{
if (mesh is Cube cube)
{
var collisionResult = cube.Collided(cam.Location);
if (collisionResult.result == CollidedResult.True)
{
cam.Translate(-xVel, -yVel, -zVel);
minJumpZ = collisionResult.top;
break;
}
else
minJumpZ = 0;
}
}
}
public void Run()
{
while (isRunning)
{
getFPS();
updateJump();
checkMovement();
cam?.Render();
cam?.Draw(g);
g.DrawString($"{fps} fps", DefaultFont, Brushes.Red, new PointF(50.0F, 50.0F));
g.DrawString($"{cam?.Location}", DefaultFont, Brushes.Black, new PointF(50.0F, 100.0F));
g.DrawString($"{minJumpZ}", DefaultFont, Brushes.Black, new PointF(50.0F, 125.0F));
var cube = Scene.Current.Meshes[0] as Cube;
if (cam?.Location is not null)
g.DrawString($"Collided: {cube?.Collided(cam.Location)}", DefaultFont, Brushes.Black, new PointF(50.0F, 150.0F));
pb?.Refresh();
Application.DoEvents();
}
}
#region FPS
private int fps = 0;
private int precisionFPS = 19;
private Queue<DateTime> renderTimes = new();
void getFPS()
{
var now = DateTime.Now;
renderTimes.Enqueue(now);
if (renderTimes.Count > precisionFPS)
{
DateTime old = renderTimes.Dequeue();
var time = now - old;
fps = (int)(precisionFPS / time.TotalSeconds);
}
}
#endregion
#region Jump
private bool isJumping = false;
void Jump()
{
if (isJumping)
return;
zVel = 10;
isJumping = true;
}
void updateJump()
{
if (!isJumping)
return;
zVel -= 1f;
if (cam?.Location.Z < minJumpZ)
{
cam.Location = cam.Location with
{
Z = minJumpZ
};
zVel = 0;
isJumping = false;
}
}
#endregion
}