This commit is contained in:
Patricia Aas 2021-10-05 10:16:47 +02:00
parent 30facca41d
commit 99dab72e9b
1 changed files with 21 additions and 9 deletions

View File

@ -4,13 +4,16 @@ In this exercise we will create a class that abstracts away common functionality
## Visibility
The main difference between `class` and `struct` is the default visibility. All members of a struct are publicly accessible by default, so no accessors are needed if that is the intended behavior. A class on the other hand is private by default, so some sort of `public:` access is needed to give users access to things like constructors and functions.
The main difference between `class` and `struct` is the default visibility. All members of a struct are publicly
accessible by default, so no accessors are needed if that is the intended behavior. A class on the other hand is private
by default, so some sort of `public:` access is needed to give users access to things like constructors and functions.
Everything can be made private in a C++ class, even constructors.
## Default behavior
If possible, we want to rely on the default behavior of the member variables of a class. This simplifies the construction of your class and reduces the things you need to worry about.
If possible, we want to rely on the default behavior of the member variables of a class. This simplifies the
construction of your class and reduces the things you need to worry about.
```cpp
struct Position {
@ -27,26 +30,35 @@ struct GridPosition {
};
```
When someone creates an instance of `Position` the x and y variables are always set to 0 and there is no other way to create the class.
When someone creates an instance of `Position` the x and y variables are always set to 0 and there is no other way to
create the class.
When someone wants to create an instance of `GridPosition`, they need to give the constructor the x and y values. You could set them to 0 but they still need to be given. Trying to create an instance of `GridPosition` with no values given will not compile.
When someone wants to create an instance of `GridPosition`, they need to give the constructor the x and y values. You
could set them to 0 but they still need to be given. Trying to create an instance of `GridPosition` with no values given
will not compile.
## Game State
`GameState` is a class that holds the instances of all the game objects (ghosts, pacman, pellets, etc). Roughly every 16 milliseconds the `step()` function inside of `GameState` will be called.
`GameState` is a class that holds the instances of all the game objects (ghosts, pacman, pellets, etc). Roughly every 16
milliseconds the `step()` function inside of `GameState` will be called.
`GameState` will call each member's `update()` function with the current delta time, this allows each object to know that it should update and how much time has passed since last it updated. The class also does some checks like if any pellets have been eaten and if any of the member objects are colliding.
`GameState` will call each member's `update()` function with the current delta time, this allows each object to know
that it should update and how much time has passed since last it updated. The class also does some checks like if any
pellets have been eaten and if any of the member objects are colliding.
## Exercise
The ghost functions within `GameState` are usually grouped together. So lets abstract the ghost updating code into its own class.
The ghost functions within `GameState` are usually grouped together. So lets abstract the ghost updating code into its
own class.
1. Create files called `GhostState.hpp` and `GhostState.cpp` and run CMake so they are visible within the project.
2. Create a class called `GhostState` within the `.hpp` file.
3. Move all of the ghosts from the `GameState` class into the `GhostState` class. Then create a member variable within `GameState` that is of type `GhostState`.
3. Move all of the ghosts from the `GameState` class into the `GhostState` class. Then create a member variable
within `GameState` that is of type `GhostState`.
4. Move all of the ghost functionality from `GameState` into the `GhostState` class. Create functions for this behavior and call those functions from their respective locations within `GameState` (update, checkCollision, reset, frighten)
4. Move all of the ghost functionality from `GameState` into the `GhostState` class. Create functions for this behavior
and call those functions from their respective locations within `GameState` (update, checkCollision, reset, frighten)
5. Compile and run the game, check if everything is working correctly.