public Tank ToTech(Vector3 position, Quaternion rotation)
            {
                var spawnParams = new ManSpawn.TankSpawnParams()
                {
                    forceSpawn = true,
                    grounded   = true,
                    position   = position,
                    rotation   = rotation,
                    placement  = ManSpawn.TankSpawnParams.Placement.PlacedAtPosition,
                    techData   = new TechData()
                    {
                        Name           = this.Name,
                        m_CreationData = new TechData.CreationData(),
                        m_BlockSpecs   = new List <TankPreset.BlockSpec>()
                    }
                };

                /*var root = this.Blocks.First(b => b.IsRootBlock);
                 * this.Blocks.Remove(root);
                 * var rblock = root.ToBlock(position - Vector3.one, rotation);
                 * rblock.trans.localPosition = root.Position;
                 * cachedLocalRotation.SetValue(rblock, root.OrthoRotation, null);
                 * var rblockspec = new TankPreset.BlockSpec();
                 * rblockspec.InitFromBlockState(rblock, true);
                 * spawnParams.techData.m_BlockSpecs.Add(rblockspec);
                 *
                 * rblock.visible.RemoveFromGame();*/

                foreach (var b in Blocks)
                {
                    var rB = b.ToBlock(position - Vector3.one, rotation);
                    rB.trans.localPosition = b.Position;
                    cachedLocalRotation.SetValue(rB, b.OrthoRotation, null);
                    var rBSpec = new TankPreset.BlockSpec();
                    rBSpec.InitFromBlockState(rB, true);
                    spawnParams.techData.m_BlockSpecs.Add(rBSpec);
                    rB.visible.RemoveFromGame();
                }

                var tech = ManSpawn.inst.SpawnUnmanagedTank(spawnParams);

                var blockman = tech.blockman;

                foreach (var b in Blocks)
                {
                    var block = blockman.GetBlockAtPosition(b.Position);
                    block.trans.localPosition    = b.localPosition;
                    block.trans.localEulerAngles = b.localEulerAngles;
                    block.trans.localScale       = b.localScale;
                }

                /*Blocks.Add(root);
                 *
                 * var blocks = tech.blockman.GetLowestBlocks();
                 * foreach (var b in blocks)
                 * {
                 *  var jb = Blocks.First(bb => bb.Position == b.cachedLocalPosition);
                 *  b.trans.localEulerAngles = jb.localEulerAngles;
                 *  b.trans.localPosition = jb.localPosition;
                 *  b.trans.localScale = jb.localScale;
                 * }*/

                return(tech);
            }
Ejemplo n.º 2
0
        private void DoWindow(int id)
        {
            scrollPos = GUILayout.BeginScrollView(scrollPos);
            foreach (var image in Directory.GetFiles(Path.GetFullPath(ProceduralBlocks.ProceduralBlocksMod.TechArtFolder), "*.png"))
            {
                if (GUILayout.Button(Path.GetFileNameWithoutExtension(image), new GUIStyle(GUI.skin.button)
                {
                    richText = true, alignment = TextAnchor.MiddleLeft
                }))
                {
                    path = image;
                }
            }
            GUILayout.EndScrollView();
            GUILayout.TextField(path);

            useModBlock = GUILayout.Toggle(useModBlock, "Use mod block");
            if (!useModBlock)
            {
                useFlesh = GUILayout.Toggle(useFlesh, "Use flesh blocks");
            }
            if (GUILayout.Button("Load"))
            {
                try
                {
                    Texture2D image = new Texture2D(0, 0);
                    image.LoadImage(File.ReadAllBytes(path));

                    if (image.width <= 64 && image.height <= 64)
                    {
                        blocks = new List <Temp>();
                        Vector3    position = Singleton.playerTank.trans.position;
                        Quaternion rotation = Singleton.playerTank.trans.rotation;

                        TankBlock root = Singleton.Manager <ManSpawn> .inst.SpawnBlock(BlockTypes.GSOCockpit_111, Vector3.zero, rotation);

                        root.trans.localPosition = Vector3.zero;

                        var spawnParams = new ManSpawn.TankSpawnParams()
                        {
                            forceSpawn = true,
                            grounded   = true,
                            position   = position,
                            rotation   = rotation,
                            placement  = ManSpawn.TankSpawnParams.Placement.PlacedAtPosition,
                            techData   = new TechData()
                            {
                                Name           = Path.GetFileNameWithoutExtension(path),
                                m_CreationData = new TechData.CreationData(),
                                m_BlockSpecs   = new List <TankPreset.BlockSpec>()
                            }
                        };

                        var spec = new TankPreset.BlockSpec();
                        spec.InitFromBlockState(root, false);

                        spawnParams.techData.m_BlockSpecs.Add(spec);

                        for (int x = 0; x < image.width; x++)
                        {
                            for (int y = 0; y < image.height; y++)
                            {
                                if (x == 0 && y == 0)
                                {
                                    continue;
                                }
                                var c    = image.GetPixel(x, y);
                                var type = (BlockTypes)7000;
                                if (!useModBlock)
                                {
                                    var sw = float.MaxValue;
                                    var cc = Color.white;
                                    foreach (var color in color_blocks.Keys.ToList())
                                    {
                                        if (!useFlesh && color_blocks[color].ToString().Contains("Flesh"))
                                        {
                                            continue;
                                        }
                                        var w = (float)Math.Sqrt(Math.Pow(c.r - color.r, 2) + Math.Pow(c.g - color.g, 2) + Math.Pow(c.b - color.b, 2));
                                        if (w < sw)
                                        {
                                            sw = w;
                                            cc = color;
                                        }
                                    }
                                    type = color_blocks[cc];
                                }

                                TankBlock b = Singleton.Manager <ManSpawn> .inst.SpawnBlock(type, new Vector3(x, y, 0), rotation);

                                b.trans.localPosition = new Vector3(x, y, 0);
                                if (useModBlock)
                                {
                                    b.GetComponent <ModuleColor>().Color = c;
                                }
                                spec = new TankPreset.BlockSpec();
                                spec.InitFromBlockState(b, false);
                                spawnParams.techData.m_BlockSpecs.Add(spec);
                                b.visible.RemoveFromGame();
                            }
                        }
                        tech = ManSpawn.inst.SpawnUnmanagedTank(spawnParams);
                        Tank playerTank = Singleton.playerTank;
                        Singleton.Manager <ManTechs> .inst.SetPlayerTankLocally(null, true);

                        playerTank.visible.RemoveFromGame();

                        Singleton.Manager <ManTechs> .inst.SetPlayerTankLocally(tech, true);
                    }
                }
                catch (Exception e) { Console.WriteLine(e); }
                visible = false;
            }
            if (GUILayout.Button("Cancel"))
            {
                visible = false;
            }
            GUI.DragWindow();
        }