-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameExecution.cs
165 lines (133 loc) · 4.86 KB
/
GameExecution.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 AsLegacy.Characters;
using AsLegacy.GUI;
using AsLegacy.GUI.Elements;
using AsLegacy.Progression;
using SadConsole.Components;
using SadConsole.Themes;
using System;
using Game = SadConsole.Game;
namespace AsLegacy;
/// <summary>
/// Handles the set up and updating of game systems.
/// </summary>
[Behavior]
[Dependency<ConsoleCollection>(Binding.Unique, Fulfillment.Existing, Consoles)]
[Dependency<Display>(Binding.Unique, Fulfillment.Existing, Display)]
[Dependency<GameState>(Binding.Unique, Fulfillment.SelfCreated, State)]
public class GameExecution : UpdateConsoleComponent
{
private const string Consoles = "consoles";
private const string Display = "display";
private const string State = "state";
private static readonly string LocalFontsFile = System.IO.Path.Combine(
"Resources",
"Fonts",
"AsLegacy.font");
#region Remove through CP refactor
/// <summary>
/// The number of legacy points required for a game to be won.
/// </summary>
public const int Goal = 25;
/// <summary>
/// The current Character being focused on by the game player.
/// While playing, this is the World Player, when dead and in 'viewer mode' this may
/// be any other living Character.
/// </summary>
public static World.Character Focus { get => Player ?? ObservedCharacter; }
/// <summary>
/// The character for the player to observer while they have no character of their own.
/// </summary>
/// <remarks>
/// Temporarily exposed during CP refactor.
/// </remarks>
public static World.Character ObservedCharacter = null;
/// <summary>
/// Specifies whether the game has a current Player Character.
/// </summary>
public static bool HasPlayer => Player != null;
/// <summary>
/// The Player Character of the game.
/// This is null if the Player is in 'viewer mode'.
/// </summary>
public static Player Player => Player.Character;
/// <summary>
/// Selects the provided Character, either as the target of
/// the Player Character or the focus Character of the game.
/// </summary>
/// <param name="character">The Character being selected.</param>
public static void SelectCharacter(World.Character character)
{
if (Player != null)
Player.Target = character;
else
ObservedCharacter = character;
}
#endregion
/// <summary>
/// Initializes the game with SadConsole, which executes within an embedded loop
/// after <see cref="Game.OnInitialize"/>.
/// </summary>
public static void Initialize()
{
ConsoleCollection consoles = new();
Display display = new();
int width = display.Width;
int height = display.Height;
ContextState<Console> primaryConsole = consoles.PrimaryConsole;
Game.Create(width, height);
Game.OnInitialize = () =>
{
Library.Default.SetControlTheme(typeof(ClearingTextBox), new TextBoxTheme());
SadConsole.Global.LoadFont(LocalFontsFile);
SadConsole.Global.FontDefault = SadConsole.Global.Fonts["AsLegacy"]
.GetFont(SadConsole.Font.FontSizes.One);
Console console = new(width, height);
primaryConsole.Value = console;
SadConsole.Global.CurrentScreen = console;
App.Initialize();
Contextualize(consoles);
Contextualize(display);
};
}
public GameExecution(out GameState state)
{
ConsoleCollection consoles = GetContext<ConsoleCollection>();
state = new();
consoles.PrimaryConsole.Value.Components.Add(this);
}
/// <summary>
/// Exits the game if the state specifies that it should no longer continue running.
/// </summary>
[Operation]
[OnChange(State, nameof(GameState.ContinueRunning))]
public void CloseGame(GameState state)
{
//if (!state.ContinueRunning)
// TODO :: Teardown and close the game.
}
// TODO :: Phase out most of the below operation through CP refactor.
/// <summary>
/// Updates whether the game is in its play stage.
/// </summary>
[Operation]
[OnChange(State, nameof(GameState.CurrentStage))]
public void EvaluateCurrentStage(GameState state)
{
_isOnPlayStage = state.CurrentStage == GameStageMap.Stage.Play;
}
private bool _isOnPlayStage = false;
/// <inheritdoc/>
public override void Update(Console console, TimeSpan delta)
{
App.Update();
// TODO :: Phase out the below through CP refactor.
// World updating should be handled as part of the app update.
if (_isOnPlayStage)
{
if (World.HighestRankedCharacter.Legacy < Goal)
World.Update(delta.Milliseconds);
else
Contextualize(new GameEndMessage());
}
}
}