Skip to content

Commit

Permalink
feat: use dto types for spritemap registry
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Dec 12, 2024
1 parent 91737a0 commit 97f95b0
Show file tree
Hide file tree
Showing 31 changed files with 256 additions and 334 deletions.
7 changes: 6 additions & 1 deletion clients/player-client/src/context/Input/InputProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import React, { useReducer } from 'react';
import { InputContext } from './InputContext';
import { InputReducer } from './InputReducer';
import { InputState } from './InputState.type';
import { InputEventRecordKey } from '../../dtos/Player/InputEventRecordKey.type';

const initialState: InputState = { x: undefined, y: undefined, key: '' };
const initialState: InputState = {
x: undefined,
y: undefined,
key: InputEventRecordKey.NONE,
};

export const InputProvider: React.FC<{ children: React.ReactNode }> = ({
children,
Expand Down
30 changes: 24 additions & 6 deletions clients/player-client/src/core/GameEngine/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, 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 { DateTime, Duration } from 'luxon';
import { InputActionType } from '../../context/Input/InputActionType.type';

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

useEffect(() => {
inputContextRef.current = inputContext;
}, [inputContext]);

useEffect(() => {
if (
!inputContext.state.key ||
Expand All @@ -30,18 +35,31 @@ export const GameEngine: React.FC<GameEngineProps> = props => {
return;
}

// TODO automate this
const actorIndex = 0;

data.actors[actorIndex].animation.currentFrame = 0;
data.actors[actorIndex].animation.startTimestamp = DateTime.now();
data.actors[actorIndex].movement.isInMotion = true;
data.actors[actorIndex].movement.currentPosition =
data.actors[actorIndex].movement.targetPosition;
data.actors[actorIndex].movement.targetPositionReached = false;
data.actors[actorIndex].movement.movementDuration = Duration.fromObject({
seconds: 10,
});

switch (inputContext.state.key) {
case InputEventRecordKey.LEFT:
data.actors[0].movement.targetPosition.x--;
data.actors[actorIndex].movement.targetPosition.x--;
break;
case InputEventRecordKey.RIGHT:
data.actors[0].movement.targetPosition.x++;
data.actors[actorIndex].movement.targetPosition.x++;
break;
case InputEventRecordKey.UP:
data.actors[0].movement.targetPosition.y--;
data.actors[actorIndex].movement.targetPosition.y--;
break;
case InputEventRecordKey.DOWN:
data.actors[0].movement.targetPosition.y++;
data.actors[actorIndex].movement.targetPosition.y++;
break;
case InputEventRecordKey.DEBUG:
inputContext.dispatch({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IsometricRender } from './utils/IsometricRender';
import React, { useRef, useEffect, useState, useCallback } from 'react';
import { IsometricCanvasProps } from './IsometricCanvasProps';
import { Coordinate2D } from './types/Coordinates.type';
import { useResize } from './hooks/useResize';
import { canvasToGridCoordinate } from './utils/IsometricTransformer';
Expand All @@ -10,12 +9,13 @@ import { useInterval } from './hooks/useInterval';
import { useInputContext } from '../../context/Input/useInputContext';
import './IsometricCanvas.scss';
import { InputActionType } from '../../context/Input/InputActionType.type';
import { GameState } from '../../dtos/GameState.dto';

const isometricRender = new IsometricRender({
drawCoordinates: true,
});

export const IsometricCanvas = (props: IsometricCanvasProps) => {
export const IsometricCanvas = (props: GameState) => {
const {
grid,
actors,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { BackgroundColor } from '../types/BackgroundColor.type';
import { BackgroundColor } from '../../../dtos/Area/BackgroundColor.type';

interface Properties {
backgroundColor?: BackgroundColor;
Expand Down
1 change: 0 additions & 1 deletion clients/player-client/src/core/IsometricCanvas/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './IsometricCanvas';
export * from './IsometricCanvasProps';

This file was deleted.

15 changes: 0 additions & 15 deletions clients/player-client/src/core/IsometricCanvas/types/Actor.type.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 97f95b0

Please sign in to comment.