/// <summary> /// Commands from client are stored in each instance of the client class, unique to each player. /// On every frame, if a client command is set to true, process the command and reset flag. /// </summary> public static void ProcessCommands() { double shipTurnRate = (double)gameSettings["ShipTurnRate"]; // Process the current flag in the correct way for every client. foreach (KeyValuePair <int, Client> c in ClientConnections) { Client client = c.Value; Ship ship = TheWorld.GetShipAtId(client.ID); if (client.left == true) { Vector2D tempDir = new Vector2D(ship.dir); tempDir.Rotate(-shipTurnRate); // ship turn rate is specified as a game setting. ship.SetDir(tempDir); client.left = false; } if (client.right == true) { Vector2D tempDir = new Vector2D(ship.dir); tempDir.Rotate(shipTurnRate); ship.SetDir(tempDir); client.right = false; } if (client.thrust == false) { ship.SetThrust(false); } if (client.thrust == true) { ship.SetThrust(true); client.thrust = false; } // if we have waited long enough after previous fire, fire projectile if (client.fire == true && ship.fireRateCounter == (int)gameSettings["FramesPerShot"]) { // fire projectile Vector2D projVel = new Vector2D(ship.dir * (double)gameSettings["ProjectileSpeed"]); Vector2D startPos = new Vector2D(ship.loc + (ship.dir * 20)); InsertProjectile(startPos, ship.dir, projVel, ship); client.fire = false; //Reset fireRateCounter after a ship fires ship.SetFireRateCounter(-1); } // if we haven't waited long enough after previous fire else if (client.fire == true && ship.fireRateCounter < (int)gameSettings["FramesPerShot"]) { // increment the counter. ship.SetFireRateCounter(ship.fireRateCounter + 1); } } }
/// <summary> /// Randomize spawn location for ship /// </summary> /// <param name="ship"></param> public static void SpawnShip(Ship ship) { Random rand = new Random(); int LocX; int LocY; bool safeSpawn = false; Vector2D position = new Vector2D(0.0, 0.0); //loop through potential random spawn locations to find location that is //not near a star or a ship. while (!safeSpawn) { int worldSize = (int)gameSettings["UniverseSize"] - 1; // finding possible random variables for the spawn location LocX = rand.Next(-(worldSize / 2), worldSize / 2); LocY = rand.Next(-(worldSize / 2), worldSize / 2); position = new Vector2D(LocX, LocY); // flags to determine if ship should spawn or not. bool starSpawn = true; // set for spawning on top of stars bool shipSpawn = true; // set for spawning on top of other ships //checks to see if potential spawn location is too close to a star foreach (Star star in TheWorld.GetStars()) { if ((star.loc - position).Length() <= 50) { starSpawn = false; } } //checks to see if potential spawn location is too close to a ship foreach (Ship shipLoop in TheWorld.GetShipsAll()) { if ((shipLoop.loc - position).Length() <= 50) { shipSpawn = false; } } //If neither star or ship is hindering spawn, break out of loop if (starSpawn == true && shipSpawn == true) { safeSpawn = true; } } ship.SetLoc(position); ship.SetHp((int)gameSettings["StartingHP"]); ship.SetThrust(false); Vector2D vel = new Vector2D(0, 0); ship.SetVelocity(vel); ship.SetDeathCounter(0); Vector2D Dir = new Vector2D(0, 1); ship.SetDir(Dir); ship.SetFireRateCounter((int)(gameSettings["FramesPerShot"])); }