Skip to content

Commit 6fafb32

Browse files
authored
Background Tile and Tile color tinting (#110)
1 parent 5f16ca7 commit 6fafb32

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Platformer2D/Core/Game/Level.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ private Tile LoadTile(char tileType, int x, int y)
387387
case 'P':
388388
return LoadStartTile(x, y);
389389

390+
// Background block
391+
case ',':
392+
Tile tile = LoadVarietyTile("BlockB", 2, TileCollision.Passable);
393+
tile.TintColor = new Color(120, 130, 150);
394+
return tile;
395+
390396
// Unknown tile type character
391397
default:
392398
throw new NotSupportedException(String.Format(Resources.ErrorUnsupportedTileType, tileType, x, y));
@@ -830,8 +836,9 @@ private void DrawTiles(SpriteBatch spriteBatch)
830836
int right = (int)(left + screenManager.BaseScreenSize.X / Tile.Width);
831837
right = Math.Min(right, Width - 1);
832838

833-
// Reuse a single Vector2 object for tile positions to reduce memory allocations.
839+
// Reuse a single Vector2 and Color object for tile positions and tint color to reduce memory allocations.
834840
var position = new Vector2();
841+
var tintColor = Color.White;
835842

836843
// Loop through each tile position within the visible range.
837844
for (int y = 0; y < Height; ++y)
@@ -845,7 +852,9 @@ private void DrawTiles(SpriteBatch spriteBatch)
845852
position.X = x * Tile.Size.X;
846853
position.Y = y * Tile.Size.Y;
847854

848-
spriteBatch.Draw(texture, position, Color.White);
855+
tintColor = tiles[x, y].TintColor;
856+
857+
spriteBatch.Draw(texture, position, tintColor);
849858
}
850859
}
851860
}

Platformer2D/Core/Game/Tile.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ struct Tile
1313
/// </summary>
1414
public Texture2D Texture;
1515

16+
/// <summary>
17+
/// Specifies the color tint to apply to the Texture when drawing.
18+
/// </summary>
19+
public Color TintColor = Color.White;
20+
1621
/// <summary>
1722
/// The type of collision behavior this tile exhibits.
1823
/// </summary>

0 commit comments

Comments
 (0)