Skip to content

Commit

Permalink
Conversation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
csinkers committed Nov 2, 2024
1 parent 84a6250 commit b39fe35
Show file tree
Hide file tree
Showing 23 changed files with 502 additions and 215 deletions.
1 change: 1 addition & 0 deletions src/Api/Eventing/AdHocComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ sealed class Helper : IAdHocComponentHelper
public void Off<T>() => _this.Off<T>();
public void Detach() => _this.Detach();
public T AttachChild<T>(T child) where T : IComponent => _this.AttachChild(child);
public void Remove() => _this.Remove();
public void RemoveAllChildren() => _this.RemoveAllChildren();
public void RemoveChild(IComponent child) => _this.RemoveChild(child);
public T Var<T>(IVar<T> varInfo) => _this.ReadVar(varInfo);
Expand Down
12 changes: 9 additions & 3 deletions src/Api/Eventing/AlbionTaskCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum TaskStatus

public int Id { get; }
TaskStatus _status = TaskStatus.Unused;

[DiagEdit]
public bool BreakOnCompletion { get; set; }
public string? Description { get; set; }
#endif
Expand All @@ -38,21 +40,25 @@ enum TaskStatus
#endif

object? _continuation;
T? _result;
T _result; // Only meaningful when IsCompleted == true

public bool IsCompleted { get; private set; }
internal int OutstandingCompletions { get; set; }

public AlbionTask<T> Task => new(this);
public AlbionTask UntypedTask => new(this);
internal int OutstandingCompletions { get; set; }

public AlbionTaskCore() : this(null) { }
public AlbionTaskCore(string? description)
{
_result = default!;

#if DEBUG
Id = Tasks.GetNextId();
Description = description;
Tasks.AddTask(this);
#endif

#if RECORD_TASK_STACKS
_stack = Environment.StackTrace;
#endif
Expand Down Expand Up @@ -112,7 +118,7 @@ public T GetResult()
if (!IsCompleted)
throw new InvalidOperationException("Tried to get result of an incomplete task");

return _result!;
return _result;
}

