-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceManager.cs
185 lines (157 loc) · 6.32 KB
/
ResourceManager.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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using SFML.Audio;
using SFML.Graphics;
using Newtonsoft.Json;
using Match3.Animation;
namespace Match3
{
public class ResourceManager
{
#region Singleton
private static object syncRoot = new object();
private static ResourceManager instance;
public static ResourceManager Instance
{
get
{
if (instance == null) {
lock (syncRoot) {
if (instance == null) {
instance = new ResourceManager();
}
}
}
return instance;
}
}
#endregion
#region Fields
private Resources resources;
#endregion
#region Properties
public static List<string> Tiles => Instance.resources.Tiles;
public static List<Resources.BossResource> Bosses => Instance.resources.Bosses;
#endregion
private ResourceManager()
{
resources = new Resources();
}
#region Utils
public static void LoadResources(string configPath)
{
if (!File.Exists(configPath)) {
throw new InvalidOperationException($"{configPath} not found");
}
var json = File.ReadAllText(configPath);
Instance.resources = JsonConvert.DeserializeObject<Resources>(json);
}
public static Texture LoadTexture(string name, string path = null)
{
name = name.ToLower();
if (!Instance.resources.Textures.ContainsKey(name)) {
if (path == null) {
throw new InvalidOperationException("Texture isn't loaded, path is null");
}
var texture = SFML.Loaders.AutoTexture(path);
Instance.resources.Textures.Add(name, texture);
return texture;
}
return Instance.resources.Textures[name];
}
public static Music LoadMusic(string name)
{
name = name.ToLower();
if (!Instance.resources.Music.ContainsKey(name)) {
var music = SFML.Loaders.AutoMusic($"Assets/Music/{name}");
Instance.resources.Music.Add(name, music);
return music;
}
return Instance.resources.Music[name];
}
public static Sound LoadSound(string name)
{
name = name.ToLower();
if (!Instance.resources.Sounds.ContainsKey(name)) {
var buffer = SFML.Loaders.AutoSound($"Assets/SFX/{name}");
Instance.resources.Sounds.Add(name, buffer);
return new Sound(buffer);
}
return new Sound(Instance.resources.Sounds[name]);
}
public static Shader LoadShader(string name)
{
name = name.ToLower();
if (!Instance.resources.Shaders.ContainsKey(name)) {
var shader = new Shader(null, null, $"Assets/Shaders/{name}.frag");
Instance.resources.Shaders.Add(name, shader);
return shader;
}
return Instance.resources.Shaders[name];
}
public static AnimatedSprite LoadSprite(string name)
{
if (!Instance.resources.Sprites.ContainsKey(name)) {
throw new InvalidOperationException($"Sprite {name} isn't preloaded to call LoadSprite");
}
var resource = Instance.resources.Sprites[name];
var sheetName = resource.Spritesheet;
if (!Instance.resources.Spritesheets.ContainsKey(sheetName)) {
throw new InvalidOperationException($"Spritesheet {sheetName} isn't preloaded to call LoadSprite");
}
var sheet = Instance.resources.Spritesheets[sheetName];
var anims = Instance.resources.Animations;
var animations = resource.Animations.ToDictionary(
key => key,
key => new Anim(resource.GetFilteredFrames(anims[key].Frames, sheet.Width), anims[key].Delay)
);
return new AnimatedSprite(sheet, animations);
}
public static Spritesheet GetSpritesheetByTileId(int id)
{
var sprite = Instance.resources.Sprites[Tiles[id]];
return Instance.resources.Spritesheets[sprite.Spritesheet];
}
public class Resources
{
public Dictionary<string, SoundBuffer> Sounds = new Dictionary<string, SoundBuffer>();
public Dictionary<string, Music> Music = new Dictionary<string, Music>();
public Dictionary<string, Texture> Textures = new Dictionary<string, Texture>();
public Dictionary<string, Spritesheet> Spritesheets = new Dictionary<string, Spritesheet>();
public Dictionary<string, SpriteResource> Sprites = new Dictionary<string, SpriteResource>();
public Dictionary<string, AnimResource> Animations = new Dictionary<string, AnimResource>();
public Dictionary<string, Shader> Shaders = new Dictionary<string, Shader>();
public List<BossResource> Bosses = new List<BossResource>();
public List<string> Tiles = new List<string>();
public class SpriteResource
{
public List<string> Animations;
public string Spritesheet;
public int OffsetX = 0;
public int OffsetY = 0;
public int Width = 0;
public int[] GetFilteredFrames(int[] frames, int sheetWidth)
{
return frames.Select(frame => GetRealFrame(frame, sheetWidth)).ToArray();
}
public int GetRealFrame(int frame, int sheetWidth)
{
return OffsetX + frame % Width + (OffsetY + frame / Width) * sheetWidth;
}
}
public class AnimResource
{
public int[] Frames;
public float Delay;
}
public class BossResource
{
public string sprite;
public float health;
}
}
#endregion
}
}