1
\$\begingroup\$

I'm making a text-based role playing game written in PHP.

I have a hard time figuring out how to handle items in game - there will be a few types of objects (weapon, machine, tool, building etc) each of these types will have its own stats, attributes etc, and items itself.

My initial plan was to make a interface and abstract class called Object

interface IObject
{

   public function takeObject() {};
   public function dropObject() {};
   public function examineObject() {};
   // etc
}

abstract class WeaponObject implements IObject
{
   // Some generic properties valid for this type of objects
   public $name = "";
   public $weight = 0;
   public $damage = 0;

   // etc
}

and then make another class for each item that extends the base Object

e.g

class HuntingBow extends WeaponObject
{
   //
}

But there's a problem I think. There's going to be a lot of objects in the game - and that means a lot of classes, and composer need to load them all. So I think that performance may decrease significantly?

My second idea was to make a generic table called objects

In which I would store every item. But setting up object attributes, handling stats, etc, would be a nightmare.

Is the first idea the best way to approach the problem? Is there a better way of doing thins that I don't about?

\$\endgroup\$
  • 1
    \$\begingroup\$ I suggest you have your objects in data tables of some sort. In the long run, hundreds of classes is much more unwieldy than neatly stored data. \$\endgroup\$ – Alexandre Vaillancourt May 25 '16 at 12:57
  • \$\begingroup\$ Why would you make a game in PHP? \$\endgroup\$ – KaareZ May 25 '16 at 16:19
  • \$\begingroup\$ @KaareZ That's pretty simple. Because I can \$\endgroup\$ – user3343366 May 25 '16 at 17:16
  • \$\begingroup\$ @KaareZ We have a policy of not getting into arguments about technology choices here, because they are solely based on personal preference. That's why we always close technology recommendation questions. When the Op want to torment themselves with PHP that's not for us to judge. \$\endgroup\$ – Philipp May 25 '16 at 20:09
  • \$\begingroup\$ @Philipp To be honest I wouldn't call it a torment. I like PHP especially after PHP7. It's going to be a MUD - Today, when people are making gameboy emulators and minecraft servers written in PHP, I think that it should fit it perfectly. Even erepublik runs on Symfony 2 :) But I would happily hear out your argument on why PHP is wrong for this kind of project. \$\endgroup\$ – user3343366 May 25 '16 at 23:37
5
\$\begingroup\$

A rule of thumb is that you use different classes when objects require different code and instances of the same class when the objects only require different values.

So when ShortBow, HuntingBow, CompositeBow, ElvenBow etc. all behave identical except for having different stats, then they would all be instances of the same class.

When bows have different mechanics for resolving attacks than other ranged weapons, that class would be Bow. When guns, crossbows and slingshots also use the same mechanics as bows do, they would all be class RangedWeapon. When you don't have any mechanical differences between ranged weapons and melee weapons, they would all be of class Weapon.

But in any case, when you have one really special bow with very unique mechanics, like LordKibosMagicBowWhichAlwaysDoesCriticalDamageExceptOnThursdays that might again justify an unique class which extends whatever class the other bows use.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.