-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHasInventory.php
180 lines (157 loc) · 4.83 KB
/
HasInventory.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace JaxWilko\Game\Classes\Engine\Core\Objects\Traits;
use JaxWilko\Game\Classes\Engine\Core\Objects\Generic\GenericItemObject;
use JaxWilko\Game\Classes\Engine\Core\Providers\ItemDataProvider;
use JaxWilko\Game\Classes\Engine\Core\Utils\Vector;
use JaxWilko\Game\Classes\Engine\Engine;
use JaxWilko\Game\Classes\Engine\Modules\World\Level;
/**
* @trait HasInventory
* Provides inventory support to `WorldObjects`
*/
trait HasInventory
{
/**
* @var array the objects current inventory
*/
protected array $inventory = [];
/**
* Returns the objects current inventory
*
* @return array
*/
public function getInventory(): array
{
return $this->inventory;
}
/**
* Returns whether the object has an item in its inventory, and the has a specific quantity if specified
*
* @param string $code
* @param int|null $quantity
* @return bool
*/
public function hasInventoryItem(string $code, ?int $quantity = null): bool
{
if (is_null($quantity)) {
return isset($this->inventory[$code]);
}
return isset($this->inventory[$code]) && $this->inventory[$code]['quantity'] >= $quantity;
}
/**
* Adds X amount of an item to the inventory
*
* @param string $code
* @param int $quantity
* @return $this
*/
public function addInventoryItem(string $code, int $quantity = 1): static
{
if (!isset($this->inventory[$code])) {
if (!Engine::getProvider(ItemDataProvider::class)->isItem($code)) {
return $this;
}
$this->inventory[$code] = Engine::getProvider(ItemDataProvider::class)->getItem($code);
$this->inventory[$code]['quantity'] = 0;
}
$this->inventory[$code]['quantity'] += $quantity;
return $this;
}
/**
* Removes X amount of an item to the inventory
*
* @param string $code
* @param int $quantity
* @return $this
*/
public function removeInventoryItem(string $code, int $quantity = 1): static
{
$this->inventory[$code]['quantity'] -= $quantity;
if ($this->inventory[$code]['quantity'] <= 0) {
unset($this->inventory[$code]);
}
return $this;
}
/**
* Triggers the usage of an item if it has one
*
* @param string $code
* @return $this
*/
public function useInventoryItem(string $code): static
{
if (!$this->hasInventoryItem($code)) {
return $this;
}
if (!($callable = Engine::getProvider(ItemDataProvider::class)->getItemUse($code))) {
return $this;
}
$callable($this);
return $this;
}
/**
* Drops an inventory item, creating a new object in the level
*
* @param Level $level
* @param string $code
* @return $this
*/
public function dropInventoryItem(Level $level, string $code): static
{
if (!$this->hasInventoryItem($code)) {
return $this;
}
$size = new Vector(32, 32);
$level->pushLayer(
$level::LAYER_TRIGGERS,
new GenericItemObject(
$level->getSpawnablePosition($this->getSurroundingArea(0.7), $size) ?? $this->vector->clone(),
$size,
[
'code' => $code,
'quantity' => 1
]
)
);
// Remove the items from the inventory
$this->removeInventoryItem($code, 1);
return $this;
}
/**
* Drops all items in the inventory to the level, uses a loot table if the object implements one
*
* @param Level $level
* @return $this
*/
public function dropInventory(Level $level): static
{
if (
$this->lootTable
&& ($items = Engine::getProvider(ItemDataProvider::class)->getLootTable($this->lootTable))
) {
foreach ($items as $code => $chance) {
if (rand(1, 100) < ($chance * 100)) {
$this->addInventoryItem($code);
}
}
}
$size = new Vector(32, 32);
foreach ($this->inventory as $code => $item) {
// Create level objects
$level->pushLayer(
$level::LAYER_TRIGGERS,
new GenericItemObject(
$level->getSpawnablePosition($this->getSurroundingArea(0.7), $size) ?? $this->vector->clone(),
$size->clone(),
[
'code' => $code,
'quantity' => $item['quantity']
]
)
);
// Remove the items from the inventory
$this->removeInventoryItem($code, $item['quantity']);
}
return $this;
}
}