-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRendering.cs
395 lines (342 loc) · 12.8 KB
/
Rendering.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
namespace Astroshooter
{
public partial class SpaceField : Form
{
Ship ship;
static Image ShipTexture;
static Vec2 shipCords;
float angle = 0;
private Controller controller;
Point mousecords;
private List<SpaceObject> spaceObjects;
private bool isSpacePressed = false;
private bool isWPressed = false;
private bool isRPressed = false;
private bool isLPressed = false;
private bool debugCollisions = false;
private bool debugModeEnabled = false;
public bool isSoundEnabled { get; private set; } = true;
private bool isHelpEnabled = false;
private Label velocity;
private Label acceleration;
private Label thrustforce;
private Label direction;
private Label help;
private Label score;
List<Image> asteroidTextures;
Random random;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
System.Windows.Forms.Timer spawnTimer = new System.Windows.Forms.Timer();
public SpaceField(string[] args)
{
this.WindowState = FormWindowState.Maximized;
if (args.Contains("-debug"))
debugModeEnabled = true;
asteroidTextures = InitializeAsteroidImages();
ship = new Ship(new Vec2(ClientSize.Width / 2 ,ClientSize.Height / 2));
shipCords = ship.Location;
random = new Random((int)DateTime.Now.Ticks);
spaceObjects = new List<SpaceObject>(512);
spaceObjects.Add(ship);
controller = new Controller(ship, this, spaceObjects, asteroidTextures);
InitializeComponent();
InitializeControlHelp();
InitializeScore();
InitializeDebugLabels();
HideDebugLabels();
ShipTexture = ship.ShipTexture;
}
public SpaceField()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Text = "Astroshooter";
DoubleBuffered = true;
BackColor = Color.Black;
InitializeTimer();
if(debugModeEnabled)
InitializeDebugLabels();
//Cursor.Hide();
//this.TopMost = true;
//this.FormBorderStyle = FormBorderStyle.None;
}
void InitializeControlHelp()
{
int X = 0;
int Y = this.Bottom / 2;
Font font = new Font("Arial", 12.0f,
FontStyle.Bold);
this.help = new Label
{
Location = new Point(X, Y),
Size = new Size(180, 400),
Text = "Нажмите H чтобы показать помощь.",
ForeColor = Color.White,
BackColor = Color.Transparent,
Font = font,
};
Controls.Add(this.help);
}
void ToggleHelp()
{
isHelpEnabled = !isHelpEnabled;
if(isHelpEnabled)
help.Text =
"Esc - выйти из игры. \n" +
"Пробел - стрелять. \n" +
"Мышь - наведение. \n" +
"W - двигаться вперед. \n" +
"J - переключить звук. \n" +
"Pause - переключить паузу. \n" +
"E - воскреснуть, и потерять очки. \n";
if (!isHelpEnabled)
help.Text = "Нажмите H чтобы показать помощь.";
}
void InitializeScore()
{
Font font = new Font("Arial", 21.0f,
FontStyle.Bold);
score = new Label
{
Location = new Point(ClientSize.Width / 2, 30),
Size = new Size(300, 70),
ForeColor = Color.Gainsboro,
BackColor = Color.Transparent,
Font = font,
AutoSize = true,
};
Controls.Add(score);
}
public void UpdateScore(int newScore)
{
score.Text = newScore.ToString();
}
void InitializeDebugLabels()
{
velocity = new Label
{
Location = new Point(0, 10),
Size = new Size(100, 15),
Text = "Velocity = ",
ForeColor = Color.White,
BackColor = Color.Transparent
};
acceleration = new Label
{
Location = new Point(0, 70),
Size = new Size(100, 15),
Text = "Acceleration = ",
ForeColor = Color.White,
BackColor = Color.Transparent
};
thrustforce = new Label
{
Location = new Point(0, 130),
Size = new Size(100, 15),
Text = "Acceleration = ",
ForeColor = Color.White,
BackColor = Color.Transparent
};
direction = new Label
{
Location = new Point(0, 190),
Size = new Size(130, 15),
Text = "Direction = ",
ForeColor = Color.White,
BackColor = Color.Transparent
};
Controls.Add(velocity);
Controls.Add(acceleration);
Controls.Add(thrustforce);
Controls.Add(direction);
}
List<Image> InitializeAsteroidImages()
{
string asteroidDirectoryPath = @".\textures\asteroid";
string directory = Directory.GetCurrentDirectory();
var files = Directory.EnumerateFiles(asteroidDirectoryPath, "*.png", SearchOption.AllDirectories);
var imageList = files.Select(x => Image.FromFile(x)).ToList();
return imageList;
}
void InitializeTimer()
{
timer.Interval = 10;
timer.Tick += (sender, args) => ship.SimulateTimeFrame(timer.Interval);
timer.Tick += (sender, args) => controller.UpdateSimulation(timer.Interval);
timer.Tick += (sender, args) => MovementDataTick();
timer.Tick += (sender, args) => Invalidate(sender, args);
timer.Tick += (sender, args) => InputTick();
timer.Start();
spawnTimer.Interval = 1000;
timer.Tick += (sender, args) => controller.CreateRandomAsteroid();
}
void MovementDataTick()
{
var vel = ship.GetVelocity();
var thrust = ship.GetThrustForce();
var accel = ship.GetAcceleration();
if (debugModeEnabled)
{
velocity.Text = "Velocity = " + Math.Round(Math.Sqrt(vel.X * vel.X + vel.Y * vel.Y), 5).ToString();
thrustforce.Text = "Thrust = " + thrust.ToString();
acceleration.Text = "Acceleration = " + Math.Sqrt(accel.X * accel.X + accel.Y * accel.Y);
direction.Text = "Direction = " + ship.Direction.ToString();
}
}
void InputTick()
{
if (isSpacePressed && !ship.IsDead())
{
controller.CreateBullet(angle);
}
if (isWPressed && !ship.IsDead())
ship.ApplyForce();
if (isRPressed)
BackColor = Color.Black;
if (isLPressed)
controller.CreateRandomAsteroid();
}
void HideDebugLabels()
{
velocity.Hide();
acceleration.Hide();
thrustforce.Hide();
direction.Hide();
}
void ShowDebugLabels()
{
velocity.Show();
acceleration.Show();
thrustforce.Show();
direction.Show();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Space)
isSpacePressed = true;
if (e.KeyCode == Keys.W)
isWPressed = true;
if (e.KeyCode == Keys.R)
isRPressed = true;
if (e.KeyCode == Keys.Oemtilde)
{
debugModeEnabled = !debugModeEnabled;
if (!debugModeEnabled)
HideDebugLabels();
else
ShowDebugLabels();
}
if (e.KeyCode == Keys.H)
ToggleHelp();
if (debugModeEnabled)
{
if (e.KeyCode == Keys.T)
debugCollisions = !debugCollisions;
if (e.KeyCode == Keys.L)
controller.CreateRandomAsteroid();
}
if (e.KeyCode == Keys.O)
Cursor.Hide();
if (e.KeyCode == Keys.P)
Cursor.Show();
if (e.KeyCode == Keys.E)
controller.Respawn();
if (e.KeyCode == Keys.Escape)
this.Dispose();
if (e.KeyCode == Keys.Pause)
timer.Enabled = !timer.Enabled;
if (e.KeyCode == Keys.J)
isSoundEnabled = !isSoundEnabled;
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if (e.KeyCode == Keys.Space)
isSpacePressed = false;
if (e.KeyCode == Keys.W)
isWPressed = false;
if (e.KeyCode == Keys.R)
isRPressed = false;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
isSpacePressed = true;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
isSpacePressed = false;
}
void Invalidate(object sender, EventArgs e) {this.Invalidate(); }
protected override void OnPaint(PaintEventArgs e)
{
shipCords = ship.GetCurrentCoordinates();
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//рендер корабля
if (!ship.IsDead())
{
g.TranslateTransform((float)shipCords.X,
(float)shipCords.Y);
g.RotateTransform(angle);
ship.ChangeDirection(angle);
g.DrawImage(ShipTexture, new Point(-ShipTexture.Width / 2, -ShipTexture.Height / 2));
e.Graphics.ResetTransform();
if(debugModeEnabled)
e.Graphics.DrawLine(Pens.Red, new PointF((float)shipCords.X, (float)shipCords.Y), mousecords);
}
RenderAsteroids(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
mousecords = e.Location;
RotateImageAngle(e);
}
float RotateImageAngle(MouseEventArgs e)
{
var y2 = e.Y;
var y1 = shipCords.Y + ShipTexture.Height / 2;
var x2 = e.X;
var x1 = shipCords.X + ShipTexture.Width / 2;
angle = (float)(Math.Atan2((y1 - y2), (x1 - x2)) * 180 / Math.PI);
return 0;
}
void RenderAsteroids(PaintEventArgs e)
{
foreach (var spaceobject in spaceObjects)
{
if(spaceobject != null && !(spaceobject is Ship))
{
var objectLocation = spaceobject.GetCoordinates();
e.Graphics.DrawImage(spaceobject.GetImage(), Vec2.TransformToPointF(objectLocation));
if (debugCollisions)
{
var kek = spaceobject.GetImage();
var objectSize = spaceobject.GetSize();
var rect = new RectangleF(Vec2.TransformToPointF(objectLocation), new SizeF((float)kek.Size.Width, (float)kek.Size.Height));
//e.Graphics.DrawRectangle(new Pen(Color.Red), rect);
e.Graphics.DrawRectangles(new Pen(Color.Red), new RectangleF[] { rect });
e.Graphics.DrawString("KEKW", SystemFonts.DefaultFont, Brushes.LightGreen, Vec2.TransformToPointF(objectLocation));
}
}
}
}
}
}