-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
116 lines (96 loc) · 3.25 KB
/
Copy pathGameManager.cs
File metadata and controls
116 lines (96 loc) · 3.25 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
using System;
using System.IO;
using System.Diagnostics;
using SFML.System;
using SFML.Window;
using SFML.Graphics;
using Newtonsoft.Json;
namespace Match3
{
public sealed class GameManager
{
#region Singleton
private static GameManager instance;
private static object syncRoot = new object();
public static GameManager Instance
{
get
{
if (instance == null) {
lock (syncRoot) {
if (instance == null) {
instance = new GameManager();
}
}
}
return instance;
}
}
#endregion
#region Properties
public static RenderWindow Window { get; private set; }
public static Random Rand { get; private set; }
public static int Score { get; set; }
public static bool IsDefeated { get; set; }
#endregion
private GameManager()
{
SFML.Portable.Activate();
Rand = new Random();
var settingsPath = "Config/settings.json";
if (File.Exists(settingsPath)) {
var json = File.ReadAllText(settingsPath);
JsonConvert.DeserializeObject<Settings>(json);
}
}
#region Callbacks
public void Start(string caption)
{
// Bootstraping
Window = new RenderWindow(new VideoMode(Settings.Width, Settings.Height), caption, Styles.Close);
Window.SetMouseCursorVisible(true);
Window.SetFramerateLimit(Settings.FrameRate);
// Attach to SFML events
Window.Closed += (_, e) => Window.Close();
Window.Resized += (_, e) => Window.Size = new Vector2u(Settings.Width, Settings.Height);
Window.MouseButtonPressed += (_, e) => RoomManager.CurrentRoom?.MouseDown(e);
Window.MouseButtonReleased += (_, e) => RoomManager.CurrentRoom?.MouseUp(e);
Window.KeyPressed += (_, e) => RoomManager.CurrentRoom?.KeyDown(e);
// Load resources, start the first room
ResourceManager.LoadResources("Config/resources.json");
RoomManager.Start();
// Game loop
float deltaTime = 0f;
Stopwatch timer = Stopwatch.StartNew();
while (Window.IsOpen) {
Update(deltaTime);
Draw();
deltaTime = (float) timer.Elapsed.TotalSeconds;
timer.Restart();
}
}
private void Window_Resized(object sender, SizeEventArgs e)
{
throw new NotImplementedException();
}
private void Update(float deltaTime)
{
Window.DispatchEvents();
RoomManager.CurrentRoom?.Update(deltaTime);
SoundManager.Instance.Update(deltaTime);
}
private void Draw()
{
Window.Clear(Color.Black);
RoomManager.CurrentRoom?.Draw();
Window.Display();
}
#endregion
#region Utils
public static float Random()
{
return (float) Rand.Next() / Int32.MaxValue;
}
#endregion
}
}