/
StateManager.cs
executable file
·97 lines (86 loc) · 3.1 KB
/
StateManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TapAndConquer
{
public static class StateManager
{
public static void ShowMainMenu(this Game game)
{
game.CleanupComponents();
var menu = new MainMenu(game);
menu.loadContent();
menu.playGame += game.StartGame;
menu.quit += game.Exit;
game.Components.Add(menu);
}
public static void StartGame(this Game game)
{
game.CleanupComponents();
var mainGame = new Manager(game);
mainGame.win += () => game.EndGame(true);
mainGame.loose += () => game.EndGame(false);
game.Components.Add(mainGame);
}
public static void EndGame(this Game game, bool win)
{
game.CleanupComponents();
var endMenu = new EndGameMenu(game, win);
endMenu.restart += game.StartGame;
endMenu.menu += game.ShowMainMenu;
game.Components.Add(endMenu);
}
public static void CleanupComponents(this Game game)
{
game.StartNewTransitionScreen();
for (int i = 0; i < game.Components.Count; i++)
{
if (game.Components[i] is TransitionScreen) continue;
((GameComponent)game.Components[i]).Dispose();
i--;
}
}
static bool first_transition = true;
public static void StartNewTransitionScreen(this Game game)
{
TransitionScreen transition = null;
if (first_transition != true)
transition = new TransitionScreen(game);
if (first_transition != true)
{
game.Components.Add(transition);
transition.OnTransitionStart += () =>
{
for (int i = 0; i < game.Components.Count; i++)
{
GameComponent gc = (GameComponent)game.Components[i];
if (gc != transition)
gc.Enabled = false;
}
};
transition.OnTransitionEnd += () =>
{
for (int i = 0; i < game.Components.Count; i++)
{
GameComponent gc = (GameComponent)game.Components[i];
if (gc != transition)
gc.Enabled = true;
else
{
game.Components.RemoveAt(i);
i--;
}
}
};
}
first_transition = false;
}
}
}