feat: camera rotation

This commit is contained in:
cha0s 2021-03-15 05:15:39 -05:00
parent a954084f1d
commit 10c7d675aa

View File

@ -8,6 +8,7 @@ import {useRoom, useSelfEntity} from '@humus/core';
import {PixiComponent, useApp} from '@inlet/react-pixi'; import {PixiComponent, useApp} from '@inlet/react-pixi';
const container = new Container(); const container = new Container();
const VIEWPORT = [320, 180];
const Component = PixiComponent('RoomView', { const Component = PixiComponent('RoomView', {
create: () => container.internal, create: () => container.internal,
@ -22,8 +23,11 @@ const Component = PixiComponent('RoomView', {
} }
const [roomView] = container.children; const [roomView] = container.children;
if (roomView) { if (roomView) {
[roomView.x, roomView.y] = Vector.scale(newProps.offset, -1); const halfViewport = Vector.scale(VIEWPORT, 0.5);
roomView.pivot = Vector.sub(halfViewport, Vector.scale(newProps.offset, -1));
[roomView.x, roomView.y] = halfViewport;
[roomView.scaleX, roomView.scaleY] = newProps.scale; [roomView.scaleX, roomView.scaleY] = newProps.scale;
roomView.rotation = newProps.rotation;
} }
}, },
}); });
@ -41,6 +45,7 @@ const RoomViewComponent = () => {
const selfEntity = useSelfEntity(); const selfEntity = useSelfEntity();
const [offset, setOffset] = useState([0, 0]); const [offset, setOffset] = useState([0, 0]);
const [scale, setScale] = useState([1, 1]); const [scale, setScale] = useState([1, 1]);
const [rotation, setRotation] = useState(0);
const room = useRoom(); const room = useRoom();
useEffect(() => { useEffect(() => {
if (!room || !selfEntity) { if (!room || !selfEntity) {
@ -50,12 +55,17 @@ const RoomViewComponent = () => {
const onCameraRealOffsetChanged = () => { const onCameraRealOffsetChanged = () => {
setOffset(selfEntity.camera.realOffset); setOffset(selfEntity.camera.realOffset);
}; };
const onCameraRotationChanged = () => {
setRotation(selfEntity.camera.rotation);
};
const onCameraScaleChanged = () => { const onCameraScaleChanged = () => {
setScale(selfEntity.camera.scale); setScale(selfEntity.camera.scale);
}; };
onCameraRealOffsetChanged(); onCameraRealOffsetChanged();
onCameraRotationChanged();
onCameraScaleChanged(); onCameraScaleChanged();
selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged); selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged);
selfEntity.camera.on('rotationChanged', onCameraRotationChanged);
selfEntity.camera.on('scaleChanged', onCameraScaleChanged); selfEntity.camera.on('scaleChanged', onCameraScaleChanged);
return () => { return () => {
const {camera} = selfEntity || {}; const {camera} = selfEntity || {};
@ -77,7 +87,15 @@ const RoomViewComponent = () => {
destroyLoop(handle); destroyLoop(handle);
}; };
}, [room]); }, [room]);
return <Component renderer={renderer} room={room} offset={offset} scale={scale} />; return (
<Component
offset={offset}
renderer={renderer}
room={room}
rotation={rotation}
scale={scale}
/>
);
}; };
export default RoomViewComponent; export default RoomViewComponent;