summaryrefslogtreecommitdiff
path: root/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs')
-rw-r--r--Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs63
1 files changed, 63 insertions, 0 deletions
diff --git a/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs b/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs
new file mode 100644
index 0000000..2aa1ad8
--- /dev/null
+++ b/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/SnapToGround.cs
@@ -0,0 +1,63 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+[ExecuteInEditMode]
+public class SnapToGround : MonoBehaviour
+{
+ public float m_offset;
+
+ private static List<SnapToGround> m_allSnappers = new List<SnapToGround>();
+
+ private bool m_inList;
+
+ private void Awake()
+ {
+ m_allSnappers.Add(this);
+ m_inList = true;
+ }
+
+ private void OnDestroy()
+ {
+ if (m_inList)
+ {
+ m_allSnappers.Remove(this);
+ m_inList = false;
+ }
+ }
+
+ private void Snap()
+ {
+ if (!(ZoneSystem.instance == null))
+ {
+ float groundHeight = ZoneSystem.instance.GetGroundHeight(base.transform.position);
+ Vector3 position = base.transform.position;
+ position.y = groundHeight + m_offset;
+ base.transform.position = position;
+ ZNetView component = GetComponent<ZNetView>();
+ if (component != null && component.IsOwner())
+ {
+ component.GetZDO().SetPosition(position);
+ }
+ }
+ }
+
+ public bool HaveUnsnapped()
+ {
+ return m_allSnappers.Count > 0;
+ }
+
+ public static void SnappAll()
+ {
+ if (m_allSnappers.Count == 0)
+ {
+ return;
+ }
+ Heightmap.ForceGenerateAll();
+ foreach (SnapToGround allSnapper in m_allSnappers)
+ {
+ allSnapper.Snap();
+ allSnapper.m_inList = false;
+ }
+ m_allSnappers.Clear();
+ }
+}