Skip to content

Commit

Permalink
wip: upgrade to logic blocks v5 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Mar 20, 2024
1 parent 6b593e9 commit c0badd0
Show file tree
Hide file tree
Showing 62 changed files with 360 additions and 316 deletions.
8 changes: 6 additions & 2 deletions GameDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@
<PackageReference Include="Chickensoft.AutoInject" Version="1.5.0" PrivateAssets="all" />
<PackageReference Include="Chickensoft.GoDotCollections" Version="1.4.0" />
<PackageReference Include="Chickensoft.GodotNodeInterfaces" Version="2.1.18" />
<PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="4.2.1" PrivateAssets="all" OutputItemType="analyzer" />
<PackageReference Include="Chickensoft.LogicBlocks" Version="4.2.1" />
<!-- <PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="4.2.1" PrivateAssets="all" OutputItemType="analyzer" />
<PackageReference Include="Chickensoft.LogicBlocks" Version="4.2.1" /> -->

<ProjectReference Include="../../Chickensoft/LogicBlocks/Chickensoft.LogicBlocks/Chickensoft.LogicBlocks.csproj" />
<ProjectReference Include="../../Chickensoft/LogicBlocks/Chickensoft.LogicBlocks.Generator/Chickensoft.LogicBlocks.Generator.csproj" />

