feat: debug
This commit is contained in:
parent
7a2485b588
commit
4c0e2cec9b
|
@ -26,14 +26,14 @@ export default function addKeyListener(target, listener) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
keysDown[key] = true;
|
keysDown[key] = true;
|
||||||
listener({type: 'keyDown', payload: key});
|
listener({event, type: 'keyDown', payload: key});
|
||||||
}
|
}
|
||||||
function onKeyUp(event) {
|
function onKeyUp(event) {
|
||||||
const {key} = event;
|
const {key} = event;
|
||||||
keyUpDelays[key] = setTimeout(() => {
|
keyUpDelays[key] = setTimeout(() => {
|
||||||
delete keyUpDelays[key];
|
delete keyUpDelays[key];
|
||||||
delete keysDown[key];
|
delete keysDown[key];
|
||||||
listener({type: 'keyUp', payload: key});
|
listener({event, type: 'keyUp', payload: key});
|
||||||
}, 20);
|
}, 20);
|
||||||
}
|
}
|
||||||
window.addEventListener('blur', onBlur);
|
window.addEventListener('blur', onBlur);
|
||||||
|
|
9
app/context/debug.js
Normal file
9
app/context/debug.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import {createContext, useContext} from 'react';
|
||||||
|
|
||||||
|
const context = createContext();
|
||||||
|
|
||||||
|
export default context;
|
||||||
|
|
||||||
|
export function useDebug() {
|
||||||
|
return useContext(context);
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
import {Container, Graphics} from '@pixi/react';
|
import {Container, Graphics} from '@pixi/react';
|
||||||
import {useCallback} from 'react';
|
import {useCallback} from 'react';
|
||||||
|
|
||||||
|
import {useDebug} from '@/context/debug.js';
|
||||||
|
|
||||||
import Sprite from './sprite.jsx';
|
import Sprite from './sprite.jsx';
|
||||||
|
|
||||||
function Crosshair({x, y}) {
|
function Crosshair({x, y}) {
|
||||||
|
@ -27,6 +29,7 @@ function Crosshair({x, y}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Entities({entities}) {
|
export default function Entities({entities}) {
|
||||||
|
const [debug] = useDebug();
|
||||||
const renderables = [];
|
const renderables = [];
|
||||||
for (const id in entities) {
|
for (const id in entities) {
|
||||||
const entity = entities[id];
|
const entity = entities[id];
|
||||||
|
@ -40,7 +43,9 @@ export default function Entities({entities}) {
|
||||||
<Sprite
|
<Sprite
|
||||||
entity={entity}
|
entity={entity}
|
||||||
/>
|
/>
|
||||||
<Crosshair x={entity.Position.x} y={entity.Position.y} />
|
{debug && (
|
||||||
|
<Crosshair x={entity.Position.x} y={entity.Position.y} />
|
||||||
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {createElement, useContext} from 'react';
|
||||||
|
|
||||||
import {RESOLUTION} from '@/constants.js';
|
import {RESOLUTION} from '@/constants.js';
|
||||||
import ClientContext from '@/context/client.js';
|
import ClientContext from '@/context/client.js';
|
||||||
|
import DebugContext from '@/context/debug.js';
|
||||||
import EcsContext from '@/context/ecs.js';
|
import EcsContext from '@/context/ecs.js';
|
||||||
import MainEntityContext from '@/context/main-entity.js';
|
import MainEntityContext from '@/context/main-entity.js';
|
||||||
|
|
||||||
|
@ -15,7 +16,7 @@ import styles from './pixi.module.css';
|
||||||
|
|
||||||
BaseTexture.defaultOptions.scaleMode = SCALE_MODES.NEAREST;
|
BaseTexture.defaultOptions.scaleMode = SCALE_MODES.NEAREST;
|
||||||
|
|
||||||
const Contexts = [ClientContext, EcsContext, MainEntityContext];
|
const Contexts = [ClientContext, DebugContext, EcsContext, MainEntityContext];
|
||||||
|
|
||||||
const ContextBridge = ({children, render}) => {
|
const ContextBridge = ({children, render}) => {
|
||||||
const contexts = Contexts.map(useContext);
|
const contexts = Contexts.map(useContext);
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {useContext, useEffect, useState} from 'react';
|
||||||
import addKeyListener from '@/add-key-listener.js';
|
import addKeyListener from '@/add-key-listener.js';
|
||||||
import {RESOLUTION} from '@/constants.js';
|
import {RESOLUTION} from '@/constants.js';
|
||||||
import ClientContext from '@/context/client.js';
|
import ClientContext from '@/context/client.js';
|
||||||
|
import {useDebug} from '@/context/debug.js';
|
||||||
import {useEcs} from '@/context/ecs.js';
|
import {useEcs} from '@/context/ecs.js';
|
||||||
import {useMainEntity} from '@/context/main-entity.js';
|
import {useMainEntity} from '@/context/main-entity.js';
|
||||||
import usePacket from '@/hooks/use-packet.js';
|
import usePacket from '@/hooks/use-packet.js';
|
||||||
|
@ -23,6 +24,7 @@ export default function Ui({disconnected}) {
|
||||||
// Key input.
|
// Key input.
|
||||||
const client = useContext(ClientContext);
|
const client = useContext(ClientContext);
|
||||||
const [mainEntity, setMainEntity] = useMainEntity();
|
const [mainEntity, setMainEntity] = useMainEntity();
|
||||||
|
const [debug, setDebug] = useDebug();
|
||||||
const [ecs] = useEcs();
|
const [ecs] = useEcs();
|
||||||
const [showDisconnected, setShowDisconnected] = useState(false);
|
const [showDisconnected, setShowDisconnected] = useState(false);
|
||||||
const [bufferSlot, setBufferSlot] = useState();
|
const [bufferSlot, setBufferSlot] = useState();
|
||||||
|
@ -43,13 +45,22 @@ export default function Ui({disconnected}) {
|
||||||
};
|
};
|
||||||
}, [disconnected]);
|
}, [disconnected]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return addKeyListener(document.body, ({type, payload}) => {
|
return addKeyListener(document.body, ({event, type, payload}) => {
|
||||||
const KEY_MAP = {
|
const KEY_MAP = {
|
||||||
keyDown: 1,
|
keyDown: 1,
|
||||||
keyUp: 0,
|
keyUp: 0,
|
||||||
};
|
};
|
||||||
let actionPayload;
|
let actionPayload;
|
||||||
switch (payload) {
|
switch (payload) {
|
||||||
|
case 'F3': {
|
||||||
|
if (event) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
if ('keyDown' === type) {
|
||||||
|
setDebug(!debug);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'w': {
|
case 'w': {
|
||||||
actionPayload = {type: 'moveUp', value: KEY_MAP[type]};
|
actionPayload = {type: 'moveUp', value: KEY_MAP[type]};
|
||||||
break;
|
break;
|
||||||
|
@ -138,7 +149,7 @@ export default function Ui({disconnected}) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [client]);
|
}, [client, debug, setDebug]);
|
||||||
usePacket('Tick', (payload) => {
|
usePacket('Tick', (payload) => {
|
||||||
if (0 === Object.keys(payload.ecs).length) {
|
if (0 === Object.keys(payload.ecs).length) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {useEffect, useState} from 'react';
|
||||||
import {useOutletContext, useParams} from 'react-router-dom';
|
import {useOutletContext, useParams} from 'react-router-dom';
|
||||||
|
|
||||||
import ClientContext from '@/context/client.js';
|
import ClientContext from '@/context/client.js';
|
||||||
|
import DebugContext from '@/context/debug.js';
|
||||||
import EcsContext from '@/context/ecs.js';
|
import EcsContext from '@/context/ecs.js';
|
||||||
import MainEntityContext from '@/context/main-entity.js';
|
import MainEntityContext from '@/context/main-entity.js';
|
||||||
import Ecs from '@/ecs/ecs.js';
|
import Ecs from '@/ecs/ecs.js';
|
||||||
|
@ -20,6 +21,7 @@ export default function PlaySpecific() {
|
||||||
const Client = useOutletContext();
|
const Client = useOutletContext();
|
||||||
const [client, setClient] = useState();
|
const [client, setClient] = useState();
|
||||||
const mainEntityTuple = useState();
|
const mainEntityTuple = useState();
|
||||||
|
const debugTuple = useState(false);
|
||||||
const ecsTuple = useState(new Ecs({Components, Systems}));
|
const ecsTuple = useState(new Ecs({Components, Systems}));
|
||||||
const [disconnected, setDisconnected] = useState(false);
|
const [disconnected, setDisconnected] = useState(false);
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
|
@ -93,7 +95,9 @@ export default function PlaySpecific() {
|
||||||
<ClientContext.Provider value={client}>
|
<ClientContext.Provider value={client}>
|
||||||
<MainEntityContext.Provider value={mainEntityTuple}>
|
<MainEntityContext.Provider value={mainEntityTuple}>
|
||||||
<EcsContext.Provider value={ecsTuple}>
|
<EcsContext.Provider value={ecsTuple}>
|
||||||
<Ui disconnected={disconnected} />
|
<DebugContext.Provider value={debugTuple}>
|
||||||
|
<Ui disconnected={disconnected} />
|
||||||
|
</DebugContext.Provider>
|
||||||
</EcsContext.Provider>
|
</EcsContext.Provider>
|
||||||
</MainEntityContext.Provider>
|
</MainEntityContext.Provider>
|
||||||
</ClientContext.Provider>
|
</ClientContext.Provider>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user