diff --git a/src/__tests__/materials.test.ts b/src/__tests__/materials.test.ts new file mode 100644 index 0000000..383b04e --- /dev/null +++ b/src/__tests__/materials.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, test } from "bun:test"; +import { + BEHAVIOR_GAS, + BEHAVIOR_LIQUID, + BEHAVIOR_POWDER, + BEHAVIOR_SOLID, + type Material, +} from "../materials/types"; + +describe("material types", () => { + test("behavior type constants are distinct integers 0-3", () => { + const behaviors = [ + BEHAVIOR_POWDER, + BEHAVIOR_LIQUID, + BEHAVIOR_GAS, + BEHAVIOR_SOLID, + ]; + expect(new Set(behaviors).size).toBe(4); + for (const b of behaviors) { + expect(b).toBeGreaterThanOrEqual(0); + expect(b).toBeLessThanOrEqual(3); + } + }); + + test("Material interface has required fields", () => { + const sand: Material = { + id: 1, + name: "sand", + behavior: BEHAVIOR_POWDER, + density: 1.5, + color: [0.76, 0.7, 0.5, 1.0], + hardness: 0.1, + angleOfRepose: 34, + }; + expect(sand.behavior).toBe(BEHAVIOR_POWDER); + expect(sand.density).toBe(1.5); + }); +}); diff --git a/src/materials/types.ts b/src/materials/types.ts new file mode 100644 index 0000000..f26413a --- /dev/null +++ b/src/materials/types.ts @@ -0,0 +1,23 @@ +export const BEHAVIOR_POWDER = 0; +export const BEHAVIOR_LIQUID = 1; +export const BEHAVIOR_GAS = 2; +export const BEHAVIOR_SOLID = 3; + +export type BehaviorType = + | typeof BEHAVIOR_POWDER + | typeof BEHAVIOR_LIQUID + | typeof BEHAVIOR_GAS + | typeof BEHAVIOR_SOLID; + +// RGBA color, each component 0-1 +export type Color4 = [number, number, number, number]; + +export interface Material { + id: number; // 0-255, index into lookup texture + name: string; + behavior: BehaviorType; + density: number; // relative weight, determines displacement order + color: Color4; + hardness: number; // 0-1, resistance to ant digging / degradation + angleOfRepose: number; // degrees, used in tier 2 gravity +}