-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grzegorz Tańczyk
committed
Apr 21, 2024
1 parent
dbfccf8
commit 826bb0c
Showing
20 changed files
with
627 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,33 @@ | ||
import styled from 'styled-components'; | ||
import { EntityType } from '../world/world-state-types'; | ||
import { useSelectedObject } from '../controls/selection'; | ||
import { usePointer } from '../controls/pointer'; | ||
|
||
export function LaunchHighlight() { | ||
const selectedObject = useSelectedObject(); | ||
const pointer = usePointer(); | ||
|
||
if (selectedObject?.type !== EntityType.LAUNCH_SITE) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<HighlightContainer | ||
style={ | ||
{ | ||
'--x': pointer.x, | ||
'--y': pointer.y, | ||
} as React.CSSProperties | ||
} | ||
> | ||
{pointer.x}, {pointer.y} | ||
</HighlightContainer> | ||
); | ||
} | ||
|
||
const HighlightContainer = styled.div` | ||
position: absolute; | ||
transform: translate(calc(var(--x) * 1px), calc(var(--y) * 1px)); | ||
pointer-events: none; | ||
color: red; | ||
`; |
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 { useCustomEvent } from '../events'; | ||
import { EntityType, Explosion, Missile, WorldState } from '../world/world-state-types'; | ||
import { usePointer } from './pointer'; | ||
import { useSelectedObject } from './selection'; | ||
|
||
export function Command({ | ||
worldState, | ||
setWorldState, | ||
}: { | ||
worldState: WorldState; | ||
setWorldState: (worldState: WorldState) => void; | ||
}) { | ||
const selectedObject = useSelectedObject(); | ||
const pointer = usePointer(); | ||
|
||
useCustomEvent('world-click', () => { | ||
if (selectedObject?.type !== EntityType.LAUNCH_SITE || pointer.pointingObjects.length === 0) { | ||
return; | ||
} | ||
|
||
const missile: Missile = { | ||
id: Math.random() + '', | ||
launch: selectedObject.position, | ||
launchTimestamp: worldState.timestamp, | ||
|
||
target: pointer.pointingObjects[0].position, | ||
targetTimestamp: worldState.timestamp + 10, | ||
}; | ||
|
||
const explosion: Explosion = { | ||
id: Math.random() + '', | ||
startTimestamp: missile.targetTimestamp, | ||
endTimestamp: missile.targetTimestamp + 5, | ||
position: missile.target, | ||
radius: 30, | ||
}; | ||
|
||
setWorldState({ | ||
...worldState, | ||
missiles: [...worldState.missiles, missile], | ||
explosions: [...worldState.explosions, explosion], | ||
}); | ||
}); | ||
|
||
return null; | ||
} |
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,75 @@ | ||
import React, { createContext, useContext, useReducer } from 'react'; | ||
import { City, LaunchSite } from '../world/world-state-types'; | ||
|
||
type PointableObject = LaunchSite | City; | ||
|
||
type PointerDispatchAction = | ||
| { | ||
type: 'move'; | ||
x: number; | ||
y: number; | ||
} | ||
| { | ||
type: 'point' | 'unpoint'; | ||
object: PointableObject; | ||
}; | ||
|
||
type Pointer = { | ||
x: number; | ||
y: number; | ||
pointingObjects: PointableObject[]; | ||
}; | ||
|
||
const initialPointer: Pointer = { x: 0, y: 0, pointingObjects: [] }; | ||
|
||
const pointerReducer: React.Reducer<Pointer, PointerDispatchAction> = ( | ||
pointer: Pointer, | ||
action: PointerDispatchAction, | ||
) => { | ||
if (action.type === 'move') { | ||
return { x: action.x, y: action.y, pointingObjects: pointer.pointingObjects }; | ||
} else if (action.type === 'point' && !pointer.pointingObjects.some((object) => object.id === action.object.id)) { | ||
return { x: pointer.x, y: pointer.y, pointingObjects: [...pointer.pointingObjects, action.object] }; | ||
} else if (action.type === 'unpoint' && pointer.pointingObjects.some((object) => object.id === action.object.id)) { | ||
return { | ||
x: pointer.x, | ||
y: pointer.y, | ||
pointingObjects: pointer.pointingObjects.filter((object) => object.id === action.object.id), | ||
}; | ||
} else { | ||
return pointer; | ||
} | ||
}; | ||
|
||
const PointerContext = createContext<Pointer>(initialPointer); | ||
|
||
const PointerDispatchContext = createContext<React.Dispatch<PointerDispatchAction>>(() => {}); | ||
|
||
export function PointerContextWrapper({ children }: { children: React.ReactNode }) { | ||
const [selection, reducer] = useReducer(pointerReducer, initialPointer); | ||
|
||
return ( | ||
<PointerContext.Provider value={selection}> | ||
<PointerDispatchContext.Provider value={reducer}>{children}</PointerDispatchContext.Provider> | ||
</PointerContext.Provider> | ||
); | ||
} | ||
|
||
export function usePointer() { | ||
const pointer = useContext(PointerContext); | ||
|
||
return pointer; | ||
} | ||
|
||
export function usePointerMove() { | ||
const dispatch = useContext(PointerDispatchContext); | ||
return (x: number, y: number) => dispatch({ type: 'move', x, y }); | ||
} | ||
|
||
export function useObjectPointer() { | ||
const dispatch = useContext(PointerDispatchContext); | ||
return [ | ||
(object: PointableObject) => dispatch({ type: 'point', object }), | ||
(object: PointableObject) => dispatch({ type: 'unpoint', object }), | ||
]; | ||
} |
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,21 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export function dispatchCustomEvent<T>(eventName: string, data?: T) { | ||
const event = new CustomEvent(eventName, { | ||
bubbles: true, | ||
detail: data, | ||
}); | ||
document.dispatchEvent(event); | ||
} | ||
|
||
export function useCustomEvent<T>(eventName: string, callback: (data: T) => void) { | ||
useEffect(() => { | ||
const handler = (event: Event | CustomEvent<T>) => { | ||
callback((event as CustomEvent).detail as T); | ||
}; | ||
document.addEventListener(eventName, handler, false); | ||
return () => { | ||
document.removeEventListener(eventName, handler, false); | ||
}; | ||
}, [eventName, callback]); | ||
} |
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
5 changes: 5 additions & 0 deletions
5
games/nukes/src/render/city-render.tsx → games/nukes/src/world-render/city-render.tsx
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
10 changes: 8 additions & 2 deletions
10
...s/nukes/src/render/world-state-render.tsx → ...s/src/world-render/world-state-render.tsx
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
Oops, something went wrong.