<PackageReference Include="Chickensoft.PowerUps" Version="3.0.1-godot4.2.0-beta.5" PrivateAssets="all" />
<PackageReference Include="Chickensoft.SuperNodes.Types" Version="1.6.1" />
<PackageReference Include="Chickensoft.SuperNodes" Version="1.6.1" PrivateAssets="all" OutputItemType="analyzer" />
Expand Down
16 changes: 8 additions & 8 deletions src/app/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,40 @@ public void OnReady() {
AppBinding = AppLogic.Bind();

AppBinding
.Handle<AppLogic.Output.ShowSplashScreen>(_ => {
.Handle((in AppLogic.Output.ShowSplashScreen _) => {
HideMenus();
BlankScreen.Hide();
Splash.Show();
})
.Handle<AppLogic.Output.HideSplashScreen>(_ => {
.Handle((in AppLogic.Output.HideSplashScreen _) => {
BlankScreen.Show();
FadeToBlack();
})
.Handle<AppLogic.Output.RemoveExistingGame>(_ => {
.Handle((in AppLogic.Output.RemoveExistingGame _) => {
GamePreview.RemoveChildEx(Game);
Game.QueueFree();
Game = default!;
})
.Handle<AppLogic.Output.LoadGame>(_ => {
.Handle((in AppLogic.Output.LoadGame _) => {
Game = Instantiator.LoadAndInstantiate<Game>(GAME_SCENE_PATH);
GamePreview.AddChildEx(Game);

Instantiator.SceneTree.Paused = false;
})
.Handle<AppLogic.Output.ShowMainMenu>(_ => {
.Handle((in AppLogic.Output.ShowMainMenu _) => {
// Load everything while we're showing a black screen, then fade in.
HideMenus();
Menu.Show();
Game.Show();

FadeInFromBlack();
})
.Handle<AppLogic.Output.FadeToBlack>(_ => FadeToBlack())
.Handle<AppLogic.Output.ShowGame>(_ => {
.Handle((in AppLogic.Output.FadeToBlack _) => FadeToBlack())
.Handle((in AppLogic.Output.ShowGame _) => {
HideMenus();
FadeInFromBlack();
})
.Handle<AppLogic.Output.HideGame>(_ => FadeToBlack());
.Handle((in AppLogic.Output.HideGame _) => FadeToBlack());

// Enter the first state to kick off the binding side effects.
AppLogic.Start();
Expand Down
8 changes: 4 additions & 4 deletions src/app/state/AppLogic.State.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class AppLogic {
public interface IState : IStateLogic {
}
public interface IState : IStateLogic<IState>;

public abstract partial record State : StateLogic, IState {
}
public abstract partial record State : StateLogic<IState>, IState;
}
4 changes: 2 additions & 2 deletions src/app/state/AppLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace GameDemo;
using Chickensoft.LogicBlocks;
using Chickensoft.LogicBlocks.Generator;

public interface IAppLogic : ILogicBlock<AppLogic.IState> { }
public interface IAppLogic : ILogicBlock<AppLogic.IState>;

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

Expand Down
14 changes: 8 additions & 6 deletions src/app/state/states/InGame.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class AppLogic {
public partial record State {
public record InGame : State, IGet<Input.EndGame> {
public InGame() {
OnEnter<InGame>(_ => {
this.OnEnter(() => {
Get<IAppRepo>().OnEnterGame();
Context.Output(new Output.ShowGame());
Output(new Output.ShowGame());
});
OnExit<InGame>(_ => Context.Output(new Output.HideGame()));
this.OnExit(() => Output(new Output.HideGame()));

OnAttach(() => Get<IAppRepo>().GameExited += OnGameExited);
OnDetach(() => Get<IAppRepo>().GameExited -= OnGameExited);
}

public void OnRestartGameRequested() =>
Context.Input(new Input.EndGame(PostGameAction.RestartGame));
Input(new Input.EndGame(PostGameAction.RestartGame));

public void OnGameExited(PostGameAction reason) =>
Context.Input(new Input.EndGame(reason));
Input(new Input.EndGame(reason));

public IState On(Input.EndGame input) =>
public IState On(in Input.EndGame input) =>
new LeavingGame(input.PostGameAction);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/state/states/LeavingGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public LeavingGame(PostGameAction postGameAction) {
PostGameAction = postGameAction;
}

public IState On(Input.FadeOutFinished input) {
public IState On(in Input.FadeOutFinished input) {
// We are either supposed to restart the game or go back to the main
// menu. More complex games might have more post-game destinations,
// but it's pretty simple for us.
Context.Output(new Output.RemoveExistingGame());
Output(new Output.RemoveExistingGame());

if (PostGameAction is not PostGameAction.RestartGame) {
return new MainMenu();
}

Context.Output(new Output.LoadGame());
Output(new Output.LoadGame());
return new InGame();
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/app/state/states/LeavingMenu.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class AppLogic {
public partial record State {
public record LeavingMenu : State, IGet<Input.FadeOutFinished> {
public LeavingMenu() {
OnEnter<LeavingMenu>(
previous => Context.Output(new Output.FadeToBlack())
);
this.OnEnter(() => Output(new Output.FadeToBlack()));
}

public IState On(Input.FadeOutFinished input) =>
new InGame();
public IState On(in Input.FadeOutFinished input) => new InGame();
}
}
}
12 changes: 7 additions & 5 deletions src/app/state/states/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class AppLogic {
public partial record State {
public record MainMenu : State, IGet<Input.StartGame> {
public MainMenu() {
OnEnter<MainMenu>(
previous => {
Context.Output(new Output.LoadGame());
this.OnEnter(
() => {
Output(new Output.LoadGame());
Get<IAppRepo>().OnMainMenuEntered();
Context.Output(new Output.ShowMainMenu());
Output(new Output.ShowMainMenu());
}
);
}

public IState On(Input.StartGame input) => new LeavingMenu();
public IState On(in Input.StartGame input) => new LeavingMenu();
}
}
}
10 changes: 5 additions & 5 deletions src/app/state/states/SplashScreen.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class AppLogic {
public partial record State {
public record SplashScreen : State, IGet<Input.FadeOutFinished> {
public SplashScreen() {
OnEnter<SplashScreen>(
(previous) => Context.Output(new Output.ShowSplashScreen())
);
this.OnEnter(() => Output(new Output.ShowSplashScreen()));

OnAttach(
() => Get<IAppRepo>().SplashScreenSkipped += OnSplashScreenSkipped
Expand All @@ -17,10 +17,10 @@ public SplashScreen() {
);
}

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

public void OnSplashScreenSkipped() =>
Context.Output(new Output.HideSplashScreen());
Output(new Output.HideSplashScreen());
}
}
}
11 changes: 6 additions & 5 deletions src/coin/Coin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void OnResolved() {
CoinBinding = CoinLogic.Bind();

CoinBinding
.When<CoinLogic.State.ICollecting>().Call(state => {
.When<CoinLogic.State.ICollecting>(state => {
// We want to start receiving physics ticks so we can orient ourselves
// toward the entity that's collecting us.
SetPhysicsProcess(true);
Expand All @@ -80,13 +80,14 @@ public void OnResolved() {
});

CoinBinding
.Handle<CoinLogic.Output.Move>(
output => GlobalPosition = output.GlobalPosition
.Handle(
(in CoinLogic.Output.Move output) =>
GlobalPosition = output.GlobalPosition
)
.Handle<CoinLogic.Output.SelfDestruct>(
.Handle(
// We're done being collected, so we can remove ourselves from the
// scene tree.
output => QueueFree()
(in CoinLogic.Output.SelfDestruct output) => QueueFree()
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/coin/state/CoinLogic.State.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class CoinLogic {
public interface IState : IStateLogic { }
public interface IState : IStateLogic<IState>;

public abstract partial record State : StateLogic, IState;
public abstract partial record State : StateLogic<IState>, IState;
}
2 changes: 1 addition & 1 deletion src/coin/state/CoinLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GameDemo;
public interface ICoinLogic : ILogicBlock<CoinLogic.IState> {
}

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

Expand Down
18 changes: 9 additions & 9 deletions src/coin/state/states/CoinLogic.State.Collecting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace GameDemo;

using Chickensoft.LogicBlocks;

public partial class CoinLogic {
public partial record State {
public interface ICollecting : IState {
Expand All @@ -12,22 +14,20 @@ public record Collecting : State, ICollecting, IGet<Input.PhysicsProcess> {
public Collecting(ICoinCollector target) {
Target = target;

OnEnter<Collecting>(
previous => Get<IGameRepo>().StartCoinCollection(Get<ICoin>())
);
this.OnEnter(() => Get<IGameRepo>().StartCoinCollection(Get<ICoin>()));
}

public IState On(Input.PhysicsProcess input) {
var settings = Context.Get<Settings>();
public IState On(in Input.PhysicsProcess input) {
var settings = Get<Settings>();
var collectionTime = settings.CollectionTimeInSeconds;

_elapsedTime += (float)input.Delta;

if (_elapsedTime >= collectionTime) {
Context.Output(new Output.SelfDestruct());
Output(new Output.SelfDestruct());

var coin = Context.Get<ICoin>();
var gameRepo = Context.Get<IGameRepo>();
var coin = Get<ICoin>();
var gameRepo = Get<IGameRepo>();

gameRepo.OnFinishCoinCollection(coin);
}
Expand All @@ -36,7 +36,7 @@ public IState On(Input.PhysicsProcess input) {
Target.CenterOfMass, (float)(_elapsedTime / collectionTime)
);

Context.Output(new Output.Move(nextPosition));
Output(new Output.Move(nextPosition));
return this;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/coin/state/states/CoinLogic.State.Idle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace GameDemo;

public partial class CoinLogic {
public partial record State {
public interface IIdle : IState { }
public interface IIdle : IState;

public record Idle : State, IIdle, IGet<Input.StartCollection> {
public IState On(Input.StartCollection input) => new Collecting(
public IState On(in Input.StartCollection input) => new Collecting(
input.Target
);
}
Expand Down
40 changes: 21 additions & 19 deletions src/game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,43 @@ public void Setup() {
public void OnResolved() {
GameBinding = GameLogic.Bind();
GameBinding
.Handle<GameLogic.Output.StartGame>(
_ => {
.Handle(
(in GameLogic.Output.StartGame _) => {
PlayerCamera.UsePlayerCamera();
InGameUi.Show();
})
.Handle<GameLogic.Output.SetPauseMode>(
output => GetTree().Paused = output.IsPaused
.Handle(
(in GameLogic.Output.SetPauseMode output) =>
GetTree().Paused = output.IsPaused
)
.Handle<GameLogic.Output.CaptureMouse>(
output => Input.MouseMode = output.IsMouseCaptured
? Input.MouseModeEnum.Captured
: Input.MouseModeEnum.Visible
.Handle(
(in GameLogic.Output.CaptureMouse output) =>
Input.MouseMode = output.IsMouseCaptured
? Input.MouseModeEnum.Captured
: Input.MouseModeEnum.Visible
)
.Handle<GameLogic.Output.ShowLostScreen>(_ => {
.Handle((in GameLogic.Output.ShowLostScreen _) => {
DeathMenu.Show();
DeathMenu.FadeIn();
DeathMenu.Animate();
})
.Handle<GameLogic.Output.ExitLostScreen>(_ => DeathMenu.FadeOut())
.Handle<GameLogic.Output.ShowPauseMenu>(_ => {
.Handle((in GameLogic.Output.ExitLostScreen _) => DeathMenu.FadeOut())
.Handle((in GameLogic.Output.ShowPauseMenu _) => {
PauseMenu.Show();
PauseMenu.FadeIn();
})
.Handle<GameLogic.Output.ShowWonScreen>(_ => {
.Handle((in GameLogic.Output.ShowWonScreen _) => {
WinMenu.Show();
WinMenu.FadeIn();
})
.Handle<GameLogic.Output.ExitWonScreen>(_ => WinMenu.FadeOut())
.Handle<GameLogic.Output.ExitPauseMenu>(_ => PauseMenu.FadeOut())
.Handle<GameLogic.Output.HidePauseMenu>(_ => PauseMenu.Hide())
.Handle<GameLogic.Output.ShowPauseSaveOverlay>(
_ => PauseMenu.OnSaveStarted()
.Handle((in GameLogic.Output.ExitWonScreen _) => WinMenu.FadeOut())
.Handle((in GameLogic.Output.ExitPauseMenu _) => PauseMenu.FadeOut())
.Handle((in GameLogic.Output.HidePauseMenu _) => PauseMenu.Hide())
.Handle((in GameLogic.Output.ShowPauseSaveOverlay _) =>
PauseMenu.OnSaveStarted()
)
.Handle<GameLogic.Output.HidePauseSaveOverlay>(
_ => PauseMenu.OnSaveFinished()
.Handle((in GameLogic.Output.HidePauseSaveOverlay _) =>
PauseMenu.OnSaveFinished()
);

// Trigger the first state's OnEnter callbacks so our bindings run.
Expand Down
Loading

0 comments on commit c0badd0

Please sign in to comment.