Software Engineering: Extending Classes
For this in-class exercise, we will be extending the project we worked on yesterday. First, downlowd the solution to the exercise from last class:
We are going to make a few changes to this project: Multiple players, homining missles. If there is sufficient time, we will make the changes as data-driven as possible
Adding Multiple Players
FIrst up, we need to add multiple players. If you look at the Player.css class, there are a few things that get in the way of multiple players. The first is in the Update method -- we have hardcoded the keys for movement and firing of the player. That won't work for mutliple players. So, we need to:
- Pass a keymap (and potentially an initial position) into the constructor for the player, and use that keymap in the update method. They keymap can be just an array of Keys, with keyMap[0] holding the key for left, keyMap[1] holding the key for rightm and so on
- Modify the world Update method to handle multiple players. As a general rule -- when you go from 1 to 2, you should instead go from 1 to many: allow any number of players, Paradoxically, this can actually make your life easier instead of harder. With 2 players, you may be tempted to cut and paste code, for instance, but with n players you need to be smarter about code reuse.
Homing Bullets
With proper program decomposition, adding new features is not hard. Create a new Particle class, which is a subclass of the existing partilce class. The constructor should take as input parameter the object (for now, just a Player object) that is being targeted. (Really, we should make a superclass (or an interface) of all objects, of which the Player is a subclass. This superclass (or interface) should contain a position -- then the homing projectiles can home on anything that is a sublcass (or implementation) of this class (or interface)
For the easiest homing bullet, have the bullet's velocity point directly at the target every frame. For added challenge (and a better visual effect), give the bullets a maximum rotational velocity, and rotate the bullet's velocity in the direction of the target each frame using the rotational velocity to determine how far to rotate each frame
Collision
If you still have time, add simple collision with particles and ships (or ships and ships). Given that we are rotating rectangles, this will be difficult to do with any accuracy until we go over collision detection, later this semester. However, you should be able to get something (even with a really inaccurate bounding box)
Some things to think about:
- Where should the collision detection occur? The World update function is a good choice, but there are other ways to skin that particular cat
- Do you need to add any other accessor methods to ships and/or particles for the world to effectively do collision?