public static void Flatten(Transform transform, Vector2 floorSize, List <PieceEntry> pieces) { Logger.LogDebug($"Entered FlattenTerrain {transform} / {floorSize} with {pieces.Count}"); var groundPrefab = ZNetScene.instance.GetPrefab("raise"); if (groundPrefab) { var lowestY = pieces.Min(x => x.posY); var startPosition = transform.position + Vector3.down * 0.5f; var rotation = transform.rotation; var forward = 0f; while (forward < floorSize.y) { var right = 0f; while (right < floorSize.x) { var lowestAtPosition = pieces.OrderBy(x => x.posY) .FirstOrDefault(x => Math.Abs(x.posX - forward) < 4f && Math.Abs(x.posZ - right) < 4f); if (lowestAtPosition != null) { Debug.Log("Lowest: " + lowestAtPosition.posY); Object.Instantiate(groundPrefab, startPosition + transform.forward * forward + transform.right * right + new Vector3(0, lowestAtPosition.posY, 0), rotation); } right++; } forward++; } } }
public bool CreatePrefab() { if (m_prefab != null) { return(false); } Logger.LogDebug($"Creating dynamic prefab {m_prefabname}"); if (m_pieceEntries == null) { Logger.LogWarning("No pieces loaded"); return(false); } // Get Stub from PrefabManager var stub = PrefabManager.Instance.GetPrefab(BlueprintPrefabName); if (stub == null) { Logger.LogWarning("Could not load blueprint stub from prefabs"); return(false); } // Instantiate clone from stub ZNetView.m_forceDisableInit = true; m_prefab = Object.Instantiate(stub); ZNetView.m_forceDisableInit = false; m_prefab.name = m_prefabname; var piece = m_prefab.GetComponent <Piece>(); if (File.Exists(Path.Combine(BlueprintManager.BlueprintPath, m_name + ".png"))) { var tex = new Texture2D(2, 2); tex.LoadImage(File.ReadAllBytes(Path.Combine(BlueprintManager.BlueprintPath, m_name + ".png"))); piece.m_icon = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero); } piece.m_name = m_name; piece.m_enabled = true; // Instantiate child objects if (!GhostInstantiate(m_prefab)) { Logger.LogWarning("Could not create prefab"); Object.DestroyImmediate(m_prefab); return(false); } // Add to known prefabs CustomPiece CP = new CustomPiece(m_prefab, new PieceConfig { PieceTable = "_BlueprintPieceTable" }); CP.Piece.m_description += "\nFile location: " + Path.GetFullPath(m_fileLocation); PieceManager.Instance.AddPiece(CP); PieceManager.Instance.GetPieceTable(BlueprintRunePrefab.PieceTableName).m_pieces.Add(m_prefab); PrefabManager.Instance.RegisterToZNetScene(m_prefab); return(true); }
public bool Capture(Vector3 position, float radius) { var rot = Camera.main.transform.rotation.eulerAngles; Logger.LogDebug("Collecting piece information"); var numPieces = 0; var collected = new List <Piece>(); var snapPoints = new List <Vector3>(); Transform centerPiece = null; foreach (var piece in BlueprintManager.GetPiecesInRadius(position, radius)) { if (piece.name.StartsWith(BlueprintRunePrefab.BlueprintSnapPointName)) { snapPoints.Add(piece.transform.position); WearNTear wearNTear = piece.GetComponent <WearNTear>(); wearNTear.Remove(); continue; } if (piece.name.StartsWith(BlueprintRunePrefab.BlueprintCenterPointName)) { if (centerPiece == null) { centerPiece = piece.transform; } else { Logger.LogWarning("Multiple center points! Ignoring @ " + piece.transform.position); } WearNTear wearNTear = piece.GetComponent <WearNTear>(); wearNTear.Remove(); continue; } piece.GetComponent <WearNTear>()?.Highlight(); collected.Add(piece); numPieces++; } Logger.LogDebug($"Found {numPieces} in a radius of {radius:F2}"); Vector3 center; if (centerPiece == null) { // Relocate Z var minZ = 9999999.9f; var minX = 9999999.9f; var minY = 9999999.9f; foreach (var piece in collected.Where(x => x.m_category != Piece.PieceCategory.Misc && x.IsPlacedByPlayer())) { minX = Math.Min(piece.m_nview.GetZDO().m_position.x, minX); minZ = Math.Min(piece.m_nview.GetZDO().m_position.z, minZ); minY = Math.Min(piece.m_nview.GetZDO().m_position.y, minY); } Logger.LogDebug($"{minX} - {minY} - {minZ}"); center = new Vector3(minX, minY, minZ); } else { center = centerPiece.position; } // select and order instance piece entries var pieces = collected.Where(x => x.IsPlacedByPlayer() && x.m_category != Piece.PieceCategory.Misc) .OrderBy(x => x.transform.position.y) .ThenBy(x => x.transform.position.x) .ThenBy(x => x.transform.position.z); if (m_pieceEntries == null) { m_pieceEntries = new PieceEntry[pieces.Count()]; } else if (m_pieceEntries.Length > 0) { Array.Clear(m_pieceEntries, 0, m_pieceEntries.Length - 1); Array.Resize(ref m_pieceEntries, pieces.Count()); } uint i = 0; foreach (var piece in pieces) { var pos = piece.m_nview.GetZDO().m_position - center; var quat = piece.m_nview.GetZDO().m_rotation; quat.eulerAngles = piece.transform.eulerAngles; var additionalInfo = piece.GetComponent <TextReceiver>() != null?piece.GetComponent <TextReceiver>().GetText() : ""; string pieceName = piece.name.Split('(')[0]; if (pieceName.EndsWith(PlanPiecePrefab.PlannedSuffix)) { pieceName = pieceName.Replace(PlanPiecePrefab.PlannedSuffix, null); } m_pieceEntries[i++] = new PieceEntry(pieceName, piece.m_category.ToString(), pos, quat, additionalInfo); } if (m_snapPoints == null) { m_snapPoints = new Vector3[snapPoints.Count()]; } for (int j = 0; j < snapPoints.Count(); j++) { m_snapPoints[j] = snapPoints[j] - center; } return(true); }
public bool Capture(Vector3 startPosition, float startRadius, float radiusDelta) { var vec = startPosition; var rot = Camera.main.transform.rotation.eulerAngles; Logger.LogDebug("Collecting piece information"); var numPieces = 0; var collected = new List <Piece>(); collected.Clear(); foreach (var piece in Piece.m_allPieces) { if (Vector2.Distance(new Vector2(startPosition.x, startPosition.z), new Vector2(piece.transform.position.x, piece.transform.position.z)) < startRadius && piece.transform.position.y >= startPosition.y) { collected.Add(piece); numPieces++; } } Logger.LogDebug($"Found {numPieces} in a radius of {startRadius:F2}"); // Relocate Z var minZ = 9999999.9f; var minX = 9999999.9f; var minY = 9999999.9f; foreach (var piece in collected.Where(x => x.m_category != Piece.PieceCategory.Misc && x.IsPlacedByPlayer())) { if (piece.m_nview.GetZDO().m_position.x < minX) { minX = piece.m_nview.GetZDO().m_position.x; } if (piece.m_nview.GetZDO().m_position.z < minZ) { minZ = piece.m_nview.GetZDO().m_position.z; } if (piece.m_nview.GetZDO().m_position.y < minY) { minY = piece.m_nview.GetZDO().m_position.y; } } Logger.LogDebug($"{minX} - {minY} - {minZ}"); var bottomleft = new Vector3(minX, minY, minZ); // select and order instance piece entries var pieces = collected.Where(x => x.IsPlacedByPlayer() && x.m_category != Piece.PieceCategory.Misc).OrderBy(x => x.transform.position.y) .ThenBy(x => x.transform.position.x).ThenBy(x => x.transform.position.z); if (m_pieceEntries == null) { m_pieceEntries = new PieceEntry[pieces.Count()]; } else if (m_pieceEntries.Length > 0) { Array.Clear(m_pieceEntries, 0, m_pieceEntries.Length - 1); Array.Resize(ref m_pieceEntries, pieces.Count()); } uint i = 0; foreach (var piece in pieces) { var pos = new Vector3(piece.m_nview.GetZDO().m_position.x - bottomleft.x, piece.m_nview.GetZDO().m_position.y - bottomleft.y, piece.m_nview.GetZDO().m_position.z - bottomleft.z); var quat = piece.m_nview.GetZDO().m_rotation; quat.eulerAngles = new Vector3(0, quat.eulerAngles.y, 0); quat.eulerAngles = piece.transform.eulerAngles; var additionalInfo = piece.GetComponent <TextReceiver>() != null?piece.GetComponent <TextReceiver>().GetText() : ""; m_pieceEntries[i++] = new PieceEntry(piece.name, piece.m_category.ToString(), pos, quat, additionalInfo); } return(true); }