You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.2 KiB
134 lines
5.2 KiB
using System; |
|
using System.Diagnostics; |
|
using System.Globalization; |
|
using System.Numerics; |
|
using System.Threading; |
|
using gaemstone; |
|
using gaemstone.Bloxel; |
|
using gaemstone.Utility; |
|
using Silk.NET.Windowing; |
|
using static gaemstone.Bloxel.Components.CoreComponents; |
|
using static gaemstone.Client.Components.CameraComponents; |
|
using static gaemstone.Client.Components.RenderingComponents; |
|
using static gaemstone.Client.Components.ResourceComponents; |
|
using static gaemstone.Client.Systems.FreeCameraController; |
|
using static gaemstone.Client.Systems.Windowing; |
|
using static gaemstone.Components.TransformComponents; |
|
|
|
var culture = CultureInfo.InvariantCulture; |
|
Thread.CurrentThread.CurrentCulture = culture; |
|
CultureInfo.DefaultThreadCurrentCulture = culture; |
|
|
|
var universe = new Universe<Program>(); |
|
var world = universe.World; |
|
|
|
// TODO: Figure out a nice way to get rid of "compile errors" here. |
|
universe.Modules.Import<flecs.timer>(); |
|
universe.Modules.Import<flecs.script>(); |
|
universe.Modules.Import<flecs.rest>(); |
|
universe.Modules.Import<flecs.monitor>(); |
|
universe.Modules.Import<flecs.units>(); |
|
universe.Modules.Import<flecs.metrics>(); |
|
|
|
var window = Window.Create(WindowOptions.Default with { |
|
Title = "gæmstone - Immersion", |
|
Size = new(1280, 720), |
|
PreferredDepthBufferBits = 24, |
|
}); |
|
window.Initialize(); |
|
window.Center(); |
|
|
|
// universe.Modules.Register<Immersion.ObserverTest>(); |
|
// universe.Modules.Register<Immersion.ManagedComponentTest>(); |
|
|
|
universe.Modules.Register<gaemstone.Client.Systems.Windowing>(); |
|
universe.Modules.Register<gaemstone.Components.TransformComponents>(); |
|
|
|
universe.Modules.Register<gaemstone.Client.Components.RenderingComponents>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.Renderer>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.ImGuiManager>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.ImGuiDemoWindow>(); |
|
|
|
universe.Modules.Register<gaemstone.Client.Components.ResourceComponents>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.TextureManager>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.MeshManager>(); |
|
|
|
universe.Modules.Register<gaemstone.Client.Components.InputComponents>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.InputManager>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.ImGuiInputDebug>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.EntityInspector>(); |
|
|
|
universe.Modules.Register<gaemstone.Client.Components.CameraComponents>(); |
|
universe.Modules.Register<gaemstone.Client.Systems.FreeCameraController>(); |
|
|
|
using (var disabledModules = world.Filter(new("ModuleInfo, Disabled"))) |
|
foreach (var module in disabledModules.Iter().GetAllEntities()) |
|
throw new InvalidOperationException($"Module '{module.Path}' is not ednbled"); |
|
|
|
// Initialize Canvas and GameWindow singletons with actual values. |
|
world.Entity<Canvas>().Set(new Canvas(Silk.NET.OpenGL.ContextSourceExtensions.CreateOpenGL(window))); |
|
world.Entity<GameWindow>().Set(new GameWindow(window)); |
|
|
|
world.New("MainCamera") |
|
.Set(Camera.Default3D) |
|
.Set((GlobalTransform)Matrix4x4.CreateTranslation(0.0F, 2.0F, 0.0F)) |
|
.Set(new CameraController { MouseSensitivity = 12.0F }) |
|
.Build(); |
|
|
|
var heartMesh = world.New("/Immersion/Resources/heart.glb").Add<Mesh>().Build(); |
|
var swordMesh = world.New("/Immersion/Resources/sword.glb").Add<Mesh>().Build(); |
|
|
|
var entities = world.New("Entities").Build(); |
|
var rnd = new Random(); |
|
for (var x = -12; x <= 12; x++) |
|
for (var z = -12; z <= 12; z++) { |
|
var position = Matrix4x4.CreateTranslation(x * 2, 0.0F, z * 2); |
|
var rotation = Matrix4x4.CreateRotationY(rnd.NextFloat(MathF.PI * 2)); |
|
var (type, mesh) = rnd.Pick(("Heart", heartMesh), ("Sword", swordMesh)); |
|
entities.NewChild() |
|
.Set((GlobalTransform)(rotation * position)) |
|
.Add<Mesh>(mesh) |
|
.Build(); |
|
} |
|
|
|
universe.Modules.Register<gaemstone.Bloxel.Components.CoreComponents>(); |
|
universe.Modules.Register<gaemstone.Bloxel.Systems.BasicWorldGenerator>(); |
|
universe.Modules.Register<gaemstone.Bloxel.Client.Systems.ChunkMeshGenerator>(); |
|
|
|
var texture = world.New("/Immersion/Resources/terrain.png").Add<Texture>().Build(); |
|
|
|
var stone = world.New("Stone").Set(TextureCoords4.FromGrid(4, 4, 1, 0)).Build(); |
|
var dirt = world.New("Dirt" ).Set(TextureCoords4.FromGrid(4, 4, 2, 0)).Build(); |
|
var grass = world.New("Grass").Set(TextureCoords4.FromGrid(4, 4, 3, 0)).Build(); |
|
|
|
var chunks = world.New("Chunks").Build(); |
|
var sizeH = 4; var sizeY = 2; |
|
for (var cx = -sizeH; cx < sizeH; cx++) |
|
for (var cy = -sizeY; cy < sizeY; cy++) |
|
for (var cz = -sizeH; cz < sizeH; cz++) { |
|
var pos = new ChunkPos(cx, cy - 2, cz); |
|
var storage = new ChunkStoreBlocks(); |
|
chunks.NewChild() |
|
.Set((GlobalTransform)Matrix4x4.CreateTranslation(pos.GetOrigin())) |
|
.Set(new Chunk(pos)) |
|
.Set(storage) |
|
.Add<Texture>(texture) |
|
.Build(); |
|
} |
|
|
|
// universe.Modules.Register<ObserverTest>(); |
|
|
|
var stopwatch = Stopwatch.StartNew(); |
|
var minFrameTime = TimeSpan.FromSeconds(1) / 60; |
|
window.Run(() => { |
|
var delta = stopwatch.Elapsed; |
|
stopwatch.Restart(); |
|
|
|
if (!world.Progress(delta)) |
|
window.Close(); |
|
|
|
var requiredTime = stopwatch.Elapsed; |
|
while (stopwatch.Elapsed < minFrameTime) Thread.Sleep(0); |
|
var totalTime = stopwatch.Elapsed; |
|
// Console.WriteLine($"Frame time: req={requiredTime.TotalMilliseconds:F2}ms, total={totalTime.TotalMilliseconds:F2}ms"); |
|
});
|
|
|