public void SetResult(T value)
Expand Down
6 changes: 1 addition & 5 deletions src/Api/Eventing/EventExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ static AlbionTask<int> RaiseInvoker(List<Handler> handlers, IEvent e, object sen

case IAsyncHandler asyncHandler:
{
core ??= new($"RaiseAInvoker helper for {e.GetType()} from {sender}");

var innerTask = asyncHandler.InvokeAsAsync(e);
if (innerTask.IsCompleted)
{
core.SetResult(Unit.V);
break;
}

core ??= new($"RaiseAInvoker helper for {e.GetType().Name} from {sender}");
core.OutstandingCompletions++;
var core1 = core;
innerTask.OnCompleted(() =>
Expand Down
1 change: 1 addition & 0 deletions src/Api/Eventing/IAdHocComponentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IAdHocComponentHelper
void Off<T>();
void Detach();
T AttachChild<T>(T child) where T : IComponent;
void Remove();
void RemoveAllChildren();
void RemoveChild(IComponent child);
T Var<T>(IVar<T> varInfo);
Expand Down
12 changes: 6 additions & 6 deletions src/Core.Veldrid/Reflection/FloatReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace UAlbion.Core.Veldrid.Reflection;

public class FloatReflector(string typeName, Func<object, float> toFloat) : IReflector
{
const float _defaultMin = -10.0f;
const float _defaultMax = 10.0f;
const float DefaultMin = -10.0f;
const float DefaultMax = 10.0f;
public void Reflect(in ReflectorState state)
{
ImGui.Indent();
Expand All @@ -33,21 +33,21 @@ void RenderSlider(in ReflectorState state)
var label = state.Meta.Name ?? state.Index.ToString();
var options = state.Meta.Options;

float min = _defaultMin;
float max = _defaultMax;
float min = DefaultMin;
float max = DefaultMax;

if (options.Min is float minFloat) min = minFloat;
else if (options.MinProperty != null)
{
options.GetMinProperty ??= ReflectorUtil.BuildPropertyGetter(options.MinProperty, state.Parent.GetType());
min = options.GetMinProperty(state.Parent) as float? ?? _defaultMin;
min = options.GetMinProperty(state.Parent) as float? ?? DefaultMin;
}

if (options.Max is float maxFloat) max = maxFloat;
else if (options.MaxProperty != null)
{
options.GetMaxProperty ??= ReflectorUtil.BuildPropertyGetter(options.MaxProperty, state.Parent.GetType());
max = options.GetMaxProperty(state.Parent) as float? ?? _defaultMax;
max = options.GetMaxProperty(state.Parent) as float? ?? DefaultMax;
}

ImGui.TextUnformatted(label);
Expand Down
6 changes: 4 additions & 2 deletions src/Core.Veldrid/Reflection/IntReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace UAlbion.Core.Veldrid.Reflection;

public class IntReflector(string typeName, Func<object, int> toInt) : IReflector
{
const int DefaultMin = -1024;
const int DefaultMax = 1024;
public void Reflect(in ReflectorState state)
{
ImGui.Indent();
Expand All @@ -31,8 +33,8 @@ void RenderSlider(in ReflectorState state)
var label = state.Meta.Name ?? state.Index.ToString();
var options = state.Meta.Options;

int min = -1024;
int max = 1024;
int min = DefaultMin;
int max = DefaultMax;

if (options.Min is int minInt) min = minInt;
else if (options.MinProperty != null)
Expand Down
96 changes: 96 additions & 0 deletions src/Core/Visual/MonsterSprite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Numerics;
using UAlbion.Api;
using UAlbion.Api.Eventing;
using UAlbion.Api.Visual;

namespace UAlbion.Core.Visual;

public class MonsterSprite : Component
{
readonly Sprite _sprite;
readonly Sprite _shadow;

Vector2 _scale = Vector2.One;
Vector2 _maxSize = Vector2.One;
Vector3 _position;

public MonsterSprite(
IAssetId id,
DrawLayer layer,
SpriteKeyFlags keyFlags,
Func<IAssetId, ITexture> textureLoaderFunc = null,
IBatchManager<SpriteKey, SpriteInfo> batchManager = null)
{
var flags = SpriteFlags.BottomMid | SpriteFlags.Transparent;
_sprite = AttachChild(new Sprite(id, layer, keyFlags, flags, textureLoaderFunc, batchManager));
_shadow = AttachChild(new Sprite(id, layer, keyFlags, flags, textureLoaderFunc, batchManager));
}

protected override void Subscribed()
{
base.Subscribed();

_maxSize = Vector2.Zero;
foreach (var region in _sprite.Texture.Regions)
{
if (region.Width > _maxSize.X) _maxSize.X = region.Width;
if (region.Height > _maxSize.Y) _maxSize.Y = region.Height;
}

Update();
}

public Vector3 Position
{
get => _position;
set
{
if (_position == value)
return;

_position = value;
Update();
}
}

public int Frame
{
get => _sprite.Frame / 2;
set
{
if (_sprite.Frame == 2 * value)
return;

_sprite.Frame = 2 * value;
_shadow.Frame = 2 * value + 1;

Update();
}
}

public Vector2 Scale
{
get => _scale; set
{
if (_scale == value)
return;

_scale = value;
Update();
}
}

public Vector2 MaxSize => _maxSize * Scale;
public int FrameCount => _sprite.FrameCount / 2;

public override string ToString() => $"MonsterSprite {_sprite.Id}";

void Update()
{
_sprite.Position = _position;
_shadow.Position = _position + new Vector3(0, 0, 0.1f);
_sprite.Size = _scale * _sprite.FrameSize;
_shadow.Size = _scale * _shadow.FrameSize;
}
}
24 changes: 13 additions & 11 deletions src/Core/Visual/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ namespace UAlbion.Core.Visual;
public class Sprite : Component, IPositioned
{
readonly Action<PrepareFrameEvent> _onPrepareFrameDelegate;
readonly PositionedComponentMovedEvent _moveEvent;
readonly DrawLayer _layer;
readonly SpriteKeyFlags _keyFlags;
readonly Func<IAssetId, ITexture> _textureLoaderFunc;
readonly PositionedComponentMovedEvent _moveEvent;
readonly IBatchManager<SpriteKey, SpriteInfo> _batchManager;

BatchLease<SpriteKey, SpriteInfo> _spriteLease;
Vector3 _position;
Vector2? _size;
IAssetId _id;
int _frame;
SpriteFlags _flags;
readonly IBatchManager<SpriteKey, SpriteInfo> _batchManager;
bool _dirty = true;

public Sprite(
Expand All @@ -34,6 +34,13 @@ public Sprite(
{
_moveEvent = new PositionedComponentMovedEvent(this);
_onPrepareFrameDelegate = OnPrepareFrame;
_layer = layer;
_keyFlags = keyFlags;
_flags = flags;
_batchManager = batchManager;
_id = id;
_textureLoaderFunc = textureLoaderFunc ?? DefaultLoader;

On<BackendChangedEvent>(_ => Dirty = true);
On(_onPrepareFrameDelegate);
On<WorldCoordinateSelectEvent>(Select);
Expand All @@ -47,13 +54,6 @@ public Sprite(
if ((ReadVar(V.Core.User.EngineFlags) & EngineFlags.HighlightSelection) == EngineFlags.HighlightSelection)
Flags &= ~SpriteFlags.Highlight;
});

_layer = layer;
_keyFlags = keyFlags;
_flags = flags;
_batchManager = batchManager;
_id = id;
_textureLoaderFunc = textureLoaderFunc ?? DefaultLoader;
}

[DiagEdit]
Expand All @@ -80,6 +80,7 @@ public Vector3 Position
{
if (_position == value)
return;

_position = value;
Dirty = true;
if (IsSubscribed)
Expand All @@ -91,7 +92,7 @@ public Vector3 Position
public int DebugZ => DepthUtil.DepthToLayer(Position.Z);

[DiagEdit(Style = DiagEditStyle.Size2D)]
public Vector2 Size
public Vector2 Size // Logical size, may differ from actual size (RenderSize).
{
get => _size ?? Vector2.One;
set
Expand All @@ -107,6 +108,7 @@ public Vector2 Size
}
}

public ITexture Texture => _spriteLease?.Key.Texture;
public Vector2 FrameSize => _spriteLease?.Key.Texture.Regions[Frame].Size ?? Vector2.One;

[DiagEdit(Style = DiagEditStyle.NumericSlider, Min = 0, MaxProperty = nameof(FrameCount))]
Expand Down Expand Up @@ -144,7 +146,7 @@ bool Dirty

protected override void Subscribed()
{
Dirty = true;
UpdateSprite();
Raise(new AddPositionedComponentEvent(this));
}

Expand Down
19 changes: 19 additions & 0 deletions src/Formats/MapEvents/StartDialogueEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ namespace UAlbion.Formats.MapEvents;
"talk")]
public class StartDialogueEvent : MapEvent
{
/*
Good test conversations:
111 Frill - Ask about item (FineIskaiDagger)
123 Fasiir - Learn spells
127 Rejira - Merchant, Heal, Cure, RemoveCurse
144 Zirr - SleepInRoom, OrderFood
146 Snird - Merchant, RepairItem
156 Garris - Unk2 ???, AskAbout(TharnossPermit)
159 Ferina - LearnCloseCombat
173 Torko - AskOpinion (2x)
176 Roves - ScrollMerchant
224 Unk - Unk9 ?? (also 241_Riko)
285 Konny - AskToJoin
314 Drannagh - RestoreItemEnergy, LearnSpells
981 Tom - UnkE
984 Sira2 - AskToLeave, PartySleeps, Unk2D, Unk17
986 Harriet - DropItem(GodesssAmulet)
*/

StartDialogueEvent() { }
public StartDialogueEvent(NpcSheetId npcId) => NpcId = npcId;

Expand Down
Loading

0 comments on commit b39fe35

Please sign in to comment.