Skip to content

Commit

Permalink
Monster positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
csinkers committed Nov 4, 2024
1 parent bd09e89 commit 0d26c38
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 66 deletions.
1 change: 0 additions & 1 deletion mods/Shaders/Shaders/BlendedSpriteSF.h.frag
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define SF_FLOOR 0x10U
#define SF_BILLBOARD 0x20U
#define SF_ONLY_EVEN_FRAMES 0x40U
#define SF_TRANSPARENT 0x80U
#define SF_HIGHLIGHT 0x100U
#define SF_RED_TINT 0x200U
#define SF_GREEN_TINT 0x400U
Expand Down
1 change: 0 additions & 1 deletion mods/Shaders/Shaders/BlendedSpriteSV.h.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define SF_FLOOR 0x10U
#define SF_BILLBOARD 0x20U
#define SF_ONLY_EVEN_FRAMES 0x40U
#define SF_TRANSPARENT 0x80U
#define SF_HIGHLIGHT 0x100U
#define SF_RED_TINT 0x200U
#define SF_GREEN_TINT 0x400U
Expand Down
4 changes: 1 addition & 3 deletions mods/Shaders/Shaders/SpriteSF.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ vec4 Pal(float color)

void main()
{
vec2 uv = ((iFlags & SF_FLIP_VERTICAL) != 0)
? vec2(iTexPosition.x, 1 - iTexPosition.y)
: iTexPosition;
vec2 uv = iTexPosition;

if ((uFlags & SKF_CLAMP_EDGES) != 0)
uv = vec2(clamp(uv.x, iUvClamp.x, iUvClamp.z), clamp(uv.y, iUvClamp.y, iUvClamp.w));
Expand Down
1 change: 0 additions & 1 deletion mods/Shaders/Shaders/SpriteSF.h.frag
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define SF_FLOOR 0x10U
#define SF_BILLBOARD 0x20U
#define SF_ONLY_EVEN_FRAMES 0x40U
#define SF_TRANSPARENT 0x80U
#define SF_HIGHLIGHT 0x100U
#define SF_RED_TINT 0x200U
#define SF_GREEN_TINT 0x400U
Expand Down
1 change: 0 additions & 1 deletion mods/Shaders/Shaders/SpriteSV.h.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define SF_FLOOR 0x10U
#define SF_BILLBOARD 0x20U
#define SF_ONLY_EVEN_FRAMES 0x40U
#define SF_TRANSPARENT 0x80U
#define SF_HIGHLIGHT 0x100U
#define SF_RED_TINT 0x200U
#define SF_GREEN_TINT 0x400U
Expand Down
6 changes: 5 additions & 1 deletion mods/Shaders/Shaders/SpriteSV.vert
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ void main()
}
else oUvClamp = vec4(0,0,1,1);

oTexPosition = iTexCoords * iTexSize + iTexOffset;
vec2 uv = ((iFlags & SF_FLIP_VERTICAL) != 0)
? vec2(iTexCoords.x, 1 - iTexCoords.y)
: iTexCoords;

