Skip to content

Commit

Permalink
Fix turret anim frame calculation to match game
Browse files Browse the repository at this point in the history
  • Loading branch information
Starkku authored Feb 7, 2025
1 parent aeda5c5 commit a3f570f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
17 changes: 11 additions & 6 deletions src/TSMapEditor/Models/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ public override int GetXDrawOffset()
(IsBuildingAnim ? BuildingAnimDrawConfig.X : 0);
}

// Turret anims have their facing frames reversed
// Turret anims also only have 32 facings
// Game uses lookup table containing frame indices (frame index 28 at array index 0,
// decrementing and wrapping around to 31 after 0) to determine the correct frame from
// direction normalized to range 0-31, the formula is replicated here without the LUT - Starkku
private int GetTurretAnimFrameIndex() => (28 - Facing / 8 + 32) % 32;

public override int GetFrameIndex(int frameCount)
{
if (IsTurretAnim)
{
// Turret anims have their facing frames reversed
// Turret anims also only have 32 facings
byte facing = (byte)(255 - Facing - 31);
return facing / (256 / 32);
}
return GetTurretAnimFrameIndex();

if (IsBuildingAnim && ParentBuilding != null)
{
Expand All @@ -60,6 +62,9 @@ public override int GetFrameIndex(int frameCount)

public override int GetShadowFrameIndex(int frameCount)
{
if (IsTurretAnim)
return GetTurretAnimFrameIndex() + frameCount / 2;

if (IsBuildingAnim && ParentBuilding != null)
{
if (ParentBuilding.HP < Constants.ConditionYellowHP)
Expand Down
8 changes: 0 additions & 8 deletions src/TSMapEditor/Rendering/ObjectRenderers/AnimRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,8 @@ public override void DrawShadow(Animation gameObject)

var drawParams = GetDrawParams(gameObject);
var drawPoint = GetDrawPoint(gameObject);

int shadowFrameIndex = gameObject.GetShadowFrameIndex(drawParams.ShapeImage.GetFrameCount());

if (gameObject.IsTurretAnim)
{
// Turret anims have their facing frames reversed
byte facing = (byte)(255 - gameObject.Facing - 31);
shadowFrameIndex += facing / (512 / drawParams.ShapeImage.GetFrameCount());
}

if (shadowFrameIndex > 0 && shadowFrameIndex < drawParams.ShapeImage.GetFrameCount())
{
var frame = drawParams.ShapeImage.GetFrame(shadowFrameIndex);
Expand Down

0 comments on commit a3f570f

Please sign in to comment.