-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix blackbaord tracker for custom variables.
- Loading branch information
1 parent
214b84e
commit b7bf1ca
Showing
2 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Murder.Core.Dialogs; | ||
|
||
/// <summary> | ||
/// This is used to track variables created on the fly. This used to be an | ||
/// object, but the serialization doesn't really like that, so I'll follow a | ||
/// similar pattern to other facts. | ||
/// </summary> | ||
public readonly struct OrphanBlackboardContext | ||
{ | ||
public readonly FactKind Kind = FactKind.Invalid; | ||
|
||
public readonly string? StrValue = null; | ||
|
||
public readonly int? IntValue = null; | ||
|
||
public readonly float? FloatValue = null; | ||
|
||
public readonly bool? BoolValue = null; | ||
|
||
public OrphanBlackboardContext() { } | ||
|
||
[JsonConstructor] | ||
public OrphanBlackboardContext(FactKind kind, bool? boolValue, int? intValue, float? floatValue, string? strValue) | ||
{ | ||
(Kind, StrValue, IntValue, FloatValue, BoolValue) = (kind, strValue, intValue, floatValue, boolValue); | ||
} | ||
|
||
public OrphanBlackboardContext(object @value) | ||
{ | ||
bool? @bool = null; | ||
int? @int = null; | ||
float? @float = null; | ||
string? @string = null; | ||
|
||
FactKind kind = FactKind.Invalid; | ||
|
||
// Do not propagate previous values. | ||
switch (@value) | ||
{ | ||
case bool toBool: | ||
kind = FactKind.Bool; | ||
@bool = toBool; | ||
|
||
break; | ||
|
||
case int toInt: | ||
kind = FactKind.Int; | ||
@int = toInt; | ||
|
||
break; | ||
|
||
case float toFloat: | ||
kind = FactKind.Float; | ||
@float = toFloat; | ||
|
||
break; | ||
|
||
case string toString: | ||
kind = FactKind.String; | ||
@string = toString; | ||
|
||
break; | ||
} | ||
|
||
(Kind, StrValue, IntValue, FloatValue, BoolValue) = (kind, @string, @int, @float, @bool); | ||
} | ||
|
||
public object? GetValue() | ||
{ | ||
return Kind switch | ||
{ | ||
FactKind.Bool => BoolValue, | ||
FactKind.Int => IntValue, | ||
FactKind.Float => FloatValue, | ||
FactKind.String => StrValue, | ||
_ => null, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters