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
  1. Create the class ‘movingCircle’. It should contain a Circle, a Vec2 ‘direction’ and a float ‘speed’.
  2. 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.
  3. 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.
  4. Create a ‘draw’ method to display the circle on the screen.
  5. 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.
  6. Add a new class ‘circleManager’ which contains a memory container for the class ‘movingCircle’.
  7. Provide an ‘add’ method to add a new circle to this container.
  8. Provide an ‘update’ method which calls the update method of every circle in the container.
  9. Add a ‘draw’ method to draw all circles on the screen.
  10. 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’.
  11. Remove the object of the class ‘movingCircle’ from your application. (The lines where you used this object should also be erased.)
  12. In the update function of your app, execute the ‘add’ method of CM whenever the space bar is pressed.
  13. 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.
  14. 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.