public void RunGameLoop()
{
Console.WriteLine(" Starting RunGameLoop Thread.");
//isRunning = true;
if(playintro == false)
introlength = new TimeSpan(0, 0, 7);
else
introlength = new TimeSpan(0, 0, 21);
beatrate = new TimeSpan(0, 0, 1);
introtime = DateTime.Now;
lastBeat = DateTime.Now;
NetOutgoingMessage outmsg;
crab = new CrabBehavior();
//while(isRunning)
int sendRate = 1000 / ticksPerSecond; // 1 sec = 1000ms as Sleep uses ms.
for ( isRunning=true; isRunning; Thread.Sleep(sendRate) )
{
if (gamePhase != (int)GameState.Lobby && players.Count == 0)
{
Console.WriteLine("All players disconnected, returning to lobby gamestate.");
gamePhase = (int)GameState.Lobby;
}
if (gamePhase == (int)GameState.Intro && ((introtime + introlength) < DateTime.Now))
{
//Intro has ran for it's length, lets start the game proper.
Console.WriteLine("PlayIntro was set to '"+playintro+"' sending StartGame packet to clients...");
//outmsg = server.CreateMessage();
//outmsg.Write((byte)PacketTypes.StartGame);
//server.SendMessage(outmsg, server.Connections, NetDeliveryMethod.ReliableOrdered, 1);
crab.Direction = false;
gamePhase = (int)GameState.InGame;
}
if(gamePhase == (int)GameState.InGame && crab.CurrentHealth>0)
{
crab.GoGoBattleCrab();
}
// Handle dropping players and long idle connections. Note Client must send a KeepAlive packet within 15s
if (NetTime.Now > last15sec+15)
{
foreach (PlayerObject player in players)
{
//Console.WriteLine("KeepAlive Check "+player.Name+" (Id"+player.Id+") "+ player.Connection.Status+" RTT "+player.Connection.AverageRoundtripTime +" keepAlive "+player.keepAlive+" vs. lastKeepAlive "+player.lastKeepAlive);
if (player.keepAlive > player.lastKeepAlive)
player.lastKeepAlive = NetTime.Now;
else if (player.keepAlive != player.lastKeepAlive)
{
SendLobbyMessage("Server","You may need the latest github update v1.1 for multiplayer support.",player.Connection);
SendConsoleMessage("You may need the lastest github update v1.1 for multiplayer support.",player.Connection);
SendMessageDebug("You may need the lastest github update v1.1 for multiplayer support.",player.Connection);
player.Connection.Deny(player.Name + " (Id"+player.Id+") idle connection's keepAlive "+player.keepAlive+" is < lastKeepAlive "+player.lastKeepAlive);
}
else if (player.keepAlive == player.lastKeepAlive)
player.lastKeepAlive = NetTime.Now;
}
last15sec = NetTime.Now;
}
//Send update beats.
if ((lastBeat + beatrate) < DateTime.Now && gamePhase >=1)
{
//Send a beat to all users. Because the server doesn't really know if they disconnected unless we are sending packets to them.
//Beats go out every 2 seconds.
foreach (PlayerObject p in players)
{
outmsg = server.CreateMessage();
outmsg.Write((byte)PacketTypes.Beat);
outmsg.Write((Int16)beatnum);
outmsg.Write(p.Connection.AverageRoundtripTime/2f);
server.SendMessage(outmsg, p.Connection, NetDeliveryMethod.ReliableOrdered, 4);
}
beatnum++;
lastBeat = DateTime.Now;
}
}
Console.WriteLine(" Stopped RunGameLoop Thread.");
}