| 1 | <?php |
| 2 | |
| 3 | class NPC |
| 4 | { |
| 5 | private $id; |
| 6 | private $saveId; |
| 7 | private $name; |
| 8 | private $npcType; // 'tribe' or 'individual' |
| 9 | private $isRival; |
| 10 | private $data = []; |
| 11 | |
| 12 | public function __construct($id, $saveId, $name, $npcType, $isRival = false) |
| 13 | { |
| 14 | $this->id = $id; |
| 15 | $this->saveId = $saveId; |
| 16 | $this->name = $name; |
| 17 | $this->npcType = $npcType; |
| 18 | $this->isRival = $isRival; |
| 19 | } |
| 20 | |
| 21 | public static function create($saveId, $name, $npcType, $isRival = false) |
| 22 | { |
| 23 | $id = uniqid('npc_'); |
| 24 | return new self($id, $saveId, $name, $npcType, $isRival); |
| 25 | } |
| 26 | |
| 27 | public static function fromArray($data) |
| 28 | { |
| 29 | $npc = new self( |
| 30 | $data['id'] ?? uniqid('npc_'), |
| 31 | $data['save_id'] ?? null, |
| 32 | $data['name'] ?? '', |
| 33 | $data['npc_type'] ?? 'individual', |
| 34 | $data['is_rival'] ?? false |
| 35 | ); |
| 36 | |
| 37 | if (isset($data['data'])) { |
| 38 | $npc->data = $data['data']; |
| 39 | } |
| 40 | |
| 41 | return $npc; |
| 42 | } |
| 43 | |
| 44 | public function toArray() |
| 45 | { |
| 46 | return [ |
| 47 | 'id' => $this->id, |
| 48 | 'save_id' => $this->saveId, |
| 49 | 'name' => $this->name, |
| 50 | 'npc_type' => $this->npcType, |
| 51 | 'is_rival' => $this->isRival, |
| 52 | 'data' => $this->data |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | public function getId() |
| 57 | { |
| 58 | return $this->id; |
| 59 | } |
| 60 | |
| 61 | public function getName() |
| 62 | { |
| 63 | return $this->name; |
| 64 | } |
| 65 | |
| 66 | public function getNpcType() |
| 67 | { |
| 68 | return $this->npcType; |
| 69 | } |
| 70 | |
| 71 | public function isRival() |
| 72 | { |
| 73 | return $this->isRival; |
| 74 | } |
| 75 | |
| 76 | public function setData($key, $value) |
| 77 | { |
| 78 | $this->data[$key] = $value; |
| 79 | } |
| 80 | |
| 81 | public function getData($key = null) |
| 82 | { |
| 83 | if ($key === null) { |
| 84 | return $this->data; |
| 85 | } |
| 86 | return $this->data[$key] ?? null; |
| 87 | } |
| 88 | |
| 89 | public function save() |
| 90 | { |
| 91 | // NPCs are saved as part of the Tribe's npcs.json |
| 92 | } |
| 93 | } |