Skip to content

Commit

Permalink
Fix several bugs where cell lighting values were not updated on cell …
Browse files Browse the repository at this point in the history
…level change
  • Loading branch information
Rampastring committed Jun 23, 2024
1 parent 2a6a224 commit f0a2837
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/TSMapEditor/Mutations/Classes/DrawCliffMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private void PlaceTile(TileImage tile, Point2D targetCellCoords)

mapTile.ChangeTileIndex(tile.TileID, (byte)i);
mapTile.Level = (byte)Math.Min(originLevel + image.TmpImage.Height, Constants.MaxMapHeightLevel);
mapTile.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}
}
Expand All @@ -191,6 +192,7 @@ public override void Undo()
{
mapTile.ChangeTileIndex(data.TileIndex, data.SubTileIndex);
mapTile.Level = data.Level;
mapTile.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ protected void Process()

ApplyRamps();

RefreshLighting();

MutationTarget.InvalidateMap();
}

Expand All @@ -118,13 +120,24 @@ protected void ProcessCells()

protected abstract void ApplyRamps();

private void RefreshLighting()
{
for (int i = 0; i < totalProcessedCells.Count; i++)
{
var cellCoords = totalProcessedCells[i];
var cell = Map.GetTile(cellCoords);
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

public override void Undo()
{
foreach (var entry in undoData)
{
var cell = Map.GetTile(entry.CellCoords);
cell.ChangeTileIndex(entry.TileIndex, (byte)entry.SubTileIndex);
cell.Level = (byte)entry.HeightLevel;
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}

MutationTarget.InvalidateMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.Rendering;
using TSMapEditor.UI;

Expand Down Expand Up @@ -32,38 +33,38 @@ public override void Perform()
Point2D cellCoords = targetCellCoords + offset;

var cell = MutationTarget.Map.GetTile(cellCoords);
if (cell == null)
return;

if (cell.Level > 0)
{
cell.Level--;
affectedCells.Add(cellCoords);
}
LowerCellLevel(cell);
});
}
else
{
var targetTile = MutationTarget.Map.GetTile(targetCellCoords);

if (targetTile == null || targetTile.Level <= 0)
return;

var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics);

// Process tiles
foreach (Point2D cellCoords in tilesToProcess)
{
var cell = MutationTarget.Map.GetTile(cellCoords);

cell.Level--;
affectedCells.Add(cellCoords);
LowerCellLevel(cell);
}
}

MutationTarget.AddRefreshPoint(targetCellCoords);
}

private void LowerCellLevel(MapTile cell)
{
if (cell == null)
return;

if (cell.Level > 0)
{
cell.Level--;
affectedCells.Add(cell.CoordsToPoint());
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

public override void Undo()
{
foreach (Point2D cellCoords in affectedCells)
Expand All @@ -73,7 +74,10 @@ public override void Undo()
return;

if (cell.Level < Constants.MaxMapHeight)
{
cell.Level++;
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

MutationTarget.AddRefreshPoint(targetCellCoords);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.Rendering;
using TSMapEditor.UI;

Expand Down Expand Up @@ -32,38 +33,39 @@ public override void Perform()
Point2D cellCoords = targetCellCoords + offset;

var cell = MutationTarget.Map.GetTile(cellCoords);
if (cell == null)
return;

if (cell.Level < Constants.MaxMapHeightLevel)
{
cell.Level++;
affectedCells.Add(cellCoords);
}
RaiseCellLevel(cell);
});
}
else
{
var targetTile = MutationTarget.Map.GetTile(targetCellCoords);

if (targetTile == null || targetTile.Level >= Constants.MaxMapHeightLevel)
return;

var tilesToProcess = Helpers.GetFillAreaTiles(targetTile, MutationTarget.Map, MutationTarget.TheaterGraphics);

// Process tiles
foreach (Point2D cellCoords in tilesToProcess)
{
var cell = MutationTarget.Map.GetTile(cellCoords);

cell.Level++;
affectedCells.Add(cellCoords);
RaiseCellLevel(cell);
}
}

MutationTarget.AddRefreshPoint(targetCellCoords);
}

private void RaiseCellLevel(MapTile cell)
{
if (cell == null)
return;

if (cell.Level < Constants.MaxMapHeightLevel)
{
cell.Level++;
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
affectedCells.Add(cell.CoordsToPoint());
}
}

public override void Undo()
{
foreach (Point2D cellCoords in affectedCells)
Expand All @@ -73,7 +75,10 @@ public override void Undo()
return;

if (cell.Level > 0)
{
cell.Level--;
cell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

MutationTarget.AddRefreshPoint(targetCellCoords);
Expand Down
2 changes: 2 additions & 0 deletions src/TSMapEditor/Mutations/Classes/PlaceTerrainTileMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public override void Perform()
{
mapTile.ChangeTileIndex(tile.TileID, (byte)i);
mapTile.Level = (byte)Math.Min(originLevel + image.TmpImage.Height, Constants.MaxMapHeightLevel);
mapTile.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}
});
Expand Down Expand Up @@ -309,6 +310,7 @@ public override void Undo()
{
mapCell.ChangeTileIndex(originalTerrainData.TileIndex, originalTerrainData.SubTileIndex);
mapCell.Level = originalTerrainData.Level;
mapCell.RefreshLighting(Map.Lighting, MutationTarget.LightingPreviewState);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/TSMapEditor/Rendering/MapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface IMutationTarget
BrushSize BrushSize { get; }
Randomizer Randomizer { get; }
bool AutoLATEnabled { get; }
LightingPreviewMode LightingPreviewState { get; }
bool OnlyPaintOnClearGround { get; }
}

Expand Down Expand Up @@ -119,6 +120,7 @@ public MapView(WindowManager windowManager, Map map, TheaterGraphics theaterGrap
public BrushSize BrushSize { get => EditorState.BrushSize; set => EditorState.BrushSize = value; }
public bool Is2DMode => EditorState.Is2DMode;
public DeletionMode DeletionMode => EditorState.DeletionMode;
public LightingPreviewMode LightingPreviewState => EditorState.LightingPreviewState;
public Randomizer Randomizer => EditorState.Randomizer;
public bool AutoLATEnabled => EditorState.AutoLATEnabled;
public bool OnlyPaintOnClearGround => EditorState.OnlyPaintOnClearGround;
Expand Down

0 comments on commit f0a2837

Please sign in to comment.