Skip to content

Commit

Permalink
Snap rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal78900 committed May 20, 2024
1 parent 7cd9d8d commit 8786800
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
29 changes: 13 additions & 16 deletions Assets/DONT TOUCH/Scripts/BlockComponents/PrimitiveComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DONT_TOUCH.Scripts.Editors;
using UnityEditor;
using UnityEngine;
using Random = UnityEngine.Random;

[ExecuteInEditMode]
public class PrimitiveComponent : SchematicBlock
Expand All @@ -16,21 +17,24 @@ public class PrimitiveComponent : SchematicBlock
[Tooltip("Whether the primitive should be visible in game.")]
public bool Visible;

[Tooltip("Snaps the object rotation to real in-game rotation.")]
public bool SnapRotation;

public override BlockType BlockType => BlockType.Primitive;

public override bool Compile(SchematicBlockData block, Schematic _)
{
block.Rotation = transform.eulerAngles;
block.Scale = transform.localScale;
block.BlockType = BlockType.Primitive;

PrimitiveFlags primitiveFlags = PrimitiveFlags.None;
if (Collidable)
primitiveFlags |= PrimitiveFlags.Collidable;

if (Visible)
primitiveFlags |= PrimitiveFlags.Visible;

block.Properties = new Dictionary<string, object>
{
{ "PrimitiveType", (PrimitiveType)Enum.Parse(typeof(PrimitiveType), tag) },
Expand All @@ -53,35 +57,28 @@ private void Update()
{
_filter.hideFlags = HideFlags.HideInInspector;
_renderer.hideFlags = HideFlags.HideInInspector;

#if UNITY_EDITOR
if (EditorUtility.IsPersistent(gameObject))
return;
#endif

_renderer.sharedMaterial = Color.a >= 1f ? _sharedRegular : _sharedTransparent;
_renderer.sharedMaterial.color = Color;

_renderer.enabled = Visible;

if (SnapRotation)
transform.localRotation = PrimitiveEditor.GetRealRotation(this);
}

private void OnDrawGizmos()
{
Quaternion realRotation = PrimitiveEditor.GetRealRotation(this);

if (Visible)
{
_renderer.enabled = true;
// if (realRotation.eulerAngles != transform.localEulerAngles)
// {
// Gizmos.color = new Color(1f - Color.r, 1f - Color.g, 1f - Color.b, 1f);
// Gizmos.DrawWireMesh(_filter.sharedMesh, 0, transform.position, Quaternion.Euler(realRotation.eulerAngles), transform.localScale);
// }

return;
}

_renderer.enabled = false;
Gizmos.color = new Color(Color.r, Color.g, Color.b, 1f);
Gizmos.DrawWireMesh(_filter.sharedMesh, 0, transform.position, Quaternion.Euler(realRotation.eulerAngles), transform.localScale);
Gizmos.DrawWireMesh(_filter.sharedMesh, 0, transform.position, transform.rotation, transform.localScale);
}

internal MeshFilter _filter;
Expand Down
2 changes: 1 addition & 1 deletion Assets/DONT TOUCH/Scripts/Editors/PrimitiveEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PrimitiveEditor : Editor
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GUILayout.Label($"Real in-game rotation: {GetRealRotation((PrimitiveComponent)target).eulerAngles.ToString()}");
GUILayout.Label($"Real in-game rotation: {GetRealRotation((PrimitiveComponent)target).eulerAngles}");
}

public static Quaternion GetRealRotation(PrimitiveComponent primitiveComponent)
Expand Down

0 comments on commit 8786800

Please sign in to comment.