Skip to content

Commit

Permalink
refactor: update to logicblocks 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Dec 23, 2023
1 parent f3de12a commit 32b0f0f
Show file tree
Hide file tree
Showing 89 changed files with 354 additions and 419 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,5 @@ dotnet_diagnostic.RCS1181.severity = none
dotnet_diagnostic.IDE0046.severity = none
# Don't make me use expression bodies for methods
dotnet_diagnostic.IDE0022.severity = none
# Don't use collection shorhand.
dotnet_diagnostic.IDE0300.severity = none
6 changes: 4 additions & 2 deletions GameDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
<PackageReference Include="Chickensoft.SuperNodes.Types" Version="1.6.1" />
<PackageReference Include="Chickensoft.AutoInject" Version="1.5.0" PrivateAssets="all" />
<PackageReference Include="Chickensoft.PowerUps" Version="3.0.1-godot4.2.0-beta.5" PrivateAssets="all" />
<PackageReference Include="Chickensoft.LogicBlocks" Version="3.4.0" />
<PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="3.4.0" PrivateAssets="all" OutputItemType="analyzer" />
<!-- <PackageReference Include="Chickensoft.LogicBlocks" Version="3.4.0" /> -->
<!-- <PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="3.4.0" PrivateAssets="all" OutputItemType="analyzer" /> -->
<ProjectReference Include="/Users/joanna/Dropbox/GameDev/Chickensoft/LogicBlocks/Chickensoft.LogicBlocks/Chickensoft.LogicBlocks.csproj" />
<ProjectReference Include="/Users/joanna/Dropbox/GameDev/Chickensoft/LogicBlocks/Chickensoft.LogicBlocks.Generator/Chickensoft.LogicBlocks.Generator.csproj" PrivateAssets="all" OutputItemType="analyzer" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.1.1" PrivateAssets="all" OutputItemType="analyzer" />
<PackageReference Include="Chickensoft.GoDotCollections" Version="1.4.0" />
<PackageReference Include="Chickensoft.GodotNodeInterfaces" Version="2.1.0-godot4.2.0-beta.5 " />
Expand Down
9 changes: 4 additions & 5 deletions src/app/state/AppLogic.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ public partial class AppLogic {
public interface IState : IStateLogic { }

public partial record State : StateLogic, IState {
public State(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();
OnEnter<State>(
(previous) => appRepo.IsMouseCaptured.Sync += OnMouseCaptured
public State() {
OnAttach(
() => Get<IAppRepo>().IsMouseCaptured.Sync += OnMouseCaptured
);
OnExit<State>((next) => appRepo.IsMouseCaptured.Sync -= OnMouseCaptured);
OnDetach(() => Get<IAppRepo>().IsMouseCaptured.Sync -= OnMouseCaptured);
}

public void OnMouseCaptured(bool isMouseCaptured) =>
Expand Down
3 changes: 1 addition & 2 deletions src/app/state/AppLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public interface IAppLogic : ILogicBlock<AppLogic.IState> { }

[StateMachine]
public partial class AppLogic : LogicBlock<AppLogic.IState>, IAppLogic {
public override IState GetInitialState(IContext context)
=> new State.SplashScreen(context);
public override IState GetInitialState() => new State.SplashScreen();

public AppLogic(IAppRepo appRepo) {
Set(appRepo);
Expand Down
4 changes: 2 additions & 2 deletions src/app/state/states/LeavingMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record LeavingMenu : State, IGet<Input.FadeOutFinished> {
public LeavingMenu(IContext context) : base(context) {
public LeavingMenu() {
OnEnter<LeavingMenu>(
(previous) => Context.Output(new Output.FadeOut())
);
}

public IState On(Input.FadeOutFinished input) =>
new PlayingGame(Context);
new PlayingGame();
}
}
}
7 changes: 3 additions & 4 deletions src/app/state/states/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record MainMenu : State, IGet<Input.StartGame> {
public MainMenu(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();
public MainMenu() {
OnEnter<MainMenu>(
(previous) => {
appRepo.OnMainMenuEntered();
Get<IAppRepo>().OnMainMenuEntered();
Context.Output(new Output.LoadGame());
Context.Output(new Output.ShowMainMenu());
}
);
}

public IState On(Input.StartGame input) => new LeavingMenu(Context);
public IState On(Input.StartGame input) => new LeavingMenu();
}
}
}
6 changes: 2 additions & 4 deletions src/app/state/states/RestartingGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record RestartingGame : State, IGet<Input.FadeOutFinished> {
public RestartingGame(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();

public RestartingGame() {
OnEnter<RestartingGame>(
(previous) => Context.Output(new Output.FadeOut())
);
Expand All @@ -17,7 +15,7 @@ public RestartingGame(IContext context) : base(context) {
}

public IState On(Input.FadeOutFinished input) =>
new PlayingGame(Context);
new PlayingGame();
}
}
}
19 changes: 10 additions & 9 deletions src/app/state/states/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record SplashScreen : State, IGet<Input.FadeOutFinished> {
public SplashScreen(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();
public SplashScreen() {
OnEnter<SplashScreen>(
(previous) => {
Context.Output(new Output.ShowSplashScreen());
appRepo.SplashScreenSkipped += OnSplashScreenSkipped;
}
(previous) => Context.Output(new Output.ShowSplashScreen())
);
OnExit<SplashScreen>(
(next) => appRepo.SplashScreenSkipped -= OnSplashScreenSkipped

OnAttach(
() => Get<IAppRepo>().SplashScreenSkipped += OnSplashScreenSkipped
);

OnDetach(
() => Get<IAppRepo>().SplashScreenSkipped -= OnSplashScreenSkipped
);
}

public IState On(Input.FadeOutFinished input) => new MainMenu(Context);
public IState On(Input.FadeOutFinished input) => new MainMenu();

public void OnSplashScreenSkipped() =>
Context.Output(new Output.HideSplashScreen());
Expand Down
7 changes: 3 additions & 4 deletions src/app/state/states/in_game/GamePaused.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record GamePaused : InGame, IGet<Input.PauseButtonPressed> {
public GamePaused(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();
public GamePaused() {
OnEnter<GamePaused>(
(previous) => {
Context.Output(new Output.ShowPauseMenu());
appRepo.Pause();
Get<IAppRepo>().Pause();
}
);
OnExit<GamePaused>(
Expand All @@ -16,7 +15,7 @@ public GamePaused(IContext context) : base(context) {
}

public IState On(Input.PauseButtonPressed input)
=> new ResumingGame(Context);
=> new ResumingGame();
}
}
}
18 changes: 8 additions & 10 deletions src/app/state/states/in_game/InGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ public partial class AppLogic {
public partial record State {
public record InGame : State,
IGet<Input.GoToMainMenu>, IGet<Input.GameOver> {
public InGame(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();

public InGame() {
OnEnter<InGame>((previous) => {
appRepo.OnStartGame();
appRepo.GameEnded += OnGameOver;
Get<IAppRepo>().OnStartGame();
Context.Output(new Output.ShowGame());
});

OnExit<InGame>((next) => appRepo.GameEnded -= OnGameOver);
OnAttach(() => Get<IAppRepo>().GameEnded += OnGameOver);
OnDetach(() => Get<IAppRepo>().GameEnded -= OnGameOver);
}

public void OnGameOver(GameOverReason reason) {
Expand All @@ -27,14 +25,14 @@ public IState On(Input.GameOver input) {
appRepo.Pause();

return input.Reason switch {
GameOverReason.PlayerWon => new WonGame(Context),
GameOverReason.PlayerDied => new LostGame(Context),
GameOverReason.Exited => new MainMenu(Context),
GameOverReason.PlayerWon => new WonGame(),
GameOverReason.PlayerDied => new LostGame(),
GameOverReason.Exited => new MainMenu(),
_ => throw new InvalidOperationException()
};
}

public IState On(Input.GoToMainMenu input) => new LeavingGame(Context);
public IState On(Input.GoToMainMenu input) => new LeavingGame();
}
}
}
2 changes: 1 addition & 1 deletion src/app/state/states/in_game/LeavingGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record LeavingGame : InGame, IGet<Input.FadeOutFinished> {
public LeavingGame(IContext context) : base(context) {
public LeavingGame() {
OnEnter<LeavingGame>(
(previous) => Context.Output(new Output.FadeOut())
);
Expand Down
4 changes: 2 additions & 2 deletions src/app/state/states/in_game/LostGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record LostGame : InGame, IGet<Input.StartGame> {
public LostGame(IContext context) : base(context) {
public LostGame() {
OnEnter<LostGame>(
(previous) => Context.Output(new Output.ShowPlayerDied())
);
}

public IState On(Input.StartGame input) =>
new RestartingGame(Context);
new RestartingGame();
}
}
}
4 changes: 2 additions & 2 deletions src/app/state/states/in_game/PlayingGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record PlayingGame : InGame, IGet<Input.PauseButtonPressed> {
public PlayingGame(IContext context) : base(context) { }
public PlayingGame() { }

public IState On(Input.PauseButtonPressed input)
=> new GamePaused(Context);
=> new GamePaused();
}
}
}
7 changes: 3 additions & 4 deletions src/app/state/states/in_game/ResumingGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record ResumingGame : InGame, IGet<Input.PauseMenuTransitioned> {
public ResumingGame(IContext context) : base(context) {
var appRepo = Context.Get<IAppRepo>();
OnEnter<ResumingGame>((previous) => appRepo.Resume());
public ResumingGame() {
OnEnter<ResumingGame>((previous) => Get<IAppRepo>().Resume());
OnExit<ResumingGame>(
(next) => Context.Output(new Output.DisablePauseMenu())
);
}

public IState On(Input.PauseMenuTransitioned input) =>
new PlayingGame(Context);
new PlayingGame();
}
}
}
2 changes: 1 addition & 1 deletion src/app/state/states/in_game/WonGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace GameDemo;
public partial class AppLogic {
public partial record State {
public record WonGame : InGame {
public WonGame(IContext context) : base(context) {
public WonGame() {
OnEnter<WonGame>(
(previous) => Context.Output(new Output.ShowPlayerWon())
);
Expand Down
3 changes: 1 addition & 2 deletions src/coin/state/CoinLogic.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ namespace GameDemo;
public partial class CoinLogic {
public interface IState : IStateLogic { }

public abstract partial record State(IContext Context) :
StateLogic(Context), IState;
public abstract partial record State : StateLogic, IState;
}
3 changes: 1 addition & 2 deletions src/coin/state/CoinLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public interface ICoinLogic : ILogicBlock<CoinLogic.IState> { }

[StateMachine]
public partial class CoinLogic : LogicBlock<CoinLogic.IState>, ICoinLogic {
public override IState GetInitialState(IContext context) =>
new State.Idle(context);
public override IState GetInitialState() => new State.Idle();

public record Settings(double CollectionTimeInSeconds);

Expand Down
10 changes: 4 additions & 6 deletions src/coin/state/states/CoinLogic.State.Collecting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ public record Collecting : State, ICollecting, IGet<Input.PhysicsProcess> {
public ICoinCollector Target { get; }
private double _elapsedTime;

public Collecting(IContext context, ICoinCollector target) :
base(context) {
var coin = Context.Get<ICoin>();
var appRepo = Context.Get<IAppRepo>();

public Collecting(ICoinCollector target) {
Target = target;

OnEnter<Collecting>((previous) => appRepo.StartCoinCollection(coin));
OnEnter<Collecting>(
(previous) => Get<IAppRepo>().StartCoinCollection(Get<ICoin>())
);
}

public IState On(Input.PhysicsProcess input) {
Expand Down
6 changes: 2 additions & 4 deletions src/coin/state/states/CoinLogic.State.Idle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ public partial class CoinLogic {
public partial record State {
public interface IIdle : IState { }

public record Idle(
IContext Context
) : State(Context), IIdle, IGet<Input.StartCollection> {
public record Idle : State, IIdle, IGet<Input.StartCollection> {
public IState On(Input.StartCollection input) => new Collecting(
Context, input.Target
input.Target
);
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/game/state/GameLogic.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ namespace GameDemo;
public partial class GameLogic {
public interface IState : IStateLogic { }

public record State :
StateLogic, IState, IGet<Input.Initialize> {
public State(IContext context) : base(context) {
var appRepo = context.Get<IAppRepo>();

OnEnter<State>(
(previous) => {
public record State : StateLogic, IState, IGet<Input.Initialize> {
public State() {
OnAttach(
() => {
var appRepo = Context.Get<IAppRepo>();
appRepo.GameStarting += GameAboutToStart;
appRepo.GamePaused += GamePaused;
appRepo.GameResumed += GameResumed;
}
);

OnExit<State>((next) => {
OnDetach(() => {
var appRepo = Context.Get<IAppRepo>();
appRepo.GameStarting -= GameAboutToStart;
appRepo.GamePaused -= GamePaused;
appRepo.GameResumed -= GameResumed;
Expand Down
3 changes: 1 addition & 2 deletions src/game/state/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public interface IGameLogic : ILogicBlock<GameLogic.IState> { }

[StateMachine]
public partial class GameLogic : LogicBlock<GameLogic.IState>, IGameLogic {
public override IState GetInitialState(IContext context) =>
new State(context);
public override IState GetInitialState() => new State();

public GameLogic(IAppRepo appRepo) {
Set(appRepo);
Expand Down
10 changes: 5 additions & 5 deletions src/in_game_audio/state/InGameAudioLogic.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public partial class InGameAudioLogic :
public interface IState : IStateLogic { }

public record State : StateLogic, IState {
public State(IContext context) : base(context) {
var appRepo = context.Get<IAppRepo>();

OnEnter<State>((previous) => {
public State() {
OnAttach(() => {
var appRepo = Context.Get<IAppRepo>();
appRepo.CoinCollected += OnCoinCollected;
appRepo.JumpshroomUsed += OnJumpshroomUsed;
appRepo.GameEnded += OnGameEnded;
Expand All @@ -19,7 +18,8 @@ public State(IContext context) : base(context) {
appRepo.GameStarting += OnGameStarting;
});

OnExit<State>((previous) => {
OnDetach(() => {
var appRepo = Context.Get<IAppRepo>();
appRepo.CoinCollected -= OnCoinCollected;
appRepo.JumpshroomUsed -= OnJumpshroomUsed;
appRepo.GameEnded -= OnGameEnded;
Expand Down
3 changes: 1 addition & 2 deletions src/in_game_audio/state/InGameAudioLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public interface IInGameAudioLogic : ILogicBlock<InGameAudioLogic.IState> { }
[StateMachine]
public partial class InGameAudioLogic :
LogicBlock<InGameAudioLogic.IState>, IInGameAudioLogic {
public override IState GetInitialState(IContext context) =>
new State(context);
public override IState GetInitialState() => new State();

public InGameAudioLogic(IAppRepo appRepo) {
Set(appRepo);
Expand Down
Loading

0 comments on commit 32b0f0f

Please sign in to comment.