-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
162 lines (136 loc) · 4.36 KB
/
Form1.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
using System;
using System.Drawing;
using System.Windows.Forms;
using AsteroidsGdiApp.Core;
namespace AsteroidsGdiApp
{
public partial class Form1 : Form, IGameForm, IGameController
{
Timer _timer = new Timer();
AsteroidsGame _game;
public Form1()
{
InitializeComponent();
MouseState = new MouseState
{
IsMouseButtonDown = false,
MousePosition = new Point(-1, -1)
};
KeyboardState = new KeyboardState
{
IsLeftKeyDown = false,
IsRightKeyDown = false
};
_timer.Enabled = true;
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Start();
_timer.Interval = 350;
}
void _timer_Tick(object sender, EventArgs e)
{
_gameController.Update();
}
public MouseState MouseState
{
get; set;
}
public KeyboardState KeyboardState
{ get; set;}
public bool Standalone
{
get; set;
}
public void Initialize()
{
ShowIcon = false;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.Black;
MaximizeBox = false;
Size = new Size(Constants.CanvasWidth,
Constants.CanvasWidth);
}
RunAloneGameController _gameController = new RunAloneGameController();
private void Form1_Load(object sender, EventArgs e)
{
_game = new AsteroidsGame(this, _gameController);
GameManager.CreateTheGame(_game);
_game.Run();
}
private void StartNewGame(IGameController gameController)
{
GameManager.TheGameManager.ReInitializeGame(gameController);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MouseState.IsMouseButtonDown = true;
MouseState.MousePosition = new Point(e.X, e.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
MouseState.IsMouseButtonDown = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Right)
{
KeyboardState.IsRightKeyDown = true;
}
else if(e.KeyCode == Keys.Left)
{
KeyboardState.IsLeftKeyDown = true;
}
if (e.KeyCode == Keys.Up)
{
KeyboardState.IsUpKeyDown = true;
}
else if (e.KeyCode == Keys.Down)
{
KeyboardState.IsDownKeyDown = true;
}
if (e.KeyCode == Keys.F)
{
KeyboardState.IsFireKeyDown = true;
}
if(e.KeyCode == Keys.S)
{
StartNewGame(this);
}
if (e.KeyCode == Keys.Space)
{
KeyboardState.IsShieldKeyDown = true;
}
if (e.KeyCode == Keys.X)
{
this.Close();
this.DialogResult = DialogResult.Cancel;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
KeyboardState.IsRightKeyDown = false;
}
else if (e.KeyCode == Keys.Left)
{
KeyboardState.IsLeftKeyDown = false;
}
if (e.KeyCode == Keys.Up)
{
KeyboardState.IsUpKeyDown = false;
}
else if (e.KeyCode == Keys.Down)
{
KeyboardState.IsDownKeyDown = false;
}
if (e.KeyCode == Keys.F)
{
KeyboardState.IsFireKeyDown = false;
}
if (e.KeyCode == Keys.Space)
{
KeyboardState.IsShieldKeyDown = false;
}
}
}
}