Skip to content

Commit 5e80f05

Browse files
authored
Update README.md
1 parent d5a65b1 commit 5e80f05

File tree

1 file changed

+67
-36
lines changed

1 file changed

+67
-36
lines changed

README.md

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# 🗺️ Tilemap Pathfinding Game
22

3-
This project is based on **Unity Week 5: Two-dimensional Scene-building and Path-finding**. It demonstrates how to construct a 2D scene using tilemaps and implements path-finding using both **BFS** and **Dijkstra's algorithm**.
3+
This project demonstrates 2D scene construction using tilemaps and pathfinding algorithms in Unity. It builds on concepts from **Unity Week 5: Two-dimensional Scene-building and Pathfinding**, incorporating both **BFS** and **Dijkstra's algorithm** for navigation.
44

5-
📖 **Text explanations** for the foundational concepts are available [here](https://github.com/gamedev-at-ariel/gamedev-5782) in folder 07.
5+
📖 **Foundational text explanations** are available [here](https://github.com/gamedev-at-ariel/gamedev-5782) in folder 07.
66

77
---
88

@@ -38,25 +38,25 @@ This project is based on **Unity Week 5: Two-dimensional Scene-building and Path
3838
```csharp
3939
void Awake()
4040
{
41-
// Initialize with only grass and swamp as allowed tiles
42-
allowedTileList = new List<TileBase> { grassTile, swampTile };
43-
Debug.Log("Initial allowed tiles: " + string.Join(", ", allowedTileList));
41+
InitializeAllowedTiles();
42+
}
43+
44+
private void InitializeAllowedTiles()
45+
{
46+
allowedTileSet = new HashSet<TileBase>(defaultAllowedTiles);
47+
Debug.Log("Initial allowed tiles: " + string.Join(", ", allowedTileSet));
4448
}
4549

4650
public void AddTile(TileBase tile)
4751
{
48-
if (!allowedTileList.Contains(tile))
52+
if (tile != null && !allowedTileSet.Contains(tile))
4953
{
50-
allowedTileList.Add(tile);
54+
allowedTileSet.Add(tile);
5155
Debug.Log($"Tile '{tile.name}' added to allowed tiles.");
5256
}
5357
}
5458
```
5559

56-
- **Highlights**:
57-
- Starts with **grass** and **swamp** as valid movement tiles.
58-
- Updates dynamically when power-ups are collected.
59-
6060
---
6161

6262
### **2. ItemTileDetector** 🎯
@@ -68,16 +68,18 @@ switch (tileName)
6868
{
6969
case "BoatTile":
7070
allowedTiles.AddTile(TileManager.Instance.GetDeepSeaTile());
71-
allowedTiles.AddTile(TileManager.Instance.GetMediumSeaTile());
7271
Debug.Log("Boat collected! You can now sail on water.");
7372
break;
73+
7474
case "GoatTile":
7575
allowedTiles.AddTile(TileManager.Instance.GetMountainTile());
7676
Debug.Log("Goat collected! Mountains are now passable.");
7777
break;
78+
7879
case "PickaxeTile":
7980
PlayerInteraction.Instance.EnableTileModification(
80-
TileManager.Instance.GetMountainTile(), TileManager.Instance.GetGrassTile()
81+
TileManager.Instance.GetMountainTile(),
82+
TileManager.Instance.GetGrassTile()
8183
);
8284
Debug.Log("Pickaxe collected! You can now transform mountains into grass.");
8385
break;
@@ -86,32 +88,34 @@ switch (tileName)
8688

8789
---
8890

89-
### **3. StartGameManager** 🖼️
91+
### **3. TilemapGraph** 🔗
9092

91-
**Purpose**: Manages the start and game-over states, including transitions between scenes.
93+
**Purpose**: Represents the tilemap as a graph for pathfinding. Provides neighbors and edge weights for Dijkstra's Algorithm.
9294

9395
```csharp
94-
public void ShowGameOver(string message)
96+
public float GetEdgeWeight(Vector3Int from, Vector3Int to)
9597
{
96-
if (gameOverPanel != null)
98+
TileBase tile = tilemap.GetTile(to);
99+
if (tile != null && tileWeights.ContainsKey(tile))
97100
{
98-
gameOverPanel.SetActive(true);
99-
100-
if (gameOverText != null)
101-
{
102-
gameOverText.text = message; // Display the specific Game Over message
103-
}
101+
return tileWeights[tile];
104102
}
103+
return float.MaxValue; // Treat unknown tiles as impassable
104+
}
105105

106-
Time.timeScale = 0; // Pause the game
107-
StartCoroutine(WaitAndLoadNextScene());
106+
public IEnumerable<Vector3Int> Neighbors(Vector3Int node)
107+
{
108+
foreach (var direction in directions)
109+
{
110+
Vector3Int neighborPos = node + direction;
111+
TileBase neighborTile = tilemap.GetTile(neighborPos);
112+
113+
if (neighborTile != null)
114+
yield return neighborPos;
115+
}
108116
}
109117
```
110118

111-
- **Highlights**:
112-
- Displays a specific **Game Over** message when the player steps on an invalid tile.
113-
- Automatically transitions to the next scene after a delay.
114-
115119
---
116120

117121
### **4. Dijkstra Algorithm Integration** 📈
@@ -130,13 +134,40 @@ public static List<NodeType> GetPath<NodeType>(
130134
var distances = new Dictionary<NodeType, float>();
131135
var previousNodes = new Dictionary<NodeType, NodeType>();
132136
var visited = new HashSet<NodeType>();
133-
// ... Implementation continues
137+
// Implementation continues
134138
}
135139
```
136140

137-
- **Highlights**:
138-
- Implements **Dijkstra’s algorithm** to find the least-cost path in a weighted graph.
139-
- Uses the `TilemapGraph` to represent the tilemap.
141+
---
142+
143+
### **5. TargetMover** 🎯
144+
145+
**Purpose**: Handles click-based movement on the tilemap. Ensures that clicks are valid and calculates the least-cost path to the target using Dijkstra's Algorithm.
146+
147+
```csharp
148+
public void SetTarget(Vector3 newTarget)
149+
{
150+
Vector3Int gridPosition = tilemap.WorldToCell(newTarget);
151+
152+
if (!tilemap.HasTile(gridPosition))
153+
{
154+
Debug.LogWarning("Invalid Target: Clicked outside the tilemap.");
155+
return;
156+
}
157+
158+
if (gridPosition == tilemap.WorldToCell(transform.position))
159+
{
160+
Debug.LogWarning("Invalid Target: You are already on this tile.");
161+
return;
162+
}
163+
164+
targetInWorld = tilemap.GetCellCenterWorld(gridPosition);
165+
targetInGrid = gridPosition;
166+
atTarget = false;
167+
168+
StartCoroutine(MoveTowardsTheTarget());
169+
}
170+
```
140171

141172
---
142173

@@ -145,9 +176,9 @@ public static List<NodeType> GetPath<NodeType>(
145176
### **Part 1: Tilemap Movement**
146177
1. Use the **arrow keys** to move the player.
147178
2. Collect items to unlock movement across specific tiles:
148-
- **Boat**: Sail on water.
149-
- **Goat**: Climb mountains.
150-
- **Pickaxe**: Transform mountains into grass.
179+
- 🛶 **Boat**: Sail on water.
180+
- 🐐 **Goat**: Climb mountains.
181+
- ⛏️ **Pickaxe**: Transform mountains into grass.
151182
3. Step on an invalid tile to transition to the next part.
152183

153184
### **Part 2: Dijkstra Navigation**

0 commit comments

Comments
 (0)