html-tower/static/transform.js

48 lines
1.4 KiB
JavaScript

console.log("loading transform system");
const transform = (engine) => (
entity,
transformData = { x: {x: 1, y:0}, y: {x:0, y:1}, o: {x:0,y:0}}
) => {
let {x, y, o} = transformData;
let transform = [x.x, x.y, y.x, y.y, o.x, o.y];
const updateEntity = () => {
entity.dataset.transform = JSON.stringify (transform);
};
// these may be pulled out in the future into a separate renderer
const setEntityStyleFromTransform = (entity) => {
entity.style.transform =
`matrix(${entity.dataset.transform.replace(/\[|\]/g,'')})`;
};
const renderEntity = () => setEntityStyleFromTransform (entity);
return (message, ...messageArgs) => {
switch (message) {
case "origin": {
return [transform[4], transform[5]];
}
case "x": {
return [transform[0], transform[1]];
}
case "y": {
return [transform[2], transform [3]];
}
case "translate": {
let [xTranslation, yTranslation] = messageArgs [0];
transform [4] += xTranslation;
transform [5] += yTranslation;
break;
}
}
updateEntity ();
renderEntity ();
};
};
const initTransformSystem = (engine) => {
engine("registerComponentType", "transform", transform);
};
// transform needs to actually provide transformation utilities