示例#1
0
      public static Position GenerateRandomEmptyPosition(Terrarium terrarium)
      {
          // To check if position is empty
          bool empty;
          // Position container
          Position pos;

          do
          {
              // Reset empty
              empty = true;
              // Generate random position
              pos = new Position(random.Next(0, terrarium.Width), random.Next(0, terrarium.Height));
              // Check if position already exists, if so set fals to empty
              foreach (IOrganism organism in terrarium.Organisms)
              {
                  if (organism.Position.X == pos.X && organism.Position.Y == pos.Y)
                  {
                      empty = false;
                  }
              }
          } while (!empty);

          // Return position
          return(pos);
      }
示例#2
0
 public WorldController(Terrarium terrarium, TimeController timecontroller, TerrariumSettings terrariumSettings, bool loaded)
 {
     TerrariumSettings = terrariumSettings;
     Terrarium         = terrarium;
     TimeController    = timecontroller;
     loadedGame        = loaded;
 }
示例#3
0
        public TimeController(int startTime, Terrarium terrarium)
        {
            EventsDict = new Dictionary <int, string>()
            {
                { -1000000, "Big Bang" },
                { -750000, "Lorem ipsum dolor sit amet" },
                { -500000, "Phasellus tortor nibh" },
                { -300000, "Laoreet at metus id" },
                { -50000, "Morbi sollicitudin hendrerit consectetur." },
                { -10000, "Suspendisse pulvinar nunc" },
                { -2000, "Cras rutrum diam quis massa feugiat" },
                { 0, "Ut maximus ligula sit amet enim posuere tristique." },
                { 1000, "Praesent a felis odior" },
                { 1300, "at pulvinar justo mollis." },
                { 1600, "Pellentesque pharetra, eros id luctus posuere" },
                { 1700, "mi felis pellentesque orci" },
                { 1850, "Vestibulum elementum lobortis elit" },
                { 1940, "Cras non laoreet leo. Integer nec" },
                { 2016, "Sed quis placerat nulla." }
            };

            Day = startTime;
            ChangeTimeStep();
            // Milliseconds per day
            StepTimeoutBase = 1000;
            // Start step time, gets changed later according to amount of animals in list
            StepTimeout = 100;
            Terrarium   = terrarium;
        }
示例#4
0
 /* Constructor */
 public Animal(Position position, Terrarium terrarium)
 {
     Position     = position;
     Terrarium    = terrarium;
     LastPosition = new Position(Position.X, Position.Y);
     Health       = random.Next(1, 4);
 }
示例#5
0
 public WorldController(TerrariumSettings terrariumSettings, ScreenController screenController)
 {
     TerrariumSettings = terrariumSettings;
     Terrarium         = new Terrarium(TerrariumSettings.Width, TerrariumSettings.Height);
     TimeController    = new TimeController(-1000000, Terrarium);
     ScreenController  = screenController;
 }
示例#6
0
 /* Constructor */
 public Carnivore(Position position, Terrarium terrarium)
     : base(position, terrarium)
 {
     DisplayColor  = GetHealthColor(Health);
     DisplayLetter = Letter;
     Health        = random.Next(1, 4);
 }
示例#7
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);
      }
示例#8
0
 private void addIOrganism(IOrganism organism)
 {
     organism.Position     = Position.GenerateRandomEmptyPosition(this.Terrarium);
     organism.LastPosition = new Position(organism.Position.X, organism.Position.Y);
     organism.Terrarium    = this.Terrarium;
     if (organism is Plant)
     {
         Plant plant = (Plant)organism;
         Terrarium.RenderPlant(plant);
     }
     this.Terrarium.Organisms.Add(organism);
 }
示例#9
0
      private void PauseGame(bool isNotPaused)
      {
          isNotPaused = false;
          isNotPaused = ScreenController.PauseGame(this);
          int width  = Terrarium.Width + 1;
          int height = Terrarium.Height + 5;

          Console.SetWindowSize(width, height);
          Console.SetBufferSize(width, height);
          Console.Clear();
          Terrarium.CreateEmptyTerrarium();
          Terrarium.RenderAnimals();
          Terrarium.RenderPlants();
      }
示例#10
0
      private void FirstDay()
      {
          // Clear the console
          Console.Clear();
          // Print the terrarium to the console using colors
          Terrarium.CreateEmptyTerrarium();

          if (!loadedGame)
          {
              // Add Organisms to List
              for (int i = 0; i < TerrariumSettings.Carnivores; i++)
              {
                  addIOrganism(new Carnivore());
              }
              for (int i = 0; i < TerrariumSettings.Herbivores; i++)
              {
                  addIOrganism(new Herbivore());
              }
              for (int i = 0; i < TerrariumSettings.Plants; i++)
              {
                  addIOrganism(new Plant());
              }
              for (int i = 0; i < TerrariumSettings.Humans; i++)
              {
                  addIOrganism(new Human());
              }
          }



          // Render the animals
          Terrarium.RenderAnimals();
          Terrarium.RenderPlants();

          // Update timestep according to n organisms (n / animals)
          // This way turns always last n ms
          TimeController.SetStepTimeout();

          // Wait for input
          Console.ForegroundColor = ConsoleColor.White;
          Console.SetCursorPosition(0, Terrarium.Height + 3);
          Console.Write("Press 'Enter' to start");


          string input = Console.ReadLine();

          Console.SetCursorPosition(0, Terrarium.Height + 3);
          Console.WriteLine("                       ");
      }
