/
Program.cs
369 lines (349 loc) · 14.9 KB
/
Program.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
using System;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
using SFML.Audio;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace Spleef
{
internal class Program
{
public static RenderWindow App;
public static float DefaultVolume = 10;
public static Random Generator;
public static SoundBuffer MainTheme;
public static SoundBuffer[] Musics;
public static bool SoundEnabled = true;
public static void HelpPannel()
{
var text = new CustomText(
@"but : survivre le plus longtemps possible.
le terrain se degrade a mesure que la partie avance, evitez de tomber
dans la lave avant les autres !
a chaque deplacement, vous rendez l'ancienne place instable, qui se
deteriorera a son tour !
faire un long saut fissure egalement la case ou vous atterissez, donc
faites attention si vous sautez sur une case deja fissuree !
le dernier en vie gagne la partie.")
{ Height = 20, Position = new Vector2f(5, 25) };
var finish = false;
void keyDown(object sender, KeyEventArgs e) => finish = Utilities.IsCancelKey(e.Code) || Utilities.IsConfirmKey(e.Code);
App.KeyPressed += keyDown;
while (App.IsOpen && !finish)
{
App.WaitAndDispatchEvents();
App.Clear(new Color(255, 150, 0));
App.Draw(text);
App.Display();
}
App.KeyPressed -= keyDown;
}
private static void Main(string[] args)
{
Generator = new Random();
App = new RenderWindow(new VideoMode(900, 600), "SPLEEF!", Styles.Close);
App.SetVerticalSyncEnabled(true);
Game.LightMap = new RenderTexture(900, 600);
App.Closed += (sender, e) => App.Close();
App.SetIcon(256, 256, Utilities.Icon.Pixels);
var title = new RectangleShape(new Vector2f(43, 26) * 400 / 43) { Texture = new Texture("assets/images/spleef.png") };
title.Origin = title.Size / 2;
title.Position = (Vector2f)App.Size / 2;
var startMessage = new CustomText("Appuyer sur une touche") { Height = 20 };
startMessage.Origin = new Vector2f(startMessage.LocalBounds.Width / 2, 10);
startMessage.Position = new Vector2f(App.Size.X / 2, 560);
bool bigger = true;
var clock = new Clock();
var darker = true;
var bgGreen = .6f;
var spark = new RectangleShape(new Vector2f(startMessage.LocalBounds.Width, startMessage.LocalBounds.Width)) { Texture = Utilities.SparkTexture };
spark.Origin = spark.Size / 2;
var shrinker = Transform.Identity;
shrinker.Translate(startMessage.Position);
shrinker.Scale(1.5f, 20f / startMessage.LocalBounds.Width * 2.5f);
var cursor = new RightCursor(64);
{
void stopAll(object sender, EventArgs e) => Environment.Exit(0);
App.Closed += stopAll;
var text = new CustomText("Chargement des musiques...") { Height = 40 };
text.Origin = new Vector2f(text.LocalBounds.Width / 2, text.LocalBounds.Height / 2);
text.Position = (Vector2f)App.Size / 2;
var icon = new RectangleShape(new Vector2f(64, 64)) { Texture = new Texture("assets/images/icon.png") };
icon.Origin = icon.Size / 2;
icon.Position = (Vector2f)App.Size - new Vector2f(50, 50);
var subClock = new Clock();
var t = Task.Run(() =>
{
Utilities.InitMusics();
text.Text = "Chargement des textures...";
text.Origin = new Vector2f(text.LocalBounds.Width / 2, text.LocalBounds.Height / 2);
text.Position = (Vector2f)App.Size / 2;
Tile.InitTiles();
});
while (!t.IsCompleted)
{
var delta = subClock.Restart();
App.DispatchEvents();
icon.Rotation += delta.AsSeconds() * 90;
App.Clear(new Color(255, 150, 0));
App.Draw(text);
App.Draw(icon);
App.Display();
}
App.Closed -= stopAll;
}
var mainMusic = new Sound(MainTheme) { Loop = true, Volume = DefaultVolume };
var rotation = 0f;
bool titleTime = true;
var play = new CustomText("Jouer")
{
Height = 40,
Position = new Vector2f(450, 250)
};
play.Origin = new Vector2f(play.LocalBounds.Width / 2, 20);
var howTo = new CustomText("Comment jouer")
{
Height = 40,
Position = new Vector2f(450, 350)
};
howTo.Origin = new Vector2f(howTo.LocalBounds.Width / 2, 20);
var quit = new CustomText("Quitter")
{
Height = 40,
Position = new Vector2f(450, 450)
};
quit.Origin = new Vector2f(quit.LocalBounds.Width / 2, 20);
var sound = new CustomText("$")
{
Height = 40
};
sound.Position = new Vector2f(900 - sound.LocalBounds.Width, 550);
sound.Origin = new Vector2f(sound.LocalBounds.Width / 2, 20);
CustomText selection = play;
void keyPressed(object sender, KeyEventArgs e)
{
if (titleTime)
{
titleTime = false;
selection = play;
}
else
{
if (Utilities.IsUpKey(e.Code))
if (selection == play)
selection = quit;
else if (selection == quit)
selection = howTo;
else if (selection == howTo)
selection = play;
else if (selection == sound)
selection = quit;
if (Utilities.IsDownKey(e.Code))
if (selection == play)
selection = howTo;
else if (selection == howTo)
selection = quit;
else if (selection == quit)
selection = play;
else if (selection == sound)
selection = play;
if (Utilities.IsRightKey(e.Code) || Utilities.IsLeftKey(e.Code))
if (selection == sound)
selection = play;
else
selection = sound;
if (Utilities.IsCancelKey(e.Code))
titleTime = true;
if (Utilities.IsConfirmKey(e.Code))
{
if (selection == play)
{
App.KeyPressed -= keyPressed;
App.MouseButtonPressed -= mouseClick;
if (SoundEnabled)
mainMusic.Pause();
Game.launch();
if (SoundEnabled)
mainMusic.Play();
App.KeyPressed += keyPressed;
App.MouseButtonPressed += mouseClick;
clock.Restart();
}
if (selection == quit)
App.Close();
if (selection == sound)
{
SoundEnabled = !SoundEnabled;
sound.Text = SoundEnabled ? "$" : "£";
if (SoundEnabled)
mainMusic.Play();
else
mainMusic.Pause();
}
if (selection == howTo)
{
App.KeyPressed -= keyPressed;
App.MouseButtonPressed -= mouseClick;
HelpPannel();
App.KeyPressed += keyPressed;
App.MouseButtonPressed += mouseClick;
clock.Restart();
}
}
}
}
void mouseClick(object sender, MouseButtonEventArgs e)
{
if (titleTime)
{
titleTime = false;
selection = play;
}
else
{
if (play.GlobalBounds.Contains(App.MapPixelToCoords(e)))
{
App.KeyPressed -= keyPressed;
App.MouseButtonPressed -= mouseClick;
if (SoundEnabled)
mainMusic.Pause();
Game.launch();
if (SoundEnabled)
mainMusic.Play();
App.KeyPressed += keyPressed;
App.MouseButtonPressed += mouseClick;
clock.Restart();
}
else if (howTo.GlobalBounds.Contains(App.MapPixelToCoords(e)))
{
App.KeyPressed -= keyPressed;
App.MouseButtonPressed -= mouseClick;
HelpPannel();
App.KeyPressed += keyPressed;
App.MouseButtonPressed += mouseClick;
clock.Restart();
}
else if (quit.GlobalBounds.Contains(App.MapPixelToCoords(e)))
App.Close();
else if (sound.GlobalBounds.Contains(App.MapPixelToCoords(e)))
{
SoundEnabled = !SoundEnabled;
sound.Text = SoundEnabled ? "$" : "£";
if (SoundEnabled)
mainMusic.Play();
else
mainMusic.Pause();
}
}
}
App.KeyPressed += keyPressed;
App.MouseButtonPressed += mouseClick;
mainMusic.Play();
clock.Restart();
while (App.IsOpen)
{
App.DispatchEvents();
var delta = clock.Restart();
if (titleTime)
{
if (title.Position.Y < App.Size.Y / 2)
title.Position += new Vector2f(0, delta.AsSeconds() * 450);
if (bigger)
{
startMessage.Scale *= 1 + .2f * delta.AsSeconds();
if (startMessage.Scale.X > 1.2f)
bigger = false;
}
else
{
startMessage.Scale /= 1 + .2f * delta.AsSeconds();
if (startMessage.Scale.X < 1)
bigger = true;
}
title.Scale = startMessage.Scale * .9f;
}
else
{
if (title.Position.Y > 100)
title.Position -= new Vector2f(0, delta.AsSeconds() * 450);
if (bigger)
{
title.Scale *= 1 + .1f * delta.AsSeconds();
if (title.Scale.X > .7f)
bigger = false;
}
else
{
if (title.Scale.X > .7f)
title.Scale /= 1 + .8f * delta.AsSeconds();
title.Scale /= 1 + .1f * delta.AsSeconds();
if (title.Scale.X < .6f)
bigger = true;
}
}
if (darker)
{
bgGreen /= 1 + .2f * delta.AsSeconds();
if (bgGreen < .3f)
darker = false;
}
else
{
bgGreen *= 1 + .2f * delta.AsSeconds();
if (bgGreen > .65f)
darker = true;
}
rotation += delta.AsSeconds() * 60;
spark.Rotation = rotation;
spark.Scale = selection.Scale;
if (play.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
selection = play;
if (howTo.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
selection = howTo;
if (quit.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
selection = quit;
if (sound.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
selection = sound;
{
play.Scale = new Vector2f(1, 1);
howTo.Scale = new Vector2f(1, 1);
quit.Scale = new Vector2f(1, 1);
sound.Scale = new Vector2f(1, 1);
play.Color = Color.White;
howTo.Color = Color.White;
quit.Color = Color.White;
selection.Scale *= 1.3f;
cursor.Position = new Vector2f(selection.GlobalBounds.Left - 10, selection.Position.Y);
if (selection == play)
cursor.Color = Utilities.Green;
else if (selection == howTo)
cursor.Color = Utilities.Blue;
else if (selection == quit)
cursor.Color = Utilities.Red;
else if (selection == sound)
cursor.Color = SoundEnabled ? Utilities.Blue : Utilities.Red;
selection.Color = cursor.Color;
sound.Color = Color.White;
}
App.Clear(new Color(255, (byte)(bgGreen * 255), 0));
if (titleTime)
{
App.Draw(spark, new RenderStates(shrinker));
App.Draw(startMessage);
}
else
{
App.Draw(play);
App.Draw(howTo);
App.Draw(quit);
App.Draw(cursor);
App.Draw(sound);
}
App.Draw(title);
App.Display();
}
}
}
}