-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKGame.cs
288 lines (251 loc) · 10.6 KB
/
KGame.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using KEngine.Components;
using KEngine.Components.Colliders;
using KEngine.Components.DrawableComponents;
using KEngine.Drawing;
using KEngine.Extensions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace KEngine {
public abstract class KGame : Game {
public static KGame Instance { private set; get; }
protected GraphicsDeviceManager graphics;
protected SpriteBatch spriteBatch;
static SceneLoader sceneToLoad = null;
static int lastScene = 0;
List<GameObject> gameObjects = new();
List<Component> components = new();
Dictionary<string, List<DrawableComponent>> drawableComponents = new();
internal List<Collider> colliders = new();
Queue<Component> componentsToInitialize = new();
LinkedList<Component> componentsToStart = new();
protected bool debugDrawGameObjectsPosition = false;
protected bool debugDrawColliders = false;
protected string[] drawingLayers = new string[] {
"Default"
};
private Dictionary<string, DrawingLayerSettings> drawingLayerSettings = new();
public delegate void SceneLoader();
private static OrderedDictionary scenes = new();
protected void SetDrawingLayersSettings(DrawingLayerSettings defaultSettings, Dictionary<string, DrawingLayerSettings> layersSettings) {
foreach (var layer in drawingLayers) {
if (!layersSettings.TryGetValue(layer, out var settings)) {
settings = defaultSettings;
}
drawingLayerSettings[layer] = new DrawingLayerSettings(
settings.SortMode ?? defaultSettings.SortMode ?? SpriteSortMode.Deferred,
settings.BlendState ?? defaultSettings.BlendState,
settings.SamplerState ?? defaultSettings.SamplerState,
settings.DepthStencilState ?? defaultSettings.DepthStencilState,
settings.RasterizerState ?? defaultSettings.RasterizerState,
settings.Effect ?? defaultSettings.Effect
);
}
}
protected KGame()
{
Instance = this;
graphics = new GraphicsDeviceManager(this);
}
public static Vector2 GetScreenSize() {
return new Vector2(Instance.graphics.PreferredBackBufferWidth, Instance.graphics.PreferredBackBufferHeight);
}
static Dictionary<string, object> assets = new();
static Dictionary<string, Sprite> sprites = new();
public static T GetContent<T>(string assetName) {
if (assets.TryGetValue(assetName, out var asset)) {
return (T)asset;
}
assets[assetName] = Instance.Content.Load<T>(assetName);
return (T)assets[assetName];
}
protected static void InitSprites(params Sprite[] sprites) {
foreach (var sprite in sprites)
{
KGame.sprites[sprite.SpriteName] = sprite;
}
}
public static Sprite GetSprite(string spriteName) {
if (sprites.TryGetValue(spriteName, out var sprite))
return sprite;
sprites[spriteName] = new(spriteName, true, Vector2.Zero, Vector2.One);
return sprites[spriteName];
}
protected override void Initialize() {
base.Initialize();
foreach (var layer in drawingLayers)
{
drawableComponents[layer] = new();
}
CreateMainCamera().Load();
LoadScene(0);
}
protected virtual GameObject CreateMainCamera() {
return new GameObject(
"Main Camera",
components: new[] {
Camera.CreateMainCamera()
},
dontDestroyOnLoad: true
);
}
public static void LoadScene(int index) {
lastScene = index;
sceneToLoad = (SceneLoader)scenes[index];
}
public static void LoadScene(string name) {
lastScene = scenes.IndexOf(name);
sceneToLoad = (SceneLoader)scenes[name];
}
protected void SetScenes(params (string, SceneLoader)[] scenes) {
foreach (var scene in scenes)
{
KGame.scenes[scene.Item1] = scene.Item2;
}
}
private void LoadScene(SceneLoader sceneLoader) {
int i = 0;
while (i < gameObjects.Count) {
if (gameObjects[i].CanDestroy) {
gameObjects[i].Destroy();
} else {
i++;
}
}
gameObjects.RemoveAll(go => !go.DontDestroyOnLoad);
sceneLoader();
}
public static void ReloadScene() {
sceneToLoad = (SceneLoader)scenes[lastScene];
}
protected override void LoadContent() {
base.LoadContent();
spriteBatch = new SpriteBatch(GraphicsDevice);
SetDrawingLayersSettings(new DrawingLayerSettings(), new());
}
internal void LoadGameObject(GameObject gameObject) {
gameObjects.Add(gameObject);
foreach (var component in gameObject.components)
{
AddComponent(component);
}
foreach (var child in gameObject.Transform.children)
{
LoadGameObject(child.GameObject);
}
}
internal void AddComponent(Component component) {
for (int i = 0; i < components.Count; i++)
{
if (components[i].UpdateGroup > component.UpdateGroup) {
components.Insert(i, component);
return;
}
}
components.Add(component);
componentsToInitialize.Enqueue(component);
componentsToStart.AddLast(component);
}
internal void AddDrawableComponent(DrawableComponent component) {
drawableComponents[component.drawingLayer].Add(component);
}
internal void AddCollider(Collider collider) {
colliders.Add(collider);
}
internal void RemoveGameObject(GameObject gameObject) {
gameObjects.Remove(gameObject);
}
internal void RemoveComponent(Component component) {
components.Remove(component);
}
internal void RemoveDrawableComponent(DrawableComponent component) {
drawableComponents[component.drawingLayer].Remove(component);
}
internal void RemoveCollider(Collider collider) {
colliders.Remove(collider);
}
protected override void Update(GameTime gameTime) {
base.Update(gameTime);
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
while (componentsToInitialize.Count > 0)
{
componentsToInitialize.Dequeue().Initialize();
}
var node = componentsToStart.First;
while (node != null) {
var next = node.Next;
if (node.Value.IsActive) {
node.Value.Start();
componentsToStart.Remove(node);
}
node = next;
}
foreach (var component in components) {
if (component.IsActive)
component.Update(deltaTime);
}
for (int i = 0; i < colliders.Count; i++) {
if (!colliders[i].IsActive) continue;
for (int j = i+1; j < colliders.Count; j++) {
if (!colliders[j].IsActive) continue;
if (Collider.CheckCollision(colliders[i], colliders[j], out var hitInfo)) {
if (!hitInfo.colliderA.IsTrigger && !hitInfo.colliderB.IsTrigger) {
if (hitInfo.colliderA.IsStatic) {
hitInfo.colliderB.Transform.Position += hitInfo.direction * hitInfo.distance;
} else if (hitInfo.colliderB.IsStatic) {
hitInfo.colliderA.Transform.Position -= hitInfo.direction * hitInfo.distance;
} else {
hitInfo.colliderB.Transform.Position += hitInfo.direction * (hitInfo.distance * 0.5f);
hitInfo.colliderA.Transform.Position -= hitInfo.direction * (hitInfo.distance * 0.5f);
}
}
hitInfo.colliderB.CallOnCollision(hitInfo.colliderA, ref hitInfo);
hitInfo.direction = -hitInfo.direction;
hitInfo.colliderA.CallOnCollision(hitInfo.colliderB, ref hitInfo);
}
}
}
if (sceneToLoad != null) {
LoadScene(sceneToLoad);
sceneToLoad = null;
}
}
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
for (int i = 0; i < drawingLayers.Length; i++)
{
var layer = drawingLayers[i];
var settings = drawingLayerSettings[layer];
spriteBatch.Begin(settings.SortMode.Value, settings.BlendState, settings.SamplerState, settings.DepthStencilState, settings.RasterizerState, settings.Effect);
foreach (var component in drawableComponents[layer])
{
if (component.IsActive)
component.Draw(spriteBatch);
}
spriteBatch.End();
}
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
if (debugDrawGameObjectsPosition) {
foreach (var gameObject in gameObjects) {
spriteBatch.DrawPoint(Camera.MainCamera.WorldToScreen(gameObject.Transform.GlobalPosition), Color.Gray);
}
}
if (debugDrawColliders) {
foreach (var collider in colliders) {
collider.DebugDraw(spriteBatch);
}
}
spriteBatch.End();
}
protected record DrawingLayerSettings(
SpriteSortMode? SortMode = null,
BlendState BlendState = null,
SamplerState SamplerState = null,
DepthStencilState DepthStencilState = null,
RasterizerState RasterizerState = null,
Effect Effect = null
);
}
}