Quote:Perhaps it is a bizzare way of looking at things, but I considered the bullet as a canon that moves. They both have a target, they turn towards it, but the bullet also moves towards it. At least thats how it would work in this example.
Yes it is bizarre but only to those who are more familiar with OO. OO is about creating software constructs (classes) that represent real world things.
Inheritance, aggregation and association and the OO concepts that describe different interactions between classes.
For example inheritance describes the 'is a kind of' relationship so
we might have a class called Weapon and that class has methods to set the position, load, aim and fire the weapon.
So a
Canon is a kind of weapon so is
Rifle,
Revolver,
MissileLauncher etc so all these can inherit from
Weapon. These are all the same kind of thing because 'they fire projectiles'.
A bullet does not fire projectiles so is not a kind of Weapon/Canon so should not inherit from these classes.
Aggregation means 'is made of' or 'comprises of. So from my original reply the
EnemyGroup is an aggregation of
Enemy.
Association means 'is related to' so the
Canon might have a
Person to load, aim and fire it. The Person is not 'a kind of' or 'a part of' a Canon so inheritance and aggregation don't describe the relationship but there is a link between
Canon<>
Person and that is association.
Quote:-Should there be a "void fire(){}" method in the canon, so the bullet would be created inside the canon, or would the game class decide when a bullet is created?
A really good question since there is no truly correct answer, the game class could create the bullet and pass it to the canon to get the bullets direction based on the orientation of the canon, or the bullet is created when the canon is fired. Take your pick - personally I like the second option and you could implement it like this
Code:public class Cannon {
public Bullet fire(){
Bullet b = new Bullet();
// set the various bullet values e.g. direction and speed
// based on Canon attributes
return b;
}
}
Quote:***Also, when is it necessary to use "public" before a class?
In Processing you can ignore the
public keyword and it is probably better to do so.
Hope this helps some.