oTexPosition = uv * iTexSize + iTexOffset;
oLayer = float(iTexLayer);
oFlags = iFlags;
oNormCoords = iTexCoords;
Expand Down
21 changes: 12 additions & 9 deletions src/Core/Visual/MonsterSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace UAlbion.Core.Visual;
public class MonsterSprite : Component
{
readonly Sprite _sprite;
readonly Sprite _shadow;
// readonly Sprite _shadow;

Vector2 _scale = Vector2.One;
Vector2 _maxSize = Vector2.One;
Expand All @@ -22,20 +22,23 @@ public MonsterSprite(
Func<IAssetId, ITexture> textureLoaderFunc = null,
IBatchManager<SpriteKey, SpriteInfo> batchManager = null)
{
var flags = SpriteFlags.BottomMid | SpriteFlags.Transparent;
var flags = SpriteFlags.TopMid | SpriteFlags.FlipVertical;
_sprite = AttachChild(new Sprite(id, layer, keyFlags, flags, textureLoaderFunc, batchManager));
_shadow = 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 (_sprite.Texture != null)
{
if (region.Width > _maxSize.X) _maxSize.X = region.Width;
if (region.Height > _maxSize.Y) _maxSize.Y = region.Height;
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();
Expand Down Expand Up @@ -63,7 +66,7 @@ public int Frame
return;

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

Update();
}
Expand All @@ -89,8 +92,8 @@ public Vector2 Scale
void Update()
{
_sprite.Position = _position;
_shadow.Position = _position + new Vector3(0, 0, 0.1f);
// _shadow.Position = _position + new Vector3(0, 0, 0.1f);
_sprite.Size = _scale * _sprite.FrameSize;
_shadow.Size = _scale * _shadow.FrameSize;
// _shadow.Size = _scale * _shadow.FrameSize;
}
}
1 change: 1 addition & 0 deletions src/Core/Visual/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ bool Dirty

protected override void Subscribed()
{
Dirty = true;
UpdateSprite();
Raise(new AddPositionedComponentEvent(this));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Visual/SpriteFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum SpriteFlags : uint
Floor = 0x10, // On the floor rather than standing upright
Billboard = 0x20, // Autorotate to face the camera
OnlyEvenFrames = 0x40, // Used for monsters etc where the odd frames are shadows
Transparent = 0x80,
// 0x80 Unused
Highlight = 0x100,
RedTint = 0x200, // Debug Flag
GreenTint = 0x400, // Debug Flag
Expand Down
18 changes: 16 additions & 2 deletions src/Game/Combat/Battle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UAlbion.Formats.Assets.Save;
using UAlbion.Formats.Ids;
using UAlbion.Game.Gui.Combat;
using UAlbion.Game.Gui.Dialogs;
using UAlbion.Game.State;

namespace UAlbion.Game.Combat;
Expand All @@ -17,7 +18,6 @@ namespace UAlbion.Game.Combat;
/// </summary>
public class Battle : GameComponent, IReadOnlyBattle
{

readonly MonsterGroupId _groupId;
readonly List<ICombatParticipant> _mobs = new();
readonly ICombatParticipant[] _tiles = new ICombatParticipant[SavedGame.CombatRows * SavedGame.CombatColumns];
Expand All @@ -29,6 +29,7 @@ public Battle(MonsterGroupId groupId, SpriteId backgroundId)
{
On<EndCombatEvent>(_ => Complete?.Invoke());
OnAsync<BeginCombatRoundEvent>(BeginRoundAsync);
OnAsync<ObserveCombatEvent>(Observe);

_groupId = groupId;
Mobs = _mobs;
Expand All @@ -44,7 +45,20 @@ public Battle(MonsterGroupId groupId, SpriteId backgroundId)
});
}

AlbionTask BeginRoundAsync(BeginCombatRoundEvent _) => RaiseA(new CombatUpdateEvent(25)); // TODO
AlbionTask Observe(ObserveCombatEvent _) =>
WithFrozenClock(this, async x =>
{
Raise(new CombatDialog.ShowCombatDialogEvent(false));
var dlg = x.AttachChild(new InvisibleWaitForClickDialog());
await dlg.Task;
Raise(new CombatDialog.ShowCombatDialogEvent(true));
});

AlbionTask BeginRoundAsync(BeginCombatRoundEvent _)
{
return RaiseA(new CombatUpdateEvent(100));
// TODO
}

protected override void Subscribed()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Game/Combat/CombatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void BeginCombat(MonsterGroupId groupId, SpriteId backgroundId)
var battle = new Battle(groupId, backgroundId);
scene.Add(battle);

Raise(new DialogManager.ShowCombatDialogEvent(battle));
Raise(new DialogManager.CombatDialogEvent(battle));

battle.Complete += () =>
{
Expand Down
44 changes: 35 additions & 9 deletions src/Game/Combat/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,41 @@ public override string ToString()

void UpdatePosition()
{
var tileX = CombatPosition % SavedGame.CombatColumns;
var tileY = CombatPosition / SavedGame.CombatColumns;
int tileX = CombatPosition % SavedGame.CombatColumns;
int tileY = CombatPosition / SavedGame.CombatColumns;

const float fieldWidth = 500.0f;
const float fieldDepth = 500.0f;
/*
Fear1 (1,0) -> -90 90 -260 or -97, 104, -267 (moves around)
Animal1 (0,2) -> -160 80 -145
Krondir1 (4,2) -> 100 100 -145
_sprite.Position = new Vector3(
fieldWidth * (tileX / (float)SavedGame.CombatColumns - 0.5f),
0,
-fieldDepth * (1.0f - tileY / (float)SavedGame.CombatRows));
ty: -270 -210 -150 -90 -30 (monsters probably never actually go past 3)
tx: -150 -90 -30 30 90 150
Name tx x ty z Unk37 y Unk152 %
Fear1, 1, -090, 0, -270, 2, 105, -48, 100 Flying
Rinrii2, 3, +040, 0, -250, 0, 0, 20, 110
Rinrii2, 0, -160, 1, -210, 0, 10, 20, 110
Krondir1, 2, -025, 1, -190, 2, 90, 13, 100
Warniak1, 4, +100, 1, -220, 0, 110, -80, 100 Flying
Fear1, 5, +170, 1, -210, 2, 80, -48, 100 Flying
Animal1, 0, -160, 2, -140, 0, 80, 33, 100
Skrinn1, 1, -080, 2, -130, 18, 40, 24, 100
Krondir1, 4, +100, 2, -150, 2, 100, 13, 100
*/

const float fieldWidth = 300.0f;
const float fieldDepth = 240.0f;
const float fieldDepthOffset = 30.0f;

float x = fieldWidth * ((float)tileX / (SavedGame.CombatColumns - 1) - 0.5f);
float y = -60.0f - _sheet.Monster.Unk152;
float z = -(fieldDepthOffset + fieldDepth * (1.0f - (float)tileY / (SavedGame.CombatRows - 1)));

_sprite.Position = new Vector3(x, y, z);
}
}
}
5 changes: 5 additions & 0 deletions src/Game/Combat/ObserveCombatEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using UAlbion.Api.Eventing;

