using LE_LevelEditor.Core; using LE_LevelEditor.Events; using UnityEngine; namespace LE_LevelEditor.Commands { public class LE_CmdChangeTerrainHeights : LE_CmdBase { private LE_TerrainManager m_terrainMgr; private LE_TerrainManager.HeightData m_heightsDelta; public LE_CmdChangeTerrainHeights(LE_TerrainManager p_terrainMgr, LE_TerrainManager.HeightData p_heightsDelta) { m_terrainMgr = p_terrainMgr; m_heightsDelta = p_heightsDelta; m_isExecuted = true; } public override long GetStoredBytes() { if (m_heightsDelta != null) { return 20 + 4 * m_heightsDelta.m_heights.Length; } return 0L; } public override bool Execute() { return base.Execute() && Apply(1f); } public override bool Rollback() { return base.Rollback() && Apply(-1f); } private bool Apply(float p_direction) { if (m_terrainMgr == null || m_heightsDelta == null) { Debug.LogError("LE_CmdChangeTerrainHeight: Apply: could not execute, m_terrainMgr or m_heightsDelta are null!"); return false; } int xBase = m_heightsDelta.m_xBase; int yBase = m_heightsDelta.m_yBase; int length = m_heightsDelta.m_heights.GetLength(1); int length2 = m_heightsDelta.m_heights.GetLength(0); if (length > m_terrainMgr.TerrainData.heightmapWidth || length2 > m_terrainMgr.TerrainData.heightmapHeight) { Debug.LogError("LE_CmdChangeTerrainHeight: Apply: could not execute, terrain height map resolution was reduced in the meantime!"); return false; } float[,] heights = m_terrainMgr.TerrainData.GetHeights(xBase, yBase, length, length2); for (int i = 0; i < length; i++) { for (int j = 0; j < length2; j++) { heights[j, i] += p_direction * m_heightsDelta.m_heights[j, i]; } } m_terrainMgr.TerrainData.SetHeights(m_heightsDelta.m_xBase, m_heightsDelta.m_yBase, heights); if (LE_EventInterface.OnChangeLevelData != null) { LE_EventInterface.OnChangeLevelData(m_terrainMgr, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.TERRAIN_HEIGHTS)); } return true; } } }