1
0
Fork 0
This repository has been archived on 2019-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Galactic_Colors_Control/Galactic Colors Control GUI/States/GameState.cs

169 lines
7.2 KiB
C#

//TODO add party support
using Galactic_Colors_Control_Common;
using Galactic_Colors_Control_Common.Protocol;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MyMonoGame.GUI;
using System.Threading;
using System;
using Microsoft.Xna.Framework.Input;
namespace Galactic_Colors_Control_GUI.States
{
public class GameState : State
{
private string username;
private bool showChat = false;
private string chatText;
private string chatInput;
private bool showLoading = false;
private bool showOKMessage = false;
private Message message;
public GameState(string Username)
{
Game.singleton.client.OnEvent += new EventHandler(OnEvent); //Set OnEvent function
username = Username;
}
public override void Draw(SpriteBatch spritebatch)
{
Game.singleton.background.Draw(spritebatch);
Game.singleton.GUI.Texture(new Rectangle(0, 0, Game.singleton.ScreenWidth, 30), Game.nullSprite, new MyMonoGame.Colors(new Color(0.1f, 0.1f, 0.1f)));
if (Game.singleton.GUI.Button(new Rectangle(5, 5, 50, 20), (showChat ? "Hide" : "Show") + " chat", Game.singleton.fonts.small, new MyMonoGame.Colors(Color.White, Color.LightGray, Color.Gray))) { Game.singleton.GUI.ResetFocus(); showChat = !showChat; }
//if (Game.singleton.GUI.Button(new Rectangle(65, 5, 50, 20), (showParty ? "Leave" : "Join") + " party", smallFont, new MyMonoGame.Colors(Color.White, Color.LightGray, Color.Gray))) { new Thread(PartyClick).Start(); }
if (showChat)
{
Game.singleton.GUI.Box(new Rectangle(0, 30, 310, 310), Game.singleton.buttonsSprites[0]);
if (Game.singleton.GUI.TextField(new Rectangle(5, 35, 305, 20), ref chatInput, Game.singleton.fonts.basic, null, Manager.textAlign.centerLeft, "Enter message")) { if (chatInput != null) { new Thread(ChatEnter).Start(); } }
Game.singleton.GUI.Label(new Rectangle(5, 60, 305, 245), chatText, Game.singleton.fonts.small, null, Manager.textAlign.topLeft, true);
}
if (showLoading)
{
Game.singleton.GUI.Box(new Rectangle(Game.singleton.ScreenWidth / 2 - 150, Game.singleton.ScreenHeight / 4 + 50, 300, 50), Game.singleton.buttonsSprites[0]);
Game.singleton.GUI.Label(new Rectangle(Game.singleton.ScreenWidth / 2 - 150, Game.singleton.ScreenHeight / 4 + 50, 300, 50), "Loading", Game.singleton.fonts.basic);
}
else
{
if (showOKMessage)
{
Game.singleton.GUI.Box(new Rectangle(Game.singleton.ScreenWidth / 2 - 150, Game.singleton.ScreenHeight / 4 + 50, 300, 150), Game.singleton.buttonsSprites[0]);
Game.singleton.GUI.Label(new MyMonoGame.Vector(Game.singleton.ScreenWidth / 2, Game.singleton.ScreenHeight / 4 + 60), message.title, Game.singleton.fonts.basic, null, Manager.textAlign.bottomCenter);
Game.singleton.GUI.Label(new MyMonoGame.Vector(Game.singleton.ScreenWidth / 2, Game.singleton.ScreenHeight / 4 + 100), message.text, Game.singleton.fonts.small, null, Manager.textAlign.bottomCenter);
if (Game.singleton.GUI.Button(new Rectangle(Game.singleton.ScreenWidth / 2 - 140, Game.singleton.ScreenHeight / 4 + 150, 280, 40), Game.singleton.buttonsSprites[0], "Ok", Game.singleton.fonts.basic)) { Game.singleton.GUI.ResetFocus(); showOKMessage = false; Game.singleton.client.ExitHost(); Game.singleton.gameState = new MainMenuState(); }
}
}
}
public override void Update()
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { Game.singleton.GUI.ResetFocus(); Game.singleton.client.ExitHost(); Game.singleton.gameState = new MainMenuState(); }
}
private void ChatEnter()
{
string request = chatInput;
chatInput = null;
ResultData res = Game.singleton.client.Request(new string[2] { "say", request });
if(res.type != ResultTypes.OK)
{
//TODO Mutlilang
ChatText("Error :" + Common.ArrayToString(res.result));
}
}
private void OnEvent(object sender, EventArgs e)
{
EventData eve = ((EventDataArgs)e).Data;
switch (eve.type)
{
case EventTypes.ChatMessage:
ChatText(Common.ArrayToString(eve.data));
break;
case EventTypes.ServerJoin:
ChatText(Common.ArrayToString(eve.data) + "join the server");
break;
case EventTypes.ServerLeave:
ChatText(Common.ArrayToString(eve.data) + "leave the server");
break;
case EventTypes.ServerKick:
message.title = "Kick from server";
message.text = Common.ArrayToString(eve.data);
showOKMessage = true;
break;
}
}
public void ChatText(string text)
{
chatText += (text + Environment.NewLine);
}
/*
private void PartyClick()
{
showLoading = true;
GUI.ResetFocus();
//TODO
/*
if (showParty)
{
client.SendRequest("/party leave");
showParty = false;
showLoading = false;
}
else
{
client.Output.Clear();
client.SendRequest("/party list");
int wait = 0;
while (wait < 20)
{
if (client.Output.Count > 0)
{
wait = 20;
}
else
{
wait++;
Thread.Sleep(200);
}
}
if (client.Output.Count > 0)
{
Thread.Sleep(500);
if (client.Output.Count > 1)
{
messageTitle = "Party";
messageText = string.Empty;
foreach (string line in client.Output.ToArray()) { messageText += (line + Environment.NewLine); }
showOKMessage = true;
client.Output.Clear();
}
else
{
messageTitle = "Any party";
messageText = string.Empty;
foreach (string line in client.Output.ToArray()) { messageText += (line + Environment.NewLine); }
showOKMessage = true;
client.Output.Clear();
}
}
else
{
messageTitle = "Timeout";
messageText = "";
showOKMessage = true;
showLoading = false;
client.Output.Clear();
}
}
}*/
}
}