Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GidiRabi committed Dec 19, 2024
1 parent 9120da6 commit b15e95c
Show file tree
Hide file tree
Showing 13 changed files with 762 additions and 397 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"visualstudiotoolsforunity.vstuc"
]
}
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Unity",
"type": "vstuc",
"request": "attach"
}
]
}
60 changes: 60 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.vs": true,
"**/.gitmodules": true,
"**/.vsconfig": true,
"**/*.booproj": true,
"**/*.pidb": true,
"**/*.suo": true,
"**/*.user": true,
"**/*.userprefs": true,
"**/*.unityproj": true,
"**/*.dll": true,
"**/*.exe": true,
"**/*.pdf": true,
"**/*.mid": true,
"**/*.midi": true,
"**/*.wav": true,
"**/*.gif": true,
"**/*.ico": true,
"**/*.jpg": true,
"**/*.jpeg": true,
"**/*.png": true,
"**/*.psd": true,
"**/*.tga": true,
"**/*.tif": true,
"**/*.tiff": true,
"**/*.3ds": true,
"**/*.3DS": true,
"**/*.fbx": true,
"**/*.FBX": true,
"**/*.lxo": true,
"**/*.LXO": true,
"**/*.ma": true,
"**/*.MA": true,
"**/*.obj": true,
"**/*.OBJ": true,
"**/*.asset": true,
"**/*.cubemap": true,
"**/*.flare": true,
"**/*.mat": true,
"**/*.meta": true,
"**/*.prefab": true,
"**/*.unity": true,
"build/": true,
"Build/": true,
"Library/": true,
"library/": true,
"obj/": true,
"Obj/": true,
"Logs/": true,
"logs/": true,
"ProjectSettings/": true,
"UserSettings/": true,
"temp/": true,
"Temp/": true
},
"dotnet.defaultSolution": "05-tilemap-pathfinding.sln"
}
794 changes: 404 additions & 390 deletions Assembly-CSharp.csproj

Large diffs are not rendered by default.

30 changes: 27 additions & 3 deletions Assets/Scripts/1-tiles/AllowedTiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@
using UnityEngine.Tilemaps;

/**
* This component just keeps a list of allowed tiles.
* Such a list is used both for pathfinding and for movement.
* This component manages a list of allowed tiles.
* The list can be updated dynamically based on game interactions.
*/
public class AllowedTiles : MonoBehaviour {
[SerializeField] TileBase[] allowedTiles = null;

/**
* Checks if a tile is in the allowed tiles list.
* @param tile The tile to check.
* @return True if the tile is allowed, otherwise false.
*/
public bool Contains(TileBase tile) {
return allowedTiles.Contains(tile);
}

public TileBase[] Get() { return allowedTiles; }
/**
* Retrieves the current list of allowed tiles.
* @return An array of allowed tiles.
*/
public TileBase[] Get() {
return allowedTiles;
}

/**
* Updates the allowed tiles dynamically during gameplay.
* @param newTiles An array of TileBase objects to set as the new allowed tiles.
*/
public void UpdateAllowedTiles(TileBase[] newTiles) {
if (newTiles == null || newTiles.Length == 0) {
Debug.LogError("New allowed tiles array is null or empty!");
return;
}
allowedTiles = newTiles;
Debug.Log("Allowed tiles updated successfully!");
}
}
54 changes: 50 additions & 4 deletions Assets/Scripts/2-player/KeyboardMoverByTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,72 @@
using UnityEngine.Tilemaps;

