Change the grid to come off of any point?

During the course we create a grid coming from 0,0 to a size we determine and it doesn’t allow us to go below 0,0.

How would we change it so that it could be at any point we determine? Say I wanted the grid to start at 5,10 instead. What would I add that allowed me to determine that start location?

Offset.

You can set an offset on the GridSystem and then include that in your calculations. Here is a version that only includes the bits with the offset change. All the other stuff remains as it is. Note that it also does not include the multi-layer bits. This is the GridSystem from the first part of the course

public class GridSystem<TGridObject>
{
    private Vector2 offset;

    public GridSystem(int width, int height, float cellSize, Vector2 offset, Func<GridSystem<TGridObject>, GridPosition, TGridObject> createGridObject)
    {
        this.offset = offset;
    }

    public Vector3 GetWorldPosition(GridPosition gridPosition)
    {
        return new Vector3(offset.x + gridPosition.x, 0, offset.y + gridPosition.z) * cellSize;
    }

    public GridPosition GetGridPosition(Vector3 worldPosition)
    {
        return new GridPosition(
            Mathf.RoundToInt((worldPosition.x - offset.x) / cellSize),
            Mathf.RoundToInt((worldPosition.z - offset.y) / cellSize)
        );
    }
}

Couldn’t have set it better myself.

Awesome thank you

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms