Example #1
0
        public void AddPrefabBluePrint(PrefabBlueprint pfb)
        {
            // Get a list of the blueprints connection points
            List <ConnectionPoint> BluePrintDoorWays = pfb.GetConnectionPoints();

            // go through each of the connection points and add them to appropraite lists
            foreach (var b in BluePrintDoorWays)
            {
                switch (b.GetDirection())
                {
                case Direction.North: NorthPoints.Add(b); break;

                case Direction.South: SouthPoints.Add(b); break;

                case Direction.East: EastPoints.Add(b); break;

                case Direction.West: WestPoints.Add(b); break;
                }
            }

            // finally add the prefab blueprint
            bluePrints.Add(pfb.GetPrefabID(), pfb);
        }
        private bool FigureOutConnectionPiece()
        {
            Random          rand   = RandomManager.GetRandomInstance();
            ConnectionPoint randCp = null;
            PrefabBlueprint pfb    = null;
            bool            prefabBlueprintPieceFound = false;

            if (points.Count <= 0)
            {
                return(false);
            }

            // pick a random point from our list
            ConnectionPoint cp = points.Peek();

            // get a list of connections that pair with our chosen connectionPoint
            List <ConnectionPoint> cpList = resMan.GetListOfPairedDirections(cp.GetDirection());

            if (cpList.Count <= 0)
            {
                return(false);
            }

            // try to find a prefab blueprint piece that will fit. Only do this a couple of times or until find a fitting piece
            for (int i = 0; i < 50 && !prefabBlueprintPieceFound; i++)
            {
                randCp = cpList[rand.Next(0, cpList.Count - 1)];

                // make sure the "random" connection point does not belong to the same prefab blueprint piece. Try to get a unique point, up to 5 times total
                for (int k = 0; k < 5 && randCp.ownerID == cp.ownerID; k++)
                {
                    // get a "random" connection point from the list we get back from the resource manager
                    randCp = cpList[rand.Next(0, cpList.Count - 1)];
                }

                // Get the prefabBluePrint piece given the random connection points ID
                pfb = resMan.GetPrefabBluePrintUsingID(randCp.ownerID);

                // Move the prefabBlueprint piece to the correct location
                Point posCalc = pfb.GetPosition();

                // Get the scalar vector difference between the randCp and the pfb. This is assuming prefabBlueprint pieces X,Y is always at the top left corner
                posCalc.X -= randCp.Position.X;
                posCalc.Y -= randCp.Position.Y;

                // now add this scalar difference to the position of our original connection point
                posCalc.X += cp.Position.X;
                posCalc.Y += cp.Position.Y;

                // finally update the blueprint piece with the new calculated position
                pfb.SetPosition(posCalc);

                // don't check for collisions if we don't have any pieces placed
                if (PrefabPieces.Count > 0)
                {
                    // check collisions. If the piece doesn't collide with anything, then we know found our piece to place.
                    foreach (var pieces in PrefabPieces)
                    {
                        prefabBlueprintPieceFound = !pieces.CheckCollision(pfb.GetCollisionBox());

                        // if we have collided into anything let's break
                        if (prefabBlueprintPieceFound == false)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    prefabBlueprintPieceFound = true;
                }
            }

            // if we did not find a piece from the last step above, then we need to remove the connection point
            if (prefabBlueprintPieceFound == false || pfb == null)
            {
                points.Dequeue();
                return(false);
            }

            // We can now Generate a new PrefabPiece. This will Create a new Prefab piece with the current calues of the PrefabBluePrint
            Prefab p = pfb.GeneratePrefabPiece();

            // Reset the Prefab BluePrint
            pfb.SetPosition(new Point(0, 0));

            // Get rid of the Connection Point that we got from RandCp before adding it to our points list
            p.RemoveConnectionPoint(randCp.ID);

            // Now remove the Connection Point in our points list
            points.Dequeue();

            // Add all of the connection points from our newly generated Prefab to our points list
            List <ConnectionPoint> temp = p.GetConnectionPoints();

            foreach (var t in temp)
            {
                points.Enqueue(t);
            }

            // now that a pfb has been chosen, make a clone of it to a prefab unit.
            PrefabPieces.Add(p);

            return(true);
        }