Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Content;
- using GalacticSharp_Client.Modules.GUI;
- using GalacticSharp_Client.Modules;
- using Lidgren.Network;
- namespace GalacticSharp_Client
- {
- class MainMenu
- {
- string tmpUsername = string.Empty;
- // GameState..
- enum GameState
- {
- mainMenu,
- Login,
- Register,
- CreateChar,
- inGame,
- }
- GameState gameState;
- // GUI Work
- List<GUIElement> main = new List<GUIElement>();
- List<GUIElement> login = new List<GUIElement>();
- List<GUIElement> register = new List<GUIElement>();
- List<GUIElement> createChar = new List<GUIElement>();
- // GUITextbox's
- GUITextbox loginUsername = new GUITextbox("GUI/textbox");
- GUITextbox loginPassword = new GUITextbox("GUI/textbox");
- GUITextbox registerUsername = new GUITextbox("GUI/textbox");
- GUITextbox registerPassword = new GUITextbox("GUI/textbox");
- GUITextbox registerConfPassword = new GUITextbox("GUI/textbox");
- GUITextbox charName = new GUITextbox("GUI/textbox");
- private Keys[] lastPressedKeys = new Keys[5];
- private string myName = string.Empty;
- private SpriteFont font;
- Game1 game;
- // Constructor
- public MainMenu(Game1 game)
- {
- main.Add(new GUIElement("GUI/menu"));
- main.Add(new GUIElement("GUI/play"));
- main.Add(new GUIElement("GUI/nameBtn"));
- main.Add(new GUIElement("GUI/exit"));
- login.Add(new GUIElement("GUI/name"));
- login.Add(new GUIElement("GUI/done"));
- register.Add(new GUIElement("GUI/name"));
- register.Add(new GUIElement("GUI/doneRegister"));
- createChar.Add(new GUIElement("GUI/name"));
- createChar.Add(new GUIElement("GUI/charAccept"));
- this.game = game;
- }
- // Load our content
- public void LoadContent(ContentManager content)
- {
- // Loop through all the elements in main and load them, centre them, and add the events
- foreach(GUIElement element in main)
- {
- element.LoadContent(content);
- element.CentreElement(800, 600);
- element.clickEvent += OnClick;
- }
- // Move some elements
- main.Find(x => x.AssetName == "GUI/play").MoveElement(0, -125);
- main.Find(x => x.AssetName == "GUI/nameBtn").MoveElement(0, -92);
- main.Find(x => x.AssetName == "GUI/exit").MoveElement(0, -59);
- // Loop through all the elements in login and load them, centre them, and add the events
- foreach(GUIElement element in login)
- {
- element.LoadContent(content);
- element.CentreElement(800, 600);
- element.clickEvent += OnClick;
- }
- // Move the done button
- login.Find(x => x.AssetName == "GUI/done").MoveElement(0, 60);
- // Loop through all the elements in register and load them, centre them, and add the events
- foreach(GUIElement element in register)
- {
- element.LoadContent(content);
- element.CentreElement(800, 600);
- element.clickEvent += OnClick;
- }
- // Move the done button
- register.Find(x => x.AssetName == "GUI/doneRegister").MoveElement(0, 60);
- foreach(GUIElement element in createChar)
- {
- element.LoadContent(content);
- element.CentreElement(800, 600);
- element.clickEvent += OnClick;
- }
- // Move the done button
- createChar.Find(x => x.AssetName == "GUI/charAccept").MoveElement(0, 60);
- // Load our font!
- font = content.Load<SpriteFont>("GUI/MyFont");
- // Load, centre, and move our textboxes!
- loginUsername.LoadContent(content);
- loginUsername.element.CentreElement(800, 600);
- loginUsername.element.MoveElement(60, -65);
- loginPassword.LoadContent(content);
- loginPassword.element.CentreElement(800, 600);
- loginPassword.element.MoveElement(60, -35);
- registerUsername.LoadContent(content);
- registerUsername.element.CentreElement(800, 600);
- registerUsername.element.MoveElement(60, -65);
- registerPassword.LoadContent(content);
- registerPassword.element.CentreElement(800, 600);
- registerPassword.element.MoveElement(60, -35);
- registerConfPassword.LoadContent(content);
- registerConfPassword.element.CentreElement(800, 600);
- registerConfPassword.element.MoveElement(60, -5);
- charName.LoadContent(content);
- charName.element.CentreElement(800, 600);
- charName.element.MoveElement(60, -65);
- // Set the style of the textbox
- loginPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
- registerPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
- registerConfPassword.textStyle = GUITextbox.TextStyle.PASSWORD;
- }
- // Update
- public void Update(GameTime gameTime)
- {
- // Check our gameState
- switch(gameState)
- {
- // Main menu game state
- case GameState.mainMenu:
- // Only update the elements in the main list
- foreach (GUIElement element in main)
- {
- element.Update();
- }
- break;
- // Login game state
- case GameState.Login:
- // Only update the elements in the login list
- foreach (GUIElement element in login)
- {
- element.Update();
- }
- // Update our textboxes!
- loginUsername.Update(gameTime);
- loginPassword.Update(gameTime);
- //Console.WriteLine(loginUsername.enter() + " | " + loginPassword.enter());
- if(loginUsername.enter() || loginPassword.enter())
- {
- //if (loginUsername.text == "" || loginPassword.text == "")
- //{
- // System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
- //}
- //else
- //{
- // gameState = GameState.mainMenu;
- // ClientTCP.Login(loginUsername.text, loginPassword.text);
- // loginUsername.Clear();
- // loginPassword.Clear();
- //}
- }
- break;
- // Register gameState
- case GameState.Register:
- // Only update the elements in the register list
- foreach(GUIElement element in register)
- {
- element.Update();
- }
- // Update our textboxes!
- registerUsername.Update(gameTime);
- registerPassword.Update(gameTime);
- registerConfPassword.Update(gameTime);
- if(registerUsername.enter() || registerPassword.enter() || registerConfPassword.enter())
- {
- //if (registerUsername.text == "" || registerPassword.text == "" || registerConfPassword.text == "")
- //{
- // System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
- //}
- //else
- //{
- // if (registerPassword.text == registerConfPassword.text)
- // {
- // gameState = GameState.mainMenu;
- // ClientTCP.Register(registerUsername.text, registerPassword.text);
- // registerUsername.Clear();
- // registerPassword.Clear();
- // registerConfPassword.Clear();
- // }
- // else
- // {
- // System.Windows.Forms.MessageBox.Show("The passwords don't match!", "GalacticSharp");
- // }
- //}
- }
- break;
- case GameState.CreateChar:
- // Only update the elements in the createChar list
- foreach(GUIElement element in createChar)
- {
- element.Update();
- }
- // Update our textboxes!
- charName.Update(gameTime);
- break;
- // inGame gameState
- case GameState.inGame:
- break;
- }
- }
- // Draw!
- public void Draw(SpriteBatch spriteBatch)
- {
- switch (gameState)
- {
- case GameState.mainMenu:
- foreach (GUIElement element in main)
- {
- element.Draw(spriteBatch);
- }
- break;
- case GameState.Login:
- foreach (GUIElement element in login)
- {
- element.Draw(spriteBatch);
- }
- loginUsername.Draw(spriteBatch);
- loginPassword.Draw(spriteBatch);
- spriteBatch.DrawString(font, "Username", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, loginUsername.element.GUIRect.Y + 5), Color.White);
- spriteBatch.DrawString(font, "Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, loginPassword.element.GUIRect.Y + 5), Color.White);
- break;
- case GameState.Register:
- foreach (GUIElement element in register)
- {
- element.Draw(spriteBatch);
- }
- registerUsername.Draw(spriteBatch);
- registerPassword.Draw(spriteBatch);
- registerConfPassword.Draw(spriteBatch);
- spriteBatch.DrawString(font, "Username", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerUsername.element.GUIRect.Y + 5), Color.White);
- spriteBatch.DrawString(font, "Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerPassword.element.GUIRect.Y + 5), Color.White);
- spriteBatch.DrawString(font, "Confirm Password", new Vector2(login.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, registerConfPassword.element.GUIRect.Y + 5), Color.White);
- break;
- case GameState.CreateChar:
- foreach(GUIElement element in createChar)
- {
- element.Draw(spriteBatch);
- }
- charName.Draw(spriteBatch);
- spriteBatch.DrawString(font, "Character Name", new Vector2(createChar.Find(x => x.AssetName == "GUI/name").GUIRect.X + 15, charName.element.GUIRect.Y + 5), Color.White);
- break;
- case GameState.inGame:
- break;
- }
- }
- // Our click event
- public void OnClick(string element)
- {
- if(element == "GUI/play")
- {
- // Play the game
- gameState = GameState.Register;
- }
- if(element == "GUI/nameBtn")
- {
- gameState = GameState.Login;
- }
- if(element == "GUI/done")
- {
- // Check if a textbox is blank...
- if (loginUsername.text == "" || loginPassword.text == "")
- {
- // Tell user one of the fields are blank
- System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
- }
- else
- {
- // Change gameState
- gameState = GameState.inGame;
- // Call the Login method (username as String, password as String )
- ClientTCP.Login(loginUsername.text, loginPassword.text);
- ClientTCP.GetPlayerName(loginUsername.text);
- // Clear the textboxes
- loginUsername.Clear();
- loginPassword.Clear();
- }
- }
- if (element == "GUI/doneRegister")
- {
- // Check if a textbox is blank...
- if (registerUsername.text == "" || registerPassword.text == "" || registerConfPassword.text == "")
- {
- // tell user one of the fields are blank
- System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
- }
- else
- {
- // Make sure the passwords match!
- if (registerPassword.text == registerConfPassword.text)
- {
- tmpUsername = registerUsername.text;
- // Change gameState
- gameState = GameState.CreateChar;
- // Call the Register method (username as String, password as String )
- ClientTCP.Register(registerUsername.text, registerPassword.text);
- // Clear the textboxes
- registerUsername.Clear();
- registerPassword.Clear();
- registerConfPassword.Clear();
- }
- else
- {
- // tell user that the passwords don't match!
- System.Windows.Forms.MessageBox.Show("The passwords don't match!", "GalacticSharp");
- }
- }
- }
- if(element == "GUI/charAccept")
- {
- if (charName.text == "")
- {
- // tell user one of the fields are blank
- System.Windows.Forms.MessageBox.Show("One of the fields are blank!", "GalacticSharp");
- }
- else
- {
- gameState = GameState.inGame;
- ClientTCP.SendNewChar(tmpUsername, charName.text, Vector2.Zero, 0);
- Globals.playerName = tmpUsername;
- // Clear the textboxes
- charName.Clear();
- }
- }
- if (element == "GUI/exit")
- {
- // Simply exit the game via .Exit() method
- Game1.client.Disconnect("ExitClosed");
- game.Exit();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement