21.7 C
New York
Monday, April 28, 2025

C# – Stairs and isometric ramps


I’m attempting to implement a Tilemap in Godot, nevertheless, it appears that evidently I can not remedy an issue associated to the steps/ramps. These mosaics appear to occupy a couple of mosaic area (roughly 4) and I can not discover a technique to consistently assign the place of the mouse to the true coordinates. This may also have an effect on the discovering of my path, since I might anticipate the steps to be neighbors of my common flat tiles.

I’m utilizing layers of tile maps.

TEJES SETTING:

  • Mosaic kind: isometric
  • Mosaic Design: DIAMOND DOWN
  • Mosaic displacement axis: vertical displacement
  • Mosaic measurement: 32px – 16px

Right here is my code:

public partial class MapManager : Node
{
    personal const int TileWidth = 32;
    personal const int TileHeight = 32;
    
    personal readonly Dictionary _tilemaps = new();
    personal readonly Dictionary _elevations = new();
    
    personal readonly Dictionary _coordinatesToPoints = new();
    personal readonly Dictionary _pointsToCoordinates = new();
    
    personal readonly Vector2 _tileSize = new(TileWidth, TileHeight);
    personal readonly AStar3D _astar = new();

    public override void _Ready()
    {
        SetupTilemaps();
        SetupAStar();
    }
    
    public override void _Process(double delta)
    {
        
        var tile = GetHoveredTile();

        if (tile != null)
        {
            var tilemap = _tilemaps(tile.Worth.Z);
            var information = tilemap.GetCellTileData(tile.Worth.ToVector2I());

            GD.Print((string)information.GetCustomData("tile-name"));
        }
    }
    
    personal Vector3I() GetNeighbours(Vector3I level)
        => _astar.GetPointConnections(_coordinatesToPoints(level)).Choose(p => _pointsToCoordinates(p)).ToArray();
    
    personal lengthy CoordsToId(Vector3I coords) 
        => _coordinatesToPoints(coords);

    personal Vector3I IdToCoords(int id)
        => _pointsToCoordinates(id);

    personal void SetupTilemaps()
    {
        var tilemaps = this.GetChildrenOfType();
        var elevation = 0;

        foreach (var tilemap in tilemaps.Reverse())
        {
            _tilemaps(elevation--) = tilemap;
        }
    }

    personal void SetupAStar()
    {
        _astar.Clear();
        
        foreach (var (elevation, tilemap) in _tilemaps)
        {
            foreach (var coords in tilemap.GetUsedCells())
            {
                var coords3D = new Vector3I(coords.X, coords.Y, elevation);
                var id = _astar.GetAvailablePointId();
                
                _coordinatesToPoints(coords3D) = id;
                _pointsToCoordinates(id) = coords3D;

                _elevations(coords3D) = elevation;
                _astar.AddPoint(id, coords3D);
            }
        }
        
        foreach (var (elevation, tilemap) in _tilemaps)
        {
            foreach (var coords in tilemap.GetUsedCells())
            {
                ConnectNeighbors(new Vector3I(coords.X, coords.Y, elevation));
            }
        }
    }

    personal void ConnectNeighbors(Vector3I coords)
    {
        var coords2d = new Vector2I(coords.X, coords.Y);
        var currentId = CoordsToId(coords);

        Vector3I() neighbors = {
            // Identical elevation
            new(1, 0, 0), new(-1, 0, 0),
            new(0, 1, 0), new(0, -1, 0),
            
            // Beneath
            new(1, 0, -1), new(-1, 0, -1),
            new(0, 1, -1), new(0, -1, -1)
        };
        
        foreach (var offset in neighbors)
        {
            var neighborCoords = coords + offset;

            if (!_tilemaps.TryGetValue(neighborCoords.Z, out var tilemap))
            {
                proceed;
            }
            
            var neighbourCellId = tilemap.GetCellSourceId(new Vector2I(coords2d.X + offset.X, coords2d.Y + offset.Y));
            
            if (neighbourCellId == -1)
            {
                proceed;
            }

            if (IsValidConnection(coords, neighborCoords))
            {
                _astar.ConnectPoints(currentId, CoordsToId(neighborCoords), false);
            }
        }
    }
    
    personal bool IsValidConnection(Vector3I currentCoords, Vector3I potentialNeighborCoords)
    {
        return true;
    }

    public Vector3I? GetHoveredTile()
    {
        // PROBLEM HERE
        
        foreach (var (elevation, tilemap) in _tilemaps)
        {
            var tileCoords = tilemap.LocalToMap(tilemap.GetLocalMousePosition());
            
            if (tilemap.CellExistsAtCoordinates(tileCoords))
            {
                return new Vector3I(tileCoords.X, tileCoords.Y, elevation);
            }
        }
        
        return null;
    }

    public Vector3() FindAStarPath(Vector3I begin, Vector3I purpose)
    {
        var startId = CoordsToId(begin);
        var goalId = CoordsToId(purpose);

        if (!_astar.HasPoint(startId) || !_astar.HasPoint(goalId))
        {
            GD.PrintErr("No factors discovered for begin/purpose");
            return Array.Empty();
        }
        
        return _astar.GetPointPath(startId, goalId);
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles