Skip to content

Commit 9ccdf3a

Browse files
committed
Take YSortAdjust into account for animations, improve handling of depth for bibs and animations
1 parent 33c6f0a commit 9ccdf3a

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/TSMapEditor/Models/Animation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public struct BuildingAnimDrawConfig
8585
public int X { get; set; }
8686
public int YSort { get; set; }
8787
public int ZAdjust { get; set; }
88-
public readonly int SortValue => YSort - ZAdjust * ZAdjustMult;
88+
89+
public int SortValue(int ySortAdjust) => YSort + ySortAdjust - ZAdjust * ZAdjustMult;
8990
}
9091
}

src/TSMapEditor/Models/ArtConfig/AnimArtConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public AnimArtConfig() { }
1010
public string Image { get; set; }
1111
public int YDrawOffset { get; set; }
1212
public int XDrawOffset { get; set; } // Phobos
13+
public int YSortAdjust { get; set; }
1314
public bool NewTheater { get; set; }
1415
public bool Theater { get; set; }
1516
public bool AltPalette { get; set; }
@@ -40,6 +41,7 @@ public void ReadFromIniSection(IniSection iniSection)
4041
Image = iniSection.GetStringValue(nameof(Image), Image);
4142
YDrawOffset = iniSection.GetIntValue(nameof(YDrawOffset), YDrawOffset);
4243
XDrawOffset = iniSection.GetIntValue(nameof(XDrawOffset), XDrawOffset);
44+
YSortAdjust = iniSection.GetIntValue(nameof(YSortAdjust), YSortAdjust);
4345
NewTheater = iniSection.GetBooleanValue(nameof(NewTheater), NewTheater);
4446
Theater = iniSection.GetBooleanValue(nameof(Theater), Theater);
4547
AltPalette = iniSection.GetBooleanValue(nameof(AltPalette), AltPalette);

