I am trying to design different weapon behaviors.
Right now, there are 3 different types of weapon behaviors that i would like to implement.
- When not pressing anything, weapon fires at a regular slow interval. When mouse Pressed, weapon fires at a rapid interval
- when not pressing anything, weapon does nothing. when mouse pressed, weapon charges up. when mouse released, weapon fires
- when not pressing anything, weapon charges up automatically. when mouse Pressed, weapon fires, and then automatically the weapon charges up.
I'm sure there are more to add, but I'm just starting to learn about composition. Can someone explain to me how I might generally write such behaviors for the weapons in a composition over inheritance manner? I'd like to be able to simply swap out one weapon behavior for another just to see how the game might change, and I don't want to have to keep copying and pasting code which is what I seemed to be doing.
I'm guessing that I should use an interface (I'm using Java) called WeaponBehavior perhaps? So then should every base weapon implement this interface? And then i put that interface somewhere within my weapon.run() loop that I have made? (Heroes have weapons, and when heroes run, they run their weapons). Or is it that there is a behavior object instead? I think this is what I'm confused about.
I'd just like to know what skeleton code might look like as opposed to what I am currently doing, which is making the behaviors for each specific weapon that inherits from some base weapon.
abstract Class Weapon //implements WeaponBehavior??
{
// should I have an object called WeaponBehavior instead of implementing an interface?
Weapon()
{
}
void run()
{
update();
//behavior somewhere here?
render();
}
void update()
{
//does behavior go in here?
}
abstract void render();//how does the weapon look
abstract void fire(); // this has to be called at some point
abstract void handleMousePress(); //
}