public static List <Vector2> ValidateBlueprint(BlueprintModel blueprint)
        {
            var positions = new List <Vector2>();

            // Add all position blocks of the blueprint to the positions list
            foreach (BlueprintBlock blueprintBlock in blueprint.Blocks)
            {
                try
                {
                    var blockBase = BlockPrefabProvider.Instance.GetGridBlockBase(blueprintBlock.BlockTypeID);

                    Vector2[] currentPositions = blockBase.BlockPositions;
                    foreach (Vector2 vector in currentPositions)
                    {
                        // Add the position to the list
                        positions.Add(new Vector2(
                                          blueprintBlock.VectorPosition.x + vector.x,
                                          blueprintBlock.VectorPosition.y + vector.y
                                          ));
                    }
                }
                catch (PrefabNotFoundException) { continue; }
            }

            // Returns the duplicates of the positions
            return(FindDuplicates(positions));
        }
Esempio n. 2
0
        /// <summary>
        /// Instantiates a blueprint into the game
        /// </summary>
        /// <param name="blueprint">The blueprint to instantiate</param>
        /// <param name="parent">The parent transform of the blueprint</param>
        /// <returns></returns>
        public List <GridBlockBase> InstantiateBlueprint(BlueprintModel blueprint, Transform parent)
        {
            List <GridBlockBase> blocks = new List <GridBlockBase>();

            // Loop over each block in the blueprint
            foreach (BlueprintBlock blueprintBlock in blueprint.Blocks)
            {
                // Instantiate the block by prefab block type
                try
                {
                    var           block     = Instantiate(BlockPrefabProvider.Instance.GetPrefab(blueprintBlock.BlockTypeID));
                    GridBlockBase blockBase = (GridBlockBase)block.GetComponent(typeof(GridBlockBase));
                    // Set the parent of the block
                    block.transform.SetParent(parent);

                    // Set the position of the block
                    Vector2 position = GetBlockPosition(blueprintBlock.VectorPosition, blockBase.Size);
                    block.transform.localPosition = new Vector3(
                        position.x,
                        position.y,
                        0);

                    // Set the rotation of the block
                    block.transform.localRotation = Quaternion.Euler(0f, 0f, blueprintBlock.RotationInDegs);

                    // Add the block to the list
                    blocks.Add(blockBase);
                }
                catch (PrefabNotFoundException) { }
            }

            return(blocks);
        }
Esempio n. 3
0
        public static MinMaxVector2 GetBlueprintCorners(BlueprintModel blueprint)
        {
            var blueprintCorners = new MinMaxVector2(0f, 0f, 0f, 0f);
            var corners          = GetCorners(blueprint);

            blueprintCorners.Apply(corners);

            return(blueprintCorners);
        }
Esempio n. 4
0
 public static Bounds GetBounds(BlueprintModel blueprint, bool useUnityAPI = false)
 {
     if (useUnityAPI)
     {
         return(GetBoundsUnityAPI(blueprint));
     }
     else
     {
         return(GetBoundsNormal(blueprint));
     }
 }
Esempio n. 5
0
        public static Vector2 GetSize(this BlueprintModel blueprint)
        {
            var blueprintCorners = new MinMaxVector2(0f, 0f, 0f, 0f);
            var corners          = GetCorners(blueprint);

            blueprintCorners.Apply(corners);

            return(new Vector2(
                       blueprintCorners.maxX - blueprintCorners.minX,
                       blueprintCorners.maxY - blueprintCorners.minY));
        }
Esempio n. 6
0
        private static Bounds GetBoundsNormal(this BlueprintModel blueprint)
        {
            var corners  = GetBlueprintCorners(blueprint);
            var size     = GetSize(corners);
            var sizev3   = new Vector3(size.x, size.y);
            var center   = GetCenter(corners);
            var centerv3 = new Vector3(center.x, center.y);
            var bounds   = new Bounds(centerv3, sizev3);

            return(bounds);
        }
        /// <summary>
        /// Parses the current blueprint to a string represatation
        /// </summary>
        /// <param name="blueprint">The blueprint to export</param>
        /// <returns>The blueprint string</returns>
        public static string Export(BlueprintModel blueprint)
        {
            // Creates the data
            var jsonData = new Dictionary <string, object>
            {
                { "Name", blueprint.Name },
                { "Blocks", blueprint.Blocks }
            };

            // Serializes the data
            string json = JsonConvert.SerializeObject(jsonData);

            return(json);
        }
Esempio n. 8
0
        public static Bounds GetBoundsUnityAPI(this BlueprintModel blueprint)
        {
            var count = blueprint.Blocks.Count;

            if (count > 0)
            {
                var bounds = GetBounds(blueprint.Blocks[0]);
                if (count > 1)
                {
                    for (int i = 1; i < count; i++)
                    {
                        bounds.Encapsulate(GetBounds(blueprint.Blocks[i]));
                    }
                }
                return(bounds);
            }
            else
            {
                throw new System.Exception("Cannot get bounds of empty blueprint");
            }
        }
Esempio n. 9
0
        public static Vector2[] GetCorners(BlueprintModel blueprint)
        {
            var count   = blueprint.Blocks.Count;
            var corners = new Vector2[blueprint.Blocks.Count * 4];

            for (int i = 0; i < count; i++)
            {
                var block = blueprint.Blocks[i];
                var size  = block.runtimeValues.size;

                if (block.Rotation % 2 == 1)
                {
                    size = new Vector2(size.y, size.x);
                }

                var halfWidth  = size.x / 2f;
                var halfHeight = size.y / 2f;

                var arrayBaseIndex = i * 4;

                corners[0 + arrayBaseIndex] = new Vector2(
                    block.VectorPosition.x - halfWidth,
                    block.VectorPosition.y - halfHeight);

                corners[1 + arrayBaseIndex] = new Vector2(
                    block.VectorPosition.x + halfWidth,
                    block.VectorPosition.y - halfHeight);

                corners[2 + arrayBaseIndex] = new Vector2(
                    block.VectorPosition.x - halfWidth,
                    block.VectorPosition.y + halfHeight);

                corners[3 + arrayBaseIndex] = new Vector2(
                    block.VectorPosition.x + halfWidth,
                    block.VectorPosition.y + halfHeight);
            }

            return(corners);
        }