in reply to Game related OO design question
Hi,
I'm not an OO expert (Toby, where are you?), but this line is the starting point for some questions:
my $monster = Enemy->new('Rat');
You instantiate an 'Enemy' object, but not every Enemy-Object will be the same because you can instatiate several 'types' of Enemy objects. But this is exactly what OO is for. When you have several 'types' (classes) of Enemy object why don't you declare them as being of that type knowing that they share a common behaviour (Enemy).
This 'Rat' isa (certain) 'Enemy' relationship is exactly what inheritance or Roles are for.
So, aks yourself the other way: When you have a variable $e and you know that this is a reference to an Enemy, would it not be better to know whether this Enemy is a Rat but can be used everywhere an Enemy is needed in terms of behaviour?
So, without knowing the details, think about having a base class Enemy and descendants which specialize the behaviour in terms of wound, attack and defense behaviour.
Another note: You can always create helper functions or class methods for simplifying or factoring out the instantiation of that objects (kind of factory classes/objects).
I'm really interested in other comments and ideas.
Regards
McA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Game related OO design question
by blindluke (Hermit) on Nov 27, 2014 at 14:59 UTC |