// 27 Slicer
// Copyright 2021 Deftly Games
// https://slicer.deftly.games/
using Slicer.Core;
using System;
using UnityEngine;
namespace Slicer
{
///
/// The base class for Slice Modifiers.
///
///
///
[REFERENCE MANUAL](xref:manual\components\slice_modifiers)
///
public abstract class SliceModifier : MonoBehaviour
{
///
/// Is this modifier currently enabled.
///
/// true by default.
[HideInInspector]
[NonSerialized]
public bool ModifierEnabled = true;
///
/// When set to true the properties for this modifier will no longer be updated when Unity is playing and its parent SlicerController is in edit mode.
///
///
/// This is useful to have turned on when you want the physics simulation to take over control of this modifier.
///
[Tooltip("Only allow updating when Unity is not playing.")]
public bool DoNotUpdateInPlayMode = true;
///
/// Gathers details required for applying this modification.
///
public virtual Hash128 GatherDetails()
{
var enabledHash = HashUtility.CalculateHash(ModifierEnabled, 1);
return enabledHash;
}
///
/// Applies the modification to the tracked items.
///
/// The final size (as a scale) of all the items that are to be sliced. See for more information.
/// The transform of the parent
/// The complete bounding box of all sliced items after being sliced. See for more information.
/// The bounding box of the slices that will be made. See for more information.
public abstract void Modify(Vector3 size, Transform rootTransform, Bounds completeBounds, Bounds slicedBounds);
///
/// Enables this Slice Modifier.
///
public virtual void EnableModifier()
{
ModifierEnabled = true;
}
///
/// Disables this Slice Modifier.
///
public virtual void DisableModifier()
{
ModifierEnabled = false;
}
///
/// Finalizes slicing for this Slice Modifier.
///
public abstract void FinalizeSlicing();
///
/// Returns true when this SliceModifier is able to update its modification.
///
protected bool ShouldUpdate()
{
return !DoNotUpdateInPlayMode || (DoNotUpdateInPlayMode && !Application.isPlaying);
}
///
/// Gets the that is manging this modifier.
///
///
/// This iterates over it's ancestors Game Object. So storing the result may be more performant than repeatedly calling this function.
///
/// The parent slicer controller.
public SlicerController GetParentSlicerController()
{
var slicerController = GetParentSlicerController(transform.parent);
return slicerController;
}
private SlicerController GetParentSlicerController(Transform transform)
{
var hasComponent = transform.TryGetComponent(out var slicerController);
if (!hasComponent)
{
GetParentSlicerController(transform.parent);
}
return slicerController;
}
}
}