refactor: components
This commit is contained in:
parent
0671b2f2f6
commit
463874ae69
|
@ -2,6 +2,7 @@ import './index.scss';
|
||||||
|
|
||||||
import {InputNormalizer} from '@avocado/input/client';
|
import {InputNormalizer} from '@avocado/input/client';
|
||||||
import {Vector} from '@avocado/math';
|
import {Vector} from '@avocado/math';
|
||||||
|
import {Room} from '@avocado/topdown';
|
||||||
import {
|
import {
|
||||||
setSelfEntity,
|
setSelfEntity,
|
||||||
useRoom,
|
useRoom,
|
||||||
|
@ -11,6 +12,7 @@ import {
|
||||||
React,
|
React,
|
||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
|
useState,
|
||||||
} from '@latus/react';
|
} from '@latus/react';
|
||||||
import {useDispatch} from '@latus/redux';
|
import {useDispatch} from '@latus/redux';
|
||||||
import {useSocket} from '@latus/socket';
|
import {useSocket} from '@latus/socket';
|
||||||
|
@ -27,22 +29,8 @@ const Play = () => {
|
||||||
const room = useRoom();
|
const room = useRoom();
|
||||||
const selfEntity = useSelfEntity();
|
const selfEntity = useSelfEntity();
|
||||||
const socket = useSocket();
|
const socket = useSocket();
|
||||||
useEffect(() => {
|
const [left, setLeft] = useState(0);
|
||||||
if (!ref.current || !room) {
|
const [top, setTop] = useState(0);
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const onEntityAdded = async (entity) => {
|
|
||||||
if (entity.is('DomNode')) {
|
|
||||||
// eslint-disable-next-line no-param-reassign
|
|
||||||
entity.parentNode = ref.current.querySelector('.entities');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Object.values(room.entities).forEach(onEntityAdded);
|
|
||||||
room.on('entityAdded', onEntityAdded);
|
|
||||||
return () => {
|
|
||||||
room.off('entityAdded', onEntityAdded);
|
|
||||||
};
|
|
||||||
}, [ref, room]);
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!room || !selfEntity) {
|
if (!room || !selfEntity) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -50,9 +38,8 @@ const Play = () => {
|
||||||
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);
|
const [left, top] = Vector.scale(selfEntity.camera.realOffset, -1);
|
||||||
const entities = ref.current.querySelector('.entities');
|
setLeft(FACTOR * left);
|
||||||
entities.style.left = `${FACTOR * left}px`;
|
setTop(FACTOR * top);
|
||||||
entities.style.top = `${FACTOR * top}px`;
|
|
||||||
};
|
};
|
||||||
onCameraRealOffsetChanged();
|
onCameraRealOffsetChanged();
|
||||||
selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged);
|
selfEntity.camera.on('realOffsetChanged', onCameraRealOffsetChanged);
|
||||||
|
@ -63,7 +50,6 @@ const Play = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [room, selfEntity]);
|
}, [room, selfEntity]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const join = async () => {
|
const join = async () => {
|
||||||
dispatch(setSelfEntity(await socket.send(['Join', uuid])));
|
dispatch(setSelfEntity(await socket.send(['Join', uuid])));
|
||||||
|
@ -94,7 +80,7 @@ const Play = () => {
|
||||||
return () => {
|
return () => {
|
||||||
clearInterval(inputHandle);
|
clearInterval(inputHandle);
|
||||||
};
|
};
|
||||||
});
|
}, [selfEntity, socket]);
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="play"
|
className="play"
|
||||||
|
@ -102,7 +88,7 @@ const Play = () => {
|
||||||
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
||||||
tabIndex="0"
|
tabIndex="0"
|
||||||
>
|
>
|
||||||
<div className="entities" />
|
<Room left={left} room={room} top={top} />
|
||||||
{selfEntity && <Hotbar selfEntity={selfEntity} />}
|
{selfEntity && <Hotbar selfEntity={selfEntity} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
.entities {
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
entity.dialog {
|
|
||||||
background-color: rgba(0, 0, 0, 0.6);
|
|
||||||
border-radius: 0.5em;
|
|
||||||
border: 2px solid #444;
|
|
||||||
display: inline-flex;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
font-family: Joystix;
|
|
||||||
font-size: 2.5em;
|
|
||||||
max-width: 75rem;
|
|
||||||
padding: 1em;
|
|
||||||
}
|
|
||||||
|
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
@ -1,4 +1,4 @@
|
||||||
import './ui.scss';
|
import './index.scss';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
PropTypes,
|
PropTypes,
|
Loading…
Reference in New Issue
Block a user