Skip to content

Commit

Permalink
🚒 Delete all async code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrijnbeek committed Oct 1, 2023
1 parent f5250fc commit 9f38454
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
30 changes: 13 additions & 17 deletions scenes/main/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Godot;
using System.Linq;
using System.Threading.Tasks;

public sealed class Main : Node
{
Expand All @@ -11,33 +10,33 @@ public sealed class Main : Node
private AnimationPlayer animations = null!;
private ShakeCamera shakeCamera = null!;

public override async void _Ready()
public override void _Ready()
{
GD.Randomize();
var dungeon = Dungeon.Make();
dungeonTraverser = new DungeonTraverser(dungeon);

var dialogue = GetNode<Dialogue>("Dialogue");
animations = GetNode<AnimationPlayer>("AnimationPlayer");
shakeCamera = GetNode<ShakeCamera>("ShakeCamera");

var sceneAnimationTasks = new[]
{
ToggleInventory(forceState: false, skipAnimation: true),
dialogue
.DisplayDialog(dungeon.Monologue.Select(x => new Sentence(Portrait.Player, x)).ToList())
};
ToggleInventory(forceState: false, skipAnimation: true);

var inventory = GetNode<Inventory>("Inventory");
inventory.Populate(dungeon.StartingItems);

await Task.WhenAll(sceneAnimationTasks);

dungeonTraverser = new DungeonTraverser(dungeon);
var dialogue = GetNode<Dialogue>("Dialogue");
dialogue
.DisplayDialog(dungeon.Monologue.Select(x => new Sentence(Portrait.Player, x)).ToList());

var room = GetNode<Room>("Room");
room.Player = GetNode<Player>("Player");
}

private void onDialogueFinished()
{
var room = GetNode<Room>("Room");
room.FillRoom(dungeonTraverser.CurrentRoom);
await ToggleInventory(forceState: true);
ToggleInventory(forceState: true);
}

private void onRoomExited()
Expand All @@ -57,7 +56,7 @@ private void onPlayerHealthChanged(int newHealth, int maxHealth, int healthChang
}

private bool currentInventoryVisibility = true;
private async Task ToggleInventory(bool? forceState = null, bool skipAnimation = false)
private void ToggleInventory(bool? forceState = null, bool skipAnimation = false)
{
var inventory = GetNode<Inventory>("Inventory");
var shouldBeVisible = forceState ?? inventory.Position.x < 0;
Expand All @@ -78,8 +77,5 @@ private async Task ToggleInventory(bool? forceState = null, bool skipAnimation =
{
animations.Advance(animations.CurrentAnimationLength);
}

// TODO(will): Wait for player input
await Task.Delay(50);
}
}
1 change: 1 addition & 0 deletions scenes/main/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ script = ExtResource( 7 )

[connection signal="RoomExited" from="Room" to="." method="onRoomExited"]
[connection signal="HealthChanged" from="Player" to="." method="onPlayerHealthChanged"]
[connection signal="DialogueFinished" from="Dialogue" to="." method="onDialogueFinished"]
20 changes: 10 additions & 10 deletions scenes/story/Dialogue.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public sealed class Dialogue : Node2D
{
[Signal] public delegate void DialogueFinished();

private Label text = null!;
private readonly Queue<Sentence> queuedDialogue = new();
private TaskCompletionSource<bool>? tsc;

public override void _Ready()
{
Expand All @@ -25,6 +25,11 @@ public override void _Process(float delta)

public override void _Input(InputEvent @event)
{
if (!Visible)
{
return;
}

if (@event is not InputEventMouseButton { Pressed: true, ButtonIndex: (int) ButtonList.Left })
{
return;
Expand All @@ -42,15 +47,12 @@ public override void _Input(InputEvent @event)
return;
}

tsc?.SetResult(true);
tsc = null;
EmitSignal(nameof(DialogueFinished));
Visible = false;
}

public Task DisplayDialog(IReadOnlyList<Sentence> dialog)
public void DisplayDialog(IReadOnlyList<Sentence> dialog)
{
tsc?.SetResult(true);
tsc = null;
queuedDialogue.Clear();

foreach (var sentence in dialog)
Expand All @@ -60,13 +62,11 @@ public Task DisplayDialog(IReadOnlyList<Sentence> dialog)

if (queuedDialogue.Count == 0)
{
return Task.CompletedTask;
return;
}

startSentence(queuedDialogue.Dequeue());
Visible = true;
tsc = new TaskCompletionSource<bool>();
return tsc.Task;
}

private void startSentence(Sentence sentence)
Expand Down

0 comments on commit 9f38454

Please sign in to comment.