diff --git a/leaky-ships/components/BorderTiles.tsx b/leaky-ships/components/BorderTiles.tsx
index 24228ba..580eb45 100644
--- a/leaky-ships/components/BorderTiles.tsx
+++ b/leaky-ships/components/BorderTiles.tsx
@@ -1,5 +1,5 @@
import { CSSProperties, Dispatch, SetStateAction } from 'react'
-import { borderCN, cornerCN, fieldIndex } from '../utils/helpers'
+import { borderCN, cornerCN, fieldIndex } from '../lib/utils/helpers'
import { Position, MouseCursor } from '../interfaces/frontend'
type TilesType = {
@@ -35,7 +35,8 @@ function BorderTiles({ props: { count, settingTarget, setMouseCursor, setLastLef
tilesProperties.push({ key, classNameString, isGameTile, x: x + 1, y: y + 1 })
}
}
- return <>
+
+ return (<>
{tilesProperties.map(({ key, classNameString, isGameTile, x, y }) => {
return
setLastLeftTile({ x, y })}
>
})}
- >
+ >)
}
export default BorderTiles
diff --git a/leaky-ships/components/EventBar.tsx b/leaky-ships/components/EventBar.tsx
new file mode 100644
index 0000000..5b55bda
--- /dev/null
+++ b/leaky-ships/components/EventBar.tsx
@@ -0,0 +1,33 @@
+import React, { Dispatch, SetStateAction } from 'react'
+import { Items, Target } from '../interfaces/frontend'
+import Item from './Item'
+
+function EventBar({ props: { setMode, setTarget } }: {
+ props: {
+ setMode: Dispatch>,
+ setTarget: Dispatch>
+ }
+}) {
+ const items: Items[] = [
+ { icon: 'burger-menu', text: 'Menu' },
+ { icon: 'radar', text: 'Radar scan', mode: 0, amount: 1 },
+ { icon: 'torpedo', text: 'Fire torpedo', mode: 1, amount: 1 },
+ { icon: 'scope', text: 'Fire missile', mode: 2 },
+ { icon: 'gear', text: 'Settings' }
+ ]
+ return (
+
+ {items.map((e, i) => (
+ - {
+ if (e.mode !== undefined)
+ setMode(e.mode)
+ setTarget(e => ({ ...e, show: false }))
+ }
+ }} />
+ ))}
+
+ )
+}
+
+export default EventBar
\ No newline at end of file
diff --git a/leaky-ships/components/Gamefield.tsx b/leaky-ships/components/Gamefield.tsx
index 6a6fcb4..8f0b65f 100644
--- a/leaky-ships/components/Gamefield.tsx
+++ b/leaky-ships/components/Gamefield.tsx
@@ -1,21 +1,21 @@
import { CSSProperties } from 'react'
// import Bluetooth from './Bluetooth'
import BorderTiles from './BorderTiles'
+import EventBar from './EventBar'
// import FogImages from './FogImages'
import HitElems from './HitElems'
import Labeling from './Labeling'
import Ships from './Ships'
-import useGameEvent from './useGameEvent'
+import useGameEvent from '../lib/hooks/useGameEvent'
+import Targets from './Targets'
function Gamefield() {
const count = 12
const {
- targets,
- eventBar,
- setLastLeftTile,
- settingTarget,
- setMouseCursor,
+ pointersProps,
+ targetsProps,
+ tilesProps,
hits
} = useGameEvent(count)
@@ -24,7 +24,7 @@ function Gamefield() {
{/* */}
{/* Bordes */}
-
+
{/* Collumn lettes and row numbers */}
@@ -36,10 +36,10 @@ function Gamefield() {
{/* Fog images */}
{/* */}
- {targets}
+
{/* Debug */}
- {eventBar}
+
)
}
diff --git a/leaky-ships/components/GamefieldPointer.tsx b/leaky-ships/components/GamefieldPointer.tsx
index 9a8ed4a..9222d8e 100644
--- a/leaky-ships/components/GamefieldPointer.tsx
+++ b/leaky-ships/components/GamefieldPointer.tsx
@@ -3,6 +3,11 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { CSSProperties } from 'react'
import classNames from 'classnames'
import { faRadar } from '@fortawesome/pro-thin-svg-icons'
+import { Target, TargetList } from '../interfaces/frontend'
+
+export interface PointerProps extends Target, TargetList {
+ imply: boolean;
+}
function GamefieldPointer({ props: {
preview,
@@ -12,17 +17,7 @@ function GamefieldPointer({ props: {
type,
edges,
imply
-} }: {
- props: {
- preview: boolean,
- x: number,
- y: number,
- show: boolean,
- type: string,
- edges: string[],
- imply: boolean,
- }
-}) {
+} }: { props: PointerProps }) {
const isRadar = type === 'radar'
const style = !(isRadar && !edges.filter(s => s).length) ? { '--x': x, '--y': y } : { '--x1': x - 1, '--x2': x + 2, '--y1': y - 1, '--y2': y + 2 }
return (
diff --git a/leaky-ships/components/Targets.tsx b/leaky-ships/components/Targets.tsx
new file mode 100644
index 0000000..1139860
--- /dev/null
+++ b/leaky-ships/components/Targets.tsx
@@ -0,0 +1,20 @@
+import React from 'react'
+import { Target } from '../interfaces/frontend'
+import GamefieldPointer, { PointerProps } from './GamefieldPointer'
+
+function Targets({ props: { composeTargetTiles, target, targetPreview } }: {
+ props: {
+ composeTargetTiles: (target: Target) => PointerProps[],
+ target: Target,
+ targetPreview: Target
+ }
+}) {
+ return (<>
+ {[
+ ...composeTargetTiles(target).map((props, i) => ),
+ ...composeTargetTiles(targetPreview).map((props, i) => )
+ ]}
+ >)
+}
+
+export default Targets
\ No newline at end of file
diff --git a/leaky-ships/components/useGameEvent.tsx b/leaky-ships/lib/hooks/useGameEvent.ts
similarity index 67%
rename from leaky-ships/components/useGameEvent.tsx
rename to leaky-ships/lib/hooks/useGameEvent.ts
index 34d0c87..ebf1e0a 100644
--- a/leaky-ships/components/useGameEvent.tsx
+++ b/leaky-ships/lib/hooks/useGameEvent.ts
@@ -1,8 +1,14 @@
import { useCallback, useEffect, useMemo, useReducer, useState } from 'react'
-import { hitReducer, initlialLastLeftTile, initlialTarget, initlialTargetPreview, initlialMouseCursor, isHit } from '../utils/helpers'
-import { Hit, Items, Mode, MouseCursor, Target, Position } from '../interfaces/frontend'
-import Item from './Item'
-import GamefieldPointer from './GamefieldPointer'
+import { hitReducer, initlialLastLeftTile, initlialTarget, initlialTargetPreview, initlialMouseCursor } from '../utils/helpers'
+import { Hit, Mode, MouseCursor, Target, Position } from '../../interfaces/frontend'
+import { arst } from '../../components/GamefieldPointer'
+
+const modes: Mode[] = [
+ { pointerGrid: Array.from(Array(3), () => Array.from(Array(3))), type: 'radar' },
+ { pointerGrid: Array.from(Array(3), () => Array.from(Array(1))), type: 'htorpedo' },
+ { pointerGrid: Array.from(Array(1), () => Array.from(Array(3))), type: 'vtorpedo' },
+ { pointerGrid: [[{ x: 0, y: 0 }]], type: 'missile' }
+]
function useGameEvent(count: number) {
const [lastLeftTile, setLastLeftTile] = useState(initlialLastLeftTile)
@@ -14,13 +20,6 @@ function useGameEvent(count: number) {
const [hits, DispatchHits] = useReducer(hitReducer, [] as Hit[])
const [mode, setMode] = useState(0)
- const modes = useMemo(() => [
- { pointerGrid: Array.from(Array(3), () => Array.from(Array(3))), type: 'radar' },
- { pointerGrid: Array.from(Array(3), () => Array.from(Array(1))), type: 'htorpedo' },
- { pointerGrid: Array.from(Array(1), () => Array.from(Array(3))), type: 'vtorpedo' },
- { pointerGrid: [[{ x: 0, y: 0 }]], type: 'missile' }
- ], [])
-
const targetList = useCallback((target: Position) => {
const { pointerGrid, type } = modes[mode]
const xLength = pointerGrid.length
@@ -46,10 +45,10 @@ function useGameEvent(count: number) {
})
})
.reduce((prev, curr) => [...prev, ...curr], [])
- }, [mode, modes])
+ }, [mode])
const settingTarget = useCallback((isGameTile: boolean, x: number, y: number) => {
- if (!isGameTile || isHit(hits, x, y).length)
+ if (!isGameTile || isHit(x, y).length)
return
setMouseCursor(e => ({ ...e, shouldShow: false }))
setTarget(t => {
@@ -67,9 +66,15 @@ function useGameEvent(count: number) {
})
}, [count, hits, targetList])
- const isSet = useCallback((x: number, y: number) => !!targetList(target).filter(field => x === field.x && y === field.y).length && target.show, [target, targetList])
+ const isSet = useCallback((x: number, y: number) => {
+ return !!targetList(target).filter(field => x === field.x && y === field.y).length && target.show
+ }, [target, targetList])
- const composeTargetTiles = useCallback((target: Target) => {
+ const isHit = useCallback((x: number, y: number) => {
+ return hits.filter(h => h.x === x && h.y === y)
+ }, [hits])
+
+ const composeTargetTiles = useCallback((target: Target): arst[] => {
const { preview, show } = target
const result = targetList(target).map(({ x, y, type, edges }) => {
return {
@@ -79,15 +84,12 @@ function useGameEvent(count: number) {
show,
type,
edges,
- imply: !!isHit(hits, x, y).length || (!!isSet(x, y) && preview)
+ imply: !!isHit(x, y).length || (!!isSet(x, y) && preview)
}
})
return result
}, [hits, isSet, targetList])
- // if (!targetPreviewPos.shouldShow)
- // return
-
// handle visibility and position change of targetPreview
useEffect(() => {
const { show, x, y } = targetPreview
@@ -97,7 +99,7 @@ function useGameEvent(count: number) {
if (show && !appearOK)
setTargetPreview(e => ({ ...e, show: false }))
- if (!show && mouseCursor.shouldShow && eventReady && appearOK && !isHit(hits, x, y).length && !isSet && !hasLeft)
+ if (!show && mouseCursor.shouldShow && eventReady && appearOK && !isHit(x, y).length && !isSet && !hasLeft)
setTargetPreview(e => ({ ...e, show: true }))
}, [targetPreview, mouseCursor.shouldShow, hits, eventReady, appearOK, lastLeftTile, target])
@@ -133,39 +135,10 @@ function useGameEvent(count: number) {
}
}, [targetPreview.show])
- const targets = useMemo(() => [
- ...composeTargetTiles(target).map((props, i) => ),
- ...composeTargetTiles(targetPreview).map((props, i) => )
- ], [composeTargetTiles, target, targetPreview])
-
- const eventBar = useMemo(() => {
- const items: Items[] = [
- { icon: 'burger-menu', text: 'Menu' },
- { icon: 'radar', text: 'Radar scan', mode: 0, amount: 1 },
- { icon: 'torpedo', text: 'Fire torpedo', mode: 1, amount: 1 },
- { icon: 'scope', text: 'Fire missile', mode: 2 },
- { icon: 'gear', text: 'Settings' }
- ]
- return (
-
- {items.map((e, i) => (
- - {
- if (e.mode !== undefined)
- setMode(e.mode)
- setTarget(e => ({ ...e, show: false }))
- }
- }} />
- ))}
-
- )
- }, [])
return {
- targets,
- eventBar,
- setLastLeftTile,
- settingTarget,
- setMouseCursor,
+ tilesProps: { count, settingTarget, setMouseCursor, setLastLeftTile },
+ pointersProps: { composeTargetTiles, target, targetPreview },
+ targetsProps: { setMode, setTarget },
hits
}
}
diff --git a/leaky-ships/utils/helpers.ts b/leaky-ships/lib/utils/helpers.ts
similarity index 88%
rename from leaky-ships/utils/helpers.ts
rename to leaky-ships/lib/utils/helpers.ts
index 6feacd9..a88dc5c 100644
--- a/leaky-ships/utils/helpers.ts
+++ b/leaky-ships/lib/utils/helpers.ts
@@ -1,4 +1,4 @@
-import { Hit, HitDispatch } from "../interfaces/frontend"
+import { Hit, HitDispatch } from "../../interfaces/frontend"
export function borderCN(count: number, x: number, y: number) {
if (x === 0)
@@ -58,6 +58,3 @@ export const initlialMouseCursor = {
x: 0,
y: 0
}
-export function isHit(hits: Hit[], x: number, y: number) {
- return hits.filter(h => h.x === x && h.y === y)
-}