Beispiel #1
0
        /// <summary>
        /// Finds a random location for the ship that is not near a star
        /// </summary>
        private void FindRandomLocation(Ship ship)
        {
            // make a randomizing object
            Random r = new Random();

            // find a random location
            int x = r.Next(-(universeSize / 2), universeSize / 2);
            int y = r.Next(-(universeSize / 2), universeSize / 2);

            // make a new location vector
            Vector2D location = new Vector2D(x, y);

            // check to make sure that no stars are near
            foreach (Star star in stars.Values)
            {
                while (WithinARadius(location, star.GetLocation(), starSize + 50))
                {
                    // find a random location
                    x = r.Next(-(universeSize / 2), universeSize / 2);
                    y = r.Next(-(universeSize / 2), universeSize / 2);

                    // make a new location vector
                    location = new Vector2D(x, y);
                }
            }

            // sets the random location to the ship
            ship.SetLocation(location);
        }
Beispiel #2
0
        /// <summary>
        /// Checks to see if the passed in ship is on the edge of the world
        /// </summary>
        private void Wraparound(Ship ship)
        {
            int borderCoordinate = universeSize / 2;

            if (Math.Abs(ship.GetLocation().GetX()) >= borderCoordinate)
            {
                // times x by -1
                double x = ship.GetLocation().GetX() * -1;
                double y = ship.GetLocation().GetY();

                // set the ship's new location
                ship.SetLocation(x, y);
            }
            if (Math.Abs(ship.GetLocation().GetY()) >= borderCoordinate)
            {
                // times y by -1
                double y = ship.GetLocation().GetY() * -1;
                double x = ship.GetLocation().GetX();

                // set the ship's new location
                ship.SetLocation(x, y);
            }
        }
        /// <summary>
        /// This is a "callMe" delegate that implements the server's part of the initial handshake.
        /// </summary>
        /// <param name="state"></param>
        private void ReceiveName(SocketState state)
        {
            if (Regex.IsMatch(state.sBuilder.ToString(), "\\Z\\n"))
            {
                string playerName  = state.sBuilder.ToString(0, state.sBuilder.Length - 1);
                int    newUniqueID = ++lastUniqueID;

                Ship newShip = new Ship(newUniqueID, playerName, myWorld.GetStartingHP());
                lock (myWorld)
                {
                    newShip.SetLocation(myWorld.MakeSpawnPoint());
                    myWorld.UpdateShip(newShip, newUniqueID);
                    shipCommands.Add(newUniqueID, "");
                }

                state.ID = newUniqueID;

                state.callMe = HandleCommandRequests;

                // Send the startup info to the client (ID and world size).
                Networking.Send(state.theSocket, "" + newUniqueID + "\n" + myWorld.GetSize() + "\n");

                // Then add the new client's socket to a list of all clients.
                lock (clients)
                {
                    clients.Add(newUniqueID, state);
                }

                state.sBuilder.Clear();

                // Ask the client for data to continue the loop
                Networking.GetData(state);

                Console.WriteLine("Added Client " + newUniqueID + ". YAY!");
            }
            else
            {
                Console.WriteLine("Failed to add Client. . . :(");
            }
        }
Beispiel #4
0
        /// <summary>
        /// Computes the acceleration, velocity, and position of the passed in ship
        /// </summary>
        public void MotionForShips(Ship ship)
        {
            // if the ship isn't alive, just skip it
            if (!ship.IsAlive())
            {
                return;
            }

            // handle left turn command
            if (ship.TurnLeft)
            {
                ship.GetDirection().Rotate(-turningRate);
                ship.TurnLeft = false;
            }
            // handle right turn command
            else if (ship.TurnRight)
            {
                ship.GetDirection().Rotate(turningRate);
                ship.TurnRight = false;
            }

            // spawn a projectile if projectile command has been given
            if (ship.FireProjectile)
            {
                Projectile p = new Projectile(ship.GetID(), projectileID++, ship.GetLocation(), ship.GetDirection());
                AddProjectile(p);
                ship.FireProjectile = false;
                ship.ResetFireTimer();

                // we just fired a projectile, increment total number of fired projectiles
                ship.ShotsFired++;
            }

            //get a zero vector
            Vector2D acceleration = new Vector2D(0.0, 0.0);

            //compute the acceleration caused by the star
            foreach (Star star in stars.Values)
            {
                Vector2D g = star.GetLocation() - ship.GetLocation();
                g.Normalize();
                acceleration = acceleration + g * star.GetMass();
            }

            if (ship.HasThrust())
            {
                //compute the acceleration due to thrust
                Vector2D t = new Vector2D(ship.GetDirection());
                t            = t * engineStrength;
                acceleration = acceleration + t;
            }

            // recalculate velocity and location
            ship.SetVelocity(ship.GetVelocity() + acceleration);
            ship.SetLocation(ship.GetVelocity() + ship.GetLocation());
            // check to see if ship is off screen
            Wraparound(ship);

            // check for collisions with any star
            foreach (Star star in stars.Values)
            {
                CollisionWithAStar(ship, star);
            }

            // check for collisions with any projectiles
            foreach (Projectile proj in projectiles.Values)
            {
                CollisionWithAProjectile(ship, proj);
            }

            ship.IncrementFireTimer();
        }