示例#11
0
      private void NextDay()
      {
          Console.CursorVisible = false;
          ClearLines();
          // Go to next day and print in console
          Console.ForegroundColor = ConsoleColor.White;
          Console.SetCursorPosition(0, Terrarium.Height + 2);
          Console.Write(TimeController);
          TimeController.ChangeTimeStep();

          // Add organisms
          addIOrganism(new Plant());

          // For every organism, perform its actions
          OrganismActions();

          // Print Terrarium to console
          Terrarium.RenderAnimals();

          // Update timestep according to n organisms (n / animals)
          // This way turns always last n ms
          TimeController.SetStepTimeout();
      }
示例#12
0
      /* Methods */
      public void Start()
      {
          int width  = Terrarium.Width + 1;
          int height = Terrarium.Height + 5;

          // Scale window size with Terrarium width and height
          Console.SetWindowSize(width, height);
          // Set buffersize to remove scroll bars from window
          Console.SetBufferSize(width, height);

          // Initial day (different from regular next day)
          FirstDay();
          TimeController.Step();

          // Game loop (user presses enter to see terrarium)
          GameLoop();

          // Close game
          Terrarium.RenderAnimals();
          Console.SetCursorPosition(0, Terrarium.Height + 4);
          Console.ForegroundColor = ConsoleColor.White;
          Console.WriteLine("Thanks for playing!");
      }
示例#13
0
 public WorldController(Terrarium terrarium, TimeController timecontroller)
 {
     Terrarium      = terrarium;
     TimeController = timecontroller;
 }
示例#14
0
 public SaveObject(Terrarium terrarium, TimeController timeController, TerrariumSettings terrariumSettings)
 {
     Terrarium         = terrarium;
     TimeController    = timeController;
     TerrariumSettings = terrariumSettings;
 }
示例#15
0
 /* Constructor */
 public WorldController(Terrarium terrarium, TimeController timecontroller, TerrariumSettings terrariumSettings)
 {
     TerrariumSettings = terrariumSettings;
     Terrarium         = terrarium;
     TimeController    = timecontroller;
 }
示例#16
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);
                  }
              }
          }
      }
示例#17
0
 public Human(Position begin, Terrarium Terrarium) : base(begin, Terrarium)
 {
     DisplayColor  = GetHealthColor(Health);
     DisplayLetter = Letter;
     Health        = random.Next(1, 4);
 }
示例#18
0
      public void Activate(Terrarium terrarium, TimeController timeController)
      {
          AudioController.PlayVulcano();

          List <Position>  vulcanoPositions  = new List <Position>();
          List <IOrganism> organismsToDelete = new List <IOrganism>();

          string vulcChar = StringManager.GetExtendedAsciiCodeAsString(176);

          Console.ForegroundColor = ConsoleColor.Yellow;
          Console.BackgroundColor = ConsoleColor.Red;

          int size = 5;

          for (int i = 0; i < size; i++)
          {
              if (Position.X + i < terrarium.Width && Position.Y + i < terrarium.Height)
              {
                  Console.SetCursorPosition(Position.X + i, Position.Y + i);
                  vulcanoPositions.Add(new Position(Position.X + i, Position.Y + i));
                  Console.Write(vulcChar);
              }
              if (Position.X - i >= 0 && Position.Y + i < terrarium.Height)
              {
                  Console.SetCursorPosition(Position.X - i, Position.Y + i);
                  vulcanoPositions.Add(new Position(Position.X - i, Position.Y + i));
                  Console.Write(vulcChar);
              }
              if (Position.X - i >= 0 && Position.Y - i >= 0)
              {
                  Console.SetCursorPosition(Position.X - i, Position.Y - i);
                  vulcanoPositions.Add(new Position(Position.X - i, Position.Y - i));
                  Console.Write(vulcChar);
              }
              if (Position.X + i < terrarium.Width && Position.Y - i >= 0)
              {
                  Console.SetCursorPosition(Position.X + i, Position.Y - i);
                  vulcanoPositions.Add(new Position(Position.X + i, Position.Y - i));
                  Console.Write(vulcChar);
              }

              for (int j = 0; j < size - 3; j++)
              {
                  if (Position.X + i < terrarium.Width && Position.Y + j < terrarium.Height)
                  {
                      Console.SetCursorPosition(Position.X + i, Position.Y + j);
                      vulcanoPositions.Add(new Position(Position.X + i, Position.Y + j));
                      Console.Write(vulcChar);
                  }
                  if (Position.X - j >= 0 && Position.Y + i < terrarium.Height)
                  {
                      Console.SetCursorPosition(Position.X - j, Position.Y + i);
                      vulcanoPositions.Add(new Position(Position.X - j, Position.Y + i));
                      Console.Write(vulcChar);
                  }
                  if (Position.X - i >= 0 && Position.Y - j >= 0)
                  {
                      Console.SetCursorPosition(Position.X - i, Position.Y - j);
                      vulcanoPositions.Add(new Position(Position.X - i, Position.Y - j));
                      Console.Write(vulcChar);
                  }
                  if (Position.X + j < terrarium.Width && Position.Y - i >= 0)
                  {
                      Console.SetCursorPosition(Position.X + j, Position.Y - i);
                      vulcanoPositions.Add(new Position(Position.X + j, Position.Y - i));
                      Console.Write(vulcChar);
                  }
              }

              timeController.Step(50);
          }

          foreach (IOrganism organism in terrarium.Organisms)
          {
              foreach (Position pos in vulcanoPositions)
              {
                  if (organism.Position.Equals(pos) && !organismsToDelete.Contains(organism))
                  {
                      organismsToDelete.Add(organism);
                  }
              }
          }


          foreach (IOrganism organism in organismsToDelete)
          {
              terrarium.Organisms.Remove(organism);
          }

          Console.ResetColor();
      }
