Skip to content

Commit a2d4a46

Browse files
committed
Add getMaterial helper method
1 parent 15b7270 commit a2d4a46

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

docs/Entity.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [`components`](#components)
1414
- [Methods](#methods)
1515
- [`addComponent(component)`](#addcomponentcomponent)
16+
- [`getMaterial()`](#getmaterial)
1617
- [`removeAllComponents()`](#removeallcomponents)
1718
- [`removeComponent(componentArg)`](#removecomponentcomponentarg)
1819
- [`toBinary(componentVersions)`](#tobinarycomponentversions)
@@ -187,6 +188,23 @@ entity.addComponent(component);
187188

188189
---
189190

191+
### `getMaterial()`
192+
193+
Gets the entity's physical material.
194+
195+
- Returns: [`<PhysicalMaterialPartHash>`](../src/types/PhysicalMaterialPartHash.ts)
196+
197+
```ts
198+
import { Entity, PhysicalMaterialPartHash } from 'att-string-transcoder';
199+
200+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
201+
202+
const materialHash = entity.getMaterial();
203+
const materialName = PhysicalMaterialPartHash[materialHash];
204+
```
205+
206+
---
207+
190208
### `removeAllComponents()`
191209

192210
Removes all components on this entity.

src/Entity.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { NetworkRigidbodyComponent } from './components/NetworkRigidbodyComponen
1010
import { PhysicalMaterialPartComponent } from './components/PhysicalMaterialPartComponent.js';
1111
import { UnsupportedComponent } from './components/UnsupportedComponent.js';
1212
import { ComponentHash } from './types/ComponentHash.js';
13+
import { PhysicalMaterialPartHash } from './types/PhysicalMaterialPartHash.js';
1314

1415
describe('new Entity()', () => {
1516
describe('when given the required props', () => {
@@ -175,6 +176,36 @@ describe('Entity.fromBinary()', () => {
175176
});
176177
});
177178

179+
describe('Entity.getMaterial()', () => {
180+
describe('when the entity has no PhysicalMaterialPart component', () => {
181+
it('returns materialHash of zero', () => {
182+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
183+
delete entity.components.PhysicalMaterialPart;
184+
185+
const materialHash = entity.getMaterial();
186+
187+
expect(materialHash).toStrictEqual(0);
188+
});
189+
});
190+
191+
describe('when the prefab has a PhysicalMaterialPart component with materialHash', () => {
192+
it('returns the stored isKinematic', () => {
193+
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968', {
194+
components: {
195+
PhysicalMaterialPart: new PhysicalMaterialPartComponent({
196+
version: 1,
197+
materialHash: PhysicalMaterialPartHash.EvinonSteelAlloy
198+
})
199+
}
200+
});
201+
202+
const materialHash = entity.getMaterial();
203+
204+
expect(materialHash).toStrictEqual(31502);
205+
});
206+
});
207+
});
208+
178209
describe('Entity.removeAllComponents()', () => {
179210
it('removes all components from the entity', () => {
180211
const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968', {

src/Entity.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { UnsupportedPrefabComponents } from './types/UnsupportedPrefabCompo
1111
import { BinaryWriter } from './BinaryWriter.js';
1212
import { ATTPrefabs } from './types/ATTPrefabs.js';
1313
import { ComponentHash } from './types/ComponentHash.js';
14+
import { PhysicalMaterialPartHash } from './types/PhysicalMaterialPartHash.js';
1415
import { readComponents } from './utils/readComponents.js';
1516
import { writeComponents } from './utils/writeComponents.js';
1617

@@ -184,6 +185,23 @@ export class Entity<TPrefabName extends ATTPrefabName> {
184185
});
185186
}
186187

188+
/**
189+
* Gets the entity's physical material.
190+
*
191+
* @since v3.1.0
192+
*
193+
* @example
194+
* import { Entity, PhysicalMaterialPartHash } from 'att-string-transcoder';
195+
*
196+
* const entity = new Entity<'Standard_Side_Pouch_Attachment'>('standard_sidePouch_backPin_L1_7968');
197+
*
198+
* const materialHash = entity.getMaterial();
199+
* const materialName = PhysicalMaterialPartHash[materialHash];
200+
*/
201+
getMaterial(): PhysicalMaterialPartHash {
202+
return this.components.PhysicalMaterialPart?.materialHash ?? 0;
203+
}
204+
187205
/**
188206
* Removes all components on this entity.
189207
*

0 commit comments

Comments
 (0)