/// <summary> /// This function changes the speed of the object based on its proximity to its Planet instance /// </summary> /// <param name="b">An instance to a Bullet</param> public void applyGravity(Bullet b) { //Gravity Calculations between bullets and the planet(object that is producing gravity) float G = MatchConfig.gravityConstant; double distanceG = Math.Sqrt((Math.Pow(b.x - this.x, 2) + Math.Pow(b.y - this.y, 2))); double speedVector = (float)((G * this.mass) / Math.Pow(distanceG, 2)); double deltaY = (b.y - this.y); double deltaX = (b.x - this.x); double angleDegree = Math.Atan(deltaY / deltaX);// * 180 / Math.PI; //float rad = (float)(Math.PI / 180) * o.angle; if (b.x != this.x) { //speedX += (float)(Math.Cos(angleDegree) * speedVector); if (b.x > this.x) b.speedX -= (float)Math.Abs((Math.Cos(angleDegree) * speedVector)); else b.speedX+= (float)Math.Abs((Math.Cos(angleDegree) * speedVector)); } if (b.y != this.y) { //speedY += (float)(Math.Sin(angleDegree) * speedVector); if (b.y < this.y) b.speedY+= (float)Math.Abs((Math.Sin(angleDegree) * speedVector)); else b.speedY-= (float)Math.Abs((Math.Sin(angleDegree) * speedVector)); } }
/// <summary> /// Setup a player action, by going through the received data and figuring out which action /// a player is performing (move, shoot, etc.) /// </summary> /// <param name="msgReceived">Received data</param> /// <returns>Data object of player action</returns> 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; }