-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (100 loc) · 3.27 KB
/
Program.cs
File metadata and controls
120 lines (100 loc) · 3.27 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
namespace Gravity_Simulation
{
public class MainForm : Form
{
private readonly Vector cameraPosition = new(9.98948e10, 0);
private float scale = 1e-7f; // scale 1e-6f
private readonly float scaleFactor = 0.01f;
new public float Scale => scale;
private readonly System.Windows.Forms.Timer timer;
private readonly double deltaTime = 120000;
private readonly List<SpaceObject> objects = Objects.objects;
private readonly ControlForm controlForm;
public MainForm()
{
Paint += new PaintEventHandler(OnPaint);
KeyDown += new KeyEventHandler(OnKeyDown);
var screenSize = Screen.PrimaryScreen?.Bounds.Size ?? new Size(800, 600);
ClientSize = new Size(screenSize.Width, screenSize.Height);
WindowState = FormWindowState.Maximized;
timer = new System.Windows.Forms.Timer
{
Interval = 15
};
timer.Tick += OnTick;
timer.Start();
controlForm = new ControlForm(this);
controlForm.Show();
DoubleBuffered = true;
}
public void UpdateScale(float newScale)
{
scale = newScale;
Invalidate();
}
private void OnTick(object? sender, EventArgs? e)
{
for (int i = 0; i < objects.Count; i++)
{
for (int j = 0; j < objects.Count; j++)
{
if (i != j)
{
objects[i].ApplyGravity(objects[j], deltaTime);
}
}
}
foreach (var obj in objects)
{
obj.UpdatePosition(deltaTime);
}
var earth = objects.FirstOrDefault(o => o.Name == "sun");
if (earth != null)
{
cameraPosition.X = earth.Pos.X;
cameraPosition.Y = earth.Pos.Y;
}
Invalidate();
}
private void OnPaint(object? sender, PaintEventArgs? e)
{
if (e?.Graphics == null)
{
return;
}
Graphics g = e.Graphics;
g.Clear(Color.Black);
float s = scale * scaleFactor;
foreach (var obj in objects)
{
obj.Draw(g, s, cameraPosition);
}
}
private void OnKeyDown(object? sender, KeyEventArgs e)
{
const float moveAmount = 100e3f * 1000;
switch (e.KeyCode)
{
case Keys.W or Keys.Up:
cameraPosition.Y -= moveAmount;
break;
case Keys.S or Keys.Down:
cameraPosition.Y += moveAmount;
break;
case Keys.A or Keys.Left:
cameraPosition.X -= moveAmount;
break;
case Keys.D or Keys.Right:
cameraPosition.X += moveAmount;
break;
}
Invalidate();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
}