Skip to content

Commit 628b48f

Browse files
committed
Add setMaterial helper method
1 parent a2d4a46 commit 628b48f

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

docs/Entity.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [`getMaterial()`](#getmaterial)
1717
- [`removeAllComponents()`](#removeallcomponents)
1818
- [`removeComponent(componentArg)`](#removecomponentcomponentarg)
19+
- [`setMaterial(materialArg)`](#setmaterialmaterialarg)
1920
- [`toBinary(componentVersions)`](#tobinarycomponentversions)
2021
- [`write(writer, componentVersions)`](#writewriter-componentversions)
2122

@@ -240,6 +241,25 @@ entity.removeComponent('NetworkRigidbody');
240241

241242
---
242243

244+
### `setMaterial(materialArg)`
245+
246+
Sets the entity's physical material. This can change both its appearance and other qualities such as durability, damage, heat retention and weight.
247+
248+
- `materialArg` [`<PhysicalMaterialPartHash | keyof typeof PhysicalMaterialPartHash>`](../src/types/PhysicalMaterialPartHash.ts) The physical material's hash or name to set on the entity.
249+
- Returns: `<this>`
250+
251+
```ts
252+
import { Entity, PhysicalMaterialPartHash } from 'att-string-transcoder';
253+
254+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
255+
256+
entity.setMaterial(PhysicalMaterialPartHash.Mythril);
257+
// or
258+
entity.setMaterial('Mythril');
259+
```
260+
261+
---
262+
243263
### `toBinary(componentVersions)`
244264

245265
Returns a `BinaryString` representation of the entity.

src/Entity.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,60 @@ describe('Entity.removeComponent()', () => {
359359
});
360360
});
361361

362+
describe('Entity.setMaterial()', () => {
363+
describe('when given invalid arguments', () => {
364+
it('throws an error', () => {
365+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
366+
367+
// @ts-expect-error Passing invalid arguments
368+
const expectedToThrow = () => entity.setMaterial();
369+
const expectedError = new Error('You must pass a PhysicalMaterialPartHash to set as the material.');
370+
371+
expect(expectedToThrow).toThrowError(expectedError);
372+
});
373+
});
374+
375+
describe('when given a material hash', () => {
376+
it('sets the given material', () => {
377+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
378+
379+
entity.setMaterial(PhysicalMaterialPartHash.EvinonSteelAlloy);
380+
const materialHash = entity.getMaterial();
381+
382+
expect(materialHash).toStrictEqual(31502);
383+
});
384+
});
385+
386+
describe('when given a material name', () => {
387+
it('sets the given material', () => {
388+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
389+
390+
entity.setMaterial('EvinonSteelAlloy');
391+
const materialHash = entity.getMaterial();
392+
393+
expect(materialHash).toStrictEqual(31502);
394+
});
395+
});
396+
397+
describe('when the entity already has a PhysicalMaterialPart component', () => {
398+
it('sets the given material', () => {
399+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968', {
400+
components: {
401+
PhysicalMaterialPart: new PhysicalMaterialPartComponent({
402+
version: 1,
403+
materialHash: PhysicalMaterialPartHash.Gold
404+
})
405+
}
406+
});
407+
408+
entity.setMaterial(PhysicalMaterialPartHash.EvinonSteelAlloy);
409+
const materialHash = entity.getMaterial();
410+
411+
expect(materialHash).toStrictEqual(31502);
412+
});
413+
});
414+
});
415+
362416
describe('Entity.toBinary()', () => {
363417
it('returns a BinaryString representation of the entity', () => {
364418
const prefabName = 'Torch';

src/Entity.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type { UnknownPrefabComponents } from './types/UnknownPrefabComponents.js
99
import type { UnsupportedPrefabComponents } from './types/UnsupportedPrefabComponents.js';
1010

1111
import { BinaryWriter } from './BinaryWriter.js';
12+
import { FALLBACK_PHYSICAL_MATERIAL_PART_VERSION } from './Prefab.js';
13+
import { PhysicalMaterialPartComponent } from './components/PhysicalMaterialPartComponent.js';
1214
import { ATTPrefabs } from './types/ATTPrefabs.js';
1315
import { ComponentHash } from './types/ComponentHash.js';
1416
import { PhysicalMaterialPartHash } from './types/PhysicalMaterialPartHash.js';
@@ -269,6 +271,40 @@ export class Entity<TPrefabName extends ATTPrefabName> {
269271
return this;
270272
}
271273

274+
/**
275+
* Sets the entity's physical material. This can change both its appearance and other qualities such
276+
* as durability, damage, heat retention and weight.
277+
*
278+
* @since v3.1.0
279+
*
280+
* @example
281+
* import { Entity, PhysicalMaterialPartHash } from 'att-string-transcoder';
282+
*
283+
* const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
284+
*
285+
* entity.setMaterial(PhysicalMaterialPartHash.Mythril);
286+
* // or
287+
* entity.setMaterial('Mythril');
288+
*/
289+
setMaterial(materialHash: PhysicalMaterialPartHash): this;
290+
setMaterial(materialName: keyof typeof PhysicalMaterialPartHash): this;
291+
setMaterial(materialArg: PhysicalMaterialPartHash | keyof typeof PhysicalMaterialPartHash): this {
292+
if (typeof materialArg === 'undefined') {
293+
throw new Error('You must pass a PhysicalMaterialPartHash to set as the material.');
294+
}
295+
296+
const version = this.components.PhysicalMaterialPart?.version ?? FALLBACK_PHYSICAL_MATERIAL_PART_VERSION;
297+
const materialHash = typeof materialArg === 'number' ? materialArg : PhysicalMaterialPartHash[materialArg];
298+
299+
this.components.PhysicalMaterialPart = new PhysicalMaterialPartComponent({
300+
...this.components.PhysicalMaterialPart,
301+
version,
302+
materialHash
303+
});
304+
305+
return this;
306+
}
307+
272308
/**
273309
* Returns a `BinaryString` representation of the entity.
274310
*

src/Prefab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const FALLBACK_LIQUID_CONTAINER_VERSION =
7272
constants.latestSupportedComponentVersions.get(ComponentHash.LiquidContainer) ??
7373
constants.latestLiquidContainerComponentVersion;
7474

75-
const FALLBACK_PHYSICAL_MATERIAL_PART_VERSION =
75+
export const FALLBACK_PHYSICAL_MATERIAL_PART_VERSION =
7676
constants.latestSupportedComponentVersions.get(ComponentHash.PhysicalMaterialPart) ??
7777
constants.latestPhysicalMaterialPartComponentVersion;
7878

0 commit comments

Comments
 (0)