UNPKG

@typed-tabletop-simulator/lib

Version:
131 lines (130 loc) 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addAsset = exports.createColor = exports.createTransform = exports.createGuid = exports.createStates = exports.createBaseObject = void 0; const math_1 = require("../math"); const createBaseObject = (properties, type) => { return { GUID: properties.guid ?? (0, exports.createGuid)(), Name: type, Nickname: properties.name, Description: properties.description, GMNotes: properties.gmNotes, Memo: properties.memo, Tags: properties.tags, Transform: (0, exports.createTransform)(properties), Locked: properties.locked, ColorDiffuse: properties.color ? (0, exports.createColor)(properties.color) : undefined, AltLookAngle: properties.viewAngle, Tooltip: properties.tooltip, CustomUIAssets: createAssets(properties), AttachedSnapPoints: createSnapPoints(properties), LuaScript: properties.script, XmlUI: properties.ui, }; }; exports.createBaseObject = createBaseObject; /** * Creates a stated object from the given object data. * * @param objects The list of objects to add as states. The order in this list will represent the state numbers. * @param startingState The index of `objects` of the object which should be the current object. Defaults to the first entry. */ const createStates = (objects, startingState = 0) => { const baseObject = objects[startingState]; baseObject.States = {}; for (let i = 0; i < objects.length; i++) { if (i === startingState) { continue; } const stateId = i + 1; baseObject.States[stateId] = objects[i]; } return baseObject; }; exports.createStates = createStates; const createGuid = () => { return "123456"; }; exports.createGuid = createGuid; const createTransform = (properties) => { const position = properties.position || { x: 0, y: 0, z: 0 }; const rotation = properties.rotation || { x: 0, y: 0, z: 0 }; let scale = properties.scale || 1; if (typeof scale === "number") { scale = { x: scale, y: scale, z: scale }; } return { posX: position.x, posY: position.y, posZ: position.z, rotX: rotation.x, rotY: rotation.y, rotZ: rotation.z, scaleX: scale.x, scaleY: scale.y, scaleZ: scale.z, }; }; exports.createTransform = createTransform; const createColor = (color) => { if (typeof color === "string") { color = color.replace("#", ""); const [r, g, b] = [color.slice(0, 2), color.slice(2, 4), color.slice(4, 6)]; const a = color.length > 6 ? color.slice(6) : undefined; color = { r: parseInt(r, 16), g: parseInt(g, 16), b: parseInt(b, 16), a: a ? parseInt(a, 16) : undefined }; } if (color.r > 1 || color.g > 1 || color.b > 1) { const adjust = (value) => math_1.default.round(value / 255, 4); return { r: adjust(color.r), g: adjust(color.g), b: adjust(color.b), a: color.a ? adjust(color.a) : undefined, }; } return color; }; exports.createColor = createColor; const addAsset = (object, asset) => { object.CustomUIAssets ??= []; const newAsset = createAsset(asset); if (!object.CustomUIAssets.find((a) => a.Name === newAsset.Name || a.URL === newAsset.URL)) { object.CustomUIAssets.push(newAsset); } }; exports.addAsset = addAsset; const createSnapPoints = (properties) => { if (!properties.snapPoints) { return undefined; } return properties.snapPoints.map((s) => createSnapPoint(s)); }; const createSnapPoint = (snapPoint) => { if ("position" in snapPoint) { return { Position: { ...snapPoint.position }, Rotation: snapPoint.rotation ? { ...snapPoint.rotation } : undefined, Tags: snapPoint.tags, }; } return { Position: snapPoint, }; }; const createAssets = (properties) => { return properties.assets?.map((a) => createAsset(a)); }; const createAsset = (asset) => { if ("image" in asset) { return { Type: 0, Name: asset.name, URL: asset.image, }; } return { Type: 1, Name: asset.name, URL: asset.assetBundle, }; };