KevinM1 Posted September 29, 2008 Share Posted September 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126291-game-design-special-attacks/ Share on other sites More sharing options...
genericnumber1 Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126291-game-design-special-attacks/#findComment-655859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.