38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class TileSpace : ITileSpace
|
|
{
|
|
public float TileSize { get; set; }
|
|
|
|
public World World { get; set; }
|
|
|
|
public int2 WorldToTile(Vector3 position)
|
|
{
|
|
var tileX = Mathf.FloorToInt(position.x / TileSize);
|
|
var tileY = Mathf.FloorToInt(position.z / TileSize);
|
|
return new int2(tileX, tileY);
|
|
}
|
|
|
|
public Vector3 TileToWorld(int2 tile, float tileX = 0.5f, float tileY = 0.5f)
|
|
{
|
|
tileX = Mathf.Clamp01(tileX);
|
|
tileY = Mathf.Clamp01(tileY);
|
|
return new Vector3((tile.x + tileX) * TileSize, World.Heightmap.GetValue(tile), (tile.y + tileY) * TileSize);
|
|
}
|
|
|
|
public int GetElevation(float y)
|
|
{
|
|
return Mathf.RoundToInt(y);
|
|
}
|
|
|
|
public Vector3 GetRectWorldCenter(in TileRect rect)
|
|
{
|
|
var worldCenter = TileToWorld(rect.Min, 0, 0);
|
|
worldCenter += new Vector3(rect.Width, 0, rect.Height) * (0.5f * TileSize);
|
|
return worldCenter;
|
|
}
|
|
}
|
|
} |