I’m in the early process of creating a top down 2D dungeon crawler using arrow keys for movement/interaction, and have been trying to implement a door tile. Scriptable Tiles seem pretty useful and I have been trying to get to grips with how they work, but I can’t seem to get the desired behaviour.
I have two scriptable tiles, NavigableTile and DoorTile, with DoorTile inheriting from NavigableTile.
When my player controller recieves movement input the following code checks the type of Tile the player is trying to move to and tests if it is set as isNavigable, if so player moves, if not it then checks to see if it is a closed door. If so it sets open and isNavigable to true.
void MoveOnAxis()
{
Vector2Int input = m_PlayerInput.GetMoveInput();
// if pressing keys on both axis default to vertical
// no diagonal movement
if (input.x != 0f && input.y != 0f) input.x = 0;
if (input != Vector2Int.zero)
{
Vector3Int targetGridPos = m_Grid.WorldToCell(transform.position + new Vector3(input.x, input.y, 0));
NavigableTile navTile = m_Tilemap.GetTile<NavigableTile>(targetGridPos);
if (navTile && navTile.isNavigable)
{
Debug.Log("Move into Tile : " + navTile.name);
transform.position = targetGridPos;
m_GameManager.EndTurn();
}
else if (navTile is DoorTile)
{
if (!((DoorTile)navTile).open)
{
Debug.Log("Opening a closed door");
((DoorTile)navTile).SetOpen(true);
}
m_Tilemap.RefreshTile(targetGridPos);
m_GameManager.EndTurn();
}
}
}
My DoorTile class:
public class DoorTile : NavigableTile
{
public Sprite openSprite;
public Sprite closedSprite;
public bool open;
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
base.GetTileData(position, tilemap, ref tileData);
if (open)
{
tileData.sprite = openSprite;
}
else
{
tileData.sprite = closedSprite;
}
}
public override void RefreshTile(Vector3Int position, ITilemap tilemap)
{
tilemap.RefreshTile(position);
}
public void SetOpen(bool isOpen)
{
if (isOpen)
{
open = true;
isNavigable = true;
}
else
{
open = false;
isNavigable = false;
}
}
}
This works in a sense in that when the player tries to move to a tile containing a closed door, the door sprite changes to openSprite and the tile can be moved through. However when I include multiple doors interacting with one door seems to modify all the DoorTiles in the scene, rather than the single Tile that I want. Is it possible to modify a single instance of a scripted tile like this?