private Data PlayerAction(Data msgReceived)
{
//Required variables
Player playermoved = null;
double accelerate = 0;
int rot = 0;
//Figure out who is making the action.
foreach (Player playerp in playerList)
{
if (playerp.id == msgReceived.id)
{
playermoved = playerp;
}
}
//Setup our new message to send.
Data msgToSend = new Data();
//Rotate right
if (msgReceived.strMessage == "right")
{
rot++;
rot = rot * MatchConfig.rotationRate;
playermoved.angle += rot;
}
//Rotate left
if (msgReceived.strMessage == "left")
{
rot++;
rot = rot * MatchConfig.rotationRate;
playermoved.angle -= rot;
}
//Increase speed
if (msgReceived.strMessage == "up")
{
// this is to highlight that the Math.Sin and Math.Cos use the radian
// for its "angle" It also demenstrates that we need to store our
// locations using floats as it will allow for nice turning rather
// than fixed 8-directional (N, NE, E, SE, S, SW, W, NW) movement
accelerate++;
accelerate = accelerate * MatchConfig.accelerationRate;
float rad = (float)(Math.PI / 180) * playermoved.angle;
playermoved.speedX += (float)((float)Math.Sin(rad) * accelerate);
playermoved.speedY += -1 * (float)((float)Math.Cos(rad) * accelerate);
//Note we don't actually increase the x or y here, just the speed.
}
//Decrease speed
if (msgReceived.strMessage == "down")
{
accelerate++;
accelerate = accelerate * MatchConfig.brakeRate;
float rad = (float)(Math.PI / 180) * playermoved.angle;
playermoved.speedX -= (float)((float)Math.Sin(rad) * accelerate);
playermoved.speedY -= -1 * (float)((float)Math.Cos(rad) * accelerate);
//Note we don't actually decrease the x or y here, just the speed.
}
//Shoot command
if (msgReceived.strMessage == "shoot")
{
//Check that the player has less than 10 bullets onscreen.
if (playermoved.bcount < 10)
{
playermoved.bcount++;
Bullet bullet = new Bullet();
bullet.speedX= playermoved.speedX;
//Setup bullet's speeds
if (playermoved.speedX >= 0)
{
bullet.speedX= (float)(playermoved.speedX + Math.Sin((Math.PI / 180) * playermoved.angle) * MatchConfig.bulletSpeed);
}
else
{
bullet.speedX= (float)(playermoved.speedX + Math.Sin((Math.PI / 180) * playermoved.angle) * MatchConfig.bulletSpeed);
}
if (playermoved.speedY >= 0)
{
bullet.speedY= ((float)(playermoved.speedY + (-1 * (Math.Cos((Math.PI / 180) * playermoved.angle))) * MatchConfig.bulletSpeed));
}
else
{
bullet.speedY= ((float)(playermoved.speedY + (-1 * (Math.Cos((Math.PI / 180) * playermoved.angle))) * MatchConfig.bulletSpeed));
}
//Setup bullet location
bullet.x = (float)(playermoved.x + Math.Sin((Math.PI / 180) * playermoved.angle) * (playermoved.radius + 10));
bullet.y = (float)(playermoved.y + (-1 * (Math.Cos((Math.PI / 180) * playermoved.angle))) * (playermoved.radius + 10));
//Add who shot the bullet
bullet.whoshot = playermoved.id;
//Increase the overall bullet count (used for ids)
bulletcount++;
bullet.id = bulletcount;
//Add the bullet to the list
bulletList.Add(bullet);
}
}//End shoot command
//Return the player action
return msgToSend;
}