|
| 1 | +import { createString } from './createString'; |
| 2 | +import { Prefab } from './decoders'; |
| 3 | +import { PrefabHash } from './PrefabHash'; |
| 4 | +import { PrefabSlot } from './PrefabSlot'; |
| 5 | + |
| 6 | +export type PrefabFactory = { |
| 7 | + prefab: Prefab; |
| 8 | + slots: { [slotName: string]: number }; |
| 9 | + setPosition: (x: number, y: number, z: number) => PrefabFactory; |
| 10 | + setRotation: (x: number, y: number, z: number, w: number) => PrefabFactory; |
| 11 | + setKinematic: (isKinematic?: boolean) => PrefabFactory; |
| 12 | + setServerSleeping: (isServerSleeping?: boolean) => PrefabFactory; |
| 13 | + setVelocity: (x: number, y: number, z: number) => PrefabFactory; |
| 14 | + setAngularVelocity: (x: number, y: number, z: number) => PrefabFactory; |
| 15 | + setMaterial: (materialHash: number) => PrefabFactory; |
| 16 | + setIntegrity: (integrity: number) => PrefabFactory; |
| 17 | + setOnFire: (isLit?: boolean) => PrefabFactory; |
| 18 | + useSlot: (slotHash: number, childPrefab: PrefabFactory) => PrefabFactory; |
| 19 | + toString: () => string; |
| 20 | + print: () => void; |
| 21 | +}; |
| 22 | + |
| 23 | +const availableSlots = (prefabObjectHash: number) => { |
| 24 | + const prefabName = PrefabHash[prefabObjectHash]; |
| 25 | + const slots = PrefabSlot[prefabName as keyof typeof PrefabSlot] ?? {}; |
| 26 | + const filteredEntries = Object.entries(slots).filter(([key]) => key.startsWith('Slot_')); |
| 27 | + |
| 28 | + return Object.fromEntries(filteredEntries); |
| 29 | +}; |
| 30 | + |
| 31 | +export const createPrefab = (prefabObjectHash: number): PrefabFactory => ({ |
| 32 | + prefab: { |
| 33 | + prefabObject: { hash: prefabObjectHash }, |
| 34 | + components: {}, |
| 35 | + embeddedEntities: {}, |
| 36 | + childPrefabs: [] |
| 37 | + }, |
| 38 | + |
| 39 | + slots: availableSlots(prefabObjectHash), |
| 40 | + |
| 41 | + setPosition(x, y, z) { |
| 42 | + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { |
| 43 | + throw new Error(`setPosition called with invalid arguments.\n\nUsage: .setPosition(x, y, z)`); |
| 44 | + } |
| 45 | + |
| 46 | + const position = { x, y, z }; |
| 47 | + |
| 48 | + this.prefab.prefabObject.position = position; |
| 49 | + this.prefab.components!.NetworkRigidbody = { |
| 50 | + ...this.prefab.components!.NetworkRigidbody, |
| 51 | + position |
| 52 | + }; |
| 53 | + |
| 54 | + return this; |
| 55 | + }, |
| 56 | + |
| 57 | + setRotation(x, y, z, w) { |
| 58 | + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined' || typeof w === 'undefined') { |
| 59 | + throw new Error(`setRotation called with invalid arguments.\n\nUsage: .setRotation(x, y, z, w)`); |
| 60 | + } |
| 61 | + |
| 62 | + const rotation = { x, y, z, w }; |
| 63 | + |
| 64 | + this.prefab.prefabObject.rotation = rotation; |
| 65 | + this.prefab.components!.NetworkRigidbody = { |
| 66 | + ...this.prefab.components!.NetworkRigidbody, |
| 67 | + rotation |
| 68 | + }; |
| 69 | + |
| 70 | + return this; |
| 71 | + }, |
| 72 | + |
| 73 | + setKinematic(isKinematic = true) { |
| 74 | + this.prefab.components!.NetworkRigidbody = { |
| 75 | + ...this.prefab.components!.NetworkRigidbody, |
| 76 | + isKinematic |
| 77 | + }; |
| 78 | + |
| 79 | + return this; |
| 80 | + }, |
| 81 | + |
| 82 | + setServerSleeping(isServerSleeping = true) { |
| 83 | + this.prefab.components!.NetworkRigidbody = { |
| 84 | + ...this.prefab.components!.NetworkRigidbody, |
| 85 | + isServerSleeping |
| 86 | + }; |
| 87 | + |
| 88 | + return this; |
| 89 | + }, |
| 90 | + |
| 91 | + setVelocity(x, y, z) { |
| 92 | + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { |
| 93 | + throw new Error(`setVelocity called with invalid arguments.\n\nUsage: .setVelocity(x, y, z)`); |
| 94 | + } |
| 95 | + |
| 96 | + const velocity = { x, y, z }; |
| 97 | + |
| 98 | + this.prefab.components!.NetworkRigidbody = { |
| 99 | + ...this.prefab.components!.NetworkRigidbody, |
| 100 | + velocity |
| 101 | + }; |
| 102 | + |
| 103 | + return this; |
| 104 | + }, |
| 105 | + |
| 106 | + setAngularVelocity(x, y, z) { |
| 107 | + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { |
| 108 | + throw new Error(`setAngularVelocity called with invalid arguments.\n\nUsage: .setAngularVelocity(x, y, z)`); |
| 109 | + } |
| 110 | + |
| 111 | + const angularVelocity = { x, y, z }; |
| 112 | + |
| 113 | + this.prefab.components!.NetworkRigidbody = { |
| 114 | + ...this.prefab.components!.NetworkRigidbody, |
| 115 | + angularVelocity |
| 116 | + }; |
| 117 | + |
| 118 | + return this; |
| 119 | + }, |
| 120 | + |
| 121 | + setMaterial(materialHash) { |
| 122 | + if (typeof materialHash === 'undefined') { |
| 123 | + throw new Error(`setMaterial called with invalid arguments.\n\nUsage: .setMaterial(materialHash)`); |
| 124 | + } |
| 125 | + |
| 126 | + this.prefab.components = { |
| 127 | + ...this.prefab.components, |
| 128 | + PhysicalMaterialPart: { materialHash } |
| 129 | + }; |
| 130 | + |
| 131 | + return this; |
| 132 | + }, |
| 133 | + |
| 134 | + setIntegrity(integrity) { |
| 135 | + if (typeof integrity === 'undefined') { |
| 136 | + throw new Error(`setIntegrity called with invalid arguments.\n\nUsage: .setIntegrity(integrity)`); |
| 137 | + } |
| 138 | + |
| 139 | + this.prefab.components = { |
| 140 | + ...this.prefab.components, |
| 141 | + DurabilityModule: { integrity } |
| 142 | + }; |
| 143 | + |
| 144 | + return this; |
| 145 | + }, |
| 146 | + |
| 147 | + setOnFire(isLit = true) { |
| 148 | + this.prefab.embeddedEntities!.Fire = { |
| 149 | + ...this.prefab.embeddedEntities!.Fire, |
| 150 | + isAlive: isLit, |
| 151 | + components: { |
| 152 | + ...this.prefab.embeddedEntities!.Fire!.components, |
| 153 | + HeatSourceBase: { |
| 154 | + ...this.prefab.embeddedEntities!.Fire!.components!.HeatSourceBase, |
| 155 | + isLit |
| 156 | + } |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + return this; |
| 161 | + }, |
| 162 | + |
| 163 | + useSlot(slotHash, childPrefab) { |
| 164 | + if (typeof slotHash === 'undefined' || typeof childPrefab === 'undefined') { |
| 165 | + throw new Error(`useSlot called with invalid arguments.\n\nUsage: .useSlot(slotHash, prefab)`); |
| 166 | + } |
| 167 | + |
| 168 | + const validHashes = Object.values(this.slots); |
| 169 | + |
| 170 | + if (!validHashes.includes(slotHash)) { |
| 171 | + const prefabName = PrefabHash[prefabObjectHash]; |
| 172 | + |
| 173 | + throw new Error( |
| 174 | + `useSlot called with invalid slot hash.\n\n${prefabName} has ${ |
| 175 | + validHashes.length ? `these valid slots: ${JSON.stringify(this.slots, null, 2)}` : 'no available slots.' |
| 176 | + }` |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + this.prefab.childPrefabs = [ |
| 181 | + ...this.prefab.childPrefabs!.filter(({ parentHash }) => parentHash !== slotHash), |
| 182 | + { |
| 183 | + parentHash: slotHash, |
| 184 | + prefab: { ...childPrefab.prefab } |
| 185 | + } |
| 186 | + ]; |
| 187 | + |
| 188 | + return this; |
| 189 | + }, |
| 190 | + |
| 191 | + toString() { |
| 192 | + return createString(this.prefab); |
| 193 | + }, |
| 194 | + |
| 195 | + print() { |
| 196 | + const string = this.toString(); |
| 197 | + |
| 198 | + return console.log(string); |
| 199 | + } |
| 200 | +}); |
0 commit comments