-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
203 lines (181 loc) · 4.72 KB
/
Game.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
using System;
namespace Bounce
{
public delegate void GameWonHandler (object sender, EventArgs e);
public delegate void GameLostHandler (object sender, EventArgs e);
public delegate void LivesChangedHandler (object sender, int value);
public delegate void FilledAreaChangedHandler (object sender, int value);
public delegate void RemainingTimeChangedHandler (object sender, int value);
public class Game
{
Board board;
Random random = new Random ();
public int Filled {
get;
protected set;
}
public int Lives {
get;
protected set;
}
public bool Running {
get;
protected set;
}
protected uint renderTimeoutID;
protected uint limitTimeoutID;
int passed, limit;
const int victoryCondition = 70;
public event GameWonHandler GameWon;
public event GameLostHandler GameLost;
public event LivesChangedHandler LivesChanged;
public event FilledAreaChangedHandler FilledAreaChanged;
public event RemainingTimeChangedHandler RemainingTimeChanged;
public Game (Board board)
{
this.board = board;
board.AreaFilled += delegate(object sender, AreaFilledEventArgs e) {
Filled += e.FilledArea;
if (FilledAreaChanged != null) {
FilledAreaChanged (this, getFilledPercents ());
}
if (getFilledPercents () >= victoryCondition) {
this.End ();
if (GameWon != null) {
GameWon (this, EventArgs.Empty);
}
}
};
board.PlayerCollision += delegate(object sender, EventArgs e) {
Lives -= 1;
if (LivesChanged != null) {
LivesChanged (this, Lives);
}
if (Lives == 0) {
this.End ();
if (GameLost != null) {
GameLost (this, EventArgs.Empty);
}
}
};
Running = false;
}
public void Start (Config config)
{
Filled = 2 * board.Width + 2 * board.Height - 4;
if (FilledAreaChanged != null) {
FilledAreaChanged (this, getFilledPercents ());
}
Lives = config.Lives;
if (LivesChanged != null) {
LivesChanged (this, Lives);
}
for (int i = 0; i < config.BallCount; i++) {
spawnBall ();
}
for (int i = 0; i < config.MonsterCount; i++) {
spawnMonster ();
}
startRenderTimeout ();
limit = config.TimePerBall * config.BallCount;
if (RemainingTimeChanged != null) {
RemainingTimeChanged (this, limit);
}
passed = 0;
startLimitTimeout ();
Running = true;
}
public void End ()
{
board.MoveBalls ();
board.Render ();
GLib.Source.Remove (renderTimeoutID);
GLib.Source.Remove (limitTimeoutID);
Running = false;
}
public void Pause ()
{
GLib.Source.Remove (renderTimeoutID);
GLib.Source.Remove (limitTimeoutID);
Running = false;
}
public void Resume ()
{
Running = true;
startRenderTimeout ();
startLimitTimeout ();
}
protected void startRenderTimeout ()
{
uint fps = 33;
double speed = 6;
renderTimeoutID = GLib.Timeout.Add (1000 / fps, delegate {
for (int i = 0; i < (int)Math.Ceiling(speed * board.FieldSize / fps); i++) {
board.MoveBalls ();
}
for (int i = 0; i < (int)Math.Ceiling(speed * board.FieldSize / fps); i++) {
board.MoveMonsters ();
}
for (int i = 0; i < (int)Math.Ceiling(speed * board.FieldSize / fps); i++) {
board.MovePlayer ();
}
board.Render ();
return true;
});
}
protected void startLimitTimeout ()
{
DateTime start = DateTime.Now;
int initPassed = passed;
limitTimeoutID = GLib.Timeout.Add (200, delegate {
int newPassed = initPassed + (int)(DateTime.Now - start).TotalSeconds;
if (newPassed > passed) {
passed = newPassed;
if (RemainingTimeChanged != null) {
RemainingTimeChanged (this, Math.Max (limit - passed, 0));
}
if (passed > limit && GameLost != null) {
End ();
GameLost (this, EventArgs.Empty);
}
}
return true;
});
}
protected int getFilledPercents ()
{
return (int)Math.Floor ((decimal)((100 * Filled) / (board.Width * board.Height)));
}
protected void spawnBall ()
{
board.AddBall (
random.Next (2, board.Width - 1),
random.Next (2, board.Height - 1),
(int)Math.Pow (-1, random.Next (0, 2)),
(int)Math.Pow (-1, random.Next (0, 2))
);
}
protected void spawnMonster ()
{
string[] types = new string[] {
"Wanderer",
"Circulator",
"Sniffer"
};
board.AddMonster (types [random.Next (0, types.Length)]);
}
}
public class Config
{
public int Width, Height, BallCount, MonsterCount, Lives, TimePerBall;
public Config (int Width = 0, int Height = 0, int BallCount = 0, int MonsterCount = 0, int Lives = 0, int TimePerBall = 0)
{
this.Width = Width;
this.Height = Height;
this.BallCount = BallCount;
this.MonsterCount = MonsterCount;
this.Lives = Lives;
this.TimePerBall = TimePerBall;
}
}
}