10.5 Manager Classes
When you create a class for moving circles, because you will need a lot of moving
circles in your application, it might be a good idea to create a special class to handle
them. Such a ’manager’ class will be responsible for creating new moving
circles, updating and drawing them, and even delete them when no longer
needed.
Suppose you have the following class:
class myCircle {
private:
Circle c;
5public:
void update() {
// update code here
}
10 void draw() {
// draw code here
}
}
15class myCircleManager {
private:
Memx<myCircle> list;
public:
20 void createCircle() {
myCircle & temp = list.New();
// do something else with the new circle if you like
}
25 void update() {
// update all circles
for(int i = 0; i < list.elms(); i++) {
list[i].update();
}
30 }
void draw() {
// draw all circles
for(int i = 0; i < list.elms(); i++) {
35 list[i].draw();
}
}
}
}
40
myCircleManager MyCircleManager;
// your application
bool Init() {
45 MyCircleManager.createCircle();
return true;
}
bool Update() {
50 MyCircleManager.update();
return true;
}
void Draw() {
55 MyCircleManager.draw();
}
Exercise
- Create the class ‘movingCircle’. It should contain a Circle, a Vec2
‘direction’ and a float ‘speed’.
- Add a ‘create’ method to provide the circle with a radius and a position.
This method should also assign a random float between -1 and 1 to the
direction variable, and a random float between 0.5 and 2 to speed.
- Add an ‘update’ method to this class. This method will add the direction,
multiplied by Time.d() and ‘speed’, to the circle’s position. The update
method should also check the new position. Whenever the x or y value
is out of bounds (which means it is no longer visible on the screen) you
should reverse the sign of that value.
- Create a ‘draw’ method to display the circle on the screen.
- Create a global object of the class ‘movingCircle’ in your application.
Don’t forget to add the object’s methods and run your application to see
the result.
- Add a new class ‘circleManager’ which contains a memory container for
the class ‘movingCircle’.
- Provide an ‘add’ method to add a new circle to this container.
- Provide an ‘update’ method which calls the update method of every circle
in the container.
- Add a ‘draw’ method to draw all circles on the screen.
- Since you only need one object of this class, you should add it in the same
file, right below the class declaration with the name ‘CM’.
- Remove the object of the class ‘movingCircle’ from your application. (The
lines where you used this object should also be erased.)
- In the update function of your app, execute the ‘add’ method of CM
whenever the space bar is pressed.
- Also in this update function, execute the update method of CM. And of
course the draw method should be called inside your application’s Draw
function.
- Extra: write a ‘remove’ method for the circleManager. This method should
accept a position as an argument. The method should compare this
position with the circles in it’s container. If the provided position overlaps
with a circle, this circle should be erased from the container.