.
This commit is contained in:
@@ -22,7 +22,7 @@ namespace Engine;
|
||||
|
||||
public sealed class Engine
|
||||
{
|
||||
public static Engine Instance { get; private set; } = null!;
|
||||
internal static Engine Instance { get; private set; } = null!;
|
||||
|
||||
public Renderer Renderer { get; }
|
||||
public SceneManager SceneManager { get; } = new();
|
||||
@@ -61,12 +61,16 @@ public sealed class Engine
|
||||
|
||||
internal ResourceManager EngineResourceManager => _engineResourceManager;
|
||||
public IResourceManager AssetResourceManager => _assetResourceManager;
|
||||
|
||||
public string DataFolder { get; }
|
||||
|
||||
private readonly ResourceManager _engineResourceManager;
|
||||
private readonly ResourceManager _assetResourceManager;
|
||||
|
||||
private Thread? _updateThread;
|
||||
|
||||
public Engine(int parWidth, int parHeight, bool parHeadless, string parTitle, string parAssetFolder,
|
||||
string parDataFolder,
|
||||
ILogger parLogger)
|
||||
{
|
||||
if (Instance != null)
|
||||
@@ -95,6 +99,10 @@ public sealed class Engine
|
||||
|
||||
_logger.Information("Created asset resource manager in {AssetFolder}", parAssetFolder);
|
||||
|
||||
DataFolder = parDataFolder;
|
||||
|
||||
_logger.Information("Using data folder {DataFolder}", parDataFolder);
|
||||
|
||||
Renderer = new Renderer(this, parWidth, parHeight, settings);
|
||||
Window = new Window(this, Renderer.NativeWindow, parHeadless);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ public sealed class EngineBuilder
|
||||
private bool _headless;
|
||||
private int _width = 1;
|
||||
private int _height = 1;
|
||||
private string _assetFolder = "./";
|
||||
private string _assetFolder = "./asset";
|
||||
private string _dataFolder = "./data";
|
||||
|
||||
private Func<Engine, IPresenter>? _presenterFunc;
|
||||
private Func<Engine, IInputHandler>? _inputHandlerFunc;
|
||||
@@ -58,6 +59,12 @@ public sealed class EngineBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
public EngineBuilder DataFolder(string parDataFolder)
|
||||
{
|
||||
_dataFolder = parDataFolder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EngineBuilder Presenter(Func<Engine, IPresenter> parPresenterFunc)
|
||||
{
|
||||
_presenterFunc = parPresenterFunc;
|
||||
@@ -101,7 +108,7 @@ public sealed class EngineBuilder
|
||||
public Engine Build()
|
||||
{
|
||||
var logger = BuildLogger();
|
||||
var engine = new Engine(_width, _height, _headless, _title, _assetFolder, logger);
|
||||
var engine = new Engine(_width, _height, _headless, _title, _assetFolder, _dataFolder, logger);
|
||||
|
||||
var presenter = _presenterFunc?.Invoke(engine);
|
||||
if (presenter != null)
|
||||
|
||||
@@ -24,7 +24,7 @@ public class RenderLayer : IComparable<RenderLayer>
|
||||
|
||||
public int CompareTo(RenderLayer? parOther)
|
||||
{
|
||||
return _order.CompareTo(parOther?._order);
|
||||
return parOther == null ? 1 : _order.CompareTo(parOther._order);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
|
||||
@@ -62,4 +62,61 @@ public enum KeyboardButtonCode
|
||||
PageDown,
|
||||
|
||||
TotalCount = PageDown + 1
|
||||
}
|
||||
|
||||
public static class KeyboardButtonCodeHelper
|
||||
{
|
||||
public static List<KeyboardButtonCode> GetAllPrintableKeys() =>
|
||||
Enum.GetValues<KeyboardButtonCode>().Where(parX => parX.IsPrintableKey()).ToList();
|
||||
|
||||
public static bool IsPrintableKey(this KeyboardButtonCode parKey) =>
|
||||
parKey is >= KeyboardButtonCode.A and <= KeyboardButtonCode.Z
|
||||
or >= KeyboardButtonCode.D1 and <= KeyboardButtonCode.D0 or KeyboardButtonCode.Space;
|
||||
|
||||
public static char GetChar(this KeyboardButtonCode parKey)
|
||||
{
|
||||
return parKey switch
|
||||
{
|
||||
KeyboardButtonCode.A => 'A',
|
||||
KeyboardButtonCode.B => 'B',
|
||||
KeyboardButtonCode.C => 'C',
|
||||
KeyboardButtonCode.D => 'D',
|
||||
KeyboardButtonCode.E => 'E',
|
||||
KeyboardButtonCode.F => 'F',
|
||||
KeyboardButtonCode.G => 'G',
|
||||
KeyboardButtonCode.H => 'H',
|
||||
KeyboardButtonCode.I => 'I',
|
||||
KeyboardButtonCode.J => 'J',
|
||||
KeyboardButtonCode.K => 'K',
|
||||
KeyboardButtonCode.L => 'L',
|
||||
KeyboardButtonCode.M => 'M',
|
||||
KeyboardButtonCode.N => 'N',
|
||||
KeyboardButtonCode.O => 'O',
|
||||
KeyboardButtonCode.P => 'P',
|
||||
KeyboardButtonCode.Q => 'Q',
|
||||
KeyboardButtonCode.R => 'R',
|
||||
KeyboardButtonCode.S => 'S',
|
||||
KeyboardButtonCode.T => 'T',
|
||||
KeyboardButtonCode.U => 'U',
|
||||
KeyboardButtonCode.V => 'V',
|
||||
KeyboardButtonCode.W => 'W',
|
||||
KeyboardButtonCode.X => 'X',
|
||||
KeyboardButtonCode.Y => 'Y',
|
||||
KeyboardButtonCode.Z => 'Z',
|
||||
|
||||
KeyboardButtonCode.D1 => '1',
|
||||
KeyboardButtonCode.D2 => '2',
|
||||
KeyboardButtonCode.D3 => '3',
|
||||
KeyboardButtonCode.D4 => '4',
|
||||
KeyboardButtonCode.D5 => '5',
|
||||
KeyboardButtonCode.D6 => '6',
|
||||
KeyboardButtonCode.D7 => '7',
|
||||
KeyboardButtonCode.D8 => '8',
|
||||
KeyboardButtonCode.D9 => '9',
|
||||
KeyboardButtonCode.D0 => '0',
|
||||
|
||||
KeyboardButtonCode.Space => ' ',
|
||||
_ => '\0'
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public abstract class Component : IUpdate, IRender
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void PreUpdate()
|
||||
public virtual void PreUpdate(double parDeltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public sealed class GameObject : IUpdate, IRender
|
||||
|
||||
public Transform Transform { get; }
|
||||
|
||||
public Scene? Scene { get; set; }
|
||||
internal Scene? Scene { get; set; }
|
||||
|
||||
private readonly Queue<Action> _componentActions = new();
|
||||
|
||||
@@ -47,14 +47,14 @@ public sealed class GameObject : IUpdate, IRender
|
||||
Transform = GetComponent<Transform>()!;
|
||||
}
|
||||
|
||||
public void PreUpdate()
|
||||
public void PreUpdate(double parDeltaTime)
|
||||
{
|
||||
ProcessAddedComponents();
|
||||
ProcessRemovedComponents();
|
||||
|
||||
foreach (var component in _components)
|
||||
{
|
||||
component.PreUpdate();
|
||||
component.PreUpdate(parDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,9 @@ public class Scene : IUpdate, IRender
|
||||
return Hierarchy.Objects
|
||||
.Where(parGameObject => !parOnlyEnabled || parGameObject.IsEnabled)
|
||||
.Select(parGameObject => parGameObject.GetComponent<T>())
|
||||
.Where(parComponent => parComponent != null).ToList()!;
|
||||
.Where(parComponent => parComponent != null)
|
||||
.Distinct()
|
||||
.ToList()!;
|
||||
}
|
||||
|
||||
public T? FindFirstComponent<T>() where T : Component.Component
|
||||
@@ -61,7 +63,7 @@ public class Scene : IUpdate, IRender
|
||||
|
||||
foreach (var gameObject in hierarchyObjects)
|
||||
{
|
||||
gameObject.PreUpdate();
|
||||
gameObject.PreUpdate(parDeltaTime * TimeScale);
|
||||
}
|
||||
|
||||
foreach (var gameObject in hierarchyObjects)
|
||||
|
||||
24
Engine/src/Util/EngineUtil.cs
Normal file
24
Engine/src/Util/EngineUtil.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Engine.Input;
|
||||
using Engine.Resource;
|
||||
using Engine.Scene;
|
||||
|
||||
namespace Engine.Util;
|
||||
|
||||
public static class EngineUtil
|
||||
{
|
||||
public static IInputHandler InputHandler => Engine.Instance.InputHandler!;
|
||||
public static SceneManager SceneManager => Engine.Instance.SceneManager;
|
||||
public static IResourceManager AssetResourceManager => Engine.Instance.AssetResourceManager;
|
||||
public static string DataFolder => Engine.Instance.DataFolder;
|
||||
|
||||
public static void CreateObject(GameObject parGameObject)
|
||||
{
|
||||
var scene = Engine.Instance.SceneManager.CurrentScene!;
|
||||
scene.Add(parGameObject);
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
Engine.Instance.Close();
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,9 @@ public class TickableTimer
|
||||
if (value > TotalTime)
|
||||
value = TotalTime;
|
||||
|
||||
if (value == _currentTime)
|
||||
return;
|
||||
|
||||
_currentTime = value;
|
||||
OnUpdate?.Invoke(value);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user