Last active
March 7, 2021 06:41
-
-
Save diazvictor/7d248f8034c83e7c8d14871aa421436f to your computer and use it in GitHub Desktop.
Small Example Of A Game Inventory With PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This example is a port of: https://gist.github.com/diazvictor/6fe3372bce79587a3c21123a19881cb1 | |
// I create the Inventory class | |
class Inventory { | |
// What to do when the class is initialized | |
public function __construct() { | |
$this->items = array(); | |
} | |
/** | |
* I look at what I carry in my inventory | |
* @returns {Boolean} true or false if there are items | |
*/ | |
public function getItems() { | |
if ($this->items[0]) { | |
echo "You carry:\n"; | |
foreach ($this->items as $inventory) { | |
echo "* {$inventory["desc"]}\n"; | |
} | |
return true; | |
} else { | |
echo "You have nothing."; | |
return false; | |
} | |
} | |
/** | |
* I check if an item exists | |
* @param {String} item name | |
* @returns {Boolean} true or false if the item exists | |
*/ | |
public function hasItem ($name) { | |
foreach ($this->items as $inventory) { | |
if ($name == $inventory["name"]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* I add an item to the inventory | |
* @param {String} item name | |
* @param {String} item description | |
* @returns {Number} the number of items in the inventory | |
*/ | |
public function addItem ($name, $desc) { | |
array_push($this->items, array( | |
"name" => $name, | |
"desc" => $desc | |
)); | |
return count($this->items); | |
} | |
/** | |
* I remove an item from inventory | |
* @param {String} item name | |
* @returns {Boolean} true or false if the item has been removed successfully | |
*/ | |
public function removeItem ($name) { | |
foreach ($this->items as $i => $inventory) { | |
if ($name == $inventory["name"]) { | |
unset($this->items[$i]); | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
class BackPack extends Inventory { | |
/** | |
* I add an item to the inventory but first I check if the limit has not | |
* been reached. | |
* @param {String} item name | |
* @param {String} item description | |
* @returns {Number} the number of items in the inventory | |
*/ | |
public function addItem ($name, $desc) { | |
$this->size = 10; | |
if (count($this->items) >= $this->size) { | |
echo "backpack is full"; | |
return false; | |
} else { | |
parent::addItem($name, $desc); | |
return true; | |
} | |
} | |
} | |
$player = new BackPack(); | |
$player->addItem("lantern", "an oil lantern"); | |
$player->addItem("camera", "a camera, it seems to be discharged"); | |
$player->addItem("sword", "a sword with the blessing of the wind"); | |
$player->getItems(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment