Ejemplo n.º 1
0
        public bool Load(string fileLocation)
        {
            try
            {
                string extension = Path.GetExtension(fileLocation).ToLowerInvariant();

                var lines = File.ReadAllLines(fileLocation).ToList();
                Logger.LogDebug("read " + lines.Count + " pieces from " + Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint"));

                List <PieceEntry> pieceEntries = new List <PieceEntry>();
                List <Vector3>    snapPoints   = new List <Vector3>();

                bool parsingSnapPoints = false;
                foreach (var line in lines)
                {
                    if (line == HeaderSnapPoints)
                    {
                        parsingSnapPoints = true;
                        continue;
                    }
                    if (line == HeaderPieces)
                    {
                        parsingSnapPoints = false;
                        continue;
                    }
                    if (parsingSnapPoints)
                    {
                        snapPoints.Add(ParsePosition(line));
                        continue;
                    }
                    PieceEntry pieceEntry;
                    switch (extension)
                    {
                    case ".vbuild":
                        pieceEntry = PieceEntry.FromVBuild(line);
                        break;

                    case ".blueprint":
                        pieceEntry = PieceEntry.FromBlueprint(line);
                        break;

                    default:
                        Logger.LogWarning("Unknown extension " + extension);
                        return(false);
                    }

                    pieceEntries.Add(pieceEntry);
                }

                m_pieceEntries = pieceEntries.ToArray();
                m_snapPoints   = snapPoints.ToArray();

                return(true);
            }
            catch (Exception e)
            {
                Logger.LogError(e);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public void Destroy()
        {
            if (m_prefab == null)
            {
                return;
            }

            // Remove from PieceTable
            var table = PieceManager.Instance.GetPieceTable("_BlueprintPieceTable");

            if (table == null)
            {
                Logger.LogWarning("BlueprintPieceTable not found");
                return;
            }

            if (table.m_pieces.Contains(m_prefab))
            {
                Logger.LogInfo($"Removing {m_prefabname} from BlueprintRune");

                table.m_pieces.Remove(m_prefab);
            }

            // Remove from prefabs
            PieceManager.Instance.RemovePiece(m_prefabname);
            PrefabManager.Instance.DestroyPrefab(m_prefabname);
        }
Ejemplo n.º 3
0
        public bool Save()
        {
            if (m_pieceEntries == null)
            {
                Logger.LogWarning("No pieces stored to save");
            }
            else
            {
                using (TextWriter tw = new StreamWriter(m_fileLocation))
                {
                    if (m_snapPoints.Count() > 0)
                    {
                        tw.WriteLine(HeaderSnapPoints);
                        foreach (Vector3 pos in m_snapPoints)
                        {
                            tw.WriteLine(string.Join(";", PieceEntry.InvariantString(pos.x), PieceEntry.InvariantString(pos.y), PieceEntry.InvariantString(pos.z)));
                        }
                    }

                    tw.WriteLine(HeaderPieces);
                    foreach (var piece in m_pieceEntries)
                    {
                        tw.WriteLine(piece.line);
                    }

                    Logger.LogDebug("Wrote " + m_pieceEntries.Length + " pieces to " + Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint"));
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
 public void CalculateCost()
 {
     if (m_pieceEntries == null)
     {
         Logger.LogWarning("No pieces loaded");
         return;
     }
 }
Ejemplo n.º 5
0
        internal void AddPrefab(string name, GameObject prefab)
        {
            if (Prefabs.ContainsKey(name))
            {
                Logger.LogWarning($"GUIPrefab {name} already exists");
                return;
            }

            prefab.name = name;
            prefab.transform.SetParent(GUIContainer.transform, false);
            prefab.SetActive(false);
            Prefabs.Add(name, prefab);
        }
Ejemplo n.º 6
0
        public void AddToPieceTable()
        {
            if (m_prefab == null)
            {
                return;
            }

            var table = PieceManager.Instance.GetPieceTable("_BlueprintPieceTable");

            if (table == null)
            {
                Logger.LogWarning("BlueprintPieceTable not found");
                return;
            }

            if (!table.m_pieces.Contains(m_prefab))
            {
                Logger.LogDebug($"Adding {m_prefabname} to BlueprintRune");
                table.m_pieces.Add(m_prefab);
            }
        }
Ejemplo n.º 7
0
        public bool Save()
        {
            if (m_pieceEntries == null)
            {
                Logger.LogWarning("No pieces stored to save");
            }
            else
            {
                using (TextWriter tw = new StreamWriter(Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint")))
                {
                    foreach (var piece in m_pieceEntries)
                    {
                        tw.WriteLine(piece.line);
                    }

                    Logger.LogDebug("Wrote " + m_pieceEntries.Length + " pieces to " + Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint"));
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        private bool GhostInstantiate(GameObject baseObject)
        {
            var ret = true;

            ZNetView.m_forceDisableInit = true;

            try
            {
                var pieces = new List <PieceEntry>(m_pieceEntries);
                var maxX   = pieces.Max(x => x.posX);
                var maxZ   = pieces.Max(x => x.posZ);

                foreach (Vector3 snapPoint in m_snapPoints)
                {
                    GameObject snapPointObject = new GameObject
                    {
                        name  = "_snappoint",
                        layer = LayerMask.NameToLayer("piece"),
                        tag   = "snappoint"
                    };
                    snapPointObject.SetActive(false);
                    Object.Instantiate(snapPointObject, snapPoint, Quaternion.identity, baseObject.transform);
                }

                //Tiny collider for accurate placement
                GameObject gameObject = new GameObject(PlaceColliderName);
                gameObject.transform.SetParent(baseObject.transform);
                SphereCollider sphereCollider = gameObject.AddComponent <SphereCollider>();
                sphereCollider.radius = 0.002f;

                var tf = baseObject.transform;
                tf.rotation = Camera.main.transform.rotation;
                var quat = Quaternion.Euler(0, tf.rotation.eulerAngles.y, 0);
                tf.SetPositionAndRotation(tf.position, quat);
                tf.position -= tf.right * (maxX / 2f);
                tf.position += tf.forward * 5f;

                var prefabs = new Dictionary <string, GameObject>();
                foreach (var piece in pieces.GroupBy(x => x.name).Select(x => x.FirstOrDefault()))
                {
                    var go = PrefabManager.Instance.GetPrefab(piece.name);
                    if (!go)
                    {
                        Logger.LogWarning("No prefab found for " + piece.name + "! You are probably missing a dependency for blueprint " + m_name);
                    }
                    else
                    {
                        go.transform.SetPositionAndRotation(go.transform.position, quat);
                        prefabs.Add(piece.name, go);
                    }
                }

                for (int i = 0; i < pieces.Count; i++)
                {
                    PieceEntry piece = pieces[i];
                    var        pos   = tf.position + tf.right * piece.GetPosition().x + tf.forward * piece.GetPosition().z +
                                       new Vector3(0, piece.GetPosition().y, 0);

                    var q = Quaternion.Euler(0, tf.transform.rotation.eulerAngles.y + piece.GetRotation().eulerAngles.y, 0);

                    GameObject pieceObject = new GameObject("piece_entry (" + i + ")");
                    pieceObject.transform.SetParent(tf);
                    pieceObject.transform.rotation = q;
                    pieceObject.transform.position = pos;

                    if (prefabs.TryGetValue(piece.name, out var prefab))
                    {
                        GameObject ghostPrefab;
                        Vector3    ghostPosition;
                        Quaternion ghostRotation;
                        if (prefab.TryGetComponent(out WearNTear wearNTear) && wearNTear.m_new)
                        {
                            //Only instantiate the visual part
                            ghostPrefab   = wearNTear.m_new;
                            ghostRotation = ghostPrefab.transform.localRotation;
                            ghostPosition = ghostPrefab.transform.localPosition;
                        }
                        else
                        {
                            //No WearNTear?? Just use the entire prefab
                            ghostPrefab   = prefab;
                            ghostRotation = Quaternion.identity;
                            ghostPosition = Vector3.zero;
                        }

                        var child = Object.Instantiate(ghostPrefab, pieceObject.transform);
                        child.transform.localRotation = ghostRotation;
                        child.transform.localPosition = ghostPosition;
                        MakeGhost(child);

                        //Doors have a dynamic object that also needs to be added
                        if (prefab.TryGetComponent(out Door door))
                        {
                            GameObject doorPrefab = door.m_doorObject;
                            var        doorChild  = Object.Instantiate(doorPrefab, pieceObject.transform);
                            doorChild.transform.localRotation = doorPrefab.transform.localRotation;
                            doorChild.transform.localPosition = doorPrefab.transform.localPosition;
                            MakeGhost(doorChild);
                        }
                    }
                }
            }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public GameObject CreatePrefab()
        {
            if (m_prefab != null)
            {
                return(m_prefab);
            }

            Logger.LogInfo($"Creating dynamic prefab {m_prefabname}");

            if (m_pieceEntries == null)
            {
                Logger.LogWarning("No pieces loaded");
                return(null);
            }

            // Get Stub from PrefabManager
            var stub = PrefabManager.Instance.GetPrefab("piece_blueprint");

            if (stub == null)
            {
                Logger.LogWarning("Could not load blueprint stub from prefabs");
                return(null);
            }

            // 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(null);
            }

            // Instantiate plan objects
            if (!PlanInstantiate())
            {
                Logger.LogWarning("Could not create plan");
                Object.DestroyImmediate(m_prefab);
                return(null);
            }

            // Add to known prefabs
            CustomPiece CP = new CustomPiece(m_prefab, new PieceConfig
            {
                PieceTable = "_BlueprintPieceTable"
            });

            PieceManager.Instance.AddPiece(CP);

            return(m_prefab);
        }