GUIStyle improvements & some simple UI graphics, networking bugfixes, separate Random class, some StyleCop cleanup
This commit is contained in:
@@ -14,11 +14,6 @@ namespace Subsurface.Networking
|
||||
private Character myCharacter;
|
||||
private CharacterInfo characterInfo;
|
||||
|
||||
string name;
|
||||
|
||||
// Create timer that tells client, when to send update
|
||||
// System.Timers.Timer update;
|
||||
|
||||
public Character Character
|
||||
{
|
||||
get { return myCharacter; }
|
||||
@@ -30,16 +25,6 @@ namespace Subsurface.Networking
|
||||
get { return characterInfo; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrEmpty(name)) return;
|
||||
name = value;
|
||||
}
|
||||
}
|
||||
|
||||
public GameClient(string newName)
|
||||
{
|
||||
name = newName;
|
||||
@@ -64,7 +49,7 @@ namespace Subsurface.Networking
|
||||
Client.Start();
|
||||
|
||||
outmsg.Write((byte)PacketTypes.Login);
|
||||
outmsg.Write(Game1.version.ToString());
|
||||
outmsg.Write(Game1.Version.ToString());
|
||||
outmsg.Write(name);
|
||||
|
||||
// Connect client, to ip previously requested from user
|
||||
@@ -163,7 +148,7 @@ namespace Subsurface.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
public override void Update()
|
||||
{
|
||||
if (updateTimer > DateTime.Now) return;
|
||||
|
||||
@@ -222,8 +207,10 @@ namespace Subsurface.Networking
|
||||
case (byte)PacketTypes.StartGame:
|
||||
if (gameStarted) continue;
|
||||
|
||||
if (this.Character != null) Character.Remove();
|
||||
|
||||
int seed = inc.ReadInt32();
|
||||
Game1.random = new Random(seed);
|
||||
Rand.SetSyncedSeed(seed);
|
||||
|
||||
string mapName = inc.ReadString();
|
||||
string mapHash = inc.ReadString();
|
||||
@@ -241,7 +228,7 @@ namespace Subsurface.Networking
|
||||
|
||||
//int gameModeIndex = inc.ReadInt32();
|
||||
Game1.GameSession = new GameSession(Submarine.Loaded);
|
||||
Game1.GameSession.StartShift(duration, 1);
|
||||
Game1.GameSession.StartShift(duration, "asdf");
|
||||
|
||||
myCharacter = ReadCharacterData(inc);
|
||||
Character.Controlled = myCharacter;
|
||||
@@ -270,14 +257,6 @@ namespace Subsurface.Networking
|
||||
|
||||
Game1.NetLobbyScreen.AddPlayer(otherClient.name);
|
||||
|
||||
//string newPlayerName = inc.ReadString();
|
||||
//int newPlayerID = inc.ReadInt32();
|
||||
|
||||
//CharacterInfo ch = new CharacterInfo("Content/Characters/Human/human.xml", newPlayerName);
|
||||
//ch.ID = newPlayerID;
|
||||
|
||||
//Character.newCharacterQueue.Enqueue(ch);
|
||||
|
||||
AddChatMessage(otherClient.name + " has joined the server", ChatMessageType.Server);
|
||||
|
||||
break;
|
||||
@@ -289,7 +268,7 @@ namespace Subsurface.Networking
|
||||
break;
|
||||
|
||||
case (byte)PacketTypes.KickedOut:
|
||||
string msg= inc.ReadString();
|
||||
string msg = inc.ReadString();
|
||||
|
||||
DebugConsole.ThrowError(msg);
|
||||
|
||||
@@ -336,7 +315,7 @@ namespace Subsurface.Networking
|
||||
gameStarted = false;
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
public override void Disconnect()
|
||||
{
|
||||
NetOutgoingMessage msg = Client.CreateMessage();
|
||||
msg.Write((byte)PacketTypes.PlayerLeft);
|
||||
@@ -350,8 +329,9 @@ namespace Subsurface.Networking
|
||||
|
||||
NetOutgoingMessage msg = Client.CreateMessage();
|
||||
msg.Write((byte)PacketTypes.CharacterInfo);
|
||||
msg.Write(characterInfo.name);
|
||||
msg.Write(characterInfo.gender == Gender.Male);
|
||||
msg.Write(characterInfo.Name);
|
||||
msg.Write(characterInfo.Gender == Gender.Male);
|
||||
msg.Write(characterInfo.HeadSpriteId);
|
||||
|
||||
Client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
|
||||
|
||||
@@ -359,11 +339,11 @@ namespace Subsurface.Networking
|
||||
|
||||
private Character ReadCharacterData(NetIncomingMessage inc)
|
||||
{
|
||||
string newName = inc.ReadString();
|
||||
int ID = inc.ReadInt32();
|
||||
bool isFemale = inc.ReadBoolean();
|
||||
int inventoryID = inc.ReadInt32();
|
||||
Vector2 position = new Vector2(inc.ReadFloat(), inc.ReadFloat());
|
||||
string newName = inc.ReadString();
|
||||
int ID = inc.ReadInt32();
|
||||
bool isFemale = inc.ReadBoolean();
|
||||
int inventoryID = inc.ReadInt32();
|
||||
Vector2 position = new Vector2(inc.ReadFloat(), inc.ReadFloat());
|
||||
|
||||
CharacterInfo ch = new CharacterInfo("Content/Characters/Human/human.xml", newName, isFemale ? Gender.Female : Gender.Male);
|
||||
Character character = new Character(ch, position);
|
||||
@@ -373,11 +353,11 @@ namespace Subsurface.Networking
|
||||
return character;
|
||||
}
|
||||
|
||||
public void SendChatMessage(string message)
|
||||
public override void SendChatMessage(string message, ChatMessageType type = ChatMessageType.Default)
|
||||
{
|
||||
//AddChatMessage(message);
|
||||
|
||||
ChatMessageType type = (gameStarted && myCharacter != null && myCharacter.IsDead) ? ChatMessageType.Dead : ChatMessageType.Default;
|
||||
type = (gameStarted && myCharacter != null && myCharacter.IsDead) ? ChatMessageType.Dead : ChatMessageType.Default;
|
||||
|
||||
NetOutgoingMessage msg = Client.CreateMessage();
|
||||
msg.Write((byte)PacketTypes.Chatmessage);
|
||||
|
||||
@@ -9,22 +9,20 @@ namespace Subsurface.Networking
|
||||
{
|
||||
class GameServer : NetworkMember
|
||||
{
|
||||
// Server object
|
||||
NetServer Server;
|
||||
// Configuration object
|
||||
NetPeerConfiguration Config;
|
||||
|
||||
public List<Client> connectedClients = new List<Client>();
|
||||
|
||||
//NetIncomingMessage inc;
|
||||
|
||||
const int sparseUpdateInterval = 150;
|
||||
const int SparseUpdateInterval = 150;
|
||||
int sparseUpdateTimer;
|
||||
|
||||
Client myClient;
|
||||
|
||||
public GameServer()
|
||||
{
|
||||
name = "Server";
|
||||
|
||||
Config = new NetPeerConfiguration("subsurface");
|
||||
|
||||
Config.Port = 14242;
|
||||
@@ -54,7 +52,7 @@ namespace Subsurface.Networking
|
||||
|
||||
}
|
||||
|
||||
public void Update()
|
||||
public override void Update()
|
||||
{
|
||||
// Server.ReadMessage() Returns new messages, that have not yet been read.
|
||||
// If "inc" is null -> ReadMessage returned null -> Its null, so dont do this :)
|
||||
@@ -109,10 +107,10 @@ namespace Subsurface.Networking
|
||||
|
||||
if (sender == null) break;
|
||||
|
||||
if (sender.version != Game1.version.ToString())
|
||||
if (sender.version != Game1.Version.ToString())
|
||||
{
|
||||
DisconnectClient(sender, sender.name+" was unable to connect to the server (nonmatching game version)",
|
||||
"Subsurface version " + Game1.version + " required to connect to the server (Your version: " + sender.version + ")");
|
||||
"Subsurface version " + Game1.Version + " required to connect to the server (Your version: " + sender.version + ")");
|
||||
|
||||
}
|
||||
else
|
||||
@@ -208,7 +206,7 @@ namespace Subsurface.Networking
|
||||
|
||||
private void SparseUpdate()
|
||||
{
|
||||
foreach (Character c in Character.characterList)
|
||||
foreach (Character c in Character.CharacterList)
|
||||
{
|
||||
bool isClient = false;
|
||||
foreach (Client client in connectedClients)
|
||||
@@ -220,12 +218,12 @@ namespace Subsurface.Networking
|
||||
|
||||
if (!isClient)
|
||||
{
|
||||
c.largeUpdateTimer = 0;
|
||||
c.LargeUpdateTimer = 0;
|
||||
new NetworkEvent(c.ID, false);
|
||||
}
|
||||
}
|
||||
|
||||
sparseUpdateTimer = sparseUpdateInterval;
|
||||
sparseUpdateTimer = SparseUpdateInterval;
|
||||
}
|
||||
|
||||
private void SendMessage(NetOutgoingMessage msg, NetDeliveryMethod deliveryMethod, NetConnection excludedConnection)
|
||||
@@ -268,14 +266,14 @@ namespace Subsurface.Networking
|
||||
public bool StartGame(GUIButton button, object obj)
|
||||
{
|
||||
int seed = DateTime.Now.Millisecond;
|
||||
Game1.random = new Random(seed);
|
||||
Rand.SetSyncedSeed(seed);
|
||||
|
||||
Submarine selectedMap = Game1.NetLobbyScreen.SelectedMap as Submarine;
|
||||
|
||||
//selectedMap.Load();
|
||||
|
||||
Game1.GameSession = new GameSession(selectedMap, Game1.NetLobbyScreen.SelectedMode);
|
||||
Game1.GameSession.StartShift(Game1.NetLobbyScreen.GameDuration, 1);
|
||||
Game1.GameSession.StartShift(Game1.NetLobbyScreen.GameDuration, "asdf", 1);
|
||||
//EventManager.SelectEvent(Game1.netLobbyScreen.SelectedEvent);
|
||||
|
||||
foreach (Client client in connectedClients)
|
||||
@@ -446,17 +444,17 @@ namespace Subsurface.Networking
|
||||
|
||||
|
||||
|
||||
public void SendChatMessage(string message, ChatMessageType type = ChatMessageType.Server)
|
||||
public override void SendChatMessage(string message, ChatMessageType type = ChatMessageType.Server)
|
||||
{
|
||||
AddChatMessage(message, type);
|
||||
|
||||
if (Server.Connections.Count == 0) return;
|
||||
|
||||
NetOutgoingMessage msg = Server.CreateMessage();
|
||||
msg.Write((byte)PacketTypes.Chatmessage);
|
||||
msg.Write((byte)type);
|
||||
msg.Write(message);
|
||||
|
||||
if (Server.Connections.Count == 0) return;
|
||||
|
||||
if (type==ChatMessageType.Dead)
|
||||
{
|
||||
List<NetConnection> recipients = new List<NetConnection>();
|
||||
@@ -478,13 +476,15 @@ namespace Subsurface.Networking
|
||||
|
||||
private void ReadCharacterData(NetIncomingMessage message)
|
||||
{
|
||||
string name = message.ReadString();
|
||||
Gender gender = message.ReadBoolean() ? Gender.Male : Gender.Female;
|
||||
string name = message.ReadString();
|
||||
Gender gender = message.ReadBoolean() ? Gender.Male : Gender.Female;
|
||||
int headSpriteId = message.ReadInt32();
|
||||
|
||||
foreach (Client c in connectedClients)
|
||||
{
|
||||
if (c.Connection != message.SenderConnection) continue;
|
||||
c.characterInfo = new CharacterInfo("Content/Characters/Human/human.xml", name, gender);
|
||||
c.characterInfo.HeadSpriteId = headSpriteId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +492,7 @@ namespace Subsurface.Networking
|
||||
{
|
||||
message.Write(name);
|
||||
message.Write(character.ID);
|
||||
message.Write(character.info.gender==Gender.Female);
|
||||
message.Write(character.Info.Gender == Gender.Female);
|
||||
message.Write(character.Inventory.ID);
|
||||
message.Write(character.SimPosition.X);
|
||||
message.Write(character.SimPosition.Y);
|
||||
|
||||
@@ -30,10 +30,22 @@ namespace Subsurface.Networking
|
||||
{
|
||||
protected static Color[] messageColor = { Color.Black, Color.DarkRed, Color.DarkBlue, Color.DarkGreen };
|
||||
|
||||
protected string name;
|
||||
|
||||
protected TimeSpan updateInterval;
|
||||
protected DateTime updateTimer;
|
||||
|
||||
protected bool gameStarted;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrEmpty(name)) return;
|
||||
name = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddChatMessage(string message, ChatMessageType messageType)
|
||||
{
|
||||
@@ -42,6 +54,12 @@ namespace Subsurface.Networking
|
||||
|
||||
GUI.PlayMessageSound();
|
||||
}
|
||||
|
||||
public virtual void SendChatMessage(string message, ChatMessageType type = ChatMessageType.Server) { }
|
||||
|
||||
public virtual void Update() { }
|
||||
|
||||
public virtual void Disconnect() { }
|
||||
}
|
||||
|
||||
enum ChatMessageType
|
||||
|
||||
Reference in New Issue
Block a user