/// <summary> /// Creates a GUIElement that fits the given parameters. /// </summary> /// <param name="game">The current Game object.</param> /// <param name="state">The State to be visualized.</param> /// <param name="role">The Role to view the given state from.</param> /// <param name="gameState">An object that holds some game properties that is needed for some States.</param> /// <returns>A new GUIElement that visualizes the given State.</returns> public static GUIElement CreateStateElement(Game game, State state, Role role, GameState gameState) { Contract.Requires(game != null); Contract.Ensures(Contract.Result<GUIElement>() != null); Contract.Ensures(Contract.Result<GUIElement>().Bound.X == 0); Contract.Ensures(Contract.Result<GUIElement>().Bound.Y == 0); Contract.Ensures(Contract.Result<GUIElement>().Bound.Width == game.GraphicsDevice.Viewport.Width); Contract.Ensures(Contract.Result<GUIElement>().Bound.Height == game.GraphicsDevice.Viewport.Height); Viewport g = game.GraphicsDevice.Viewport; GUIElement root = CreateEmptyRoot(game); switch (state) { // TODO: fill the root here for all states that is drawn case State.InLobby: { GUIElement box = new GUIElement(game, "players", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 80)); GUIElement p1 = new GUIElement(game, "player1", RelW(g, 40), RelH(g, 15), RelW(g, 20), RelH(g, 20)); GUIElement p2 = new GUIElement(game, "player2", RelW(g, 23), RelH(g, 40), RelW(g, 20), RelH(g, 20)); GUIElement p3 = new GUIElement(game, "player3", RelW(g, 56), RelH(g, 40), RelW(g, 20), RelH(g, 20)); GUIElement p4 = new GUIElement(game, "player4", RelW(g, 23), RelH(g, 65), RelW(g, 20), RelH(g, 20)); GUIElement p5 = new GUIElement(game, "player5", RelW(g, 56), RelH(g, 65), RelW(g, 20), RelH(g, 20)); root.AddChild(box); box.AddChild(p1); box.AddChild(p2); box.AddChild(p3); box.AddChild(p4); box.AddChild(p5); Vector2 pos = new Vector2(10, 15); root.AddText("player1", "Overlord:", pos); root.AddText("player2", "Hero:", pos); root.AddText("player3", "Hero:", pos); root.AddText("player4", "Hero:", pos); root.AddText("player5", "Hero:", pos); if (Player.Instance.IsServer) { GUIElement start = new GUIElement(game, "start", RelW(g, 84), RelH(g, 90), RelW(g, 12), RelH(g, 4)); root.AddChild(start); root.AddText("start", "Start Game", new Vector2(0, 0)); string ipString = ""; foreach (string ip in Player.Instance.Connection.Ips) ipString += (ip + "\n"); root.AddText(box.Name, ipString, new Vector2(10, 10)); } break; } case State.AllBuyEquipment: case State.BuyEquipment: { GUIElement box = new GUIElement(game, "shop", RelW(g, 5), RelH(g, 5), RelW(g, 90), RelH(g, 80)); int startY = RelH(g, 10); int startX = RelW(g, 10); int spacerX = RelW(g, 2); int spacerY = RelH(g, 5); int width = RelW(g, 12); int height = RelH(g, 16); Equipment[] shopContent = gameState.CurrentEquipment.Where(n => n.Rarity == EquipmentRarity.Common).Distinct().ToArray(); for (int y = 0; y < 4; y++) { for (int x = 0; x < 6; x++) { if (shopContent.Length > (x + y * 6)) { Equipment current = shopContent[x + y * 6]; EquipmentElement eq = new EquipmentElement(game, startX + x * width + x * spacerX, startY + y * width + y * spacerY, width, height, "", current, x + y * 6 + 1000); box.AddChild(eq); } } } if (role != Role.Overlord) { GUIElement done = new GUIElement(game, "done", RelW(g, 85), RelH(g, 90), RelW(g, 10), RelH(g, 5)); done.AddText(done.Name, "Done", new Vector2(0, 0)); root.AddChild(done); } root.AddChild(box); break; } case State.AllEquip: case State.Equip: { if (role != Role.Overlord) { int startX = RelW(g, 15); int startY = RelH(g, 15); int spacerX = RelW(g, 2); int width = RelW(g, 10); Equipment[] unequipped = gameState.UnequippedEquipment(Player.Instance.Id); int boxes = Math.Max(1, unequipped.Length); for (int x = 0; x < boxes; x++) { Equipment current = (x < unequipped.Length) ? unequipped[x] : null; EquipmentElement eq = GUIElementFactory.CreateEquipmentElement(game, startX + x * width + x * spacerX, startY, "", current, x + 100); root.AddChild(eq); } GUIElement done = new GUIElement(game, "done", RelW(g, 3), RelW(g, 3), RelW(g, 10), RelH(g, 5)); done.AddText(done.Name, "Done", new Vector2(0, 0)); root.AddChild(done); } break; } case State.WaitForChooseSquare: // hero placement { if (role != Role.Overlord) { GUIElement help = new GUIElement(game, "help", RelW(g, 3), RelW(g, 3), RelW(g, 33), RelH(g, 5)); root.AddChild(help); root.AddText(help.Name, "Select a square to place your hero", new Vector2(5, 5)); } break; } case State.WaitForHeroTurn: { if (role != Role.Overlord) { GUIElement takeTurn = new GUIElement(game, "take turn", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 5)); root.AddChild(takeTurn); root.AddText(takeTurn.Name, "Take Turn", new Vector2(5, 5)); } break; } case State.WaitForChooseAction: { if (role == Role.ActiveHero) { GUIElement advance = new GUIElement(game, "advance", RelW(g, 3), RelH(g, 3), RelW(g, 10), RelH(g, 5)); GUIElement run = new GUIElement(game, "run", RelW(g, 3), RelH(g, 10), RelW(g, 10), RelH(g, 5)); GUIElement battle = new GUIElement(game, "battle", RelW(g, 3), RelH(g, 17), RelW(g, 10), RelH(g, 5)); Vector2 v = new Vector2(5, 5); advance.AddText(advance.Name, "Advance", v); run.AddText(run.Name, "Run", v); battle.AddText(battle.Name, "Battle", v); root.AddChild(advance); root.AddChild(run); root.AddChild(battle); } break; } case State.WaitForPerformAction: { if (role == Role.ActiveHero) { GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 5)); root.AddChild(end); root.AddText(end.Name, "End Turn", new Vector2(5, 5)); } else if (role == Role.Overlord && Player.Instance.StateManager.HasTurn()) { GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 8)); root.AddChild(end); root.AddText(end.Name, "End Monster Turn", new Vector2(5, 5)); } break; } case State.WaitForOverlordChooseAction: { if (role == Role.Overlord) { GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 8)); root.AddChild(end); root.AddText(end.Name, "End Overlord Turn", new Vector2(5, 5)); } break; } case State.WaitForRollDice: { GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60)); if (gameState.CurrentPlayer == Player.Instance.Id) { GUIElement roll = new GUIElement(game, "roll", RelW(g, 75), RelH(g, 70), RelW(g, 10), RelH(g, 6)); root.AddChild(roll); root.AddText(roll.Name, "Roll", new Vector2(5, 5)); } root.AddChild(box); break; } case State.WaitForDiceChoice: { GUIElement box = new AttackElement(game, gameState.CurrentAttack, RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60)); if (gameState.CurrentPlayer == Player.Instance.Id) { GUIElement finish = new GUIElement(game, "finish", RelW(g, 75), RelH(g, 70), RelW(g, 10), RelH(g, 8)); root.AddChild(finish); root.AddText(finish.Name, "Inflict Wounds", new Vector2(5, 5)); } root.AddChild(box); break; } case State.EndGameHeroParty: { GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60)); GUIElement inner = new GUIElement(game, "inner", RelW(g, 30), RelH(g, 20), RelW(g, 40), RelH(g, 60)); box.AddChild(inner); inner.AddText(inner.Name, "The heroes have triumphed over Joe and won the game!", new Vector2(5, 5)); inner.SetDrawBackground(false); root.AddChild(box); break; } case State.EndGameOverlord: { GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60)); GUIElement inner = new GUIElement(game, "inner", RelW(g, 30), RelH(g, 20), RelW(g, 40), RelH(g, 60)); box.AddChild(inner); inner.AddText(inner.Name, "Joe has truimphed over the heroes and won the game!", new Vector2(5, 5)); inner.SetDrawBackground(false); root.AddChild(box); break; } } return root; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); GUI.GUI.Font = Content.Load<SpriteFont>("font"); FullModel.LoadContent(this); // creation of elements this.gui = new GUI.GUI(this); GUIElement root = GUIElementFactory.CreateStateElement(this, Descent.State.State.ActivateMonsters, Descent.Model.Player.Role.Overlord, null); GUIElement createGame = new GUIElement(this, "create", (GraphicsDevice.Viewport.Width - 300) / 2, 400, 300, 100); GUIElement joinGame = new GUIElement(this, "join", (GraphicsDevice.Viewport.Width - 300) / 2, 550, 300, 100); GUIElement changeName = new GUIElement(this, "changeName", (GraphicsDevice.Viewport.Width - 200) / 2, 250, 200, 100); InputElement nameInput = new InputElement(this, "nameInput", changeName.Bound.X + 10, changeName.Bound.Y + (changeName.Bound.Height - 30) / 2, changeName.Bound.Width - 20, 30); InputElement connectInput = new InputElement(this, "connectInput", joinGame.Bound.X + 10, joinGame.Bound.Y + (joinGame.Bound.Height - 30) / 2, 150, 30); GUIElement buttonCreateGame = new GUIElement(this, "doneCreate", createGame.Bound.X + (createGame.Bound.Width - 100) / 2, createGame.Bound.Y + (createGame.Bound.Height - 30) / 2, 120, 30); GUIElement buttonJoinGame = new GUIElement(this, "doneJoin", joinGame.Bound.X + 200, joinGame.Bound.Y + (joinGame.Bound.Height - 30) / 2, 80, 30); // assembling tree root.AddChild(createGame); root.AddChild(joinGame); root.AddChild(changeName); changeName.AddChild(nameInput); createGame.AddChild(buttonCreateGame); joinGame.AddChild(connectInput); joinGame.AddChild(buttonJoinGame); // adding visual to tree changeName.SetBackground("boxbg"); joinGame.SetBackground("boxbg"); createGame.SetBackground("boxbg"); nameInput.SetBackground("boxbg"); connectInput.SetBackground("boxbg"); buttonCreateGame.SetBackground("boxbg"); buttonJoinGame.SetBackground("boxbg"); Image logo = new Image(Content.Load<Texture2D>("logo-descent")); root.AddDrawable(root.Name, logo, new Vector2((root.Bound.Width - logo.Texture.Bounds.Width) / 2.0f, 50)); // adding logic to tree root.SetDrawBackground(false); root.AddText("changeName", "Name:", new Vector2(5, 0)); root.AddText("doneCreate", "New Game!", new Vector2(5, 0)); root.AddText("doneJoin", "Join!", new Vector2(25, 0)); root.SetClickAction("doneCreate", (n, g) => { // Start the game. TODO: Try/catch error handling. n.StartGame(1337); // Set the nickname. Since this is the server, it will be set on id 1 always. if (InputElement.GetInputFrom("nameInput").Length > 0) { n.Nickname = InputElement.GetInputFrom("nameInput"); } // Create the state manager. n.StateManager = new StateManager(gui); }); root.SetClickAction("doneJoin", (n, g) => { g.SetClickAction(g.Name, null); n.JoinGame(InputElement.GetInputFrom("connectInput"), 1337); Player.Instance.EventManager.AcceptPlayerEvent += new AcceptPlayerHandler((sender, eventArgs) => { if (eventArgs.PlayerId == Player.Instance.Id) { if (InputElement.GetInputFrom("nameInput").Length > 0) { n.Nickname = InputElement.GetInputFrom("nameInput"); } n.StateManager = new StateManager(gui); Player.Instance.EventManager.QueueEvent(EventType.PlayerJoined, new PlayerJoinedEventArgs(Player.Instance.Id, Player.Instance.Nickname)); } }); }); Player.Instance.EventManager.PlayerJoinedEvent += new PlayerJoinedHandler((sender, eventArgs) => { // If the PlayerJoined event is about our local player(the id will be set just before this, so }); // placing the root in the gui gui.ChangeStateGUI(root); }