/**
* This component allows the player to move by clicking the arrow keys,
* but only if the new position is on an allowed tile.
* This component allows the player to move using the arrow keys,
* but only if the new position is on an allowed tile.
* It also handles interactions with items like the boat and goat.
*/
public class KeyboardMoverByTile: KeyboardMover {
public class KeyboardMoverByTile : KeyboardMover {
[SerializeField] Tilemap tilemap = null;
// [SerializeField] TileBase[] allowedTiles = null;
[SerializeField] AllowedTiles allowedTiles = null;
[SerializeField] private bool isCarryingItem = false; // Tracks whether the player is carrying an item.

/**
* Gets the tile at a given world position.
* @param worldPosition The world position to check.
* @return The TileBase object at the specified position.
*/
private TileBase TileOnPosition(Vector3 worldPosition) {
Vector3Int cellPosition = tilemap.WorldToCell(worldPosition);
return tilemap.GetTile(cellPosition);
}

/**
* Updates player movement and handles interactions with items.
*/
void Update() {
Vector3 newPosition = NewPosition();
TileBase tileOnNewPosition = TileOnPosition(newPosition);

if (allowedTiles.Contains(tileOnNewPosition)) {
transform.position = newPosition;
HandleInteraction(tileOnNewPosition);
} else {
Debug.LogError("You cannot walk on " + tileOnNewPosition + "!");
}
}

/**
* Handles interactions when the player moves onto a tile with an item.
* @param tile The tile that the player has moved onto.
*/
private void HandleInteraction(TileBase tile) {
// Example interaction logic:
if (tile.name == "Boat") { // Replace with the actual name of the boat tile.
Debug.Log("Player picked up the boat!");
isCarryingItem = true;
UpdateAllowedTilesForBoat();
} else if (tile.name == "Goat") { // Replace with the actual name of the goat tile.
Debug.Log("Player picked up the goat!");
isCarryingItem = true;
UpdateAllowedTilesForGoat();
}
}

/**
* Updates allowed tiles for when the player carries the boat.
*/
private void UpdateAllowedTilesForBoat() {
TileBase[] newAllowedTiles = { /* Add water and other relevant tiles here */ };
allowedTiles.UpdateAllowedTiles(newAllowedTiles);
Debug.Log("Allowed tiles updated for carrying the boat.");
}

/**
* Updates allowed tiles for when the player carries the goat.
*/
private void UpdateAllowedTilesForGoat() {
TileBase[] newAllowedTiles = { /* Add grass and other relevant tiles here */ };
allowedTiles.UpdateAllowedTiles(newAllowedTiles);
Debug.Log("Allowed tiles updated for carrying the goat.");
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/5-Items.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions Assets/Scripts/5-Items/Boat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using UnityEngine;
using UnityEngine.Tilemaps;

/**
* This component manages the boat's interactions.
* Handles player pickup, updates allowed tiles for movement, and ensures compatibility with the game system.
*/
public class Boat : MonoBehaviour {
[SerializeField] AllowedTiles allowedTiles = null;

private bool isPickedUp = false; // Tracks whether the boat is picked up.

/**
* Handles the player's interaction with the boat.
* Updates allowed tiles when the boat is picked up.
* @param player The GameObject interacting with the boat.
*/
public void OnPlayerInteraction(GameObject player) {
if (!isPickedUp) {
Debug.Log("Boat picked up by player!");
isPickedUp = true;
UpdateAllowedTilesForBoat();
} else {
Debug.Log("Boat is already being carried!");
}
}

/**
* Updates the allowed tiles to include water tiles when the boat is picked up.
*/
private void UpdateAllowedTilesForBoat() {
TileBase[] waterTiles = { /* Add the specific water tiles here */ };
allowedTiles.UpdateAllowedTiles(waterTiles);
Debug.Log("Allowed tiles updated for boat movement.");
}

/**
* Resets the boat's state when dropped by the player.
* Updates allowed tiles back to the default state.
* @param player The GameObject dropping the boat.
*/
public void OnPlayerDrop(GameObject player) {
if (isPickedUp) {
Debug.Log("Boat dropped by player.");
isPickedUp = false;
ResetAllowedTiles();
}
}

/**
* Resets the allowed tiles back to the default state.
*/
private void ResetAllowedTiles() {
TileBase[] defaultTiles = { /* Add default tiles here */ };
allowedTiles.UpdateAllowedTiles(defaultTiles);
Debug.Log("Allowed tiles reset after boat drop.");
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/5-Items/Boat.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions Assets/Scripts/5-Items/Goat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using UnityEngine;
using UnityEngine.Tilemaps;

/**
* This component manages the goat's interactions.
* Handles player pickup, updates allowed tiles for movement, and ensures compatibility with the game system.
*/
public class Goat : MonoBehaviour {
[SerializeField] AllowedTiles allowedTiles = null;

private bool isPickedUp = false; // Tracks whether the goat is picked up.

/**
* Handles the player's interaction with the goat.
* Updates allowed tiles when the goat is picked up.
* @param player The GameObject interacting with the goat.
*/
public void OnPlayerInteraction(GameObject player) {
if (!isPickedUp) {
Debug.Log("Goat picked up by player!");
isPickedUp = true;
UpdateAllowedTilesForGoat();
} else {
Debug.Log("Goat is already being carried!");
}
}

/**
* Updates the allowed tiles to include goat-friendly terrain when picked up.
*/
private void UpdateAllowedTilesForGoat() {
TileBase[] goatAllowedTiles = { /* Add specific tiles here (e.g., grass) */ };
allowedTiles.UpdateAllowedTiles(goatAllowedTiles);
Debug.Log("Allowed tiles updated for goat movement.");
}

/**
* Resets the goat's state when dropped by the player.
* Updates allowed tiles back to the default state.
* @param player The GameObject dropping the goat.
*/
public void OnPlayerDrop(GameObject player) {
if (isPickedUp) {
Debug.Log("Goat dropped by player.");
isPickedUp = false;
ResetAllowedTiles();
}
}

/**
* Resets the allowed tiles back to the default state.
*/
private void ResetAllowedTiles() {
TileBase[] defaultTiles = { /* Add default tiles here */ };
allowedTiles.UpdateAllowedTiles(defaultTiles);
Debug.Log("Allowed tiles reset after goat drop.");
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/5-Items/Goat.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b15e95c

Please sign in to comment.