|
1 | 1 | import { Prefab } from './Prefab';
|
2 | 2 | import { createString } from './createString';
|
3 | 3 | import { PrefabData } from './decoders';
|
4 |
| -import { PhysicalMaterialPartHash } from '.'; |
| 4 | +import { PhysicalMaterialPartHash } from './PhysicalMaterialPartHash'; |
| 5 | +import * as components from './components'; |
| 6 | +import { reasonableGifts } from './utils'; |
5 | 7 |
|
6 | 8 | const VALID_MATERIALS = Object.values(PhysicalMaterialPartHash)
|
7 | 9 | .filter(key => typeof key === 'string')
|
@@ -32,6 +34,8 @@ type PrefabManager<S> = {
|
32 | 34 | setIntegrity: (integrity: number) => PrefabManager<S>;
|
33 | 35 | setServings: (servings: number) => PrefabManager<S>;
|
34 | 36 | setOnFire: (isLit?: boolean) => PrefabManager<S>;
|
| 37 | + setGiftBoxLabel: (label: string) => PrefabManager<S>; |
| 38 | + addGift: <P extends Prefab, C extends keyof P['slots']>(gift: PrefabManager<C>) => PrefabManager<S>; |
35 | 39 | useSlot: <P extends Prefab, C extends keyof P['slots']>(slot: S, childPrefab: PrefabManager<C>) => PrefabManager<S>;
|
36 | 40 | toString: () => string;
|
37 | 41 | print: () => void;
|
@@ -202,6 +206,48 @@ export const createPrefab = <P extends Prefab, S extends keyof P['slots']>(prefa
|
202 | 206 | return this;
|
203 | 207 | },
|
204 | 208 |
|
| 209 | + setGiftBoxLabel(label) { |
| 210 | + this.data.components = { |
| 211 | + ...this.data.components, |
| 212 | + SentGift: { |
| 213 | + ...this.data.components!.SentGift, |
| 214 | + senderName: label |
| 215 | + } |
| 216 | + }; |
| 217 | + |
| 218 | + return this; |
| 219 | + }, |
| 220 | + |
| 221 | + addGift(giftPrefab) { |
| 222 | + if (!reasonableGifts.includes(giftPrefab.data.prefabObject.hash)) { |
| 223 | + throw new Error('No gifts for naughty people.'); |
| 224 | + } |
| 225 | + |
| 226 | + const string = createString(giftPrefab.data); |
| 227 | + |
| 228 | + const [dataString, versionsString] = string.split('|'); |
| 229 | + const [hash, messageSizeInBytes, ...data] = dataString.split(',').map(Number); |
| 230 | + const [_, ...chunkVersioning] = versionsString.split(',').map(Number); |
| 231 | + |
| 232 | + this.data.components = { |
| 233 | + ...this.data.components, |
| 234 | + SentGift: { |
| 235 | + ...this.data.components!.SentGift, |
| 236 | + gifts: [ |
| 237 | + ...((this.data.components!.SentGift as components.SentGift | undefined)?.gifts ?? []), |
| 238 | + { |
| 239 | + data, |
| 240 | + messageSizeInBytes, |
| 241 | + hash, |
| 242 | + chunkVersioning |
| 243 | + } |
| 244 | + ] |
| 245 | + } |
| 246 | + }; |
| 247 | + |
| 248 | + return this; |
| 249 | + }, |
| 250 | + |
205 | 251 | useSlot(slotName, childPrefab) {
|
206 | 252 | if (typeof slotName === 'undefined' || typeof childPrefab === 'undefined') {
|
207 | 253 | throw new Error(`useSlot(slot, prefab) called with invalid arguments.`);
|
|
0 commit comments