SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to use classes? (example)

    I am just constantly working on ways to improve my online game, now its just a blah of an RPG... its not that great, although its not open of course... but here is an example I just had written, but I am just wondering if its the best way to use OOP with the game, Should I be doing anything different? If so, then what? Any help on this subject would be greatly appreciated.

    PHP Code:
    class DiceRolling {
       var 
    $strength 10;
       var 
    $dexterity 10;
       var 
    $enemyArmor 3;
       var 
    $enemyDexterity 11;
       var 
    $enemyStrength 8;

       function 
    DiceRolling() {
          
    $this->strength $strength;
          
    $this->dexterity $dexterity;
          
    $this->enemyArmor $enemyArmor;
       }

       function 
    CalculateDamage() {
          
    $minimum_damage = ($this->strength .20) + ($this->dexterity .05);
          
    $maximum_damage = ($this->strength .40) + ($this->dexterity .10);
          
    $damage mt_srand($minimum_damage$maximum_damage) - $this->enemyArmor;
          return 
    $damage;
       }
       function 
    CalculateHitChance() {
          
    $minimum_chance = ($this->dexterity 8) - ($this->enemyDexterity 8);
          if(
    $minimum_chance >= 1) {
             return 
    true;
          } else {
             return 
    false;
          }
       }

    Now these are just rough estimates, these are not real algorithems or whatever, they are just examples... the code is used something along these lines...

    PHP Code:
    $dice = new DiceRolling();
    if(
    $dice->CalculateHitChance()) {
       echo 
    $dice->CalculateDamage();
    } else {
       echo 
    "You miss!<br>";

    Any help on making this better? Thank you.

  2. #2
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I wanted to know is, how do you delete objects?

    I want to be able to delete characters that no longer exist;

    Code:
    $bob = new character("bob");
    
    class character() {
        var name$;
       function character(name$) {
        $this->name = name$
        } 
    } // end class
    How do I do a delete?

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    um, all the information is actually going to be called from a mysql db, this was just an example not meant for actual use hehe. I am just wondering if there is a way to make the code better then what the example shows...

    // but seriously, if you REALLY wanted to, why not just unset( $character ); where $character is the initialization of the class ?

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what worchyld was trying to say is that maybe you could create character objects instead of having enemy class var's.

    Without really knowing what your game is like maybe this will provide some new ideas:
    PHP Code:
    class Character {
        var 
    $armor;
        var 
    $dexterity;
        var 
    $strength;
        var 
    $life;
        
    // Id recommend using setters / getters.

    class 
    Combat {
        var 
    $attacker;
        var 
    $defender;
        function 
    Combat(&$attacker, &$defender) {
            
    $this->attacker = &$attacker;
            
    $this->defender = &$defender;
        } 
        function 
    fight() {
           
    // Do your calculation here.
           
    if ($this->hitChance()) {
               
    $this->defender->life -= $this->damage();
           }    
        } 
        function 
    damage() {
            return 
    mt_rand($this->hit(.20.05), 
                           
    $this->hit(.40.10)) 
                           - 
    $defender->armor;
        } 
        function 
    hit($strength$dexterity) {
            return (
    $this->attacker->strength $strength) + 
                   (
    $this->attacker->dexterity $dexterity);
        } 
        function 
    hitChance() {
           
    // ...
        
    }


    $knight = new Character(55510);
    $king = new Character(75310);

    $combat = new Combat($knight$king);
    $combat->fight(); 

  5. #5
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want something like:
    PHP Code:
    class DiceRolling {
       var 
    $strength 10;
       var 
    $dexterity 10;
       var 
    $enemyArmor 3;
       var 
    $enemyDexterity 11;
       var 
    $enemyStrength 8;

       function 
    DiceRolling() {
       }

       function 
    setCharacter($strength$dexterity$armor) {
          
    $this->strength $strength;
          
    $this->dexterity $dexterity;
          
    $this->armor $armor;
       }

       function 
    setEnemy($strength$dexterity$armor) {
          
    $this->enemyStrength $strength;
          
    $this->enemyDexterity $dexterity;
          
    $this->enemyArmor $armor;
       }

       function 
    CalculateHitChance() {

       function 
    CalculateDamage() { 
    Then:
    PHP Code:
    $character =& $_SESSION['character'];
    $enemy $db->fetchEnemy($enemyrID);
    $dice = new DiceRolling();
    $dice->setCharacter($character['strength'], $character['dexterity'], $character['armor']);
    $dice->setEnemy($enemy['strength'], $enemy['dexterity'], $enemy['armor']);
    if(
    $dice->CalculateHitChance()) {
       echo 
    $dice->CalculateDamage();
    } else {
       echo 
    "You miss!<br>";


  6. #6
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is one thing I never understood, why do you have to pass by reference here...

    PHP Code:
     function Combat(&$attacker, &$defender) { 
            
    $this->attacker = &$attacker
            
    $this->defender = &$defender
        } 
    I don't understand it Could you please explain?

  7. #7
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is done so that we can make changes to attacker or defender once they have been passed. If they are not passed by reference and we make a change to the defenders life, we will be making a change to the $defender var but not the $king var.

    http://www.phppatterns.com/index.php...leview/23/1/2/
    http://ca.php.net/manual/en/language.references.php

    Try the following code using references on the defender and not using references.
    PHP Code:
    class Character {
        var 
    $armor;
        var 
    $dexterity;
        var 
    $strength;
        var 
    $life;
        function 
    Character($a$d$s$l) {
            
    $this->armor $a;
            
    $this->dexterity $d;
            
    $this->strength $s;
            
    $this->life $l;
        }
        function 
    attack() {
           return (
    $this->strength $this->dexterity) / 2;
        }
        function 
    defend() {
           return (
    $this->armor $this->dexterity) / 2;
        }

    class 
    Combat {
       var 
    $attacker;
       var 
    $defender;
       function 
    Combat($attacker$defender) {
           
    $this->attacker $attacker;
           
    $this->defender $defender;
       }
       function 
    fight() {
           
    $hit $this->attacker->attack() - 
                  
    $this->defender->defend();
           if (
    $hit 0) {
               
    $this->defender->life -= $hit;
           }
       }
    }

    echo 
    '<pre>';
    $knight = new Character(102030100);
    $king = new Character(20520100);

    print_r($king);

    $combat = new Combat($knight$king);
    $combat->fight();

    print_r($king); 

  8. #8
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe I have become enlightened because of your explanation Brenden Vickery, Thank you very very much, if I need any more help, I will ask again...

    Thank you.

  9. #9
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you delete objects? Do you just unset them?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •