refactor: tidy
This commit is contained in:
parent
b3188aadc0
commit
9f01056ef8
|
@ -1,9 +1,63 @@
|
||||||
import {React} from '@flecks/react';
|
import {InputNormalizer} from '@avocado/input/client';
|
||||||
|
import {
|
||||||
|
React,
|
||||||
|
useEffect,
|
||||||
|
} from '@flecks/react';
|
||||||
|
import {useDispatch} from '@flecks/redux';
|
||||||
|
import {useSocket} from '@flecks/socket';
|
||||||
|
import {setSelfEntity} from '@humus/app/state';
|
||||||
|
|
||||||
|
import {
|
||||||
|
useSelfEntity,
|
||||||
|
} from '../../../hooks';
|
||||||
|
|
||||||
import Renderable from './renderable';
|
import Renderable from './renderable';
|
||||||
import Ui from './ui';
|
import Ui from './ui';
|
||||||
|
|
||||||
function Play() {
|
function Play() {
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const selfEntity = useSelfEntity();
|
||||||
|
const socket = useSocket();
|
||||||
|
// Join.
|
||||||
|
useEffect(() => {
|
||||||
|
const join = async () => {
|
||||||
|
dispatch(setSelfEntity(await socket.send(['Join'])));
|
||||||
|
};
|
||||||
|
join();
|
||||||
|
socket.on('reconnect', join);
|
||||||
|
return () => {
|
||||||
|
socket.off('reconnect', join);
|
||||||
|
};
|
||||||
|
}, [dispatch, socket]);
|
||||||
|
// Input.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selfEntity) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const inputNormalizer = new InputNormalizer();
|
||||||
|
inputNormalizer.listen(window.document.body);
|
||||||
|
selfEntity.listenForInput(inputNormalizer);
|
||||||
|
selfEntity.actionRegistry.setTransformerFor('UseItem', (type, value) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'joystick':
|
||||||
|
return value === 0 ? -1 : selfEntity.activeSlotIndex;
|
||||||
|
case 'buttonPress':
|
||||||
|
case 'keyDown':
|
||||||
|
return selfEntity.activeSlotIndex;
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const inputHandle = setInterval(() => {
|
||||||
|
const actionStream = selfEntity.drainInput();
|
||||||
|
if (actionStream.length > 0) {
|
||||||
|
socket.send(['Input', actionStream]);
|
||||||
|
}
|
||||||
|
}, 1000 / 60);
|
||||||
|
return () => {
|
||||||
|
clearInterval(inputHandle);
|
||||||
|
};
|
||||||
|
}, [selfEntity, socket]);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Renderable />
|
<Renderable />
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
import './index.scss';
|
|
||||||
|
|
||||||
import {InputNormalizer} from '@avocado/input/client';
|
|
||||||
import {Vector} from '@avocado/math';
|
import {Vector} from '@avocado/math';
|
||||||
import {Room} from '@avocado/topdown';
|
import {Room} from '@avocado/topdown';
|
||||||
import {
|
import {
|
||||||
|
@ -10,11 +7,6 @@ import {
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from '@flecks/react';
|
} from '@flecks/react';
|
||||||
import {useDispatch} from '@flecks/redux';
|
|
||||||
import {useSocket} from '@flecks/socket';
|
|
||||||
import {
|
|
||||||
setSelfEntity,
|
|
||||||
} from '@humus/app/state';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useRoom,
|
useRoom,
|
||||||
|
@ -23,83 +15,47 @@ import {
|
||||||
import Hotbar from './hotbar';
|
import Hotbar from './hotbar';
|
||||||
|
|
||||||
const PlayUi = () => {
|
const PlayUi = () => {
|
||||||
const dispatch = useDispatch();
|
|
||||||
const flecks = useFlecks();
|
const flecks = useFlecks();
|
||||||
const ref = useRef();
|
const ref = useRef();
|
||||||
const room = useRoom();
|
const room = useRoom();
|
||||||
const selfEntity = useSelfEntity();
|
const selfEntity = useSelfEntity();
|
||||||
const socket = useSocket();
|
const [position, setPosition] = useState([0, 0]);
|
||||||
const [left, setLeft] = useState(0);
|
|
||||||
const [top, setTop] = useState(0);
|
|
||||||
const [domScale, setDomScale] = useState([1, 1]);
|
const [domScale, setDomScale] = useState([1, 1]);
|
||||||
|
// Handle camera changes.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!room || !selfEntity) {
|
if (!room || !selfEntity) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
// Initial snap camera to position.
|
||||||
selfEntity.camera.realPosition = selfEntity.camera.position;
|
selfEntity.camera.realPosition = selfEntity.camera.position;
|
||||||
const onCameraRealOffsetChanged = () => {
|
const onCameraRealOffsetChanged = () => {
|
||||||
const [left, top] = Vector.scale(selfEntity.camera.realOffset, -1);
|
setPosition(Vector.scale(selfEntity.camera.realOffset, -1));
|
||||||
const {resolution} = flecks.get('@humus/app');
|
};
|
||||||
setDomScale(Vector.div(resolution, selfEntity.camera.viewSize));
|
const onCameraViewSizeChanged = () => {
|
||||||
setLeft(left);
|
setDomScale(Vector.div(flecks.get('@humus/app.resolution'), selfEntity.camera.viewSize));
|
||||||
setTop(top);
|
|
||||||
};
|
};
|
||||||
onCameraRealOffsetChanged();
|
onCameraRealOffsetChanged();
|
||||||
|
onCameraViewSizeChanged();
|
||||||
selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged);
|
selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged);
|
||||||
|
selfEntity.camera.on('viewSizeChanged', onCameraViewSizeChanged);
|
||||||
return () => {
|
return () => {
|
||||||
const {camera} = selfEntity || {};
|
const {camera} = selfEntity;
|
||||||
if (camera) {
|
if (camera) {
|
||||||
camera.off('realOffsetChanged', onCameraRealOffsetChanged);
|
camera.off('realOffsetChanged', onCameraViewSizeChanged);
|
||||||
|
camera.off('viewSizeChanged', onCameraViewSizeChanged);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [flecks, room, selfEntity]);
|
}, [flecks, room, selfEntity]);
|
||||||
|
// Focus.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const join = async () => {
|
if (ref.current) {
|
||||||
dispatch(setSelfEntity(await socket.send(['Join'])));
|
ref.current.focus();
|
||||||
};
|
|
||||||
join();
|
|
||||||
socket.on('reconnect', join);
|
|
||||||
return () => {
|
|
||||||
socket.off('reconnect', join);
|
|
||||||
};
|
|
||||||
}, [dispatch, socket]);
|
|
||||||
useEffect(() => {
|
|
||||||
if (!ref.current || !selfEntity) {
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
const inputNormalizer = new InputNormalizer();
|
});
|
||||||
inputNormalizer.listen(window.document.body);
|
|
||||||
selfEntity.listenForInput(inputNormalizer);
|
|
||||||
selfEntity.actionRegistry.setTransformerFor('UseItem', (type, value) => {
|
|
||||||
switch (type) {
|
|
||||||
case 'joystick':
|
|
||||||
return value === 0 ? -1 : selfEntity.activeSlotIndex;
|
|
||||||
case 'buttonPress':
|
|
||||||
case 'keyDown':
|
|
||||||
return selfEntity.activeSlotIndex;
|
|
||||||
default:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const inputHandle = setInterval(() => {
|
|
||||||
const actionStream = selfEntity.drainInput();
|
|
||||||
if (actionStream.length > 0) {
|
|
||||||
socket.send(['Input', actionStream]);
|
|
||||||
}
|
|
||||||
}, 1000 / 60);
|
|
||||||
ref.current.focus();
|
|
||||||
return () => {
|
|
||||||
clearInterval(inputHandle);
|
|
||||||
};
|
|
||||||
}, [selfEntity, socket]);
|
|
||||||
return (
|
return (
|
||||||
<div
|
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
||||||
className="play"
|
<div ref={ref} tabIndex="0">
|
||||||
ref={ref}
|
<Room domScale={domScale} position={position} room={room} />
|
||||||
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
|
||||||
tabIndex="0"
|
|
||||||
>
|
|
||||||
<Room domScale={domScale} left={left} room={room} top={top} />
|
|
||||||
{selfEntity && <Hotbar selfEntity={selfEntity} />}
|
{selfEntity && <Hotbar selfEntity={selfEntity} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user