Skip to content

Commit

Permalink
feat: move debug toggle to game engine
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Dec 11, 2024
1 parent 645a9a1 commit 91737a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
3 changes: 2 additions & 1 deletion clients/player-client/src/context/Input/InputAction.type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { InputActionType } from './InputActionType.type';
import { DateTime } from 'luxon';
import { InputEventRecordKey } from '../../dtos/Player/InputEventRecordKey.type';

export type InputAction =
| { type: InputActionType.SET_DEBUG; payload: { debug: boolean } }
| { type: InputActionType.SET_TOUCH_GRID; payload: { x: number; y: number } }
| {
type: InputActionType.SET_KEYSTROKE;
payload: { key: string; timestamp: DateTime };
payload: { key: InputEventRecordKey; timestamp: DateTime };
}
| {
type: InputActionType.SET_MOUSE_MOVE;
Expand Down
3 changes: 2 additions & 1 deletion clients/player-client/src/context/Input/InputState.type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { DateTime } from 'luxon';
import { InputEventRecordKey } from '../../dtos/Player/InputEventRecordKey.type';

export type InputState = {
x?: number;
y?: number;
cursorX?: number;
cursorY?: number;
key?: string;
key?: InputEventRecordKey;
timestamp?: DateTime;
debug?: boolean;
};
53 changes: 41 additions & 12 deletions clients/player-client/src/core/GameEngine/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { useInputContext } from '../../context/Input/useInputContext';
import { InputEventRecordKey } from '../../dtos/Player/InputEventRecordKey.type';
import { GameState } from '../../dtos/GameState.dto';
import { InputEventRecord } from '../../dtos/Player/InputEventRecord.dto';
import { DateTime } from 'luxon';
import { InputActionType } from '../../context/Input/InputActionType.type';

export type GameEngineProps = {
data: GameState;
Expand All @@ -11,23 +14,49 @@ export type GameEngineProps = {
export const GameEngine: React.FC<GameEngineProps> = props => {
const { data, updateData } = props;
const inputContext = useInputContext();
const [lastProcessedInput, setLastProcessedInput] =
useState<InputEventRecord>({
key: InputEventRecordKey.DEBUG,
timestamp: DateTime.now(),
});

useEffect(() => {
if (inputContext.state.key === InputEventRecordKey.LEFT) {
data.actors[0].movement.targetPosition.x--;
if (
!inputContext.state.key ||
!inputContext.state.timestamp ||
(lastProcessedInput.key === inputContext.state.key &&
lastProcessedInput.timestamp === inputContext.state.timestamp)
) {
return;
}
if (inputContext.state.key === InputEventRecordKey.RIGHT) {
data.actors[0].movement.targetPosition.x++;
}
if (inputContext.state.key === InputEventRecordKey.UP) {
data.actors[0].movement.targetPosition.y--;
}
if (inputContext.state.key === InputEventRecordKey.DOWN) {
data.actors[0].movement.targetPosition.y++;

switch (inputContext.state.key) {
case InputEventRecordKey.LEFT:
data.actors[0].movement.targetPosition.x--;
break;
case InputEventRecordKey.RIGHT:
data.actors[0].movement.targetPosition.x++;
break;
case InputEventRecordKey.UP:
data.actors[0].movement.targetPosition.y--;
break;
case InputEventRecordKey.DOWN:
data.actors[0].movement.targetPosition.y++;
break;
case InputEventRecordKey.DEBUG:
inputContext.dispatch({
type: InputActionType.SET_DEBUG,
payload: { debug: inputContext.state.debug ? false : true },
});
break;
}
setLastProcessedInput({
key: inputContext.state.key,
timestamp: inputContext.state.timestamp,
});

updateData(data);
}, [updateData, inputContext.state.key]);
}, [inputContext.state, lastProcessedInput, data, updateData]);

return null;
};
8 changes: 0 additions & 8 deletions clients/player-client/src/core/Keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useEffect, useRef } from 'react';
import useHandleInput from './useHandleInput';
import { useInputContext } from '../../context/Input/useInputContext';
import { InputActionType } from '../../context/Input/InputActionType.type';
import { InputEventRecordKey } from '../../dtos/Player/InputEventRecordKey.type';

export type KeyboardProps = {
config?: string;
Expand All @@ -26,13 +25,6 @@ export const Keyboard: React.FC<KeyboardProps> = () => {
type: InputActionType.SET_KEYSTROKE,
payload: { key: input[0]?.key, timestamp: input[0]?.timestamp },
});

if (input[0]?.key === InputEventRecordKey.DEBUG) {
inputContextRef.current.dispatch({
type: InputActionType.SET_DEBUG,
payload: { debug: !inputContextRef.current.state.debug },
});
}
}, [input]);

return (
Expand Down
2 changes: 1 addition & 1 deletion clients/player-client/src/core/Keyboard/useHandleInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function useHandleInput(): InputEventRecord[] | null {
newInputRecords.forEach(record => {
newState.unshift(record);
});
setInputState(newState);
setInputState(newInputRecords);
};

const handleKeyUp = (event: KeyboardEvent) => {
Expand Down

0 comments on commit 91737a0

Please sign in to comment.