-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
165 lines (152 loc) · 3.81 KB
/
MainWindow.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
using System;
using Gtk;
using Mono;
using Bounce;
public partial class MainWindow: Gtk.Window
{
Board board;
Game game;
int level;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
protected Board createBoard (int width, int height)
{
int fieldSize = 20;
BoardRenderer renderer = new BoardRenderer (this.canvas, width, height, fieldSize);
board = new Board (width, height, fieldSize, renderer);
return board;
}
public void StartGame (Config config)
{
game = new Game (this.createBoard (config.Width, config.Height));
game.GameWon += delegate(object sender, EventArgs e) {
MessageDialog dialog = new MessageDialog (
this,
DialogFlags.Modal,
MessageType.Info,
ButtonsType.None,
"Úroveň dokončena! Abyste zvládli víc nepřátel, dostanete další život."
);
dialog.AddButton ("Další kolo", ResponseType.Accept);
dialog.AddButton ("Konec hry", ResponseType.Cancel);
dialog.Response += delegate(object o, ResponseArgs args) {
if (args.ResponseId == ResponseType.Accept) {
NextLevel (config);
} else {
Application.Quit ();
}
};
dialog.Run ();
dialog.Destroy ();
};
game.GameLost += delegate(object sender, EventArgs e) {
MessageDialog dialog = new MessageDialog (
this,
DialogFlags.Modal,
MessageType.Info,
ButtonsType.None,
"Konec hry"
);
dialog.AddButton ("Nová hra", ResponseType.Accept);
dialog.AddButton ("Konec", ResponseType.Close);
dialog.Response += delegate(object o, ResponseArgs args) {
if (args.ResponseId == ResponseType.Accept) {
MainClass.ShowLauncher ();
this.Destroy ();
} else {
Application.Quit ();
}
};
dialog.Run ();
dialog.Destroy ();
};
game.FilledAreaChanged += delegate(object sender, int value) {
fillCounter.Text = String.Format ("Zaplněno: {0}%", value);
};
game.LivesChanged += delegate(object sender, int value) {
lifeCounter.Text = String.Format ("Životy: {0}", value);
};
game.RemainingTimeChanged += delegate(object sender, int value) {
remainingTimeCounter.Text = string.Format ("Zbývající čas: {0} sekund", value);
};
game.Start (config);
level = 1;
updateLevelCounter ();
}
protected void PauseGame ()
{
game.Pause ();
board.OverlayText = "Hra je pozastavena. Stiskněte libovolnou klávesu.";
}
protected void NextLevel (Config config)
{
level += 1;
updateLevelCounter ();
config.BallCount += 1;
if (level % 2 == 0) {
config.MonsterCount += 1;
}
config.Lives = game.Lives + 1;
board.Clear ();
game.Start (config);
}
protected void updateLevelCounter ()
{
levelCounter.Text = String.Format ("Úroveň: {0}", level);
}
protected void OnFocusOutEvent (object sender, FocusOutEventArgs args)
{
if (game.Running) {
PauseGame ();
}
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnCanvasExposeEvent (object o, ExposeEventArgs args)
{
if (game.Running) {
board.Render ();
}
}
[GLib.ConnectBefore]
protected void OnKeyPressEvent (object o, KeyPressEventArgs args)
{
if (!game.Running) {
game.Resume ();
} else {
board.SteerPlayer (keyToDirection (args.Event.Key));
}
}
protected void OnKeyReleaseEvent (object o, KeyReleaseEventArgs args)
{
board.UnsteerPlayer (keyToDirection (args.Event.Key));
}
protected Direction keyToDirection (Gdk.Key key)
{
switch (key) {
case Gdk.Key.w:
case Gdk.Key.k:
case Gdk.Key.Up:
return Bounce.Direction.Up;
case Gdk.Key.s:
case Gdk.Key.j:
case Gdk.Key.Down:
return Bounce.Direction.Down;
case Gdk.Key.a:
case Gdk.Key.h:
case Gdk.Key.Left:
return Bounce.Direction.Left;
case Gdk.Key.d:
case Gdk.Key.l:
case Gdk.Key.Right:
return Bounce.Direction.Right;
default:
return Bounce.Direction.None;
}
}
}