BlueGit

blue-star tribegen

Files

tribegen / web / src / Classes / NPC.php

NPC.php

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