using DunGen.Graph;
using System;
using UnityEngine;
namespace DunGen
{
///
/// A container for all of the information about a tile's posoitioning in the generated dungeon
///
[Serializable]
public sealed class TilePlacementData
{
///
/// Gets the depth of this tile in the dungeon along the main path
///
public int PathDepth
{
get { return pathDepth; }
internal set { pathDepth = value; }
}
///
/// Gets the normalized depth (0.0-1.0) of this tile in the dungeon along the main path
///
public float NormalizedPathDepth
{
get { return normalizedPathDepth; }
internal set { normalizedPathDepth = value; }
}
///
/// Gets the depth of this tile in the dungeon along the branch it's on
///
public int BranchDepth
{
get { return branchDepth; }
internal set { branchDepth = value; }
}
///
/// Gets the normalized depth (0.0-1.0) of this tile in the dungeon along the branch it's on
///
public float NormalizedBranchDepth
{
get { return normalizedBranchDepth; }
internal set { normalizedBranchDepth = value; }
}
///
/// An ID representing which branch the tile belongs to. All tiles on the same branch will share an ID.
/// Will be -1 for tiles not on a branch
///
public int BranchId
{
get { return branchId; }
internal set { branchId = value; }
}
///
/// Whether or not this tile lies on the dungeon's main path
///
public bool IsOnMainPath
{
get { return isOnMainPath; }
internal set { isOnMainPath = value; }
}
///
/// The boundaries of this tile
///
public Bounds Bounds
{
get => worldBounds;
private set => worldBounds = value;
}
///
/// The local boundaries of this tile
///
public Bounds LocalBounds
{
get { return localBounds; }
internal set
{
localBounds = value;
RecalculateTransform();
}
}
public TilePlacementParameters PlacementParameters
{
get { return placementParameters; }
internal set { placementParameters = value; }
}
public GraphNode GraphNode => placementParameters.Node;
public GraphLine GraphLine => placementParameters.Line;
public DungeonArchetype Archetype => placementParameters.Archetype;
public TileSet TileSet
{
get { return tileSet; }
internal set { tileSet = value; }
}
public Vector3 Position
{
get { return position; }
set
{
position = value;
RecalculateTransform();
}
}
public Quaternion Rotation
{
get { return rotation; }
set
{
rotation = value;
RecalculateTransform();
}
}
public Matrix4x4 Transform { get; private set; }
///
/// Gets the depth of this tile. Returns the branch depth if on a branch path, otherwise, returns the main path depth
///
public int Depth { get { return (isOnMainPath) ? pathDepth : branchDepth; } }
///
/// Gets the normalized depth (0-1) of this tile. Returns the branch depth if on a branch path, otherwise, returns the main path depth
///
public float NormalizedDepth { get { return (isOnMainPath) ? normalizedPathDepth : normalizedBranchDepth; } }
///
/// Data about how this tile was injected, or null if it was not placed using tile injection
///
public InjectedTile InjectionData { get; set; }
[SerializeField]
private int pathDepth;
[SerializeField]
private float normalizedPathDepth;
[SerializeField]
private int branchDepth;
[SerializeField]
private float normalizedBranchDepth;
[SerializeField]
private int branchId = -1;
[SerializeField]
private bool isOnMainPath;
[SerializeField]
private Bounds localBounds;
[SerializeField]
private Bounds worldBounds;
[SerializeField]
private TilePlacementParameters placementParameters;
[SerializeField]
private TileSet tileSet;
[SerializeField]
private Vector3 position = Vector3.zero;
[SerializeField]
private Quaternion rotation = Quaternion.identity;
public TilePlacementData()
{
RecalculateTransform();
}
public TilePlacementData(TilePlacementData copy)
{
PathDepth = copy.PathDepth;
NormalizedPathDepth = copy.NormalizedPathDepth;
BranchDepth = copy.BranchDepth;
NormalizedBranchDepth = copy.NormalizedDepth;
BranchId = copy.BranchId;
IsOnMainPath = copy.IsOnMainPath;
LocalBounds = copy.LocalBounds;
Transform = copy.Transform;
PlacementParameters = copy.PlacementParameters;
TileSet = copy.TileSet;
InjectionData = copy.InjectionData;
position = copy.position;
rotation = copy.rotation;
RecalculateTransform();
}
public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
{
this.position = position;
this.rotation = rotation;
RecalculateTransform();
}
private void RecalculateTransform()
{
Transform = Matrix4x4.TRS(position, rotation, Vector3.one);
Vector3 min = Transform.MultiplyPoint(localBounds.min);
Vector3 max = Transform.MultiplyPoint(localBounds.max);
Vector3 size = max - min;
Vector3 center = min + size / 2f;
size.x = Mathf.Abs(size.x);
size.y = Mathf.Abs(size.y);
size.z = Mathf.Abs(size.z);
Bounds = new Bounds(center, size);
}
}
}