tiny-essentials 1.21.0 โ 1.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyNeedBar.min.js +1 -0
- package/dist/v1/basics/clock.cjs +127 -11
- package/dist/v1/basics/clock.d.mts +26 -0
- package/dist/v1/basics/clock.mjs +96 -1
- package/dist/v1/build/TinyNeedBar.cjs +7 -0
- package/dist/v1/build/TinyNeedBar.d.mts +3 -0
- package/dist/v1/build/TinyNeedBar.mjs +2 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.mjs +2 -1
- package/dist/v1/libs/TinyNeedBar.cjs +272 -0
- package/dist/v1/libs/TinyNeedBar.d.mts +223 -0
- package/dist/v1/libs/TinyNeedBar.mjs +241 -0
- package/docs/v1/README.md +1 -0
- package/docs/v1/basics/clock.md +51 -11
- package/docs/v1/libs/TinyNeedBar.md +226 -0
- package/package.json +1 -1
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# ๐ ๏ธ TinyNeedBar Documentation
|
|
2
|
+
|
|
3
|
+
`TinyNeedBar` is a utility class for simulating a **"need bar" system**.
|
|
4
|
+
It allows tracking of values that decrease over time, with configurable decay factors.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ๐ฆ Types
|
|
9
|
+
|
|
10
|
+
### `TickResult` ๐
|
|
11
|
+
|
|
12
|
+
Represents the result of one tick of decay.
|
|
13
|
+
|
|
14
|
+
| Property | Type | Description |
|
|
15
|
+
| ------------------- | -------- | ----------------------------------------- |
|
|
16
|
+
| `prevValue` | `number` | Infinite value before applying decay. |
|
|
17
|
+
| `removedTotal` | `number` | Total amount removed this tick. |
|
|
18
|
+
| `removedPercent` | `number` | Percentage of max removed this tick. |
|
|
19
|
+
| `currentPercent` | `number` | Current percentage relative to max. |
|
|
20
|
+
| `remainingValue` | `number` | Current clamped value (โฅ 0). |
|
|
21
|
+
| `infiniteRemaining` | `number` | Current infinite value (can be negative). |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
### `BarFactor` โก
|
|
26
|
+
|
|
27
|
+
Represents a decay factor applied to the need bar.
|
|
28
|
+
|
|
29
|
+
| Property | Type | Description |
|
|
30
|
+
| ------------ | -------- | --------------------------------- |
|
|
31
|
+
| `amount` | `number` | Base reduction value per tick. |
|
|
32
|
+
| `multiplier` | `number` | Multiplier applied to the amount. |
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
### `SerializedData` ๐พ
|
|
37
|
+
|
|
38
|
+
Represents the serialized state of a `TinyNeedBar` instance.
|
|
39
|
+
Can be used to export/import or clone bars.
|
|
40
|
+
|
|
41
|
+
| Property | Type | Description |
|
|
42
|
+
| --------------- | --------------------------- | ------------------------------------------- |
|
|
43
|
+
| `maxValue` | `number` | Maximum value of the bar at serialization. |
|
|
44
|
+
| `currentValue` | `number` | Current clamped value (โฅ 0). |
|
|
45
|
+
| `infiniteValue` | `number` | Infinite value (can be negative). |
|
|
46
|
+
| `factors` | `Record<string, BarFactor>` | Active decay factors indexed by their keys. |
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## ๐๏ธ Class: `TinyNeedBar`
|
|
51
|
+
|
|
52
|
+
### Overview
|
|
53
|
+
|
|
54
|
+
* The bar decreases over time according to **defined factors**.
|
|
55
|
+
* Each factor has an `amount` and a `multiplier`.
|
|
56
|
+
* Tracks two values:
|
|
57
|
+
|
|
58
|
+
* `currentValue` โ cannot go below zero.
|
|
59
|
+
* `infiniteValue` โ can decrease infinitely into negative numbers.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
### Constructor
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
constructor(maxValue = 100, baseDecay = 1, baseDecayMulti = 1)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
* `maxValue` โ Maximum bar value (default `100`)
|
|
70
|
+
* `baseDecay` โ Base decay per tick (default `1`)
|
|
71
|
+
* `baseDecayMulti` โ Multiplier for base decay (default `1`)
|
|
72
|
+
|
|
73
|
+
**Example:**
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
const bar = new TinyNeedBar(100, 2, 1.5);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
### Getters
|
|
82
|
+
|
|
83
|
+
| Getter | Returns | Description |
|
|
84
|
+
| ---------------- | --------------------------- | ----------------------------------------- |
|
|
85
|
+
| `factors` | `Record<string, BarFactor>` | Snapshot of all active factors. |
|
|
86
|
+
| `currentPercent` | `number` | Current percentage relative to max. |
|
|
87
|
+
| `maxValue` | `number` | Maximum possible value of the bar. |
|
|
88
|
+
| `currentValue` | `number` | Current clamped value (โฅ 0). |
|
|
89
|
+
| `infiniteValue` | `number` | Current infinite value (can go negative). |
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
### Setters
|
|
94
|
+
|
|
95
|
+
| Setter | Parameters | Description |
|
|
96
|
+
| --------------- | --------------- | -------------------------------------------------- |
|
|
97
|
+
| `maxValue` | `value: number` | Update max value, clamps `currentValue` if needed. |
|
|
98
|
+
| `infiniteValue` | `value: number` | Update infinite value, auto-adjust `currentValue`. |
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
### Methods
|
|
103
|
+
|
|
104
|
+
#### `getFactor(key: string): BarFactor` ๐
|
|
105
|
+
|
|
106
|
+
Get a specific factor by key.
|
|
107
|
+
Throws an error if the factor does not exist.
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
bar.getFactor("main");
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
#### `hasFactor(key: string): boolean` โ
|
|
116
|
+
|
|
117
|
+
Check if a factor exists by key.
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
bar.hasFactor("main"); // true or false
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
#### `setFactor(key: string, amount: number, multiplier = 1)` โก
|
|
126
|
+
|
|
127
|
+
Add or update a decay factor.
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
bar.setFactor("hunger", 2, 1.2);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
#### `removeFactor(key: string)` โ
|
|
136
|
+
|
|
137
|
+
Remove a factor by key.
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
bar.removeFactor("hunger");
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
#### `tick(): TickResult` โฑ๏ธ
|
|
146
|
+
|
|
147
|
+
Execute one tick of decay using all active factors.
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
const result = bar.tick();
|
|
151
|
+
console.log(result.remainingValue);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
#### `toJSON(): SerializedData` ๐พ
|
|
157
|
+
|
|
158
|
+
Serialize the current state to a JSON-compatible object.
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const data = bar.toJSON();
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
#### `static fromJSON(data: SerializedData): TinyNeedBar` ๐ค
|
|
167
|
+
|
|
168
|
+
Restore a `TinyNeedBar` from serialized data.
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
const newBar = TinyNeedBar.fromJSON(data);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
#### `clone(): TinyNeedBar` ๐งฌ
|
|
177
|
+
|
|
178
|
+
Deep clone the bar.
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
const cloned = bar.clone();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
#### `clearFactors()` ๐งน
|
|
187
|
+
|
|
188
|
+
Remove all factors.
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
bar.clearFactors();
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
### โ
Example Usage
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
const bar = new TinyNeedBar(100, 2, 1.5);
|
|
200
|
+
|
|
201
|
+
// Add extra factors
|
|
202
|
+
bar.setFactor("energy", 1, 1.2);
|
|
203
|
+
bar.setFactor("fun", 0.5, 1);
|
|
204
|
+
|
|
205
|
+
// Tick simulation
|
|
206
|
+
setInterval(() => {
|
|
207
|
+
const result = bar.tick();
|
|
208
|
+
console.log(`Value: ${result.remainingValue} (${result.currentPercent}%)`);
|
|
209
|
+
}, 1000);
|
|
210
|
+
|
|
211
|
+
// Export / Import
|
|
212
|
+
const json = bar.toJSON();
|
|
213
|
+
const restored = TinyNeedBar.fromJSON(json);
|
|
214
|
+
|
|
215
|
+
// Clone
|
|
216
|
+
const clone = bar.clone();
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### ๐จ Notes
|
|
222
|
+
|
|
223
|
+
* Designed for simulation & testing.
|
|
224
|
+
* Can handle multiple independent decay factors.
|
|
225
|
+
* `infiniteValue` is useful for simulations that allow negative overflow.
|
|
226
|
+
* Fully serializable for state saving/loading.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.1",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|