cooldude832 Posted June 26, 2008 Share Posted June 26, 2008 I've finally given in and decided to write some oop. I'm writing a game and I'm writing my character functions library for my character class it looks like <?php require_once('constants.php'); require_once(SCRIPTS.'functions.php'); class Char{ Public $hp = 0; Public $attack = 0; Public $defense = 0; Public $level = 0; Public $charid = 0; Public $live = 1; Public $armor = array("ID", "Name", "Description", "Attack_Mod", "Defense_Mod", "HP_Mod", "Level_Mod"); Public $weapon = array("ID", "Name", "Description", "Attack_Mod", "Defense_Mod", "HP_Mod", "Level_Mod"); Public $inventory = array(); Public $errors = array(); function get_id($plyid){ connectSQL(SQL_GAME_DB); $q = "Select CharplyID from `playerchars` where PlayerID = '".$plyid."'"; $r = mysql_query($q) or die(mysql_error()."<Br /><br />".$q); $row = mysql_fetch_assoc($r); $this->charid = $row['CharplyID']; } function get_base_stats(){ ConnectSQL(SQL_GAME_DB); $q = "Select CharplyID, HP, Level, Attack, Defense, Max_HP from `playerchars` Where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $this->level = $row['Level']; $this->hp = $row['HP']; $this->defense = $row['Defense']; $this->attack = $row['Attack']; $this->charid = $row['CharplyID']; } function hp_up($hp){ $temp_hp = $this->hp; $totalhp = $temp_hp+$hp; $this->hp = $totalhp; ConnectSQL(SQL_GAME_DB); $q = "Update `playerchars` set HP = ".$this->hp." Where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } function hp_down($hp){ $temp_hp = $this->hp; $totalhp = $temp_hp-$hp; $this->hp = $totalhp; ConnectSQL(SQL_GAME_DB); $q = "Update `playerchars` set HP = ".$this->hp." Where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } function live(){ ConnectSQL(SQL_GAME_DB); $q = "Select HP from `playerchars` where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); if($row['HP'] < 1){ $q = "Update `playerchars` set HP = '0' Where CharplyId = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $this->live = 0; } else{ $this->live = 1; } } function status(){ if($this->live === 0){ echo "You are dead."; } elseif($this->live != 0){ echo "You are alive."; } } function get_equipment(){ ConnectSQL(SQL_GAME_DB); //Weapon $fields = array( "playerchars.Weapon as wep_id", "items.ItemName as name", "items.ItemDescrip as Description", "items.Attack_Mod as a_mod", "items.Defense_Mod as d_mod", "items.HP_Bonus as hp_mod", "items.Level_Bonus as lvl_mod" ); $fields = implode(" , ", $fields); $q = "Select ".$fields." from `playerchars` LEFT JOIN items ON (playerchars.Weapon = items.ItemID) where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $this->weapon['ID'] = $row['wep_id']; $this->weapon['Name'] = $row['name']; $this->weapon['Description'] = $row['Description']; $this->weapon['Attack_Mod'] = $row['a_mod']; $this->weapon['Defense_Mod'] = $row['d_mod']; $this->weapon['HP_Mod'] = $row['hp_mod']; $this->weapon['Level_Mod'] = $row['lvl_mod']; //Armor $fields = array( "playerchars.Armor as wep_id", "items.ItemName as name", "items.ItemDescrip as Description", "items.Attack_Mod as a_mod", "items.Defense_Mod as d_mod", "items.HP_Bonus as hp_mod", "items.Level_Bonus as lvl_mod" ); $fields = implode(" , ", $fields); $q = "Select ".$fields." from `playerchars` LEFT JOIN items ON (playerchars.Armor = items.ItemID) where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $this->armor['ID'] = $row['wep_id']; $this->armor['Name'] = $row['name']; $this->armor['Description'] = $row['Description']; $this->armor['Attack_Mod'] = $row['a_mod']; $this->armor['Defense_Mod'] = $row['d_mod']; $this->armor['HP_Mod'] = $row['hp_mod']; $this->armor['Level_Mod'] = $row['lvl_mod']; } function equip_wep($wepID){ ConnectSQL(SQL_GAME_DB); $q = "Select ItemID from `inventory` where ItemID = '".$wepID."' and PlayerID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ $q = "Select ItemType from `items` where ItemId = '".$wepID."'"; $r = mysql_query($q) or die(mysql_error()."<br /><Br />".$q); $row = mysql_fetch_assoc($r); if($row['ItemType'] == 1){ $q = "Select Weapon from `playerchars` where CharplyID = '".$this->charid."'"; echo $q."<br /><br />\n"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $old_wep = $row['Weapon']; echo "<Br /><br />OLD WEAPON:".$old_wep."<br /><Br />"; echo "<BR /><Br />".print_r($row)."<br /><br />"; $q = "Update `playerchars` set weapon = 0 where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if($old_wep > 0){ $q = "INSERT INTO `inventory` (ItemID, PlayerID) VALUES('".$old_wep."', '".$this->charid."')"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } $q = "Delete from `inventory` Where ItemID = '".$wepID."' LIMIT 1"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $q = "Update `playerchars` set weapon = '".$wepID."' where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } else{ $this->errors[] = "You can't equip that there."; } } else{ $this->errors[] = "You Don't own that weapon silly."; } } function equip_armor($wepID){ ConnectSQL(SQL_GAME_DB); $q = "Select ItemID from `inventory` where ItemID = '".$wepID."' and PlayerID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ $q = "Select ItemType from `items` where ItemId = '".$wepID."'"; $r = mysql_query($q) or die(mysql_error()."<br /><Br />".$q); $row = mysql_fetch_assoc($r); if($row['ItemType'] == 2){ $q = "Select Armor from `playerchars` where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $row = mysql_fetch_assoc($r); $old_wep = $row['Armor']; $q = "Update `playerchars` set Armor = 0 where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if($old_wep > 0){ $q = "INSERT INTO `inventory` (ItemID, PlayerID) VALUES('".$old_wep."', '".$this->charid."')"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } $q = "Delete from `inventory` Where ItemID = '".$wepID."' LIMIT 1"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $q = "Update `playerchars` set Armor = '".$wepID."' where CharplyID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } else{ $this->errors[] = "You can't equip that there."; } } else{ $this->errors[] = "You Don't own that armor silly."; } } function get_inventory(){ ConnectSQL(SQL_GAME_DB); $fields = array( "items.ItemName as name", "items.ItemDescrip as Description", "items.ItemType as Type", ); $fields = implode(" , ", $fields); $q = "Select ".$fields." from `inventory` LEFT JOIN items ON (inventory.ItemID = items.ItemID) where PlayerID = '".$this->charid."' GROUP BY inventory.ItemsID Order BY items.ItemID"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $this->inventroy = array(); $i = 0; while($row = mysql_fetch_assoc($r)){ $this->inventory[$i]['Name'] = $row['name']; $this->inventory[$i]['Description'] = $row['Description']; switch($row['Type']){ case 1: $this->inventory[$i]['Type'] = "Weapon"; break; case 2: $this->inventory[$i]['Type'] = "Armor"; break; case 3: $this->inventory[$i]['Type'] = "Potion"; break; } $i++; } } } $char = new Char; $char->get_id(1); $char->get_inventory(); $char->get_base_stats(); $char->get_equipment(); print_r($char); ?> All works fine but my question is inside a function inside of my character class can I define another function. For example I want to check if my character is "live" when I equip a new item (cause some items reduce the characters HP) is this possible? I tried just adding inside another function the function I wanted but its undefined which I figured as much. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/ Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 a good example of what I want to do just came up. I added the the function to use a potion. Well a potion adds HP to a character which is a function I already defined. How can I do this (in my Char Class) <?php function use_potion($wepID){ ConnectSQL(SQL_GAME_DB); $q = "Select ItemID from `inventory` where ItemID = '".$wepID."' and PlayerID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ $q = "Select ItemType, HP_Bonus from `items` where ItemId = '".$wepID."'"; $r = mysql_query($q) or die(mysql_error()."<br /><Br />".$q); $row = mysql_fetch_assoc($r); if($row['ItemType'] == 3){ $q = "Delete from `inventory` Where ItemID = '".$wepID."' LIMIT 1"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); #THIS IS THE PART I KNOW IS WRONG BUT DON'T KNOW WHAT IS RIGHT up_hp($row['HP_Bonus']); } else{ $this->Errors[] = "That isn't a potion!"; } } else{ $this->errors[] = "You don't own that!"; } ?> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-574795 Share on other sites More sharing options...
KevinM1 Posted June 26, 2008 Share Posted June 26, 2008 A few things: 1. From a design standpoint, you'll be better served by refactoring some aspects of your character. In particular, their armor and weapon(s) would benefit by being objects themselves. That way, you can simply have methods in your Char class that call methods in your Armor and Weapon classes: <?php class Char { private $armor; private $weapon; . . . public function getArmorHP() { return $this->armor->getHP(); } public function getWeaponAttack() { return $this->weapon->getAttack(); } } class Armor { private $hpMod; . . . public function getHP() { return $this->hpMod; } } class Weapon { private $attackMod; . . . public function getAttack() { return $this->attackMod; } } ?> 2. You can't define a function within another function (as far as I know), but you can invoke another function. Is your use_potion() method defined within your Char class? If so, then you simply need to use: <?php $this->hp_up($row['hp_bonus']); ?> To use it. If use_potion() is a function defined outside of the class, then you'll have to have a Char object to work on: <?php function use_potion($char, $wepId) { . . . $char->hp_up($row['hp_bonus']); . . . } $trogdor_the_burninator = new Char(); use_potion($trogdor_the_burninator, $someWepId); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-574963 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 I defined as arrays inside character object so I don't see a big problem with it. let me try what u said really quick Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575125 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 I tried <?php function use_potion($wepID){ ConnectSQL(SQL_GAME_DB); $q = "Select ItemID from `inventory` where ItemID = '".$wepID."' and PlayerID = '".$this->charid."'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $GLOBALS['q_count'] = $GLOBALS['q_count']+1; if(mysql_num_rows($r) >0){ $q = "Select ItemType, HP_Bonus from `items` where ItemId = '".$wepID."'"; $r = mysql_query($q) or die(mysql_error()."<br /><Br />".$q); $GLOBALS['q_count'] = $GLOBALS['q_count']+1; $row = mysql_fetch_assoc($r); if($row['ItemType'] == 3){ $q = "Delete from `inventory` Where ItemID = '".$wepID."' LIMIT 1"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); $GLOBALS['q_count'] = $GLOBALS['q_count']+1; $this->HP=hp_up($row['HP_Bonus']); } else{ $this->Errors[] = "That isn't a potion!"; } } else{ $this->errors[] = "You don't own that!"; } } ?> which is defined in my char class, but it gives me an unknown function error. I know the issue is in my scope for the function. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575143 Share on other sites More sharing options...
KevinM1 Posted June 26, 2008 Share Posted June 26, 2008 If use_potion() is defined within your Char class, the line in question should simply be: <?php $this->hp_up($row['HP_Bonus']); ?> The hp_up() function already updates the $hp value, from what you showed me, so there's no reason to assign it. Double-check your syntax. You've lost track of capital letters and even function names (up_hp()? or hp_up()?) in the examples you've written. I still think it's better to refactor the armor and weapon. Why? Well, for one thing, a Char object really shouldn't 'know' the inner workings of how armor and weapons are created/used. Yeah, it works, but it ties weapon and armor info (and behavior!) to a character. This tight coupling may pose problems down the road as your system expands. Another reason is flexability. It's much more difficult creating different weapons using an array than an object. Let's use swords as an example: <?php abstract class Sword { abstract public function getDura(); abstract public function getAttack(); abstract public function getParry(); abstract public function getHpMod(); abstract public function getMagic(); } abstract class SwordDecorator extends Sword { protected $sword; public function __construct(Sword $sword) { $this->sword = $sword; } } class ShortSword extends Sword { private $durability = 12; private $attack = 10; private $parry = 15; private $hpMod = 0; private $magic = "none"; public function getDura() { return $this->durability; } public function getAttack() { return $this->attack; } . . . } class Icebrand extends SwordDecorator { public function getDura() { return $this->sword->getDura() * 1.35; } public function getAttack() { return $this->sword->getAttack() * 1.5; } . . . public function getMagic() { return "ice"; } } $IceShortSword = new Icebrand(new ShortSword()); $Icingdeth = new Icebrand(new Scimitar()); $FireDagger = new Flametongue(new Dagger()); $HolyAvenger = new HolySword(new BastardSword()); //etc. ?> By breaking things down into their constituent parts, you make it far easier to increase the lifespan of your application. In an RPG (which I'm assuming your project is), loot is often key. Being able to come up with new items on the fly, like I illustrated above, greatly reduces the amount of grunt work you'll have to do in the long run. Still, it's just an idea. It's obviously your decision. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575251 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 When I "use a potion" it needs to update inventory and update the characters status. The use_potion function takes care of making sure its a valid potion use. if use_potion is successful it also should up the players HP. Does this make more sense I can't do <?php $char->hp_up(50); ?> because I don't know what the HP up is until the potion is "opened" so to speak using use_potion should I take some sort of return out of use_potion and say <?php $char->hp_up(use_potion($itemID)); ?> where the return of use_potion is the HP increase of the potion (or 0 if it is invalid). Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575506 Share on other sites More sharing options...
keeB Posted June 26, 2008 Share Posted June 26, 2008 I think you should have a generic method in your char class called use() Prototyped as follows: <?php class EntityDao { public static function getCharacterById($id) { //creates a inventory object //creates a weapon object //creates a armor object //creates a template user, with user->setHp, user->setMana; // calls user->setInventory($InventoryObject) // calls user->setWeapon($WeaponObject) return user } } // User model object abstract class Entity { private $id; private $totalhp; private $currhp; private $mana; private $inventory; private $weapon; private $armor public static function addHp($num) { //should be defined some other way, but this is for the sake of simplicity return array('hp' => $num); } abstract function use(GameObject $obj) {} } class PlayableCharacter extends Entity { public function use(GameObject $obj) { $vals = $obj->use(); // $vals should be like an AttributeModifier object or something like that, but i am lazy $this->setHp($vals['hp']); $this->setMana($vals['mana']); $this->setSomeOtherModifier($vals['some_bonus']); } } interface GameObject { // define all things game objects must do public function use(); } public class MinorPotion implements GameObject { public function use() { return Entity::addHp(20); } } public class MajorPotion implements GameObject { public function use() { return Entity::addHp(40); } } ////////////// game code //////////////////// $mychar = EntityDao::getCharacterById($id) $majorPotion = MajorPotion(); $mychar->use($majorPotion); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575512 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 I'm just trying to do something simple here and rework it later is what i'm trying to do possible Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575525 Share on other sites More sharing options...
keeB Posted June 26, 2008 Share Posted June 26, 2008 I'm just trying to do something simple here and rework it later is what i'm trying to do possible I understand I am explaining how I'd do it to get you thinking about improvements in the future. You posted while I was posting, I like this idea the most: <?php $char->hp_up(use_potion($itemID)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575533 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 I know I need to rewrite this eventually I just knocked out 200-300 lines off the top of my head the other night when I was commin up with the idea. I think that will work I just need to make it happen. I wish I could just automatically have that return work without have to declare it as such or redeclare what hp_up does in use_potion. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575537 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 getting a called to undefined function doing <?php $char = new Char; $char->get_id(1); $char->get_inventory(); $char->get_base_stats(); $char->get_equipment(); $char->hp_up(use_potion(6)); ?> use_potion + hp_up are both defined in the char class Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575540 Share on other sites More sharing options...
keeB Posted June 26, 2008 Share Posted June 26, 2008 use_potion should return an int, i.e use_potion() returns 10 and hp_up should accept an int. Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575555 Share on other sites More sharing options...
cooldude832 Posted June 26, 2008 Author Share Posted June 26, 2008 Fatal error: Call to undefined function use_potion() in Z:\www\oop.php on line 282 It does the use_potion can't be declared do I need to do $char->hp_up($char->use_potion(6)); that worked:) Quote Link to comment https://forums.phpfreaks.com/topic/111982-first-venture-in-oop-question/#findComment-575556 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.