aboutsummaryrefslogtreecommitdiffstats
path: root/inc/Item.inc
blob: a8ee302cb2d0a0e7f31690d51bef8a70eadf08e1 (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
<?php

require_once("perso.inc");

class Item {
	public $name = "";
	public $cost = 0;
	public $icon = "";
	public $desc = "";
	public $feat = array();

	function __construct($name, $cost, $icon, $desc, $feat) {
		$this->name = $name;
		$this->cost = $cost;
		$this->icon = $icon;
		$this->desc = $desc;
		$this->feat = $feat;
	}

	function consume() {
		foreach($this->feat as $k => $v) {
			switch($k) {
				case "hp": increasePerso("hp", +$v); break;
				case "power": increasePerso("bonusPower", +$v); break;
			}
		}
	}

	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);
	}

	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);
	}
}

?>