aboutsummaryrefslogtreecommitdiffstats
path: root/inc/Item.inc
blob: 404c46ba7aea104ec5fdb2f045854c6f55b2183b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

require_once("perso.inc");

/**
 * Represent an Item in the shop or in the Inventory.
 *
 * @package inc\Item.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */
class Item {

	/**
	 * @var string  Name of the item.
	 */
	public $name = "";

	/**
	 * @var int  Item's cost.
	 */
	public $cost = 0;

	/**
	 * @var string  Item's icon.
	 */
	public $icon = "";

	/**
	 * @var string  Item's description.
	 */
	public $desc = "";

	/**
	 * @var array  Item's features as an associative array
	 */
	public $feat = array();

	/**
	 * Item's constructor
	 *
	 * @param string $name  Item's name
	 * @param int $cost     Item's cost
	 * @param string $icon  Item's icon
	 * @param string $desc  Item's description
	 * @param array $feat   associative array of strings describing the Item's features
	 * @return void
	 */
	function __construct($name, $cost, $icon, $desc, $feat) {
		$this->name = $name;
		$this->cost = $cost;
		$this->icon = $icon;
		$this->desc = $desc;
		$this->feat = $feat;
	}

	/**
	 * Applies Item's features on the player.
	 *
	 * @return void
	 */
	function consume() {
		foreach($this->feat as $k => $v) {
			switch($k) {
				case "hp": increasePerso("hp", +$v); break;
				case "power": break; // TODO: do something with power
			}
		}
	}

	/**
	 * Generates an XML tree describing the Item
	 *
	 * @param SimpleXMLElement $xml  root XML element to add the Item's property to
	 * @param int $count             numbers of Items
	 * @return void
	 */
	function addToXML($root, $count) {
		$item = $root->addChild("item");
		$item->addChild("name", $this->name);
		$item->addChild("cost", $this->cost);
		$item->addChild("icon", $this->icon);
		$item->addChild("desc", $this->desc);
		$xmlfeat = $item->addChild("feat");
		foreach($this->feat as $k => $v)
			$xmlfeat->addChild($k, $v);
		$item->addChild("count", $count);
	}

	/**
	 * Generates an Item object from an XML tree
	 *
	 * @param SimpleXMLElement $xml  root XML element representing the Item
	 * @return void
	 */
	public static function fromXML($xml) {
		$feats = array();
		foreach($xml->feat[0] as $k => $v)
			$feats[(string)$k] = (string)$v;

		return new static((string)$xml->name, +(string)$xml->cost /* convert to number */, (string)$xml->icon, (string)$xml->desc, $feats); // Call constructor
	}
}

?>