-
Notifications
You must be signed in to change notification settings - Fork 0
/
CT_UI.cs
463 lines (397 loc) · 14.7 KB
/
CT_UI.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using VistaButtonTest;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using CrazyTaxi.Car;
using System.IO;
using CTMapUtils;
using System.Drawing.Imaging;
namespace CrazyTaxi
{
public partial class CT_UI : Form
{
public static int UPDATE_INTERVAL=30;
private Size _Dimension;
private DoubleBufferedPanel menuContainer;
private DoubleBufferedPanel menuHolder;
private HighscoreControlView highscoreView;
private Image img;
DateTime startTime = System.DateTime.Now;
private int x = 0;
private int y = 0;
private int genway=-1;
private Game game;
private Bitmap backbuffer; // Backbuffers bitmap
private Graphics backBufferGraphics; // We draw on this
private bool _initialized = false;
private Pong pong;
private bool playPong = false;
private bool showFPS = false;
//Wird Verwendet da Cursor.hide/show nicht immer bzw nicht funktioniert
// using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);
public CT_UI()
{
InitializeComponent();
Initialize();
Backgroundfader.Start();
}
private void Initialize()
{
if (!_initialized)
{
Fullscreen();
_Dimension = GetResolution();
Size size = new Size((int)(_Dimension.Width * 1.2), (int)(_Dimension.Height * 1.2));
img = Properties.Resources._3;
img = CT_Helper.resizeImage(img, size);
initGame();
#region Menu
int drawingPointWidth = _Dimension.Width / 2;
int drawingPointHeight = _Dimension.Height / 2;
Point menu = new Point(drawingPointWidth - 683 / 2, drawingPointHeight - 384 / 2);
menuContainer = new DoubleBufferedPanel(menu);
menuContainer.Width = _Dimension.Width;
menuContainer.Height = _Dimension.Height;
menuContainer.Location = new Point(0, 0);
menuContainer.Name = "Menu";
menuHolder = new DoubleBufferedPanel();
string[] buttonTexts = { "Starten", "Laden", "Speichern", "Highscore","Pong spielen","Beenden" };
int positionMultiplicator = 1;
int offset = 10;
foreach (string text in buttonTexts)
{
VistaButton button = new VistaButton();
button.ButtonText = text;
button.Width = 160;
button.Height = 38;
button.Location = new Point(offset + button.Width / 4, (button.Height + offset) * positionMultiplicator);
menuHolder.Controls.Add(button);
positionMultiplicator++;
button.Click += Button_Click;
button.KeyDown += CT_UI_KeyDown;
button.KeyUp += CT_UI_KeyUp;
button.BackColor = Color.Transparent;
button.BaseColor = Color.Transparent;
button.ButtonColor = Color.FromArgb(155, Color.DarkSlateGray);
button.GlowColor = Color.LightGray;
button.Font = new System.Drawing.Font("Pricedown", 16.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
button.CornerRadius = 8;
}
this.Controls.Add(menuContainer);
this.Controls.Add(menuHolder);
menuHolder.BringToFront();
menuHolder.BorderStyle = BorderStyle.Fixed3D;
menuHolder.Width = 683;
menuHolder.Height = 384;
menuHolder.Location = menu;
menuHolder.BackColor = Color.Black;
menuContainer.BackColor = Color.FromArgb(150, Color.Black);
#endregion
setCursor();
this.Focus();
this.TransparencyKey = Color.FromArgb(255, 255, 254);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
pong=new Pong(new Size(_Dimension.Width,_Dimension.Height));
gameBox.Size = new Size(_Dimension.Width, _Dimension.Height);
// Resize our backbuffer
backbuffer = new Bitmap(gameBox.Size.Width,gameBox.Size.Height, PixelFormat.Format32bppPArgb);
backBufferGraphics = Graphics.FromImage(backbuffer);
Updater.Interval = UPDATE_INTERVAL;
_initialized = true;
}
}
private void initGame()
{
game = new Game();
game.Initialize(this, _Dimension);
}
private void setCursor()
{
//FileStream str = new FileStream(@"C:\Users\Bongo\Desktop\Berufsschule\Diagramme AE\Game\CrazyTaxi\CrazyTaxi\Resources\BlackAngel.cur", FileMode.Open);
//this.Cursor = new Cursor(str);
this.Cursor = new Cursor(Application.StartupPath + "..\\..\\..\\Custom.cur");
}
private void Fullscreen()
{
this.WindowState = FormWindowState.Maximized;
}
private Size GetResolution()
{
int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
return new Size( width, height );
}
#region Events
public void Button_Click(object sender, EventArgs e)
{
VistaButton bt = (VistaButton)sender;
switch (bt.ButtonText)
{
case "Starten":
showMenu(false);
bt.ButtonText = "Fortsetzen";
break;
case "Fortsetzen":
if (Game.GameState.Failed.Equals(game.State)){
initGame();
}
showMenu(false);
break;
case "Laden":
showMenu(false);
//erzeuge neues GamePanel
break;
case "Speichern":
//Datenbank ...
break;
case "Beenden":
System.Environment.Exit(0);
break;
case "Highscore":
if (highscoreView != null && menuHolder.Controls.Contains(highscoreView))
{
menuHolder.Controls.Remove(highscoreView);
}
else
{
highscoreView = new HighscoreControlView();
highscoreView.Size = new Size(menuHolder.Size.Width - 225, menuHolder.Size.Height-7);
highscoreView.Location = new Point(220, 2);
//highscoreView.BackColor = Color.DimGray;
menuHolder.Controls.Add(highscoreView);
}
break;
case "Pong spielen":
playPong = true;
showMenu(false);
break;
default:
break;
}
}
private List<Keys> keyList = new List<Keys>();
private void CT_UI_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
playPong = false;
showMenu(true);
}
else if (e.KeyCode == Keys.F && e.Modifiers == Keys.Control)
{
showFPS = !showFPS;
}
else
{
if (playPong)
{
if (!keyList.Contains(e.KeyCode))
{
keyList.Add(e.KeyCode);
}
}
else if (Game.GameState.Running.Equals(game.State) || Game.GameState.MiniMap.Equals(game.State))
{
game.keyDown(e.KeyCode);
}
}
}
private void CT_UI_KeyUp(object sender, KeyEventArgs e)
{
if (playPong)
{
if (keyList.Contains(e.KeyCode))
{
keyList.Remove(e.KeyCode);
}
}
else if (Game.GameState.Running.Equals(game.State) || Game.GameState.MiniMap.Equals(game.State))
{
game.keyUp(e.KeyCode);
}
}
private void showMenu(bool state)
{
this.Focus();
game.changeGameState(state);
menuContainer.Visible = state;
menuHolder.Visible = state;
ShowCursor(state);
if (state)
{
Backgroundfader.Start();
}
else
{
Backgroundfader.Stop();
}
}
private void Backgroundfader_Tick(object sender, EventArgs e)
{
imageChange();
int speed = (_Dimension.Width+_Dimension.Height)/1000;
switch (genway)
{
case 0:
y += speed;
x += speed;
break;
case 1:
y -= speed;
x -= speed;
break;
case 2:
y -= speed;
x += speed;
break;
case 3:
y += speed;
x -= speed;
break;
}
}
private void ChangeImage()
{
/*
* genway:
* 0: DownRight
* 1: TopLeft
* 2: TopRight
* 3: DownLeft
*/
Random rnd = new Random();
genway = rnd.Next(0, 4);
if(genway==0)
{
x = _Dimension.Width - img.Width;
y = _Dimension.Height - img.Height;
}
else if (genway == 1)
{
x = 0;
y = 0;
}
else if (genway == 2)
{
x = _Dimension.Width - img.Width;
y = 0;
}
else if (genway == 3)
{
x = 0;
y = _Dimension.Height - img.Height;
}
Random rand = new Random();
Size size = new Size((int)(_Dimension.Width * 1.2), (int)(_Dimension.Height * 1.2));
switch (rand.Next(0, 3))
{
case 0:
img = CT_Helper.resizeImage(Properties.Resources._2, size);
break;
case 1:
img = CT_Helper.resizeImage(Properties.Resources._3, size);
break;
case 2:
img = CT_Helper.resizeImage(Properties.Resources._4, size);
break;
}
}
private void imageChange()
{
int difWidth = _Dimension.Width - img.Width;
int difHeight = _Dimension.Height - img.Height;
if (genway==0)
{
if (x >= 0 || y >= 0)
{
ChangeImage();
}
}
else if (genway==1)
{
if (difWidth >= x || difHeight >= y)
{
ChangeImage();
}
}
else if (genway == 2)
{
if (x >= 0 || y <= difHeight)
{
ChangeImage();
}
}
else if (genway == 3)
{
if (x <= difWidth || y >= 0)
{
ChangeImage();
}
}
else
{
genway=1;
}
}
private void CT_UI_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.CompositingMode=CompositingMode.SourceOver;
g.CompositingQuality=CompositingQuality.HighSpeed;
g.PixelOffsetMode=PixelOffsetMode.None;
g.SmoothingMode=SmoothingMode.None;
g.InterpolationMode=InterpolationMode.Default;
if (playPong)
{
long difference = (System.DateTime.Now - startTime).Milliseconds;
pong.Update(difference, keyList);
pong.Draw(backBufferGraphics);
}
else if (Game.GameState.Pause.Equals(game.State))
{
backBufferGraphics.DrawImage(img, new Point(x, y));
}
else if (Game.GameState.Running.Equals(game.State) || Game.GameState.MiniMap.Equals(game.State))
{
game.draw(backBufferGraphics);
long difference = (System.DateTime.Now - startTime).Milliseconds;
double fps = 0;
if (difference != 0)
{
fps = 1000 / difference;
}
if (showFPS)
{
backBufferGraphics.DrawString("fps:" + System.Math.Round(fps).ToString(), DefaultFont, Brushes.Yellow, 10, 10);
}
backBufferGraphics.DrawString("Score:" + game.Score.ToString(), new Font(FontFamily.GenericSerif, 10), Brushes.Yellow, 100, 10);
backBufferGraphics.DrawString("Level:" + game.MissionLevel.ToString(), new Font(FontFamily.GenericSerif, 10), Brushes.Yellow, 250, 10);
}
else if (Game.GameState.Failed.Equals(game.State))
{
backBufferGraphics.DrawString("Game Over \n Score=" + game.Score, new Font(FontFamily.GenericSansSerif, 50), Brushes.Crimson, _Dimension.Width / 2 -200, _Dimension.Height / 2-50);
}
g.DrawImageUnscaled(backbuffer, 0, 0);
startTime= System.DateTime.Now;
Application.DoEvents();
gameBox.Invalidate();
}
#endregion
private void Updater_Tick(object sender, EventArgs e)
{
game.updateGame(Updater.Interval);
}
}
}