public void Update()
{
//Required variables
byte[] message;
Player playerbullet = null;
List<Bullet> toRemove = new List<Bullet>();
Data msgToSend = new Data();
//Loop forever
while (true)
{
//Wait 40 ms
Thread.Sleep(40);
//Update planets
foreach (Planets planet in planetList)
{
planet.x = 400;
planet.y = 300;
}
//Update players
foreach (Player p in playerList)
{
//Adjust placement by speed
p.x += p.speedX;
p.y += p.speedY;
//Collision Detection between Player and Border. For now, Players will bounce off the border in opposite direction.
if (p.x < 0)
{
p.speedX = p.speedX * (float)(-0.1);
p.x = 0;
}
else if (p.x > MatchConfig.mapWidth)
{
p.speedX = p.speedX * (float)(-0.1);
p.x = MatchConfig.mapWidth;
}
if (p.y < 0)
{
p.speedY = p.speedY * (float)(-0.1);
p.y = 0;
}
else if (p.y > MatchConfig.mapHeight)
{
p.speedY = p.speedY * (float)(-0.1);
p.y = MatchConfig.mapHeight;
}
//Collision Detection between Players. For now, Players will bounce off of each other. No damage to players.
foreach (Player p2 in playerList)
{
if (p.checkCollision(p2))
{
p.treatCollision(p2);
}
}
//Gravity and Collision Detection between Planet and players
foreach (Planets planet in planetList)
{
planet.applyGravity(p);
//Collision Detection between player and planets. For now, Players will bounce off of the planet. No damage to each other.
if (p.checkCollision(planet))
{
p.treatCollision(planet);
}
}
}//end of player loop
//Update bullets
foreach (Bullet b in bulletList)
{
//Check who shot each bullet, (required for the max bullet count per player)
foreach (Player playerp in playerList)
{
if (playerp.id == b.whoshot)
{
playerbullet = playerp;
}
}
//Bullet Movement
b.x += b.speedX;
b.y += b.speedY;
//Collision Detection between bullets and border. At this time, bullets will disappear after they reach the border.
if (b.x < 0)
{
playerbullet.bcount--;
toRemove.Add(b);
}
else if (b.x > MatchConfig.mapWidth)
{
playerbullet.bcount--;
toRemove.Add(b);
}
if (b.y < 0)
{
playerbullet.bcount--;
toRemove.Add(b);
}
else if (b.y > MatchConfig.mapHeight)
{
playerbullet.bcount--;
toRemove.Add(b);
}
//Collision Detection between players and bullets. For now, the bullets will disappear when they collide with players. Players will remain.
foreach (Player p in playerList)
{
if (b.checkCollision(p))
{
//remove bullet
playerbullet.bcount--;
toRemove.Add(b);
//remove player
//TODO: Add code/lists to delete players when collide with bullets
}
}
//Collision Detection and Gravity between planet and bullets.
foreach (Planets planet in planetList)
{
//Collision Detection between planet and bullets. For now, the bullets will disappear when they collide with the planet.
if (b.checkCollision(planet))
{
playerbullet.bcount--;
toRemove.Add(b);
}
planet.applyGravity(b);
}
}//End bullet loop
//Remove bullets
foreach (Bullet r in toRemove)
{
//Add to the list of bullet ids to remove
removeids.Add(r.id);
bulletList.Remove(r);
}
//Create the message to send to the clients
message = msgToSend.ToByte(playerList, bulletList, removeids, playercount, planetList);
//Loop through all clients
foreach (ClientInfo clientInfo in clientList)
{
//Send the message to all users
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, clientInfo.endpoint, new AsyncCallback(OnSend), clientInfo.endpoint);
}
}//End while loop
}