Advertisement
Omnikron13

Merc.php WIP

Apr 3rd, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3. class Merc {
  4.     //Shared stats
  5.     protected static $i_hp        = NULL;
  6.     protected static $i_skill     = NULL;
  7.     protected static $i_strength  = NULL;
  8.     protected static $i_endurance = NULL;
  9.     //Local
  10.     protected $s_name    = "Nemo";
  11.     protected $s_race    = "Unknown";
  12.     protected $s_faction = "No allegiance";
  13.     protected $i_level   = 0;
  14.     protected $i_xp      = 0;
  15.     protected $a_traits    = [];
  16.     protected $a_skills    = [];
  17.     protected $a_inventory = [];
  18.     //Buff storage... May create a dedicated buff class to smoothen this process..
  19.     protected $a_buffs = ["hp"        => [],
  20.                           "skill"     => [],
  21.                           "strength"  => [],
  22.                           "endurance" => []];
  23.  
  24.     //Main accessor method to class properties (many of which will be faily dynamic)
  25.     public function __get($_prop) {
  26.         switch($_prop) {
  27.             //Local
  28.             case "Name":
  29.                 return $this->s_name;
  30.             case "Race":
  31.                 return $this->s_race; //Will load proper race name from file, eventually..
  32.             case "Faction":
  33.                 return $this->s_faction; //Pondering similar treatmeant for faction as race^
  34.             case "Level":
  35.                 return $this->i_level;
  36.             case "XP":
  37.                 return $this->i_xp;
  38.             case "XPGoal":           //--This algorithm doesn't really want to live here!--
  39.                 return 100 + (((($this->i_level / 2) + 0.5) * $this->i_level) * 10);
  40.             //Ad-hoc (most probably very temporary) output for array data...
  41.             case "Traits":
  42.                 $str = "";
  43.                 foreach($this->a_traits as $trait) {
  44.                     $str .= "$trait, ";
  45.                 }
  46.                 return rtrim($str, ", ");
  47.             //case "":
  48.             //case "":
  49.             //Shared stats (at least until modifiers work)
  50.             case "HP":
  51.                 return self::getStat("hp");
  52.             case "Skill":
  53.                 return self::getStat("skill");
  54.             case "Strength":
  55.                 return self::getStat("strength");
  56.             case "Endurance":
  57.                 return self::getStat("endurance");
  58.             //case "":
  59.             default:
  60.                 throw new Exception("Unknown property: $_prop. (N.B. Need to fix exception type)");
  61.         }
  62.     }
  63.     //--------------------------------[Main accessor END]--------------------------------
  64.    
  65.    
  66.     //Constructor(s)...
  67.    public function __construct($_file) {
  68.            $json = file_get_contents($_file);
  69.            $json = json_decode($json);
  70.            $this->s_name    = $json->merc->name;
  71.            $this->s_race    = $json->merc->race;
  72.            $this->s_faction = $json->merc->faction;
  73.            $this->i_level   = $json->merc->level;
  74.            $this->i_xp      = $json->merc->xp;
  75.            $this->a_traits  = $json->merc->traits;
  76.            //$this->a_       = $json->merc->;
  77.    }
  78.     //---------------------------------[Constructor END]---------------------------------
  79.    
  80.    
  81.     //--Register buff method - protected or public..?
  82.     public function registerBuff($_name, $_buff, $_stat = "hp"/*(/etc..?)*/) {
  83.         //pending data structure..
  84.         $this->a_buffs[$_stat] += [$_name => $_buff];
  85.         print_r($this->a_buffs);
  86.     }
  87.     //--------------------------------[Buff Register END]---------------------------------
  88.    
  89.    
  90.     //Access function for shared stats to make sure they're loaded when called for
  91.     public static function getStat($_stat) {
  92.         if(self::$i_hp        === NULL || self::$i_skill     === NULL ||
  93.            self::$i_strength  === NULL || self::$i_endurance === NULL) {
  94.             //Load base stats here! =D
  95.             $stats = json_decode(file_get_contents("BaseStatsMerc.json"));
  96.             self::$i_hp        = $stats->stats->hp;
  97.             self::$i_skill     = $stats->stats->skill;
  98.             self::$i_strength  = $stats->stats->strength;
  99.             self::$i_endurance = $stats->stats->endurance;
  100.         }
  101.         switch($_stat) {
  102.             case "hp":
  103.                     return self::$i_hp;
  104.             case "skill":
  105.                     return self::$i_skill;
  106.             case "strength":
  107.                     return self::$i_strength;
  108.             case "endurance":
  109.                     return self::$i_endurance;
  110.             default:
  111.                 throw new DomainException("Request for nonexistant stat: $_stat");
  112.         }
  113.     }
  114.     //-----------------------------[Shared stat accessor END]----------------------------
  115. }
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement