Beispiel #1
0
 public ChainLink(LayerBrick.Brick.ConnectionPoint firstConnection, LayerBrick.Brick.ConnectionPoint secondConnection)
 {
     mFirstConnection = firstConnection;
     mSecondConnection = secondConnection;
     if (firstConnection != null)
     {
         int firstIndex = firstConnection.mMyBrick.ConnectionPoints.IndexOf(firstConnection);
         int secondIndex = secondConnection.mMyBrick.ConnectionPoints.IndexOf(secondConnection);
         mAngleBetween = BrickLibrary.Instance.getConnectionAngleDifference(secondConnection.mMyBrick.PartNumber, secondIndex,
                                                                         firstConnection.mMyBrick.PartNumber, firstIndex);
     }
     else
     {
         // if the first connection point is null, create a dummy connection point in the world,
         // at the position of the second connection and not attached to a brick.
         // The angle between is in that case of course null
         mFirstConnection = new LayerBrick.Brick.ConnectionPoint(secondConnection.PositionInStudWorldCoord);
     }
 }
        public RotateBrick(LayerBrick layer, List<Layer.LayerItem> bricks, int rotateSteps, bool forceKeepLastCenter)
        {
            // compute the default rotation angle, used if no bricks in the list is connected to something else
            float angle = MapData.Layer.CurrentRotationStep * rotateSteps;

            // now check if we can find any brick in the list which is connected to another
            // brick not in the list: in that case we don't care about the rotation step,
            // we rotate the group of brick such as it can connect with its next connexion point.
            // we do this first because maybe it will invalidate the flag sLastCenterIsValid

            // first we gather all the free connections or those linked with bricks outside of the list of brick
            FreeConnectionSet externalConnectionSet = new FreeConnectionSet(); // all the connection connected to external bricks
            FreeConnectionSet availableConnectionSet = new FreeConnectionSet(); // all the possible connections that can be used to link to one external brick
            foreach (Layer.LayerItem item in bricks)
            {
                LayerBrick.Brick brick = item as LayerBrick.Brick;
                if (brick.HasConnectionPoint)
                    foreach (LayerBrick.Brick.ConnectionPoint connection in brick.ConnectionPoints)
                        if (connection.IsFree)
                        {
                            availableConnectionSet.add(connection);
                        }
                        else if (!bricks.Contains(connection.ConnectionLink.mMyBrick))
                        {
                            availableConnectionSet.add(connection);
                            externalConnectionSet.add(connection);
                        }
            }

            // get the biggest group of external connection among all the available types, and also get its type
            int chosenConnexionType = BrickLibrary.ConnectionType.DEFAULT;
            List<LayerBrick.Brick.ConnectionPoint> externalConnectionList = externalConnectionSet.getBiggestList(out chosenConnexionType);

            // check if there is any external connection on which we should rotate
            if (externalConnectionList.Count > 0)
            {
                // in that case we don't use the static center
                sLastCenterIsValid = false;

                // for now, without a lot of imagination, take the first connection of the list
                mOldConnectionPoint = externalConnectionList[0];

                // store the connection position
                mConnexionPosition = mOldConnectionPoint.PositionInStudWorldCoord;

                // get the fixed brick, the external brick on the other side of the chosen connection
                LayerBrick.Brick fixedBrick = mOldConnectionPoint.ConnectedBrick;
                int fixedBrickConnectionIndex = mOldConnectionPoint.ConnectionLink.Index;

                // get the same list but for available connections
                List<LayerBrick.Brick.ConnectionPoint> availableConnectionList = availableConnectionSet.getListForType(chosenConnexionType);

                // check in which direction and how many connection we should jump
                bool rotateCW = (rotateSteps < 0);
                int nbSteps = Math.Abs(rotateSteps);

                // get the index of the chosen connection in the available connection list
                int index = availableConnectionList.IndexOf(mOldConnectionPoint);
                // start from it then count forward or backward a certain number of connections
                // depending on the number of steps and the rotation direction
                if (rotateCW)
                {
                    index -= (nbSteps % availableConnectionList.Count);
                    if (index < 0)
                        index += availableConnectionList.Count;
                }
                else
                {
                    index += (nbSteps % availableConnectionList.Count);
                    if (index >= availableConnectionList.Count)
                        index -= availableConnectionList.Count;
                }
                // finally get the new connection from the chosen index
                mNewConnectionPoint = availableConnectionList[index];

                // compute the angle to rotate
                LayerBrick.Brick newConnectedBrick = mNewConnectionPoint.mMyBrick;
                angle = AddConnectBrick.sGetOrientationOfConnectedBrick(fixedBrick, fixedBrickConnectionIndex,
                    newConnectedBrick, mNewConnectionPoint.Index) - newConnectedBrick.Orientation;
            }

            // then call the normal constructor
            commonConstructor(layer, bricks, angle, forceKeepLastCenter);
        }