I am working on a game that requires resolution actions depending on the object a ball collides with.
A lot of code within the resolution method applies to all of the different types of objects that could be hit but some is also specific to a particular object type.
The pseudo code looks like this:
resolve (Object a) {
if (some test) {
if (Object a is rect) {
// do one thing
} else {
// do something else thing
} else if (another test) {
if (Object a is rect) {
// do one thing
} else {
// do something else thing
} else if (third test) {
if (Object a is rect) {
// do one thing
} else {
// do something else thing
}
}
However, to a newbie, this feels kind of cluttered (because of the tests for object type within each if clause) but I can't quite figure out how to improve it ? I could override the method and have behavior based on the subclass that is passed to it but then I end up with a lot the same code in both methods.
This method tests the current location of the ball (top, bottom etc) and adjusts the balls velocity accordingly. If object is a rectangle, the new velocity is different to say a circle.
Can anyone offer any assistance?