Skip to content

Commit 771b21b

Browse files
committed
Fix building turret rendering
1 parent 0f2767e commit 771b21b

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/TSMapEditor/Rendering/ObjectRenderers/BuildingRenderer.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ protected override bool ShouldRenderReplacementText(Structure gameObject)
108108
return base.ShouldRenderReplacementText(gameObject);
109109
}
110110

111+
protected override float GetDepthFromPosition(Structure gameObject, int bottomDrawPoint)
112+
{
113+
// As buildings can cover multiple cells and can also include turrets, the default implementation
114+
// is not suitable. For example, sprites can be rendered southward of voxel turrets facing north, leading them
115+
// the sprites to have higher depth and overlapping the voxel turrets.
116+
//
117+
// Instead, for buildings we calculate a positional depth value using the southernmost cell
118+
// of the building's foundation. This depth value is identical for the base building sprite
119+
// and turret, making it easy to draw the voxel either above or below the building
120+
// by applying DepthEpsilon.
121+
var southernmostCell = GetSouthernmostCell(gameObject);
122+
123+
int height = 0;
124+
if (southernmostCell != null)
125+
{
126+
height = southernmostCell.Level;
127+
}
128+
129+
return ((CellMath.CellTopLeftPointFromCellCoords(southernmostCell.CoordsToPoint(), Map).Y + Constants.CellSizeY) / (float)Map.HeightInPixelsWithCellHeight) * Constants.DownwardsDepthRenderSpace +
130+
(height * Constants.DepthRenderStep);
131+
}
132+
111133
protected override float GetDepthAddition(Structure gameObject)
112134
{
113135
float buildingAdditionalDepth = gameObject.ObjectType.EditorZAdjust / (float)Map.HeightInPixelsWithCellHeight;

src/TSMapEditor/Rendering/ObjectRenderers/ObjectRenderer.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,23 @@ public virtual void DrawShadow(T gameObject)
291291
RenderDependencies.ObjectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(null, texture, drawingBounds, new Color(255, 255, 255, 128), false, true, depth));
292292
}
293293

294-
private float GetDepthFromPosition(T gameObject, int bottomDrawPoint)
294+
protected virtual float GetDepthFromPosition(T gameObject, int bottomDrawPoint)
295295
{
296-
var southernmostCell = Map.GetTile(gameObject.Position); // GetSouthernmostCell(gameObject);
296+
// In this generic implementation, we only use the cell to fetch its height.
297+
// Otherwise, position-related depth is calculated from the bottom draw point of the object.
298+
var cell = Map.GetTile(gameObject.Position);
297299

298300
int height = 0;
299-
if (southernmostCell != null)
301+
if (cell != null)
300302
{
301-
height = southernmostCell.Level;
303+
height = cell.Level;
302304
}
303305

304-
return ((bottomDrawPoint + Constants.CellSizeY) / (float)Map.HeightInPixelsWithCellHeight) * Constants.DownwardsDepthRenderSpace + (height * Constants.DepthRenderStep);
306+
return ((bottomDrawPoint + (height * Constants.CellHeight) + Constants.CellSizeY) / (float)Map.HeightInPixelsWithCellHeight) * Constants.DownwardsDepthRenderSpace +
307+
(height * Constants.DepthRenderStep);
305308
}
306309

307-
private MapTile GetSouthernmostCell(T gameObject)
310+
protected MapTile GetSouthernmostCell(T gameObject)
308311
{
309312
MapTile tile = null;
310313

0 commit comments

Comments
 (0)