示例#19
0
      public void Activate(Terrarium terrarium, TimeController timeController)
      {
          AudioController.PlayEarthquake();

          List <Position>  earthQuakePositions = new List <Position>();
          List <IOrganism> organismsToShuffle  = new List <IOrganism>();

          string quakeChar = StringManager.GetExtendedAsciiCodeAsString(176);

          Console.ForegroundColor = ConsoleColor.DarkYellow;
          Console.BackgroundColor = ConsoleColor.DarkRed;


          int size = 7;

          for (int i = 0; i < size; i++)
          {
              if (Position.X + i < terrarium.Width && Position.Y + i < terrarium.Height)
              {
                  Console.SetCursorPosition(Position.X + i, Position.Y + i);
                  earthQuakePositions.Add(new Position(Position.X + i, Position.Y + i));
                  Console.Write(quakeChar);
              }
              if (Position.X - i >= 0 && Position.Y + i < terrarium.Height)
              {
                  Console.SetCursorPosition(Position.X - i, Position.Y + i);
                  earthQuakePositions.Add(new Position(Position.X - i, Position.Y + i));
                  Console.Write(quakeChar);
              }
              if (Position.X - i >= 0 && Position.Y - i >= 0)
              {
                  Console.SetCursorPosition(Position.X - i, Position.Y - i);
                  earthQuakePositions.Add(new Position(Position.X - i, Position.Y - i));
                  Console.Write(quakeChar);
              }
              if (Position.X + i < terrarium.Width && Position.Y - i >= 0)
              {
                  Console.SetCursorPosition(Position.X + i, Position.Y - i);
                  earthQuakePositions.Add(new Position(Position.X + i, Position.Y - i));
                  Console.Write(quakeChar);
              }

              for (int j = 0; j < size - 2; j++)
              {
                  if (Position.X + i < terrarium.Width && Position.Y + j < terrarium.Height)
                  {
                      Console.SetCursorPosition(Position.X + i, Position.Y + j);
                      earthQuakePositions.Add(new Position(Position.X + i, Position.Y + j));
                      Console.Write(quakeChar);
                  }
                  if (Position.X - j >= 0 && Position.Y + i < terrarium.Height)
                  {
                      Console.SetCursorPosition(Position.X - j, Position.Y + i);
                      earthQuakePositions.Add(new Position(Position.X - j, Position.Y + i));
                      Console.Write(quakeChar);
                  }
                  if (Position.X - i >= 0 && Position.Y - j >= 0)
                  {
                      Console.SetCursorPosition(Position.X - i, Position.Y - j);
                      earthQuakePositions.Add(new Position(Position.X - i, Position.Y - j));
                      Console.Write(quakeChar);
                  }
                  if (Position.X + j < terrarium.Width && Position.Y - i >= 0)
                  {
                      Console.SetCursorPosition(Position.X + j, Position.Y - i);
                      earthQuakePositions.Add(new Position(Position.X + j, Position.Y - i));
                      Console.Write(quakeChar);
                  }
              }

              timeController.Step(50);
          }

          foreach (IOrganism organism in terrarium.Organisms)
          {
              foreach (Position pos in earthQuakePositions)
              {
                  if (organism.Position.Equals(pos) && !organismsToShuffle.Contains(organism))
                  {
                      organismsToShuffle.Add(organism);
                  }
              }
          }


          foreach (IOrganism organism in organismsToShuffle)
          {
              organism.LastPosition.X = organism.Position.X;
              organism.LastPosition.Y = organism.Position.Y;

              Position randomPositionInQuake = earthQuakePositions[random.Next(earthQuakePositions.Count)];
              organism.Position.X = randomPositionInQuake.X;
              organism.Position.Y = randomPositionInQuake.Y;
          }

          Console.ResetColor();

          terrarium.RenderPlants();
          terrarium.RenderAnimals();
      }