예제 #1
0
      private void GameLoop()
      {
          bool       isNotPaused = true;
          ConsoleKey pressedKey;

          // Go to next day if user input != stop and there is space left in the terrarium
          do
          {
              do
              {
                  // As long as there is no input keep looping
                  while (!Console.KeyAvailable)
                  {
                      NextDay();
                      TimeController.Step();
                  }
                  pressedKey = Console.ReadKey(true).Key;
              } while (pressedKey != ConsoleKey.V && pressedKey != ConsoleKey.E && pressedKey != ConsoleKey.Escape && isNotPaused);

              if (pressedKey == ConsoleKey.V)
              {
                  SpawnVulcano();
              }
              if (pressedKey == ConsoleKey.E)
              {
                  SpawnEarthquake();
              }
              if (pressedKey == ConsoleKey.Escape)
              {
                  PauseGame(isNotPaused);
              }
          } while (Terrarium.IsEmptySpaceInTerrarium() && isNotPaused);
      }
예제 #2
0
      private void OrganismActions()
      {
          // List to save organisms to delete later (cannot modify list while looping through)
          List <IOrganism> organismsToDelete = new List <IOrganism>();
          List <IOrganism> organismsToAdd    = new List <IOrganism>();

          // Go through the list of all organisms
          foreach (IOrganism organism in Terrarium.Organisms)
          {
              // Only perform organisms action if it is not going to be deleted
              if (!organismsToDelete.Contains(organism))
              {
                  if (organism is Herbivore)
                  {
                      Herbivore herbivore     = organism as Herbivore;
                      IOrganism organismRight = herbivore.CheckRight();
                      if (organismRight == null)
                      {
                          herbivore.Move();
                      }
                      else if (organismRight is Plant)
                      {
                          herbivore.Eat(organismRight, organismsToDelete);
                          //Console.WriteLine("Herbivore ate Plant");
                      }
                      else if (organismRight is Herbivore)
                      {
                          herbivore.Breed(organismsToAdd);
                          // Console.WriteLine("Hebrivore breeds with Herbivore");
                      }
                      // After action re-render terrarium
                      Terrarium.RenderAnimals();
                      // Wait before rendering next step
                      TimeController.Step();
                  }
                  else if (organism is Carnivore)
                  {
                      Carnivore carnivore     = organism as Carnivore;
                      IOrganism organismRight = carnivore.CheckRight();
                      if (organismRight == null || organismRight is Plant)
                      {
                          carnivore.Move();
                      }
                      else if (organismRight is Herbivore)
                      {
                          carnivore.Eat(organismRight, organismsToDelete);
                          //Console.WriteLine("Carnivore ate Herbivore");
                      }
                      else if (organismRight is Carnivore)
                      {
                          carnivore.Fight(organismRight, organismsToDelete);
                      }
                      else if (organismRight is Human)
                      {
                          carnivore.Fight(organismRight, organismsToDelete);
                      }
                      // After action re-render terrarium
                      Terrarium.RenderAnimals();
                      // Wait before rendering next step
                      TimeController.Step();
                  }
                  else if (organism is Human)
                  {
                      Human     human         = organism as Human;
                      IOrganism organismRight = human.CheckRight();
                      if (organismRight == null || organismRight is Plant || organismRight is Herbivore)
                      {
                          human.Move();
                      }
                      else if (organismRight is Carnivore)
                      {
                          human.Fight(organismRight, organismsToDelete);
                      }
                      // After action re-render terrarium
                      Terrarium.RenderAnimals();
                      // Wait before rendering next step
                      TimeController.Step();
                  }
              }
          }
          // If there are organisms to delete
          if (organismsToDelete.Count > 0)
          {
              // Remove all killed organisms from list
              foreach (IOrganism organism in organismsToDelete)
              {
                  Terrarium.Organisms.Remove(organism);
              }
          }
          // If there are organisms to add
          if (organismsToAdd.Count > 0)
          {
              // Add organisms to terrarium
              foreach (IOrganism organism in organismsToAdd)
              {
                  if (Terrarium.IsEmptySpaceInTerrarium())
                  {
                      Terrarium.Organisms.Add(organism);
                  }
              }
          }
      }