Goals: Set up the living space for the future animals
In this step, we aim to create a first graphical program that allows us to visualize the simulated "environment" (class Environment) and the different entities evolving within it over time (animals, plants, etc.).
Our program will now have as its main component a class Application; the "simulation core" capable of doing graphics and handling events, provided in src/Application.[hpp/cpp].
The wildlife we are going to simulate will evolve in an environment. Therefore, we need to start by modeling this concept in a basic way.
A class Environment will naturally be needed here. At this stage, we will consider that an environment is characterized by:
To model a set of animals and targets, we will anticipate a little bit of our future course on data structures. The concept of list which does not allow indexing will have the advantage of enabling potentially less costly insertions/removals of elements.
Example usage:
#include <list>
...
list<int> integers; // or list<int> integers(3) or list<int> integers(3,1)
// with the same meaning as for vector constructors
integers.push_back(3); // insertion at the end
integers.push_front(2); // insertion at the front
integers.pop_front(); // removal at the front
integers.pop_back();
Since there is no indexing, the traversal of a list is done through iterations over the entire set:
for (auto animal: animals)or
for (auto& animal: animals)
The following methods are to be expected for the class Environment :
//evolve the animals of the wildlife here:to remind yourself of the task to code later.
You are free to add any other method you think is useful later.
[Question Q2.1] Which methods do you think it would be wise to declare as const?
[Question Q2.2] We want to prevent the copying of an Environment. What solution would you propose to satisfy this constraint ?
[Question Q2.3] Question Q2.2 suggests that each simulation will deal with a unique environment with its own wildlife. Moreover, it doesn't make much sense to let the animals live without attaching them to an environment. An Environment can therefore be considered responsible for the lifetime of the animals created in the simulation. What impact does this have on the destruction of an environment ?
Think about these questions and answer them in your RESPONSES file, and adapt your code accordingly.
To test the work done so far, you will use the test provided in src/Tests/GraphicalTests/EnvTest.cpp. The class EnvTest inherits from Application. It therefore has an environment on which the drawing and update methods you have drafted will be invoked in a loop. The R key on the keyboard (handled by Application::handleEvent) actually triggers a call to the clean method you just programmed.
This class also overrides the onEvent method so that pressing the 'T' key results in a call to the addTarget method you just programmed.
The provided CMakeLists.txt file allows you to compile the test via the envTest target. Don't forget to uncomment this target in the CMakeLists.txt file (lines 100, 101, and lines 143, 144) and run Build > Run Cmake when you are ready to test.
The behavior of your program at this stage should resemble what is shown in this short video (restart it if necessary):
| ← You can use the 'T' key to place targets/resources in the environment where the mouse is located. Make sure that the 'R' key clears the environment of all created targets. |
|
| [Video: placing targets in the environment] |
The keys that can be used by the program are documented in the small help menu that appears in the gray portion displayed to the right of the simulation window.