-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add keyboard input hook to player client
- Loading branch information
Showing
15 changed files
with
128 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Game assets are intentionally not realistic to help players to suspend belief | ||
due to the [uncanny valley](https://en.wikipedia.org/wiki/Uncanny_valley). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Actions } from './Actions.type'; | ||
|
||
export type Action = | ||
| { type: Actions.SET_USER; payload: string } | ||
| { type: Actions.LOGOUT } | ||
| { type: Actions.PAGE_LOADING; payload: boolean }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum Actions { | ||
PAGE_LOADING = 'PAGE_LOADING', | ||
SET_USER = 'SET_USER', | ||
LOGOUT = 'LOGOUT', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from 'react'; | ||
import { AppState } from './AppState.type'; | ||
import { Action } from './Action.type'; | ||
|
||
export const AppContext = createContext< | ||
| { | ||
state: AppState; | ||
dispatch: React.Dispatch<Action>; | ||
} | ||
| undefined | ||
>(undefined); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface AppState { | ||
user: string | null; | ||
isLoading?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useContext } from 'react'; | ||
import { AppContext } from './AppContext'; | ||
|
||
export const useAppContext = () => { | ||
const context = useContext(AppContext); | ||
if (!context) { | ||
throw new Error('useAppContext must be used within an AppProvider'); | ||
} | ||
return context; | ||
}; |
16 changes: 16 additions & 0 deletions
16
clients/player-client/src/core/config/keyboard-bindings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// keyboard | ||
export const keyboardBindings = { | ||
up: 'w', | ||
down: 's', | ||
left: 'a', | ||
right: 'd', | ||
jump: ' ', | ||
debug: 'p', | ||
select: 'Enter', | ||
back: 'u', | ||
pause: 'Escape', | ||
}; | ||
|
||
// other inputs will be touch and based | ||
// may need to display buttons on screen for touch | ||
// move by clicking a tile.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './player-input-record.type'; |
4 changes: 4 additions & 0 deletions
4
clients/player-client/src/core/types/player-input-record.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type PlayerInputRecord = { | ||
key: string; | ||
timestamp: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { keyboardBindings } from './config/keyboard-bindings'; | ||
import { DateTime } from 'luxon'; | ||
import { PlayerInputRecord } from './types'; | ||
|
||
export default function useHandleInput(): PlayerInputRecord[] | null { | ||
const [inputState, setInputState] = useState<PlayerInputRecord[] | null>( | ||
null, | ||
); | ||
const maxRecords = 5; // Set the maximum number of records | ||
|
||
useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
const newInputRecords: PlayerInputRecord[] = []; | ||
|
||
// Handle different key strokes | ||
Object.entries(keyboardBindings).forEach(([direction, key]) => { | ||
if (event.key === key) { | ||
// Collect key press for the given direction | ||
newInputRecords.push({ | ||
key: direction, | ||
timestamp: DateTime.now().toISO(), | ||
}); | ||
} | ||
}); | ||
|
||
if (newInputRecords.length > 0) { | ||
setInputState(prevState => { | ||
const newState = prevState ? [...prevState] : []; | ||
newInputRecords.forEach(record => { | ||
newState.unshift(record); | ||
}); | ||
return newState.slice(0, maxRecords); | ||
}); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyDown); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, []); | ||
|
||
return inputState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.