コード例 #1
0
ファイル: myServe.cs プロジェクト: coreyschulz/space-wars
        /// <summary>
        /// Class to build a ship and set it to a random position.
        /// </summary>
        /// <param name="ss"></param>
        public void buildship(SocketState ss)
        {
            //random starting location
            //we want to strip the '/n' off of the end
            String _name = ss.bs.ToString().Replace("\n", "");

            //making the name lower case so that it won't ever cause a fire or anything
            _name = _name.ToLower();
            Random   rnd = new Random();
            int      startingXPosition = rnd.Next(-universeSize / 2, universeSize / 2);
            int      startingYPosition = rnd.Next(-universeSize / 2, universeSize / 2);
            Vector2D loc = new Vector2D(startingXPosition, startingYPosition);

            lock (theWorld) // Adds ship to the world and increments the count so no ships have the same ID.
            {
                theWorld.AddShip(ss, new ship(countOfClients, ss, hitPoints, loc, _name, 0, new Vector2D(), projectileFiringDelay));
                countOfClients++;
            }
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: mgrayston/School-Work-
        /// <summary>
        /// Callback for receiving client name.
        /// </summary>
        /// <param name="ss">The ss.</param>
        private static void ReceiveName(SocketState ss)
        {
            // Extract name from received data
            string name = ss.Builder.ToString();

            name = name.Remove(name.Length - 1);
            Console.WriteLine("Received name: " + name);

            Network.Send(ss.Socket, ss.ID + "\n" + world.WorldSize + "\n");
            clients.Add(ss);
            Ship newShip = new Ship(ss.ID, name, random.Next(universeSize + 1) - (universeSize / 2), random.Next(universeSize + 1) - (universeSize / 2));

            // Don't start ship in star's location
            while (Math.Abs(newShip.Loc.GetX()) < starSize && Math.Abs(newShip.Loc.GetX()) < starSize)
            {
                newShip.Loc = new Vector2D(random.Next(universeSize + 1) - (universeSize / 2), random.Next(universeSize + 1) - (universeSize / 2));
            }
            world.AddShip(newShip);

            ss.CallMe = ReceiveMoveRequest;
            Network.GetData(ss);
        }