Jump to content

Recommended Posts

Like pretty much everyone else out there, I'm attempting to write my own web-based RPG.  I'm currently writing it in ASP.NET/C#, but would like to port it over to PHP if I can get it working.

 

Anyhoo, I've hit a bit of a design snag when it comes to coming up with a reasonable way to handle a character's special attacks.  I've decided that each character will get a special attack at level 1, 3, then every three levels after that (6, 9, 12, etc).  I'm just not sure how to implement them.

 

Currently, I'm employing the strategy pattern with my characters.  Each PlayerCharacter object contains a CharacterClass object, like so (translated to PHP):

class PlayerCharacter
{
   private $charClass;

   public setCharClass(ICharacterClass $charClass)
   {
      $this->charClass = $charClass;
   }
}

 

ICharacterClass is an interface that all character classes implement.

 

I'm thinking that each special attack should be an object in and of itself, deriving from a base class/interface related to the character class which they (logically) belong.  So, all soldier attacks would derive from the same base soldier attack info, all hacker attacks would derive from the same base hacker attack info, etc.  I could 'unlock' the new attacks by adding them to an array/list when the character reaches the right level.

 

My main question is whether or not this is an ideal way to approach this.  I'm trying to keep my systems as flexible as possible, so I can add new classes and attacks easily.  The one bad 'smell' I'm getting from this approach is determining which attack to add.  I mean, I don't want to have a gigantic switch statement along the lines of:

public function levelUp()
{
   if($this->currentLevel % 3 == 0)
   {
      $this->addNewAttack();
   }
}

private function addNewAttack()
{
   switch($this->currentLevel)
   {
      case 1:
         $this->attacks[] = new SimpleAttack();
         break;

      case 3:
         $this->attacks[] = new ModerateAttack();
         break;

      case 6:
         $this->attacks[] new DevestatingAttack();
         break;

      //etc
   }
}

 

I mean, I could pass the PlayerCharacter object into some sort of AttackFactory, but I'm not sure if that would really remove the conditional.  I think it would simply move it to another (albeit, more logical) location.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/126291-game-design-special-attacks/
Share on other sites

in php you can create an array of values and dynamically create a class based upon some condition.. eg. a level

 

<?php
$array = array(
1 => 'SimpleAttack',
3 => 'ModerateAttack',
6 => 'DevestatingAttack'
);
$this->attacks[] = new $array[$this->currentLevel]();
?>

...though many compiled languages don't allow things like this.

 

btw, you call addNewAttack() only when level % 3 == 0, but you have a switch case of 1, which will never be satisfied.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.