Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Content;
  11.  
  12. using GalacticSharp_Client.Modules.GUI;
  13. using GalacticSharp_Client.Modules;
  14. using Lidgren.Network;
  15.  
  16. namespace GalacticSharp_Client
  17. {
  18.     class MainMenu
  19.     {
  20.         string tmpUsername = string.Empty;
  21.         // GameState..
  22.         enum GameState
  23.         {
  24.             mainMenu,
  25.             Login,
  26.             Register,
  27.             CreateChar,
  28.             inGame,
  29.         }
  30.  
  31.         GameState gameState;
  32.  
  33.         // GUI Work
  34.         List<GUIElement> main = new List<GUIElement>();
  35.         List<GUIElement> login = new List<GUIElement>();
  36.         List<GUIElement> register = new List<GUIElement>();
  37.         List<GUIElement> createChar = new List<GUIElement>();
  38.  
  39.         // GUITextbox's
  40.         GUITextbox loginUsername = new GUITextbox("GUI/textbox");
  41.         GUITextbox loginPassword = new GUITextbox("GUI/textbox");
  42.  
  43.         GUITextbox registerUsername = new GUITextbox("GUI/textbox");
  44.         GUITextbox registerPassword = new GUITextbox("GUI/textbox");
  45.         GUITextbox registerConfPassword = new GUITextbox("GUI/textbox");
  46.  
  47.         GUITextbox charName = new GUITextbox("GUI/textbox");
  48.  
  49.         private Keys[] lastPressedKeys = new Keys[5];
  50.  
  51.         private string myName = string.Empty;
  52.         private SpriteFont font;
  53.  
  54.         Game1 game;
  55.  
  56.         // Constructor
  57.         public MainMenu(Game1 game)
  58.         {
  59.             main.Add(new GUIElement("GUI/menu"));
  60.             main.Add(new GUIElement("GUI/play"));
  61.             main.Add(new GUIElement("GUI/nameBtn"));
  62.             main.Add(new GUIElement("GUI/exit"));
  63.  
  64.             login.Add(new GUIElement("GUI/name"));
  65.             login.Add(new GUIElement("GUI/done"));      
  66.  
  67.             register.Add(new GUIElement("GUI/name"));
  68.             register.Add(new GUIElement("GUI/doneRegister"));
  69.  
  70.             createChar.Add(new GUIElement("GUI/name"));
  71.             createChar.Add(new GUIElement("GUI/charAccept"));
  72.  
  73.             this.game = game;
  74.         }
  75.  
  76.         // Load our content
  77.         public void LoadContent(ContentManager content)
  78.         {
  79.             // Loop through all the elements in main and load them, centre them, and add the events
  80.             foreach(GUIElement element in main)
  81.             {
  82.                 element.LoadContent(content);
  83.                 element.CentreElement(800, 600);
  84.                 element.clickEvent += OnClick;
  85.             }
  86.             // Move some elements
  87.             main.Find(x => x.AssetName == "GUI/play").MoveElement(0, -125);
  88.             main.Find(x => x.AssetName == "GUI/nameBtn").MoveElement(0, -92);
  89.             main.Find(x => x.AssetName == "GUI/exit").MoveElement(0, -59);
  90.  
  91.             // Loop through all the elements in login and load them, centre them, and add the events
  92.             foreach(GUIElement element in login)
  93.             {
  94.                 element.LoadContent(content);
  95.                 element.CentreElement(800, 600);
  96.                 element.clickEvent += OnClick;
  97.             }
  98.             // Move the done button
  99.             login.Find(x => x.AssetName == "GUI/done").MoveElement(0, 60);
  100.  
  101.             // Loop through all the elements in register and load them, centre them, and add the events
  102.             foreach(GUIElement element in register)
  103.             {
  104.                 element.LoadContent(content);
  105.                 element.CentreElement(800, 600);
  106.                 element.clickEvent += OnClick;
  107.             }
  108.             // Move the done button
  109.             register.Find(x => x.AssetName == "GUI/doneRegister").MoveElement(0, 60);
  110.  
  111.             foreach(GUIElement element in createChar)
  112.             {
  113.                 element.LoadContent(content);
  114.                 element.CentreElement(800, 600);
  115.                 element.clickEvent += OnClick;
  116.             }
  117.             // Move the done button
  118.             createChar.Find(x => x.AssetName == "GUI/charAccept").MoveElement(0, 60);
  119.  
  120.             // Load our font!
  121.             font = content.Load<SpriteFont>("GUI/MyFont");
  122.  
  123.             // Load, centre, and move our textboxes!
  124.             loginUsername.LoadContent(content);
  125.             loginUsername.element.CentreElement(800, 600);
  126.             loginUsername.element.MoveElement(60, -65);
  127.  
  128.             loginPassword.LoadContent(content);
  129.             loginPassword.element.CentreElement(800, 600);
  130.             loginPassword.element.MoveElement(60, -35);
  131.  
  132.             registerUsername.LoadContent(content);
  133.             registerUsername.element.CentreElement(800, 600);
  134.             registerUsername.element.MoveElement(60, -65);
  135.  
  136.             registerPassword.LoadContent(content);
  137.             registerPassword.element.CentreElement(800, 600);
  138.             registerPassword.element.MoveElement(60, -35);
  139.  
  140.             registerConfPassword.LoadContent(content);
  141.             registerConfPassword.element.CentreElement(800, 600);
  142.             registerConfPassword.element.MoveElement(60, -5);
  143.  
  144.             charName.LoadContent(content);
  145.             charName.element.CentreElement(800, 600);
  146.             charName.element.MoveElement(60, -65);
  147.  
  148.             // Set the style of the textbox
  149.             loginPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
  150.             registerPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
  151.             registerConfPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
  152.         }
  153.  
  154.         // Update
  155.         public void Update(GameTime gameTime)
  156.         {
  157.             // Check our gameState
  158.             switch(gameState)
  159.             {
  160.                 // Main menu game state
  161.                 case GameState.mainMenu:
  162.                     // Only update the elements in the main list
  163.                     foreach (GUIElement element in main)
  164.                     {
  165.                         element.Update();
  166.                     }
  167.  
  168.                     break;
  169.                 // Login game state
  170.                 case GameState.Login:
  171.                     // Only update the elements in the login list
  172.                     foreach (GUIElement element in login)
  173.                     {
  174.                        element.Update();
  175.                     }
  176.                     // Update our textboxes!
  177.                     loginUsername.Update(gameTime);
  178.                     loginPassword.Update(gameTime);
  179.  
  180.                     //Console.WriteLine(loginUsername.enter() + " | " + loginPassword.enter());
  181.  
  182.                     if(loginUsername.enter() || loginPassword.enter())
  183.                     {
  184.                         //if (loginUsername.text == "" || loginPassword.text == "")
  185.                         //{
  186.                         //    System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
  187.                         //}
  188.                         //else
  189.                         //{
  190.                         //    gameState = GameState.mainMenu;
  191.                         //    ClientTCP.Login(loginUsername.text, loginPassword.text);
  192.                         //    loginUsername.Clear();
  193.                         //    loginPassword.Clear();
  194.                         //}
  195.                     }
  196.  
  197.                     break;
  198.                 // Register gameState
  199.                 case GameState.Register:
  200.                     // Only update the elements in the register list
  201.                     foreach(GUIElement element in register)
  202.                     {
  203.                         element.Update();
  204.                     }
  205.  
  206.                     // Update our textboxes!
  207.                     registerUsername.Update(gameTime);
  208.                     registerPassword.Update(gameTime);
  209.                     registerConfPassword.Update(gameTime);
  210.  
  211.                     if(registerUsername.enter() || registerPassword.enter() || registerConfPassword.enter())
  212.                     {
  213.                         //if (registerUsername.text == "" || registerPassword.text == "" || registerConfPassword.text == "")
  214.                         //{
  215.                         //    System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
  216.                         //}
  217.                         //else
  218.                         //{
  219.                         //    if (registerPassword.text == registerConfPassword.text)
  220.                         //    {
  221.                         //        gameState = GameState.mainMenu;
  222.                         //        ClientTCP.Register(registerUsername.text, registerPassword.text);
  223.                         //        registerUsername.Clear();
  224.                         //        registerPassword.Clear();
  225.                         //        registerConfPassword.Clear();
  226.                         //    }
  227.                         //    else
  228.                         //    {
  229.                         //        System.Windows.Forms.MessageBox.Show("The passwords don't match!", "GalacticSharp");
  230.                         //    }
  231.                         //}
  232.                     }
  233.  
  234.                     break;
  235.                 case GameState.CreateChar:
  236.                     // Only update the elements in the createChar list
  237.                     foreach(GUIElement element in createChar)
  238.                     {
  239.                         element.Update();
  240.                     }
  241.                     // Update our textboxes!
  242.                     charName.Update(gameTime);
  243.                     break;
  244.  
  245.                 // inGame gameState
  246.                 case GameState.inGame:
  247.                     break;
  248.             }
  249.         }
  250.  
  251.         // Draw!
  252.         public void Draw(SpriteBatch spriteBatch)
  253.         {
  254.             switch (gameState)
  255.             {
  256.                 case GameState.mainMenu:
  257.                     foreach (GUIElement element in main)
  258.                     {
  259.                         element.Draw(spriteBatch);
  260.                     }
  261.  
  262.                     break;
  263.                 case GameState.Login:
  264.                     foreach (GUIElement element in login)
  265.                     {
  266.                         element.Draw(spriteBatch);
  267.                     }
  268.  
  269.                     loginUsername.Draw(spriteBatch);
  270.                     loginPassword.Draw(spriteBatch);
  271.  
  272.                     spriteBatch.DrawString(font, "Username", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, loginUsername.element.GUIRect.Y + 5), Color.White);
  273.                     spriteBatch.DrawString(font, "Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, loginPassword.element.GUIRect.Y + 5), Color.White);
  274.                     break;
  275.                 case GameState.Register:
  276.                     foreach (GUIElement element in register)
  277.                     {
  278.                         element.Draw(spriteBatch);
  279.                     }
  280.  
  281.                     registerUsername.Draw(spriteBatch);
  282.                     registerPassword.Draw(spriteBatch);
  283.                     registerConfPassword.Draw(spriteBatch);
  284.  
  285.                     spriteBatch.DrawString(font, "Username", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerUsername.element.GUIRect.Y + 5), Color.White);
  286.                     spriteBatch.DrawString(font, "Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerPassword.element.GUIRect.Y + 5), Color.White);
  287.                     spriteBatch.DrawString(font, "Confirm Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerConfPassword.element.GUIRect.Y + 5), Color.White);
  288.                    
  289.                     break;
  290.                 case GameState.CreateChar:
  291.                     foreach(GUIElement element in createChar)
  292.                     {
  293.                         element.Draw(spriteBatch);
  294.                     }
  295.                     charName.Draw(spriteBatch);
  296.  
  297.                     spriteBatch.DrawString(font, "Character Name", new Vector2(createChar.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, charName.element.GUIRect.Y + 5), Color.White);
  298.  
  299.                     break;
  300.                 case GameState.inGame:
  301.                     break;
  302.             }
  303.         }
  304.  
  305.         // Our click event
  306.         public void OnClick(string element)
  307.         {
  308.             if(element == "GUI/play")
  309.             {
  310.                 // Play the game
  311.                 gameState = GameState.Register;
  312.             }
  313.  
  314.             if(element == "GUI/nameBtn")
  315.             {
  316.                 gameState = GameState.Login;
  317.             }
  318.  
  319.             if(element == "GUI/done")
  320.             {
  321.                 // Check if a textbox is blank...
  322.                 if (loginUsername.text == "" || loginPassword.text == "")
  323.                 {
  324.                     // Tell user one of the fields are blank
  325.                     System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
  326.                 }
  327.                 else
  328.                 {
  329.                     // Change gameState
  330.                     gameState = GameState.inGame;
  331.                     // Call the Login method (username as String, password as String )
  332.                     ClientTCP.Login(loginUsername.text, loginPassword.text);
  333.                     ClientTCP.GetPlayerName(loginUsername.text);
  334.                     // Clear the textboxes
  335.                     loginUsername.Clear();
  336.                     loginPassword.Clear();
  337.                 }
  338.             }
  339.  
  340.             if (element == "GUI/doneRegister")
  341.             {
  342.                 // Check if a textbox is blank...
  343.                 if (registerUsername.text == "" || registerPassword.text == "" || registerConfPassword.text == "")
  344.                 {
  345.                     // tell user one of the fields are blank
  346.                     System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
  347.                 }
  348.                 else
  349.                 {
  350.                     // Make sure the passwords match!
  351.                     if (registerPassword.text == registerConfPassword.text)
  352.                     {
  353.                         tmpUsername = registerUsername.text;
  354.                         // Change gameState
  355.                         gameState = GameState.CreateChar;
  356.                         // Call the Register method (username as String, password as String )
  357.                         ClientTCP.Register(registerUsername.text, registerPassword.text);
  358.                         // Clear the textboxes
  359.                         registerUsername.Clear();
  360.                         registerPassword.Clear();
  361.                         registerConfPassword.Clear();
  362.                     }
  363.                     else
  364.                     {
  365.                         // tell user that the passwords don't match!
  366.                         System.Windows.Forms.MessageBox.Show("The passwords don't match!", "GalacticSharp");
  367.                     }
  368.                 }
  369.             }
  370.  
  371.             if(element == "GUI/charAccept")
  372.             {
  373.                 if (charName.text == "")
  374.                 {
  375.                     // tell user one of the fields are blank
  376.                     System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
  377.                 }
  378.                 else
  379.                 {
  380.                     gameState = GameState.inGame;
  381.                     ClientTCP.SendNewChar(tmpUsername, charName.text, Vector2.Zero, 0);
  382.                     Globals.playerName = tmpUsername;
  383.                     // Clear the textboxes
  384.                     charName.Clear();
  385.                 }
  386.             }
  387.  
  388.             if (element == "GUI/exit")
  389.             {
  390.                 // Simply exit the game via .Exit() method
  391.                 Game1.client.Disconnect("ExitClosed");
  392.                 game.Exit();
  393.             }
  394.         }
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement