Exemplo n.º 1
0
 /// <summary>Piece constructor.</summary>
 public TerrainClone(TerrainClone prototype)
 {
     this.prototype = (TerrainPrototype)prototype.Prototype;
     rotationAngle  = prototype.rotationAngle;
     side           = prototype.Side;
     stack.AttachedToCounterSection = false;
 }
Exemplo n.º 2
0
Arquivo: Game.cs Projeto: jimu/ZunTzu
        internal Game(GameBox gameBox, ScenarioType scenarioType, string fileName, Stream stream)
        {
            this.gameBox      = gameBox;
            this.scenarioType = scenarioType;
            this.fileName     = fileName;

            XmlDocument xml = new XmlDocument();

            xml.Load(stream);

            // Dice

            diceHands = new DiceHand[gameBox.DiceHands.Length];
            for (int i = 0; i < diceHands.Length; ++i)
            {
                diceHands[i].DiceType  = gameBox.DiceHands[i].DiceType;
                diceHands[i].BeingCast = false;
                int diceCount = gameBox.DiceHands[i].Count;
                diceHands[i].Dice = new Die[diceCount];
                for (int j = 0; j < diceCount; ++j)
                {
                    diceHands[i].Dice[j] = (gameBox.DiceHands[i].TextureFileName[j] != null ?
                                            new Die(gameBox.DiceHands[i].TextureFileName[j]) :
                                            new Die(gameBox.DiceHands[i].Colors[j], gameBox.DiceHands[i].Pips[j]));
                }
            }

            XmlElement gameNode = xml.DocumentElement;

            if (gameNode.SelectSingleNode("game-box") != null)
            {
                // old format

                // Scenario data

                scenarioName = xml.SelectSingleNode("/game/scenario/name").InnerText;
                XmlElement descriptionNode = (XmlElement)xml.SelectSingleNode("/game/scenario/description");
                scenarioDescription = (descriptionNode != null ? descriptionNode.InnerText : null);
                XmlElement copyrightNode = (XmlElement)xml.SelectSingleNode("/game/scenario/copyright");
                scenarioCopyright = (copyrightNode != null ? copyrightNode.InnerText : null);

                // Game data
                XmlNodeList layoutNodeList = gameNode.SelectNodes("layout");
                boards = new Board[layoutNodeList.Count];
                List <Piece> pieceList = new List <Piece>();
                for (int i = 0; i < boards.Length; ++i)
                {
                    string boardId   = ((XmlElement)layoutNodeList[i]).GetAttribute("board");
                    string boardName = ((XmlElement)xml.SelectSingleNode("/game/scenario/map[@id='" + boardId + "']|/game/scenario/counter-sheet[@id='" + boardId + "']")).InnerText;

                    // retrieve board properties
                    foreach (MapProperties properties in gameBox.Maps)
                    {
                        if (properties.Name == boardName)
                        {
                            boards[i] = new Map(i, properties);
                            break;
                        }
                    }
                    if (boards[i] == null)
                    {
                        foreach (CounterSheetProperties properties in gameBox.CounterSheets)
                        {
                            if (properties.Name == boardName)
                            {
                                CounterSheet counterSheet = new CounterSheet(i, properties, pieceList);
                                boards[i] = counterSheet;

                                // register each piece's stack as belonging to the counter sheet
                                foreach (ICounterSection counterSection in counterSheet.CounterSections)
                                {
                                    foreach (Piece piece in counterSection.Pieces)
                                    {
                                        Stack stack = piece.Stack;
                                        counterSheet.MoveStackToBack(stack);
                                        stack.AttachedToCounterSection = true;
                                    }
                                }

                                break;
                            }
                        }
                    }
                }
                allPieces    = pieceList.ToArray();
                visibleBoard = boards[0];

                for (int i = 0; i < layoutNodeList.Count; ++i)
                {
                    XmlElement layoutNode = (XmlElement)layoutNodeList[i];
                    Board      board      = boards[i];

                    board.VisibleArea = RectangleF.FromLTRB(
                        XmlConvert.ToSingle(layoutNode.GetAttribute("left")),
                        XmlConvert.ToSingle(layoutNode.GetAttribute("top")),
                        XmlConvert.ToSingle(layoutNode.GetAttribute("right")),
                        XmlConvert.ToSingle(layoutNode.GetAttribute("bottom")));
                    if (board is CounterSheet)
                    {
                        if (layoutNode.HasAttribute("side"))
                        {
                            ((CounterSheet)board).Side = (Side)Enum.Parse(typeof(Side), layoutNode.GetAttribute("side"));
                        }
                        else
                        {
                            ((CounterSheet)board).Side = Side.Front;
                        }
                    }
                    if (layoutNode.HasAttribute("visible") && layoutNode.GetAttribute("visible") == "true")
                    {
                        visibleBoard = board;
                    }

                    foreach (XmlElement pieceNode in layoutNode.SelectNodes("counter"))
                    {
                        Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id")));
                        Stack stack = (Stack)piece.Stack;
                        stack.AttachedToCounterSection = false;
                        if (pieceNode.HasAttribute("rot"))
                        {
                            piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * (float)Math.PI / 180.0f;
                        }
                        if (pieceNode.HasAttribute("side"))
                        {
                            piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side"));
                        }
                        else
                        {
                            piece.Side = Side.Front;
                        }
                        board.MoveStackToFront(stack);
                        stack.Position = new PointF(
                            XmlConvert.ToSingle(pieceNode.GetAttribute("x")),
                            XmlConvert.ToSingle(pieceNode.GetAttribute("y")));
                    }

                    foreach (XmlElement stackNode in layoutNode.SelectNodes("stack"))
                    {
                        Stack stack = null;
                        foreach (XmlElement pieceNode in stackNode.SelectNodes("counter"))
                        {
                            Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id")));

                            if (stack == null)
                            {
                                stack = (Stack)piece.Stack;
                                stack.AttachedToCounterSection = false;
                                board.MoveStackToFront(stack);
                                stack.Position = new PointF(
                                    XmlConvert.ToSingle(stackNode.GetAttribute("x")),
                                    XmlConvert.ToSingle(stackNode.GetAttribute("y")));
                            }
                            else
                            {
                                Stack stackToMerge = (Stack)piece.Stack;
                                ((Board)stackToMerge.Board).RemoveStack(stackToMerge);
                                stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length);
                            }

                            if (pieceNode.HasAttribute("rot"))
                            {
                                piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f);
                            }
                            if (pieceNode.HasAttribute("side"))
                            {
                                piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side"));
                            }
                            else
                            {
                                piece.Side = Side.Front;
                            }
                        }
                    }
                }
            }
            else
            {
                // new format

                // Scenario data

                scenarioName        = gameNode.GetAttribute("scenario-name");
                scenarioDescription = gameNode.GetAttribute("scenario-description");
                scenarioCopyright   = gameNode.GetAttribute("scenario-copyright");

                // Operating mode
                mode = (gameNode.HasAttribute("mode") ?
                        (Mode)Enum.Parse(typeof(Mode), gameNode.GetAttribute("mode")) :
                        Mode.Default);

                // Stacking
                stackingEnabled = (!gameNode.HasAttribute("stacking") || gameNode.GetAttribute("stacking") != "disabled");

                // Game data
                XmlNodeList layoutNodeList = gameNode.SelectNodes("layout");
                boards = new Board[layoutNodeList.Count];
                List <Piece> pieceList = new List <Piece>();
                for (int i = 0; i < boards.Length; ++i)
                {
                    XmlElement layoutNode = (XmlElement)layoutNodeList[i];

                    string boardName = layoutNode.GetAttribute("board");

                    // retrieve board properties
                    foreach (MapProperties properties in gameBox.Maps)
                    {
                        if (properties.Name == boardName)
                        {
                            boards[i] = new Map(i, properties);
                            break;
                        }
                    }
                    if (boards[i] == null)
                    {
                        foreach (CounterSheetProperties properties in gameBox.CounterSheets)
                        {
                            if (properties.Name == boardName)
                            {
                                CounterSheet counterSheet = new CounterSheet(i, properties, pieceList);
                                boards[i] = counterSheet;

                                // register each piece's stack as belonging to the counter sheet
                                foreach (ICounterSection counterSection in counterSheet.CounterSections)
                                {
                                    foreach (Piece piece in counterSection.Pieces)
                                    {
                                        Stack stack = piece.Stack;
                                        counterSheet.MoveStackToBack(stack);
                                        stack.AttachedToCounterSection = true;
                                    }
                                }

                                break;
                            }
                        }
                    }
                    if (boards[i] == null)
                    {
                        boards[i] = new Map(i, null);
                    }
                    if (layoutNode.HasAttribute("tab"))
                    {
                        boards[i].Name = layoutNode.GetAttribute("tab");
                    }
                }
                allPieces    = pieceList.ToArray();
                visibleBoard = boards[0];

                for (int i = 0; i < layoutNodeList.Count; ++i)
                {
                    XmlElement layoutNode = (XmlElement)layoutNodeList[i];
                    Board      board      = boards[i];

                    board.VisibleArea = RectangleF.FromLTRB(
                        (layoutNode.HasAttribute("left") ? XmlConvert.ToSingle(layoutNode.GetAttribute("left")) : 0.0f),
                        (layoutNode.HasAttribute("top") ? XmlConvert.ToSingle(layoutNode.GetAttribute("top")) : 0.0f),
                        (layoutNode.HasAttribute("right") ? XmlConvert.ToSingle(layoutNode.GetAttribute("right")) : 1024.0f * 4),
                        (layoutNode.HasAttribute("bottom") ? XmlConvert.ToSingle(layoutNode.GetAttribute("bottom")) : 768.0f * 4));
                    if (board is CounterSheet)
                    {
                        if (layoutNode.HasAttribute("side"))
                        {
                            ((CounterSheet)board).Side = (Side)Enum.Parse(typeof(Side), layoutNode.GetAttribute("side"));
                        }
                        else
                        {
                            ((CounterSheet)board).Side = Side.Front;
                        }
                    }
                    if (layoutNode.HasAttribute("visible") && layoutNode.GetAttribute("visible") == "true")
                    {
                        visibleBoard = board;
                    }
                    if (layoutNode.HasAttribute("owner"))
                    {
                        board.Owner = new Guid(layoutNode.GetAttribute("owner"));
                    }

                    foreach (XmlElement pieceNode in layoutNode.SelectNodes("counter"))
                    {
                        Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id")));
                        if (piece is TerrainPrototype)
                        {
                            piece = new TerrainClone((TerrainPrototype)piece);
                        }
                        Stack stack = (Stack)piece.Stack;
                        stack.AttachedToCounterSection = false;
                        if (pieceNode.HasAttribute("rot"))
                        {
                            piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * (float)Math.PI / 180.0f;
                        }
                        if (pieceNode.HasAttribute("side"))
                        {
                            piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side"));
                        }
                        else
                        {
                            piece.Side = Side.Front;
                        }
                        board.MoveStackToFront(stack);
                        stack.Position = new PointF(
                            XmlConvert.ToSingle(pieceNode.GetAttribute("x")),
                            XmlConvert.ToSingle(pieceNode.GetAttribute("y")));
                    }

                    foreach (XmlElement stackNode in layoutNode.SelectNodes("stack"))
                    {
                        Stack stack = null;
                        foreach (XmlElement pieceNode in stackNode.SelectNodes("counter"))
                        {
                            Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id")));
                            if (piece is TerrainPrototype)
                            {
                                piece = new TerrainClone((TerrainPrototype)piece);
                            }

                            if (stack == null)
                            {
                                stack = (Stack)piece.Stack;
                                stack.AttachedToCounterSection = false;
                                board.MoveStackToFront(stack);
                                stack.Position = new PointF(
                                    XmlConvert.ToSingle(stackNode.GetAttribute("x")),
                                    XmlConvert.ToSingle(stackNode.GetAttribute("y")));
                            }
                            else
                            {
                                Stack stackToMerge = (Stack)piece.Stack;
                                ((Board)stackToMerge.Board).RemoveStack(stackToMerge);
                                stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length);
                            }

                            if (pieceNode.HasAttribute("rot"))
                            {
                                piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f);
                            }
                            if (pieceNode.HasAttribute("side"))
                            {
                                piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side"));
                            }
                            else
                            {
                                piece.Side = Side.Front;
                            }
                        }
                    }
                }

                foreach (XmlElement handNode in gameNode.SelectNodes("hand"))
                {
                    Guid       guid  = new Guid(handNode.GetAttribute("owner"));
                    PlayerHand hand  = new PlayerHand();
                    Stack      stack = null;
                    foreach (XmlElement pieceNode in handNode.SelectNodes("counter"))
                    {
                        Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id")));
                        if (piece is TerrainPrototype)
                        {
                            piece = new TerrainClone((TerrainPrototype)piece);
                        }

                        if (stack == null)
                        {
                            stack = (Stack)piece.Stack;
                            if (stack.Board != null)
                            {
                                ((Board)stack.Board).RemoveStack(stack);
                            }
                            stack.AttachedToCounterSection = false;
                        }
                        else
                        {
                            Stack stackToMerge = (Stack)piece.Stack;
                            if (stackToMerge.Board != null)
                            {
                                ((Board)stackToMerge.Board).RemoveStack(stackToMerge);
                            }
                            stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length);
                        }

                        if (pieceNode.HasAttribute("rot"))
                        {
                            piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f);
                        }
                        if (pieceNode.HasAttribute("side"))
                        {
                            piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side"));
                        }
                        else
                        {
                            piece.Side = Side.Front;
                        }
                    }
                    hand.Stack = stack;
                    playerHands.Add(guid, hand);
                }
            }
        }