Skip to content

Commit

Permalink
Merge pull request #1 from Console-buche/chore/clean-up-round
Browse files Browse the repository at this point in the history
  • Loading branch information
neolectron authored Jul 25, 2023
2 parents ea5dea1 + 6478835 commit 2bba050
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMachine } from '@xstate/react';
import { button, useControls } from 'leva';
import { Scene } from './components/gl/Scene';
import { GameMachineProvider } from './hooks/use';
import { gameMachine } from './machines/game.machine';

import './style/App.css';
import { Scene } from './components/gl/Scene/Scene';

function App() {
const [state, , service] = useMachine(gameMachine);
Expand Down
10 changes: 4 additions & 6 deletions src/components/gl/Environment/DropSpots/DropSpots.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useId, useRef } from "react";
import { Mesh, Vector3 } from "three";
import { DropSpot } from "./DropSpot";
import { DROPSPOT_SIZE, RING_SIZE, RING_WIDTH } from "./dropSpots.constants";
import { useId, useRef } from 'react';
import { Mesh, Vector3 } from 'three';
import { DropSpot } from './DropSpot';
import { DROPSPOT_SIZE, RING_SIZE, RING_WIDTH } from './dropSpots.constants';

export const DropSpots = ({
positions,
Expand All @@ -11,8 +11,6 @@ export const DropSpots = ({
uuid: string;
}[];
}) => {
// const { isDragging, setIsDragging } = useContext(DraggingContext);
// TODO : get tex from type
const refDropSpot = useRef<Mesh>(null);
const uuid = useId();

Expand Down
2 changes: 1 addition & 1 deletion src/components/gl/Environment/HotSpot/Bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Hotspot } from './Hotspot';
export const Bar = () => {
const service = useInterpret(barMachine);
const maxPersons = useSelector(service, (s) => s.context.maxPersons);
const { draggingActorRef } = useContext(DraggingContext); //TODO: degager ce contexte de mort
const { draggingActorRef } = useContext(DraggingContext); //TODO: degager ce contexte de mort XD

return (
<Hotspot
Expand Down
65 changes: 55 additions & 10 deletions src/components/gl/Environment/HotSpot/Hotspot.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import type { MeshProps } from '@react-three/fiber';
import { useEffect, useId, useMemo, useRef, useState } from 'react';
import {
useContext,
useEffect,
useId,
useMemo,
useReducer,
useRef,
useState,
} from 'react';
import type { CircleGeometry, Mesh } from 'three';
import { getPositionsOnCircle } from '../../../../helpers/getPositionOnCircle';
import { DropSpots } from '../DropSpots/DropSpots';
import { DROPSPOT_SIZE, RING_SIZE } from '../DropSpots/dropSpots.constants';
import type { ActorRefFrom } from 'xstate';
import type { personMachine } from '../../../../machines/person.machine';
import {
type ReducerActionHotSpot,
type ReducerStateHotSpot,
hopSpotReducer,
} from './hotSpot.reducer';
import { DraggingContext } from '../../World/World';

type HotSpotProps = {
slotsAmount: number;
onDropHotspot: (person: ActorRefFrom<typeof personMachine>) => void;
onDropHotspot: () => void;
} & MeshProps;

// TODO : removing happens when person is dragged & dropped outside of the dropspot
export const Hotspot = ({
slotsAmount,
onDropHotspot,
slotsAmount,
...props
}: HotSpotProps) => {
const refHotSpotGeometry = useRef<CircleGeometry>(null);
const refHotSpot = useRef<Mesh>(null);
const [isHovered, setIsHovered] = useState(false);

// const { isDragging, draggingRef, draggingId } = useContext(DraggingContext);
const { isDragging, draggingRef } = useContext(DraggingContext);
const uuid = useId();
const isExists = useRef(false);

Expand All @@ -43,24 +54,58 @@ export const Hotspot = ({
slotsAmount,
i,
),
uuid: `${uuid}-${i}}`,
uuid,
})),
[slotsAmount, uuid],
[],

Check warning on line 59 in src/components/gl/Environment/HotSpot/Hotspot.tsx

View workflow job for this annotation

GitHub Actions / main - Build and deploy

React Hook useMemo has missing dependencies: 'slotsAmount' and 'uuid'. Either include them or remove the dependency array
);

const [state, dispatch] = useReducer<
React.Reducer<ReducerStateHotSpot, ReducerActionHotSpot>
>(hopSpotReducer, {
personsIdsOnSpot: new Set(),
spotIdsAndAvailability: new Map([...positions].map((_, i) => [i, true])),
});

return (
<group position-y={-1.1}>
<mesh
onPointerEnter={() => setIsHovered(true)}
onPointerLeave={() => setIsHovered(false)}
ref={refHotSpot}
uuid={uuid}
// onClick={() => onDropHotspot()}
onClick={() => {
onDropHotspot();

const availableSpotId = Array.from(
state.spotIdsAndAvailability.entries(),
).find(([, isAvailable]) => isAvailable)?.[0];

dispatch({
type: 'ADD',
payload: {
uuid,
spotId: availableSpotId ?? 0, // FIXME : PERSONS shouldn't stack. If no available spot: do something.
isDragging,
onHotSpotDrop: () => {
const worldPosition = refHotSpot.current?.localToWorld(
positions[availableSpotId ?? 0].pos.clone(),
);
if (!worldPosition) {
return;
}
const newPos = worldPosition.clone().setY(0);
draggingRef?.position.set(...newPos.toArray());
},
},
});
}}
onPointerDown={() => {}}
{...props}
>
<circleBufferGeometry args={[RING_SIZE, 16]} ref={refHotSpotGeometry} />
<meshBasicMaterial transparent opacity={0.3} color="black" />

{/* TODO : make dropspots visible/part of the UI/UX or just remove them from graphics */}
{isHovered && <DropSpots positions={positions} />}
</mesh>
</group>
Expand Down
1 change: 0 additions & 1 deletion src/components/gl/Environment/HotSpot/Toilet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const Toilet = () => {
const { draggingActorRef } = useContext(DraggingContext);
return (
<Hotspot
type="battery"
slotsAmount={5}
position={[-6, 0, 7]}
onDropHotspot={() =>
Expand Down
51 changes: 51 additions & 0 deletions src/components/gl/Environment/HotSpot/hotSpot.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export type ReducerStateHotSpot = {
personsIdsOnSpot: Set<string>;
spotIdsAndAvailability: Map<number, boolean>;
};

export type ReducerActionHotSpot =
| {
type: 'ADD';
payload: {
uuid: string;
isDragging: boolean;
onHotSpotDrop: () => void;
spotId: number;
};
}
| { type: 'REMOVE'; payload: string }; // TODO

export function hopSpotReducer(
state: ReducerStateHotSpot,
action: ReducerActionHotSpot,
): ReducerStateHotSpot {
let personsIdsOnSpot = new Set<string>();
let currentSpotIds = new Map();

switch (action.type) {
case 'ADD':
if (!action.payload.isDragging) {
return state;
}
personsIdsOnSpot = new Set(
...state.personsIdsOnSpot,
action.payload.uuid,
);

currentSpotIds = new Map(state.spotIdsAndAvailability);
currentSpotIds.set(action.payload.spotId, false);

action.payload.onHotSpotDrop();
return {
personsIdsOnSpot,
spotIdsAndAvailability: currentSpotIds,
};

case 'REMOVE':
// TODO
return state;

default:
return state;
}
}
14 changes: 8 additions & 6 deletions src/components/gl/Person/Person.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { a, useSpring } from '@react-spring/three';
import { Html, useTexture } from '@react-three/drei';
import { useFrame } from '@react-three/fiber';
import { useSelector } from '@xstate/react';
import { useControls } from 'leva';
import { useContext, useEffect, useRef } from 'react';
import {
Expand All @@ -16,9 +17,7 @@ import type { personMachine } from '../../../machines/person.machine';
import { DraggingContext } from '../World/World';
import { PersonShadowRecall } from './PersonShadowRecall';
import { Statbar } from './Statbar';
import { useSelector } from '@xstate/react';

const PERSON_HEIGHT = 6;
import { PERSON_HEIGHT } from './person.constants';

export const Person = ({
refFloor,
Expand Down Expand Up @@ -56,23 +55,24 @@ export const Person = ({

const isBeingDragged = draggingId === serviceId;

// setup easings
// init easings
const shadow = useSpring({
opacity: isBeingDragged ? 0.9 : 0.1,
color: isBeingDragged ? '#FFA500' : 'grey', // orange to grey
color: isBeingDragged ? '#FFA500' : 'grey',
scale: isBeingDragged
? ([1.25, 1.25, 1.25] as const)
: ([0.75, 0.75, 0.75] as const),
});

// position tick for the drag back shadow
useEffect(() => {
if (isBeingDragged && refGroup.current) {
beforeDragPosition.current.copy(refGroup.current.position);
}
}, [isBeingDragged]);

// init position & transform offsets
useEffect(() => {
// init position & transform offsets
if (ref.current && refGroup.current && !isExists.current) {
ref.current.geometry.translate(0, PERSON_HEIGHT * 0.5, 0);
refGroup.current.position.copy(pos || new Vector3(0, 0, 30));
Expand Down Expand Up @@ -162,6 +162,8 @@ export const Person = ({
color={shadow.color}
/>
</a.mesh>

{/* Debug */}
{showActionButtons && (
<Html>
<button
Expand Down
4 changes: 2 additions & 2 deletions src/components/gl/Person/PersonShadowRecall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const PersonShadowRecall = ({
// setup easings
const shadow = useSpring({
opacity: isBeingDragged ? 0.9 : 0,
color: isBeingDragged ? '#FFA500' : 'grey', // orange to grey
color: isBeingDragged ? '#FFA500' : 'grey',
scale: isBeingDragged
? ([1.25, 1.25, 1.25] as const)
: ([0.75, 0.75, 0.75] as const),
Expand All @@ -25,7 +25,7 @@ export const PersonShadowRecall = ({
refShadowRecall.current.position.set(
beforeDragPosition.x,
-0.9,
beforeDragPosition.z
beforeDragPosition.z,
);
}
}, [beforeDragPosition, isBeingDragged]);
Expand Down
3 changes: 0 additions & 3 deletions src/components/gl/Person/Statbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ export const Statbar = ({ value, ...meshProps }: Statbar) => {

ref.current.lookAt(camera.position);

// update stat

// TODO : add bounds + decrease on event only
ref.current.material.uniforms.percent.value = MathUtils.lerp(
ref.current.material.uniforms.percent.value,
value / 100,
Expand Down
2 changes: 2 additions & 0 deletions src/components/gl/Person/person.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// threejs constants
export const PERSON_HEIGHT = 6;
4 changes: 2 additions & 2 deletions src/components/gl/Scene/Scene.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Canvas } from '@react-three/fiber';
import { World } from '../World';
import { World } from '../World/World';

export const Scene = () => {
return (
Expand All @@ -8,8 +8,8 @@ export const Scene = () => {
gl={{ alpha: true }}
style={{ width: '100vw', height: '100vh' }}
>
<color attach={'background'} args={['#ccc']} />
<ambientLight />

<World />
</Canvas>
);
Expand Down
1 change: 0 additions & 1 deletion src/components/gl/Scene/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/components/gl/World/DummyBox.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/gl/World/Floor.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/components/gl/World/RoomBounds.tsx

This file was deleted.

Loading

0 comments on commit 2bba050

Please sign in to comment.