namespace UAlbion.Game.Combat;

public record ObserveCombatEvent : EventRecord;
13 changes: 5 additions & 8 deletions src/Game/EventChainManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace UAlbion.Game;

public sealed class EventChainManager : ServiceComponent<IEventManager>, IEventManager, IDisposable
{
static readonly StartClockEvent StartClockEvent = new();
static readonly StopClockEvent StopClockEvent = new();

readonly List<EventContext> _contexts = new();
readonly List<Breakpoint> _breakpoints = new();
public int CurrentDebugContextIndex { get; set; } = -1;
Expand Down Expand Up @@ -59,19 +56,19 @@ AlbionTask Trigger(TriggerChainEvent e)
return AlbionTask.CompletedTask;
}

var isClockRunning = Resolve<IClock>().IsRunning;
var wasClockRunning = Resolve<IClock>().IsRunning;
var firstNode = e.EventSet.Events[e.EntryPoint];
var context = new EventContext(e.Source, (EventContext)Context)
{
EntryPoint = e.EntryPoint,
EventSet = e.EventSet,
Node = firstNode,
ClockWasRunning = isClockRunning,
ClockWasRunning = wasClockRunning,
LastAction = firstNode.Event as ActionEvent
};

if (isClockRunning)
Raise(StopClockEvent);
if (wasClockRunning)
Raise(StopClockEvent.Instance);

