Beispiel #1
0
        public static Texture2D CreatePreviewIcon(string jsonData)
        {
            Build.BuildSave buildSave = JsonUtility.FromJson <Build.BuildSave>(jsonData);
            Vector2         zero      = Vector2.zero;

            PartGrid.CenterToPositon(buildSave.parts, Build.main.partDatabase, ref zero, new Vector2(0f, -200f));
            List <GameObject> list = new List <GameObject>();

            foreach (Build.BuildSave.PlacedPartSave placedPartSave in buildSave.parts)
            {
                PartData partByName = Build.main.partDatabase.GetPartByName(placedPartSave.partName);
                if (partByName != null)
                {
                    list.Add(PartGrid.LoadIcon(Build.main.buildGrid.iconPrefab, partByName.prefab, placedPartSave.position, partByName.prefab.localScale, null, 0, placedPartSave.orientation, Color.white, true).gameObject);
                }
            }
            Build.main.previewIconCamera.transform.position = new Vector3(0f, -200f, Mathf.Clamp(-Mathf.Max(zero.x, zero.y), -100f, -1f) * 0.52f);
            Build.main.previewIconCamera.Render();
            while (list.Count > 0)
            {
                UnityEngine.Object.DestroyImmediate(list[0]);
                list.RemoveAt(0);
            }
            Texture2D texture2D = new Texture2D(Build.main.previewIconCamera.targetTexture.width, Build.main.previewIconCamera.targetTexture.height, TextureFormat.RGB24, false);

            RenderTexture.active = Build.main.previewIconCamera.targetTexture;
            texture2D.ReadPixels(new Rect(0f, 0f, (float)Build.main.previewIconCamera.targetTexture.width, (float)Build.main.previewIconCamera.targetTexture.height), 0, 0);
            texture2D.Apply();
            return(texture2D);
        }
Beispiel #2
0
        public static void CenterToPositon(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase, ref Vector2 size, Vector2 targetCenter)
        {
            Vector2 zero   = Vector2.zero;
            Vector2 vector = Vector2.one * float.PositiveInfinity;

            foreach (Build.BuildSave.PlacedPartSave placedPartSave in parts)
            {
                PartData partByName = partDatabase.GetPartByName(placedPartSave.partName);
                if (!(partByName == null))
                {
                    foreach (PartData.Area area in partByName.areas)
                    {
                        Vector2 vector2 = placedPartSave.position + area.start * placedPartSave.orientation;
                        Vector2 vector3 = placedPartSave.position + (area.start + area.size) * placedPartSave.orientation;
                        zero = new Vector2(Mathf.Max(new float[]
                        {
                            zero.x,
                            vector2.x,
                            vector3.x
                        }), Mathf.Max(new float[]
                        {
                            zero.y,
                            vector2.y,
                            vector3.y
                        }));
                        vector = new Vector2(Mathf.Min(new float[]
                        {
                            vector.x,
                            vector2.x,
                            vector3.x
                        }), Mathf.Min(new float[]
                        {
                            vector.y,
                            vector2.y,
                            vector3.y
                        }));
                    }
                }
            }
            Vector2 b  = (vector + zero) / 2f;
            Vector2 b2 = targetCenter - b;

            for (int j = 0; j < parts.Count; j++)
            {
                parts[j].position += b2;
            }
            size = zero - vector;
        }
Beispiel #3
0
        public static void PositionForLaunch(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase, int rotation)
        {
            Vector2 zero = Vector2.zero;
            Vector2 a    = Vector2.one * float.PositiveInfinity;

            foreach (Build.BuildSave.PlacedPartSave placedPartSave in parts)
            {
                PartData partByName = partDatabase.GetPartByName(placedPartSave.partName);
                if (!(partByName == null))
                {
                    foreach (PartData.Area area in partByName.areas)
                    {
                        Vector2 vector  = placedPartSave.position + area.start * placedPartSave.orientation;
                        Vector2 vector2 = placedPartSave.position + (area.start + area.size) * placedPartSave.orientation;
                        zero = new Vector2(Mathf.Max(new float[]
                        {
                            zero.x,
                            vector.x,
                            vector2.x
                        }), Mathf.Max(new float[]
                        {
                            zero.y,
                            vector.y,
                            vector2.y
                        }));
                        a = new Vector2(Mathf.Min(new float[]
                        {
                            a.x,
                            vector.x,
                            vector2.x
                        }), Mathf.Min(new float[]
                        {
                            a.y,
                            vector.y,
                            vector2.y
                        }));
                    }
                }
            }
            for (int j = 0; j < parts.Count; j++)
            {
                parts[j].position -= a - new Vector2(Utility.RoundToHalf((a.x - zero.x) / 2f), 0f);
            }
        }
 public PlacedPart(Transform partIcon, Vector2 position, Orientation orientation, PartData partData)
 {
     this.position    = position;
     this.orientation = orientation;
     this.partData    = partData;
 }
Beispiel #5
0
                public static List <PlacedPart> FromSave(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase)
                {
                    List <PlacedPart> list = new List <PlacedPart>();

                    for (int i = 0; i < parts.Count; i++)
                    {
                        PartData partByName = partDatabase.GetPartByName(parts[i].partName);
                        bool     flag       = partByName != null;
                        if (flag)
                        {
                            try
                            {
                                partByName.GUID = new Guid(parts[i].GUID);
                            } catch
                            {
                                partByName.GUID = Guid.NewGuid();
                            }
                            //Tags parsing from text back to a Dictionary
                            partByName.tags = new Dictionary <string, object>();
                            try
                            {
                                foreach (string text in parts[i].tagsString.Split(new char[]
                                {
                                    '|'
                                }))
                                {
                                    if (!(text == ""))
                                    {
                                        Type type = Type.GetType(text.Split(new char[]
                                        {
                                            '#'
                                        })[0]);
                                        string key = text.Split(new char[]
                                        {
                                            '#'
                                        })[1];
                                        //ModLoader.mainConsole.log(text.Split('#')[2]);

                                        object obj = JsonUtility.FromJson(text.Split(new char[]
                                        {
                                            '#'
                                        })[2], type);

                                        //ModLoader.mainConsole.log(obj.ToString(), "Tags");
                                        partByName.tags.Add(key, obj);
                                    }
                                }

                                /*foreach (string text2 in partByName.tags.Keys)
                                 * {
                                 *  ModLoader.mainConsole.log(text2 + " " + partByName.tags[text2].ToString(), "Tags");
                                 * }*/
                            }
                            catch (Exception e)
                            {
                                ModLoader.mainConsole.logError(e);
                            }

                            list.Add(new PlacedPart(null, parts[i].position, parts[i].orientation, partByName));
                        }
                    }
                    return(list);
                }
Beispiel #6
0
 public void LoadPartDescription(PartData partData)
 {
     this.partName.text                   = partData.displayName;
     this.partDescription.text            = Utility.SplitLines(partData.GetDescriptionRaw());
     this.descriptionBackground.sizeDelta = new Vector2(445f, (float)(Utility.GetLinesCount(partData.GetDescriptionRaw()) * 32 + 55));
 }