src/TSMapEditor/Rendering/ObjectRenderers/BuildingRenderer.cs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.CodeAnalysis.CSharp.Syntax;
12
using Microsoft.Xna.Framework;
23
using System.Collections.Generic;
34
using TSMapEditor.CCEngine;
@@ -16,7 +17,9 @@ public BuildingRenderer(RenderDependencies renderDependencies) : base(renderDepe
1617

1718
private List<Animation> animationList = new List<Animation>();
1819

19-
DepthRectangle cachedDepth;
20+
private DepthRectangle cachedDepth;
21+
22+
private bool isDrawingBib = false;
2023

2124
public override void InitDrawForObject(Structure gameObject)
2225
{
@@ -114,7 +117,7 @@ private float GetFoundationCenterXPoint(Structure gameObject)
114117
return (float)foundation.Width / (foundation.Width + foundation.Height);
115118
}
116119

117-
private DepthRectangle GetDepthForAnimation(Structure gameObject, Rectangle drawingBounds)
120+
private DepthRectangle GetDepthForAnimation(Structure gameObject, Rectangle drawingBounds, bool isBelowBuilding)
118121
{
119122
float foundationCenterXPoint = GetFoundationCenterXPoint(gameObject);
120123
int distRight = (int)(drawingBounds.Width * (1.0f - foundationCenterXPoint));
@@ -132,7 +135,11 @@ private DepthRectangle GetDepthForAnimation(Structure gameObject, Rectangle draw
132135
bottom += heightReferenceCell.Level * Constants.CellHeight;
133136
}
134137

135-
int yReference = CellMath.CellBottomPointFromCellCoords(southernmostFoundationCellCoords, Map);
138+
int yReference;
139+
if (isBelowBuilding)
140+
yReference = CellMath.CellTopLeftPointFromCellCoords(gameObject.Position, Map).Y;
141+
else
142+
yReference = CellMath.CellBottomPointFromCellCoords(southernmostFoundationCellCoords, Map);
136143

137144
float topDepth = CellMath.GetDepthForPixelInCube(y, 0, yReference, heightReferenceCell, Map);
138145
float bottomDepth = CellMath.GetDepthForPixelInCube(bottom, 0, yReference, heightReferenceCell, Map);
@@ -181,23 +188,35 @@ protected override DepthRectangle GetDepthFromPosition(Structure gameObject, Rec
181188

182189
// drawingBounds includes effect of height, which is undesirable for depth rendering
183190
int y = drawingBounds.Y;
191+
int bottom = drawingBounds.Bottom;
184192

185193
if (heightReferenceCell != null && !RenderDependencies.EditorState.Is2DMode)
186194
{
187195
y += heightReferenceCell.Level * Constants.CellHeight;
196+
bottom += heightReferenceCell.Level * Constants.CellHeight;
188197
}
189198

190-
int yReference = CellMath.CellBottomPointFromCellCoords(southernmostFoundationCellCoords, Map);
199+
if (isDrawingBib)
200+
{
201+
int topYReference = CellMath.CellTopLeftPointFromCellCoords(gameObject.Position, Map).Y;
202+
float topDepth = CellMath.GetDepthForPixelInCube(y, 0, topYReference, heightReferenceCell, Map);
203+
float bottomDepth = CellMath.GetDepthForPixelInCube(bottom, 0, topYReference, heightReferenceCell, Map);
204+
return new DepthRectangle(topDepth, bottomDepth);
205+
}
206+
else
207+
{
208+
int yReference = CellMath.CellBottomPointFromCellCoords(southernmostFoundationCellCoords, Map);
191209

192-
// Used for drawing turrets and stuff, just return maximum depth since they must be on top of the building
193-
float maxDepth = CellMath.GetDepthForPixelInCube(y, 0, yReference, heightReferenceCell, Map);
210+
// Used for drawing turrets and stuff, just return maximum depth since they must be on top of the building
211+
float maxDepth = CellMath.GetDepthForPixelInCube(y, 0, yReference, heightReferenceCell, Map);
194212

195-
if (maxDepth < cachedDepth.TopLeft)
196-
return cachedDepth;
213+
if (maxDepth < cachedDepth.TopLeft)
214+
return cachedDepth;
197215

198-
cachedDepth = new DepthRectangle(maxDepth);
216+
cachedDepth = new DepthRectangle(maxDepth);
199217

200-
return cachedDepth;
218+
return cachedDepth;
219+
}
201220
}
202221

203222
private (DepthRectangle depthRectangle, Rectangle sourceRect) GetLeftDepthRectangle(Structure gameObject, PositionedTexture texture, Rectangle drawingBounds)
@@ -285,8 +304,10 @@ protected override float GetDepthAddition(Structure gameObject)
285304

286305
private void DrawBibGraphics(Structure gameObject, ShapeImage bibGraphics, Point2D drawPoint, in CommonDrawParams drawParams, bool affectedByLighting)
287306
{
307+
isDrawingBib = true;
288308
DrawShapeImage(gameObject, bibGraphics, 0, Color.White, true, gameObject.GetRemapColor(),
289309
affectedByLighting, !drawParams.ShapeImage.SubjectToLighting, drawPoint);
310+
isDrawingBib = false;
290311
}
291312

292313
protected override void Render(Structure gameObject, Point2D drawPoint, in CommonDrawParams drawParams)
@@ -314,7 +335,7 @@ protected override void Render(Structure gameObject, Point2D drawPoint, in Commo
314335

315336
// Sort the anims according to their settings
316337
animationList.Sort((anim1, anim2) =>
317-
anim1.BuildingAnimDrawConfig.SortValue.CompareTo(anim2.BuildingAnimDrawConfig.SortValue));
338+
anim1.BuildingAnimDrawConfig.SortValue(anim1.AnimType.ArtConfig.YSortAdjust).CompareTo(anim2.BuildingAnimDrawConfig.SortValue(anim2.AnimType.ArtConfig.YSortAdjust)));
318339

319340
bool affectedByAmbient = !affectedByLighting;
320341

@@ -323,14 +344,14 @@ protected override void Render(Structure gameObject, Point2D drawPoint, in Commo
323344
{
324345
var anim = animationList[i];
325346

326-
if (anim.BuildingAnimDrawConfig.SortValue < 0)
347+
if (anim.BuildingAnimDrawConfig.SortValue(anim.AnimType.ArtConfig.YSortAdjust) < 0)
327348
{
328349
var animShape = TheaterGraphics.AnimTextures[anim.AnimType.Index];
329350
if (animShape != null)
330351
{
331352
DrawAnimationImage(gameObject, anim, animShape, anim.GetFrameIndex(animShape.GetFrameCount()),
332353
nonRemapColor, true, gameObject.GetRemapColor(), affectedByLighting, affectedByAmbient,
333-
drawPoint, depthAddition);
354+
drawPoint, Constants.DepthEpsilon, true);
334355
}
335356
}
336357
}
@@ -356,7 +377,7 @@ protected override void Render(Structure gameObject, Point2D drawPoint, in Commo
356377
{
357378
var anim = animationList[i];
358379

359-
if (anim.BuildingAnimDrawConfig.SortValue >= 0)
380+
if (anim.BuildingAnimDrawConfig.SortValue(anim.AnimType.ArtConfig.YSortAdjust) >= 0)
360381
{
361382
// It gets challenging to handle depth if the anim renderer draws anims that are "above" the building,
362383
// as it does not have proper context of the building it's drawing on.
@@ -367,7 +388,7 @@ protected override void Render(Structure gameObject, Point2D drawPoint, in Commo
367388
{
368389
DrawAnimationImage(gameObject, anim, animShape, anim.GetFrameIndex(animShape.GetFrameCount()),
369390
nonRemapColor, true, gameObject.GetRemapColor(), affectedByLighting, affectedByAmbient,
370-
drawPoint, depthAddition);
391+
drawPoint, 0, false);
371392
}
372393
}
373394
}
@@ -462,7 +483,7 @@ private void DrawBuildingImage(Structure gameObject, ShapeImage image, int frame
462483

463484
private void DrawAnimationImage(Structure gameObject, Animation animation, ShapeImage image, int frameIndex, Color color,
464485
bool drawRemap, Color remapColor, bool affectedByLighting, bool affectedByAmbient, Point2D drawPoint,
465-
float depthAddition = 0f)
486+
float depthAddition, bool isBelowBuilding)
466487
{
467488
if (image == null)
468489
return;
@@ -496,9 +517,11 @@ private void DrawAnimationImage(Structure gameObject, Animation animation, Shape
496517
}
497518
}
498519

499-
var depthRectangle = GetDepthForAnimation(gameObject, drawingBounds);
520+
var depthRectangle = GetDepthForAnimation(gameObject, drawingBounds, isBelowBuilding);
500521
depthRectangle += depthAddition;
501-
depthRectangle += GetDepthAddition(gameObject);
522+
523+
if (!isBelowBuilding)
524+
depthRectangle += GetDepthAddition(gameObject);
502525

503526
RenderFrame(gameObject, frame, remapFrame, color, drawRemap, remapColor,
504527
drawingBounds, image.GetPaletteTexture(), lighting, depthRectangle);

0 commit comments

Comments
 (0)