ReadyContext(context);
_contexts.Add(context);
Expand Down Expand Up @@ -139,7 +136,7 @@ async AlbionTask Resume(EventContext context)
context.Status = EventContextStatus.Completing;

if (context.ClockWasRunning)
Raise(StartClockEvent);
Raise(StartClockEvent.Instance);

_contexts.Remove(context);

Expand Down
5 changes: 4 additions & 1 deletion src/Game/Events/StartClockEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
namespace UAlbion.Game.Events;

[Event("start_clock", "Resume automatically updating the game clock.")]
public class StartClockEvent : GameEvent { }
public class StartClockEvent : GameEvent
{
public static StartClockEvent Instance { get; } = new();
}
5 changes: 4 additions & 1 deletion src/Game/Events/StopClockEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
namespace UAlbion.Game.Events;

[Event("stop_clock", "Stop the game clock from advancing automatically.")]
public class StopClockEvent : GameEvent { }
public class StopClockEvent : GameEvent
{
public static StopClockEvent Instance { get; } = new();
}
16 changes: 14 additions & 2 deletions src/Game/GameComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ protected async AlbionTask WithFrozenClock<T>(T context, Func<T, AlbionTask> fun
{
var wasClockRunning = Resolve<IClock>()?.IsRunning ?? false;
if (wasClockRunning)
Raise(new StopClockEvent());
Raise(StopClockEvent.Instance);

await func(context);

if (wasClockRunning)
Raise(new StartClockEvent());
Raise(StartClockEvent.Instance);
}
}

public abstract class GameServiceComponent<T> : ServiceComponent<T>
{
protected IAssetManager Assets => Resolve<IAssetManager>();

protected async AlbionTask WithFrozenClock<T>(T context, Func<T, AlbionTask> func)

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'

Check warning on line 30 in src/Game/GameComponent.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'GameServiceComponent<T>'
{
var wasClockRunning = Resolve<IClock>()?.IsRunning ?? false;
if (wasClockRunning)
Raise(StopClockEvent.Instance);

await func(context);

if (wasClockRunning)
Raise(StartClockEvent.Instance);
}
}
7 changes: 7 additions & 0 deletions src/Game/Gui/Combat/CombatDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UAlbion.Api.Eventing;
using UAlbion.Formats.Assets.Save;
using UAlbion.Game.Combat;
using UAlbion.Game.Gui.Controls;
Expand All @@ -11,11 +12,17 @@ namespace UAlbion.Game.Gui.Combat;
/// </summary>
public class CombatDialog : Dialog
{
public record ShowCombatDialogEvent(bool Show) : EventRecord, IVerboseEvent;
readonly IReadOnlyBattle _battle;

public CombatDialog(int depth, IReadOnlyBattle battle) : base(DialogPositioning.Center, depth)
{
On<EndCombatEvent>(_ => Remove());
On<ShowCombatDialogEvent>(e =>
{
foreach(var child in Children)
child.IsActive = e.Show;
});

_battle = battle ?? throw new ArgumentNullException(nameof(battle));
var stack = new List<IUiElement>();
Expand Down
2 changes: 1 addition & 1 deletion src/Game/Gui/Combat/LogicalCombatTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ IText S(TextId textId, bool disabled = false)

options.Add(new ContextMenuOption(
S(Base.SystemText.Combat_Observe),
new NopEvent(),
new ObserveCombatEvent(),
ContextMenuGroup.System));

options.Add(new ContextMenuOption(
Expand Down
3 changes: 2 additions & 1 deletion src/Game/Gui/Controls/FixedPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class FixedPosition : UiElement, IFixedSizeUiElement
public FixedPosition(Rectangle extents, IUiElement child)
{
_extents = extents;
AttachChild(child);
if (child != null)
AttachChild(child);
}

public override Vector2 GetSize() => new(_extents.Width, _extents.Height);
Expand Down
Loading

0 comments on commit 0d26c38

Please sign in to comment.