-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
134 lines (115 loc) · 4.33 KB
/
Game.cs
File metadata and controls
134 lines (115 loc) · 4.33 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rift.ModernRift.Core
{
public class Game
{
public Configuration configuration;
private List<Command> commands;
public List<Command> Commands
{
get { return commands; }
}
private bool running = false;
/// <summary>
/// Initializes a new Game
/// </summary>
public Game()
{
commands = new List<Command>() { new ClearCommand(), new HelpCommand(), new QuitCommand() };
}
/// <summary>
/// Adds a command to the list of commands
/// </summary>
public void AddCommand(Command command)
{
commands.Add(command);
}
/// <summary>
/// Removes a command from the list of commands
/// </summary>
public void RemoveCommand(Command command)
{
if (command.GetType() != typeof(QuitCommand))
{
commands.Remove(command);
}
else
{
throw new ProtectedCommandException("You cannot remove the QuitCommand from the list of commands");
}
}
#region Runtime Methods
/// <summary>
/// Backend initialization method, called by the game engine. You should never need to reference this method.
/// </summary>
public void _Initialize()
{
Initialize();
if (configuration == null) {
try {
configuration = new Configuration.ConfigurationBuilder().FromConfigFile("configuration.json").Build();
} catch {
try {
configuration = new Configuration.ConfigurationBuilder().FromConfigFile("config.json").Build();
} catch {
try {
configuration = new Configuration.ConfigurationBuilder().FromConfigFile("cf.json").Build();
} catch {
Engine.PrintSystemError("Could not automatically find configuraton file.");
}
}
}
}
}
/// <summary>
/// Override this method to run code when the game is being initialized. Do not directly call this method, instead use Engine.Initialize().
/// </summary>
public virtual void Initialize() { }
/// <summary>
/// Backend start method, called by the game engine. You should never need to reference this method.
/// </summary>
public void _Start()
{
running = true;
Start();
// Add the prompt directive to the messagehandler
MessageHandler.AddDirective(new InlineConsoleDirective(() => { return configuration.Prompt; }, true), "update");
while (running)
{
MessageHandler.SendMessage("update");
Update();
string command = Console.ReadLine();
foreach(Command c in commands)
{
c.HandlePrompt(command.ToLower().Trim());
}
MessageHandler.SendMessages();
}
}
/// <summary>
/// Override this method to run code when the game is being started. Do not directly call this method, instead use Engine.Start().
/// </summary>
public virtual void Start() { }
/// <summary>
/// This is the main game loop. It will be called once per loop iteration, or "tick". In command-based games, it is called every time a command is entered. The command can be referenced from inside the method.
/// </summary>
public virtual void Update() { }
/// <summary>
/// Backend stop method, called by the game engine. You should never need to reference this method.
/// </summary>
public void _Stop()
{
Stop();
running = false;
}
/// <summary>
/// Override this method to run code when the game is being stopped. Do not directly call this method, instead use Engine.Stop().
/// </summary>
public virtual void Stop() { }
#endregion
}
}