Skip to content

Commit

Permalink
chore: nez init
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Oct 20, 2023
1 parent fd2cc80 commit 0695fde
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 120 deletions.
14 changes: 14 additions & 0 deletions MonoGame.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Systems.Debugging", "src\Li
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features.Debugging", "src\Libs\Features.Debugging\Features.Debugging.csproj", "{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nez.MG38.Persistence", "src\Libs\External\Nez\Nez.Persistence\Nez.MG38.Persistence.csproj", "{49086DC7-94D4-4715-AEFD-D685D0473FA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nez.MG38", "src\Libs\External\Nez\Nez.Portable\Nez.MG38.csproj", "{439580AE-4535-4BB8-A706-FEB2A6FF783C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +86,14 @@ Global
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Release|Any CPU.Build.0 = Release|Any CPU
{49086DC7-94D4-4715-AEFD-D685D0473FA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49086DC7-94D4-4715-AEFD-D685D0473FA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49086DC7-94D4-4715-AEFD-D685D0473FA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49086DC7-94D4-4715-AEFD-D685D0473FA8}.Release|Any CPU.Build.0 = Release|Any CPU
{439580AE-4535-4BB8-A706-FEB2A6FF783C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{439580AE-4535-4BB8-A706-FEB2A6FF783C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{439580AE-4535-4BB8-A706-FEB2A6FF783C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{439580AE-4535-4BB8-A706-FEB2A6FF783C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5E3B6238-A92E-4703-8527-1C2410D7906A} = {F5258F7E-B0BA-466D-8CF8-4DAB84722BE3}
Expand All @@ -98,5 +110,7 @@ Global
{8232C7AA-E217-465B-95A3-C42D1000FA3B} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{7AD01EBF-370B-4F35-9AEC-172A889D643F} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{439580AE-4535-4BB8-A706-FEB2A6FF783C} = {FE9CE932-6F98-4455-A80F-2A0CDB6DB46E}
{49086DC7-94D4-4715-AEFD-D685D0473FA8} = {FE9CE932-6F98-4455-A80F-2A0CDB6DB46E}
EndGlobalSection
EndGlobal
19 changes: 3 additions & 16 deletions src/Apps/GameDesktop/CompositionRoots/GameCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,11 @@ namespace GameDesktop.CompositionRoots;
internal class GameCompositionRoot : ICompositionRoot
{
private const float TargetFramesPerSecond = 120.0f;
private const bool IsMouseVisible = true;

public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register(factory =>
{
Game game = new(factory.GetInstance<ILogger>(),
factory.GetInstance<IServiceContainer>(),
TargetFramesPerSecond)
{
IsMouseVisible = IsMouseVisible, Content = { RootDirectory = AppVariable.ContentRootDirectory, }
};

// Hack. Resolving cycle dependency issue (fundamental architecture)
// Implicitly adds itself in the game services container.
new GraphicsDeviceManager(game);

return game;
});
serviceRegistry.Register(factory => new Game(factory.GetInstance<ILogger>(),
factory.GetInstance<IServiceContainer>(),
TargetFramesPerSecond));
}
}
3 changes: 3 additions & 0 deletions src/Apps/GameDesktop/Content/Content.mgcb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#---------------------------------- Content ---------------------------------#

#begin Nez/NezDefaultBMFont.xnb
/copy:Nez/NezDefaultBMFont.xnb

#begin SpriteSheets/Player.aseprite
/copy:SpriteSheets/Player.aseprite

Binary file not shown.
108 changes: 4 additions & 104 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
@@ -1,122 +1,22 @@
using System;
using Features;
using GameDesktop.CompositionRoots.Features;
using LightInject;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LightInject;
using Nez;
using Serilog;

namespace GameDesktop;

public class Game : Microsoft.Xna.Framework.Game
public class Game : Core
{
private readonly float _targetTimeStep;

private readonly ILogger _logger;
private readonly IServiceContainer _container;

private Entitas.Extended.Feature _rootFeature;
private SpriteBatch _spriteBatch;
private float _accumulatedTime;


public Game(ILogger logger, IServiceContainer container, float targetFramesPerSecond)
public Game(ILogger logger, IServiceContainer container, float targetFramesPerSecond) : base()
{
_logger = logger;
_container = container;
_targetTimeStep = 1 / targetFramesPerSecond;

_logger.ForContext<Game>().Verbose("ctor");
}

protected override void Initialize()
{
_logger.ForContext<Game>().Verbose($"Initialize(): start; available {GraphicsDevice}");
_logger.ForContext<Game>().Verbose("SpriteBatch initialization...");

_container.RegisterSingleton(_ => new SpriteBatch(GraphicsDevice));
_spriteBatch = _container.GetInstance<SpriteBatch>();
// GraphicsDeviceManager.PreferredBackBufferWidth = 640;
// GraphicsDeviceManager.PreferredBackBufferHeight = 480;
// GraphicsDeviceManager.ApplyChanges();

_logger.ForContext<Game>().Verbose("SpriteBatch initialized");

base.Initialize();

_logger.ForContext<Game>().Verbose("Initialize(): end");
}

protected override void LoadContent()
{
_logger.ForContext<Game>().Verbose("LoadContent(): start");

_container.RegisterFrom<RootFeatureCompositionRoot>();

_rootFeature = _container.GetInstance<RootFeature>();

// TODO: Logging with game flags (like LOG_MOVEMENT, etc)?
// todo: pass tru logger & log places
// TODO: Error handling
_rootFeature.Initialize();
_logger.ForContext<Game>().Verbose("LoadContent(): end");
}

protected override void BeginRun()
{
_logger.ForContext<Game>().Verbose("Beginning run...");

base.BeginRun();

_logger.ForContext<Game>().Verbose("Running");
}

protected override void EndRun()
{
_logger.ForContext<Game>().Verbose("Ending run...");

base.EndRun();

_logger.ForContext<Game>().Verbose("Ended");
}

// maybe fixed update is incorrect. needs review in the future
private void FixedUpdate(GameTime gameTime) => _rootFeature.FixedExecute(gameTime);

protected override void Update(GameTime gameTime)
{
_accumulatedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

while (_accumulatedTime >= _targetTimeStep)
{
FixedUpdate(gameTime);

_accumulatedTime -= _targetTimeStep;
}

base.Update(gameTime);
_rootFeature.Execute(gameTime);

LateUpdate(gameTime);
}

private void LateUpdate(GameTime gameTime) => _rootFeature.LateExecute(gameTime);

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

_rootFeature.Draw(gameTime, _spriteBatch);

base.Draw(gameTime);
}

protected override void Dispose(bool disposing)
{
_logger.ForContext<Game>().Verbose("Disposing...");

base.Dispose(disposing);

_logger.ForContext<Game>().Verbose("Disposed");
}
}
1 change: 1 addition & 0 deletions src/Apps/GameDesktop/GameDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Libs\Components\Components.csproj" />
<ProjectReference Include="..\..\Libs\External\Entitas.Extended\Entitas.Extended.csproj" />
<ProjectReference Include="..\..\Libs\External\Nez\Nez.Portable\Nez.MG38.csproj" />
<ProjectReference Include="..\..\Libs\Features\Features.csproj" />
<ProjectReference Include="..\..\Libs\Services\Services.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 0695fde

Please sign in to comment.