Add material type definitions

This commit is contained in:
Jared Miller 2026-03-11 13:20:32 -04:00
parent 4457368636
commit dd27634f0c
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 61 additions and 0 deletions

View file

@ -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);
});
});

23
src/materials/types.ts Normal file
View file

@ -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
}