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


In reply to Re: Game related OO design question by McA
in thread Game related OO design question by blindluke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.