I have the following GameObject interface
:
public interface GameObject {
void viewDetails();
}
Character Interface
:
interface Character{
void pickUp(Weapon weapon);
void use(Weapon weapon);
}
and abstract
Weapon class
:
public abstract class Weapon implements GameObject {
//left out constructor to focus on methods
@Override
public abstract void viewDetails();
public abstract void attack(Enemy enemyObj);
//Could be bullets, could be a mystical item.
public abstract void replenish(ReplenishItem rpItem);
}
The problem with this is, a GameObject sometimes can be used in different ways. For example, the primary use of a game weapon is to attack a target, but what if I wanted to reload? How do I let my character interface reload or beware that reload is an option?
I would use the following approach.
I would declare interfaces:
Then implement classes, like these:
Then, I would declare these interfaces:
And, I would declare character classes:
This approach let us add new weapons and characters without sufficient effort later.
In your
use()
method you can callreload()
if there is no more ammo in the weapon dispenser.But if your game character receives signal from outside, for example, reload the gun, even there is enough ammo to fire, then have an
Event->Listener
approach implemented.Create a
WeaponEvent
class, extend this class to haveFirearmWeaponEvent
,MeleeWeaponEvent
etc.Make your game character class(es) as a listener to
WeaponEvent
events, then in your game character class have a methodprocessEvent(WeaponEvent event)
, and act accordingly to the event you have received.